From a71541fb0bfa427a608ddffa48f70dc446a51621 Mon Sep 17 00:00:00 2001 From: Wade Simmons Date: Fri, 26 Mar 2021 14:16:35 -0400 Subject: [PATCH] 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. --- main.go | 2 +- stats.go | 21 ++++++++++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/main.go b/main.go index 5396a39..35dda76 100644 --- a/main.go +++ b/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) } diff --git a/stats.go b/stats.go index e09966e..76be8ce 100644 --- a/stats.go +++ b/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)