Skip to content

Commit 8d75511

Browse files
committed
collectors/version: Allow for custom label
Signed-off-by: Manuel Rüger <[email protected]>
1 parent 3692d7f commit 8d75511

File tree

3 files changed

+133
-10
lines changed

3 files changed

+133
-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: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,30 @@ import (
1919
"github.com/prometheus/common/version"
2020

2121
"github.com/prometheus/client_golang/prometheus"
22+
"maps"
2223
)
2324

2425
// NewCollector returns a collector that exports metrics about current version
2526
// information.
2627
func NewCollector(program string) prometheus.Collector {
28+
return NewCollectorWithLabels(program, nil)
29+
}
30+
31+
// NewCollectorWithLabels returns a collector that exports metrics about current
32+
// version information and allows to set additional custom labels.
33+
func NewCollectorWithLabels(program string, labels prometheus.Labels) prometheus.Collector {
34+
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+
}
44+
maps.Copy(constLabels, labels)
45+
2746
return prometheus.NewGaugeFunc(
2847
prometheus.GaugeOpts{
2948
Namespace: program,
@@ -32,16 +51,9 @@ func NewCollector(program string) prometheus.Collector {
3251
"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.",
3352
program,
3453
),
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-
},
54+
ConstLabels: constLabels,
4455
},
4556
func() float64 { return 1 },
4657
)
58+
4759
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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+
}
65+
}
66+
67+
func TestGoVersionCollectorWithLabels(t *testing.T) {
68+
reg := prometheus.NewPedanticRegistry()
69+
70+
labels := prometheus.Labels{
71+
"z-mylabel": "myvalue",
72+
}
73+
reg.MustRegister(NewCollectorWithLabels(
74+
"foo", labels),
75+
)
76+
result, err := reg.Gather()
77+
if err != nil {
78+
t.Fatal(err)
79+
}
80+
81+
if _, err := json.Marshal(result); err != nil {
82+
t.Errorf("json marshalling should not fail, %v", err)
83+
}
84+
85+
got := []string{}
86+
87+
for _, r := range result {
88+
got = append(got, r.GetName())
89+
fmt.Println("foo")
90+
m := r.GetMetric()
91+
92+
if len(m) != 1 {
93+
t.Errorf("expected 1 metric, but got %d", len(m))
94+
}
95+
96+
lk := []string{}
97+
for _, lp := range m[0].GetLabel() {
98+
lk = append(lk, lp.GetName())
99+
}
100+
101+
labels := append(defaultLabels, "z-mylabel")
102+
103+
if diff := cmp.Diff(lk, labels); diff != "" {
104+
t.Errorf("missmatch (-want +got):\n%s", diff)
105+
}
106+
107+
}
108+
109+
if diff := cmp.Diff(got, []string{"foo_build_info"}); diff != "" {
110+
}
111+
}

0 commit comments

Comments
 (0)