[pkg-go] Bug#946050: Please add systemd socket activation support

Kilian Krause kilian at debian.org
Tue Dec 3 12:12:36 GMT 2019


Package: prometheus-blackbox-exporter
Severity: wishlist
Tags: patch

Hi,

Please consider adding the systemd socket activation patch from
https://github.com/prometheus/blackbox_exporter/pull/523/commits.

The verified working version for both 0.13 and 0.14 is attached.

TIA!

Best regards,
Kilian
-------------- next part --------------
backport systemd socket activation from https://github.com/prometheus/blackbox_exporter/pull/523
Credit goes to Stefan B├╝hler
--- a/main.go
+++ b/main.go
@@ -39,6 +39,8 @@ import (
 
 	"github.com/prometheus/blackbox_exporter/config"
 	"github.com/prometheus/blackbox_exporter/prober"
+
+	"github.com/coreos/go-systemd/activation"
 )
 
 var (
@@ -204,12 +206,16 @@ func init() {
 }
 
 func main() {
-	promlogConfig := promlog.Config{}
-	flag.AddFlags(kingpin.CommandLine, &promlogConfig)
+	os.Exit(run())
+}
+
+func run() int {
+	promlogConfig := &promlog.Config{}
+	flag.AddFlags(kingpin.CommandLine, promlogConfig)
 	kingpin.Version(version.Print("blackbox_exporter"))
 	kingpin.HelpFlag.Short('h')
 	kingpin.Parse()
-	logger := promlog.New(&promlogConfig)
+	logger := promlog.New(promlogConfig)
 	rh := &resultHistory{maxResults: *historyLimit}
 
 	level.Info(logger).Log("msg", "Starting blackbox_exporter", "version", version.Info())
@@ -217,12 +223,12 @@ func main() {
 
 	if err := sc.ReloadConfig(*configFile); err != nil {
 		level.Error(logger).Log("msg", "Error loading config", "err", err)
-		os.Exit(1)
+		return 1
 	}
 
 	if *configCheck {
 		level.Info(logger).Log("msg", "Config file is ok exiting...")
-		os.Exit(0)
+		return 0
 	}
 
 	level.Info(logger).Log("msg", "Loaded config file")
@@ -329,9 +335,44 @@ func main() {
 		w.Write(c)
 	})
 
-	level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
-	if err := http.ListenAndServe(*listenAddress, nil); err != nil {
-		level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
-		os.Exit(1)
+	srv := http.Server{Addr: *listenAddress}
+	srvc := make(chan struct{})
+	term := make(chan os.Signal, 1)
+	signal.Notify(term, os.Interrupt, syscall.SIGTERM)
+
+	go func() {
+		listeners, err := activation.Listeners()
+		if err != nil {
+			level.Error(logger).Log("msg", "cannot retrieve activation listeners", "err", err)
+			return
+		}
+
+		if len(listeners) > 1 {
+			level.Error(logger).Log("msg", "unexpected number of socket activation listeners", "listeners", len(listeners))
+			return
+		}
+		if len(listeners) == 1 {
+			level.Info(logger).Log("msg", "Listening on activation socket")
+			if err := http.Serve(listeners[0], nil); err != http.ErrServerClosed {
+				level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
+				close(srvc)
+			}
+		} else {
+			level.Info(logger).Log("msg", "Listening on address", "address", *listenAddress)
+			if err := srv.ListenAndServe(); err != http.ErrServerClosed {
+				level.Error(logger).Log("msg", "Error starting HTTP server", "err", err)
+				close(srvc)
+			}
+		}
+	}()
+
+	for {
+		select {
+		case <-term:
+			level.Info(logger).Log("msg", "Received SIGTERM, exiting gracefully...")
+			return 0
+		case <-srvc:
+			return 1
+		}
 	}
 }


More information about the Pkg-go-maintainers mailing list