Skip to content

Commit 778a519

Browse files
committed
collectors/version: Allow for custom label
Signed-off-by: Manuel Rüger <manuel@rueg.eu>
1 parent 3692d7f commit 778a519

File tree

3 files changed

+138
-10
lines changed

3 files changed

+138
-10
lines changed

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
module github.com/prometheus/client_golang
22

3-
go 1.23.0
3+
go 1.24.0
44

55
require (
66
github.com/beorn7/perks v1.0.1

prometheus/collectors/version/version.go

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,32 @@ import (
1818

1919
"github.com/prometheus/common/version"
2020

21+
"maps"
22+
2123
"github.com/prometheus/client_golang/prometheus"
2224
)
2325

2426
// NewCollector returns a collector that exports metrics about current version
2527
// information.
2628
func NewCollector(program string) prometheus.Collector {
29+
return NewCollectorWithLabels(program, nil)
30+
}
31+
32+
// NewCollectorWithLabels returns a collector that exports metrics about current
33+
// version information and allows to set additional custom labels.
34+
func NewCollectorWithLabels(program string, labels prometheus.Labels) prometheus.Collector {
35+
36+
constLabels := prometheus.Labels{
37+
"version": version.Version,
38+
"revision": version.GetRevision(),
39+
"branch": version.Branch,
40+
"goversion": version.GoVersion,
41+
"goos": version.GoOS,
42+
"goarch": version.GoArch,
43+
"tags": version.GetTags(),
44+
}
45+
maps.Copy(constLabels, labels)
46+
2747
return prometheus.NewGaugeFunc(
2848
prometheus.GaugeOpts{
2949
Namespace: program,
@@ -32,16 +52,9 @@ func NewCollector(program string) prometheus.Collector {
3252
"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.",
3353
program,
3454
),
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-
},
55+
ConstLabels: constLabels,
4456
},
4557
func() float64 { return 1 },
4658
)
59+
4760
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
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+
"github.com/prometheus/client_golang/prometheus"
23+
)
24+
25+
var defaultLabels = []string{"branch", "goarch", "goos", "goversion", "revision", "tags", "version"}
26+
27+
func TestGoVersionCollector(t *testing.T) {
28+
reg := prometheus.NewPedanticRegistry()
29+
reg.MustRegister(NewCollector(
30+
"foo"),
31+
)
32+
result, err := reg.Gather()
33+
if err != nil {
34+
t.Fatal(err)
35+
}
36+
37+
if _, err := json.Marshal(result); err != nil {
38+
t.Errorf("json marshalling should not fail, %v", err)
39+
}
40+
41+
got := []string{}
42+
43+
for _, r := range result {
44+
got = append(got, r.GetName())
45+
fmt.Println("foo")
46+
m := r.GetMetric()
47+
48+
if len(m) != 1 {
49+
t.Errorf("expected 1 metric, but got %d", len(m))
50+
}
51+
52+
lk := []string{}
53+
for _, lp := range m[0].GetLabel() {
54+
lk = append(lk, lp.GetName())
55+
}
56+
57+
if diff := cmp.Diff(lk, defaultLabels); diff != "" {
58+
t.Errorf("missmatch (-want +got):\n%s", diff)
59+
}
60+
61+
}
62+
63+
if diff := cmp.Diff(got, []string{"foo_build_info"}); diff != "" {
64+
t.Errorf("missmatch (-want +got):\n%s", diff)
65+
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(NewCollectorWithLabels(
76+
"foo", 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+
}
115+
}

0 commit comments

Comments
 (0)