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.
This commit is contained in:
Wade Simmons 2021-03-26 14:16:35 -04:00 committed by GitHub
parent 3ea7e1b75f
commit a71541fb0b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 4 deletions

View File

@ -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)
}

View File

@ -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)