Skip to content

Commit 84a4734

Browse files
mruegbwplotka
andauthored
collectors/version: Allow for custom label (#1860)
Co-authored-by: bwplotka <[email protected]>
1 parent 493c96a commit 84a4734

File tree

2 files changed

+145
-10
lines changed

2 files changed

+145
-10
lines changed

prometheus/collectors/version/version.go

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,44 @@ package version
1515

1616
import (
1717
"fmt"
18+
"maps"
1819

1920
"github.com/prometheus/common/version"
2021

2122
"github.com/prometheus/client_golang/prometheus"
2223
)
2324

25+
type Option func(*options)
26+
27+
type options struct {
28+
extraConstLabels prometheus.Labels
29+
}
30+
31+
func WithExtraConstLabels(l prometheus.Labels) Option {
32+
return func(o *options) {
33+
o.extraConstLabels = l
34+
}
35+
}
36+
2437
// NewCollector returns a collector that exports metrics about current version
2538
// information.
26-
func NewCollector(program string) prometheus.Collector {
39+
func NewCollector(program string, opts ...Option) prometheus.Collector {
40+
o := options{}
41+
for _, opt := range opts {
42+
opt(&o)
43+
}
44+
45+
constLabels := prometheus.Labels{
46+
"version": version.Version,
47+
"revision": version.GetRevision(),
48+
"branch": version.Branch,
49+
"goversion": version.GoVersion,
50+
"goos": version.GoOS,
51+
"goarch": version.GoArch,
52+
"tags": version.GetTags(),
53+
}
54+
maps.Copy(constLabels, o.extraConstLabels)
55+
2756
return prometheus.NewGaugeFunc(
2857
prometheus.GaugeOpts{
2958
Namespace: program,
@@ -32,15 +61,7 @@ func NewCollector(program string) prometheus.Collector {
3261
"A metric with a constant '1' value labeled by version, revision, branch, goversion from which %s was built, and the goos and goarch for the build.",
3362
program,
3463
),
35-
ConstLabels: prometheus.Labels{
36-
"version": version.Version,
37-
"revision": version.GetRevision(),
38-
"branch": version.Branch,
39-
"goversion": version.GoVersion,
40-
"goos": version.GoOS,
41-
"goarch": version.GoArch,
42-
"tags": version.GetTags(),
43-
},
64+
ConstLabels: constLabels,
4465
},
4566
func() float64 { return 1 },
4667
)
Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
// Copyright 2025 The Prometheus Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package version
15+
16+
import (
17+
"encoding/json"
18+
"fmt"
19+
"testing"
20+
21+
"github.com/google/go-cmp/cmp"
22+
23+
"github.com/prometheus/client_golang/prometheus"
24+
)
25+
26+
var defaultLabels = []string{"branch", "goarch", "goos", "goversion", "revision", "tags", "version"}
27+
28+
func TestGoVersionCollector(t *testing.T) {
29+
reg := prometheus.NewPedanticRegistry()
30+
reg.MustRegister(NewCollector(
31+
"foo"),
32+
)
33+
result, err := reg.Gather()
34+
if err != nil {
35+
t.Fatal(err)
36+
}
37+
38+
if _, err := json.Marshal(result); err != nil {
39+
t.Errorf("json marshalling should not fail, %v", err)
40+
}
41+
42+
got := []string{}
43+
44+
for _, r := range result {
45+
got = append(got, r.GetName())
46+
fmt.Println("foo")
47+
m := r.GetMetric()
48+
49+
if len(m) != 1 {
50+
t.Errorf("expected 1 metric, but got %d", len(m))
51+
}
52+
53+
lk := []string{}
54+
for _, lp := range m[0].GetLabel() {
55+
lk = append(lk, lp.GetName())
56+
}
57+
58+
if diff := cmp.Diff(lk, defaultLabels); diff != "" {
59+
t.Errorf("missmatch (-want +got):\n%s", diff)
60+
}
61+
62+
}
63+
64+
if diff := cmp.Diff(got, []string{"foo_build_info"}); diff != "" {
65+
t.Errorf("missmatch (-want +got):\n%s", diff)
66+
}
67+
}
68+
69+
func TestGoVersionCollectorWithLabels(t *testing.T) {
70+
reg := prometheus.NewPedanticRegistry()
71+
72+
labels := prometheus.Labels{
73+
"z-mylabel": "myvalue",
74+
}
75+
reg.MustRegister(NewCollector(
76+
"foo", WithExtraConstLabels(labels)),
77+
)
78+
result, err := reg.Gather()
79+
if err != nil {
80+
t.Fatal(err)
81+
}
82+
83+
if _, err := json.Marshal(result); err != nil {
84+
t.Errorf("json marshalling should not fail, %v", err)
85+
}
86+
87+
got := []string{}
88+
89+
for _, r := range result {
90+
got = append(got, r.GetName())
91+
fmt.Println("foo")
92+
m := r.GetMetric()
93+
94+
if len(m) != 1 {
95+
t.Errorf("expected 1 metric, but got %d", len(m))
96+
}
97+
98+
lk := []string{}
99+
for _, lp := range m[0].GetLabel() {
100+
lk = append(lk, lp.GetName())
101+
}
102+
103+
labels := append(defaultLabels, "z-mylabel")
104+
105+
if diff := cmp.Diff(lk, labels); diff != "" {
106+
t.Errorf("missmatch (-want +got):\n%s", diff)
107+
}
108+
109+
}
110+
111+
if diff := cmp.Diff(got, []string{"foo_build_info"}); diff != "" {
112+
t.Errorf("missmatch (-want +got):\n%s", diff)
113+
}
114+
}

0 commit comments

Comments
 (0)