Skip to content

Commit ca21e13

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

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

go.mod

Lines changed: 2 additions & 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
@@ -13,6 +13,7 @@ require (
1313
github.com/prometheus/common v0.65.0
1414
github.com/prometheus/procfs v0.17.0
1515
go.uber.org/goleak v1.3.0
16+
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b
1617
golang.org/x/sys v0.34.0
1718
google.golang.org/protobuf v1.36.6
1819
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOf
4848
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
4949
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
5050
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
51+
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b h1:DXr+pvt3nC887026GRP39Ej11UATqWDmWuS99x26cD0=
52+
golang.org/x/exp v0.0.0-20250819193227-8b4c13bb791b/go.mod h1:4QTo5u+SEIbbKW1RacMZq1YEfOBqeXa19JeshGi+zc4=
5153
golang.org/x/net v0.40.0 h1:79Xs7wF06Gbdcg4kdCCIQArK11Z1hr5POQ6+fIYHNuY=
5254
golang.org/x/net v0.40.0/go.mod h1:y0hY0exeL2Pku80/zKK7tpntoX23cqL3Oa6njdgRtds=
5355
golang.org/x/oauth2 v0.30.0 h1:dnDm7JmhM45NNpd8FDDeLhK6FwqbOf4MLCM9zb1BOHI=

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+
"golang.org/x/exp/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
}

0 commit comments

Comments
 (0)