Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add sd_notify support #382

Merged
merged 1 commit into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 40 additions & 9 deletions cmd/ebpf_exporter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"log"
"net"
"net/http"
_ "net/http/pprof"
"os"
Expand All @@ -16,6 +17,7 @@ import (
"github.com/cloudflare/ebpf_exporter/v2/exporter"
"github.com/cloudflare/ebpf_exporter/v2/tracing"
"github.com/coreos/go-systemd/activation"
"github.com/mdlayher/sdnotify"
"github.com/prometheus/client_golang/prometheus"
version_collector "github.com/prometheus/client_golang/prometheus/collectors/version"
"github.com/prometheus/client_golang/prometheus/promhttp"
Expand Down Expand Up @@ -56,8 +58,20 @@ func main() {

libbpfgo.SetLoggerCbs(libbpfgoCallbacks)

notifier, _ := sdnotify.New()

notify := func(format string, v ...interface{}) {
if notifier == nil {
return
}

_ = notifier.Notify(sdnotify.Statusf(format, v...))
}

started := time.Now()

notify("parsing config...")

configs, err := config.ParseConfigs(*configDir, strings.Split(*configNames, ","))
if err != nil {
log.Fatalf("Error parsing configs: %v", err)
Expand All @@ -72,11 +86,15 @@ func main() {
log.Fatalf("Error creating tracing processor: %v", err)
}

notify("creating exporter...")

e, err := exporter.New(configs, tracing.NewProvider(processor), *btfPath)
if err != nil {
log.Fatalf("Error creating exporter: %s", err)
}

notify("attaching configs...")

err = e.Attach()
if err != nil {
log.Fatalf("Error attaching exporter: %s", err)
Expand All @@ -87,7 +105,9 @@ func main() {
log.Fatalf("Error dropping capabilities: %s", err)
}

log.Printf("Started with %d programs found in the config in %dms", len(configs), time.Since(started).Milliseconds())
readyString := fmt.Sprintf("%d programs found in the config in %dms", len(configs), time.Since(started).Milliseconds())

log.Printf("Started with %s", readyString)

if *configStrict {
missed := e.MissedAttachments()
Expand Down Expand Up @@ -130,33 +150,44 @@ func main() {
http.HandleFunc("/maps", e.MapsHandler)
}

err = listen(*listenAddress)
listener, err := listen(*listenAddress)
if err != nil {
log.Fatalf("Error listening on %s: %v", *listenAddress, err)
}

notify("ready with %s", readyString)

if notifier != nil {
_ = notifier.Notify(sdnotify.Ready)
}

err = http.Serve(listener, nil)
if err != nil {
log.Fatalf("Error listening on %s: %s", *listenAddress, err)
log.Fatalf("Error serving HTTP: %v", err)
}
}

func listen(addr string) error {
func listen(addr string) (net.Listener, error) {
log.Printf("Listening on %s", addr)
if strings.HasPrefix(addr, "fd://") {
fd, err := strconv.Atoi(strings.TrimPrefix(addr, "fd://"))
if err != nil {
return fmt.Errorf("error extracting fd number from %q: %v", addr, err)
return nil, fmt.Errorf("error extracting fd number from %q: %v", addr, err)
}

listeners, err := activation.Listeners()
if err != nil {
return fmt.Errorf("error getting activation listeners: %v", err)
return nil, fmt.Errorf("error getting activation listeners: %v", err)
}

if len(listeners) < fd+1 {
return errors.New("no listeners passed via activation")
return nil, errors.New("no listeners passed via activation")
}

return http.Serve(listeners[fd], nil)
return listeners[fd], nil
}

return http.ListenAndServe(addr, nil)
return net.Listen("tcp", addr)
}

func ensureCapabilities(keep string) error {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ require (
github.com/elastic/go-perf v0.0.0-20191212140718-9c656876f595
github.com/iovisor/gobpf v0.2.0
github.com/jaypipes/pcidb v1.0.0
github.com/mdlayher/sdnotify v1.0.0
github.com/prometheus/client_golang v1.19.0
github.com/prometheus/common v0.51.1
go.opentelemetry.io/contrib/exporters/autoexport v0.49.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/mdlayher/sdnotify v1.0.0 h1:Ma9XeLVN/l0qpyx1tNeMSeTjCPH6NtuD6/N9XdTlQ3c=
github.com/mdlayher/sdnotify v1.0.0/go.mod h1:HQUmpM4XgYkhDLtd+Uad8ZFK1T9D5+pNxnXQjCeJlGE=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down