Skip to content

Commit 7c65de7

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

File tree

2 files changed

+22
-10
lines changed

2 files changed

+22
-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
}

0 commit comments

Comments
 (0)