Преглед изворни кода

export build version as a prometheus label (#405)

This is how Prometheus recommends you do it, and how they do it
themselves in their client. This makes it easy to see which versions you
have deployed in your fleet, and query over it too.
Wade Simmons пре 4 година
родитељ
комит
a71541fb0b
2 измењених фајлова са 19 додато и 4 уклоњено
  1. 1 1
      main.go
  2. 18 3
      stats.go

+ 1 - 1
main.go

@@ -396,7 +396,7 @@ func Main(config *Config, configTest bool, buildVersion string, logger *logrus.L
 		go lightHouse.LhUpdateWorker(ifce)
 	}
 
-	err = startStats(l, config, configTest)
+	err = startStats(l, config, buildVersion, configTest)
 	if err != nil {
 		return nil, NewContextualError("Failed to start stats emitter", nil, err)
 	}

+ 18 - 3
stats.go

@@ -6,6 +6,7 @@ import (
 	"log"
 	"net"
 	"net/http"
+	"runtime"
 	"time"
 
 	graphite "github.com/cyberdelia/go-metrics-graphite"
@@ -16,7 +17,7 @@ import (
 	"github.com/sirupsen/logrus"
 )
 
-func startStats(l *logrus.Logger, c *Config, configTest bool) error {
+func startStats(l *logrus.Logger, c *Config, buildVersion string, configTest bool) error {
 	mType := c.GetString("stats.type", "")
 	if mType == "" || mType == "none" {
 		return nil
@@ -31,7 +32,7 @@ func startStats(l *logrus.Logger, c *Config, configTest bool) error {
 	case "graphite":
 		startGraphiteStats(l, interval, c, configTest)
 	case "prometheus":
-		startPrometheusStats(l, interval, c, configTest)
+		startPrometheusStats(l, interval, c, buildVersion, configTest)
 	default:
 		return fmt.Errorf("stats.type was not understood: %s", mType)
 	}
@@ -65,7 +66,7 @@ func startGraphiteStats(l *logrus.Logger, i time.Duration, c *Config, configTest
 	return nil
 }
 
-func startPrometheusStats(l *logrus.Logger, i time.Duration, c *Config, configTest bool) error {
+func startPrometheusStats(l *logrus.Logger, i time.Duration, c *Config, buildVersion string, configTest bool) error {
 	namespace := c.GetString("stats.namespace", "")
 	subsystem := c.GetString("stats.subsystem", "")
 
@@ -83,6 +84,20 @@ func startPrometheusStats(l *logrus.Logger, i time.Duration, c *Config, configTe
 	pClient := mp.NewPrometheusProvider(metrics.DefaultRegistry, namespace, subsystem, pr, i)
 	go pClient.UpdatePrometheusMetrics()
 
+	// Export our version information as labels on a static gauge
+	g := prometheus.NewGauge(prometheus.GaugeOpts{
+		Namespace: namespace,
+		Subsystem: subsystem,
+		Name:      "info",
+		Help:      "Version information for the Nebula binary",
+		ConstLabels: prometheus.Labels{
+			"version":   buildVersion,
+			"goversion": runtime.Version(),
+		},
+	})
+	pr.MustRegister(g)
+	g.Set(1)
+
 	if !configTest {
 		go func() {
 			l.Infof("Prometheus stats listening on %s at %s", listen, path)