From 8a031eeea0ed2f9fedea351ba5bb8c1b120eea4e Mon Sep 17 00:00:00 2001 From: Zeger-Jan van de Weg Date: Sat, 16 Nov 2019 11:07:16 +0100 Subject: [PATCH] Add monitoring.WithVersion Option When the build time is unknown, the version could still be exposed. This change formalizes that, without the need to export the version key for the prometheus metric map. --- monitoring/serve_options.go | 7 +++++++ monitoring/serve_options_test.go | 6 ++++++ 2 files changed, 13 insertions(+) diff --git a/monitoring/serve_options.go b/monitoring/serve_options.go index 0ce2bac..8206807 100644 --- a/monitoring/serve_options.go +++ b/monitoring/serve_options.go @@ -52,6 +52,13 @@ func WithListenerAddress(addr string) Option { } } +// WithVersion will configure the `gitlab_build_info` metric with the version information +func WithVersion(version string) Option { + return func(config *optionsConfig) { + config.buildInfoGaugeLabels[buildInfoVersionLabel] = version + } +} + // WithBuildInformation will configure the `gitlab_build_info` metric with appropriate labels func WithBuildInformation(version string, buildTime string) Option { return func(config *optionsConfig) { diff --git a/monitoring/serve_options_test.go b/monitoring/serve_options_test.go index a6a9064..82841df 100644 --- a/monitoring/serve_options_test.go +++ b/monitoring/serve_options_test.go @@ -51,6 +51,12 @@ func Test_applyOptions(t *testing.T) { opts: []Option{WithListenerAddress(":0")}, wantListenerCheck: isTCPListener, }, + { + name: "with_version", + opts: []Option{WithVersion("1.0.0")}, + wantListenerCheck: checkDefaultListener, + wantbuildInfoGaugeLabels: prometheus.Labels{"version": "1.0.0"}, + }, { name: "with_build_information", opts: []Option{WithBuildInformation("1.0.0", "2018-01-02T00:00:00Z")}, -- GitLab