Skip to content

Commit f23aad5

Browse files
vesaribwplotka
andauthored
add optional, explicit OM unit support (#1392)
* start adding support for units Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * do not assume units for now Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * again, do not assume units Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * assume the units Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * add units in more places Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * fix dependencies in go.mod Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Update version of common in go.mod Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Start modifying handler Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Use different common Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Use the newly named func for OM encoding Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Fix examples and comments Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Fix go.mod Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Adapt go.mod as much as possible Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Use go 1.20 Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Use go 1.21 Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Upgrade github workflows to go 1.21 needed for common Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Delete test funcs which are unused according to lint Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Upgrade to Go 1.21 in Makefile and workflows for tests Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Upgrade Go version to 1.21 in tutorial Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Adapt to forthcoming new handlers options, reinstate support for old Go Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Update prometheus/promhttp/http.go Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com> Signed-off-by: Arianna Vespri <36129782+vesari@users.noreply.github.com> * small cleanup Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Make desc funcs variadic Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Adjust http to have unit and test accordingly Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Update prometheus/metric.go Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com> Signed-off-by: Arianna Vespri <36129782+vesari@users.noreply.github.com> * Update prometheus/promhttp/http.go Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com> Signed-off-by: Arianna Vespri <36129782+vesari@users.noreply.github.com> * Fix formatting Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Add tests for unit edge cases, ensure unit field is skipped if no unit is given Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Restore original NewDesc signature, replace unit with DescOpt variadic argument in V2 NewDesc Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Eval unit asap Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * test runtime metrics unit extraction Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * reference modified common code, remove unit flag, adapt tests Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Adopt version of common with the unit flag removed Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Fix merge conflicts Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Update prometheus/desc.go Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com> Signed-off-by: Arianna Vespri <36129782+vesari@users.noreply.github.com> * Always show unit in Desc.String() output + miscellaneous Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Align go.sum Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Solve conflict Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> --------- Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> Signed-off-by: Arianna Vespri <36129782+vesari@users.noreply.github.com> Co-authored-by: Bartlomiej Plotka <bwplotka@gmail.com>
1 parent af7f59c commit f23aad5

21 files changed

Lines changed: 298 additions & 58 deletions

examples/exemplars/main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ func main() {
3232
requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
3333
Name: "http_request_duration_seconds",
3434
Help: "A histogram of the HTTP request durations in seconds.",
35+
Unit: "seconds",
3536
Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5),
3637
})
3738

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ require (
1010
github.com/klauspost/compress v1.18.5
1111
github.com/kylelemons/godebug v1.1.0
1212
github.com/prometheus/client_model v0.6.2
13-
github.com/prometheus/common v0.67.5
13+
github.com/prometheus/common v0.67.6-0.20260224092343-e4c38a0aea47
1414
github.com/prometheus/procfs v0.20.1
1515
go.uber.org/goleak v1.3.0
1616
golang.org/x/sys v0.42.0
1717
google.golang.org/protobuf v1.36.11
1818
)
1919

2020
require (
21-
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
21+
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
2222
github.com/google/uuid v1.6.0 // indirect
2323
github.com/jpillora/backoff v1.0.0 // indirect
2424
github.com/kr/pretty v0.3.1 // indirect
@@ -27,9 +27,9 @@ require (
2727
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
2828
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
2929
go.yaml.in/yaml/v2 v2.4.3 // indirect
30-
golang.org/x/net v0.48.0 // indirect
30+
golang.org/x/net v0.49.0 // indirect
3131
golang.org/x/oauth2 v0.34.0 // indirect
32-
golang.org/x/text v0.32.0 // indirect
32+
golang.org/x/text v0.33.0 // indirect
3333
)
3434

3535
exclude github.com/prometheus/client_golang v1.12.1

go.sum

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
66
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
77
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
88
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
9-
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
10-
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
9+
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
10+
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
1111
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
1212
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
1313
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
@@ -39,8 +39,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
3939
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
4040
github.com/prometheus/client_model v0.6.2 h1:oBsgwpGs7iVziMvrGhE53c/GrLUsZdHnqNwqPLxwZyk=
4141
github.com/prometheus/client_model v0.6.2/go.mod h1:y3m2F6Gdpfy6Ut/GBsUqTWZqCUvMVzSfMLjcu6wAwpE=
42-
github.com/prometheus/common v0.67.5 h1:pIgK94WWlQt1WLwAC5j2ynLaBRDiinoAb86HZHTUGI4=
43-
github.com/prometheus/common v0.67.5/go.mod h1:SjE/0MzDEEAyrdr5Gqc6G+sXI67maCxzaT3A2+HqjUw=
42+
github.com/prometheus/common v0.67.6-0.20260224092343-e4c38a0aea47 h1:DXz2mpU9UOivlNi73hwwVm7KSYGImaPDdYsniI6uOdE=
43+
github.com/prometheus/common v0.67.6-0.20260224092343-e4c38a0aea47/go.mod h1:6wuJRcHcgBM1w2vIgTuCW7XSzlfCat/lS3C39U02oDA=
4444
github.com/prometheus/procfs v0.20.1 h1:XwbrGOIplXW/AU3YhIhLODXMJYyC1isLFfYCsTEycfc=
4545
github.com/prometheus/procfs v0.20.1/go.mod h1:o9EMBZGRyvDrSPH1RqdxhojkuXstoe4UlK79eF5TGGo=
4646
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
@@ -54,14 +54,14 @@ go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
5454
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
5555
go.yaml.in/yaml/v2 v2.4.3 h1:6gvOSjQoTB3vt1l+CU+tSyi/HOjfOjRLJ4YwYZGwRO0=
5656
go.yaml.in/yaml/v2 v2.4.3/go.mod h1:zSxWcmIDjOzPXpjlTTbAsKokqkDNAVtZO0WOMiT90s8=
57-
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
58-
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
57+
golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o=
58+
golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8=
5959
golang.org/x/oauth2 v0.34.0 h1:hqK/t4AKgbqWkdkcAeI8XLmbK+4m4G5YeQRrmiotGlw=
6060
golang.org/x/oauth2 v0.34.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
6161
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
6262
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
63-
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
64-
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
63+
golang.org/x/text v0.33.0 h1:B3njUFyqtHDUI5jMn1YIr5B0IE2U0qck04r6d4KPAxE=
64+
golang.org/x/text v0.33.0/go.mod h1:LuMebE6+rBincTi9+xWTY8TztLzKHc/9C1uBCG27+q8=
6565
google.golang.org/protobuf v1.36.11 h1:fV6ZwhNocDyBLK0dj+fg8ektcVegBBuEolpbTQyBNVE=
6666
google.golang.org/protobuf v1.36.11/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
6767
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

prometheus/counter.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,11 +85,12 @@ type CounterVecOpts struct {
8585
// Both internal tracking values are added up in the Write method. This has to
8686
// be taken into account when it comes to precision and overflow behavior.
8787
func NewCounter(opts CounterOpts) Counter {
88-
desc := NewDesc(
88+
desc := V2.NewDesc(
8989
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
9090
opts.Help,
91-
nil,
91+
UnconstrainedLabels(nil),
9292
opts.ConstLabels,
93+
WithUnit(opts.Unit),
9394
)
9495
if opts.now == nil {
9596
opts.now = time.Now
@@ -205,6 +206,7 @@ func (v2) NewCounterVec(opts CounterVecOpts) *CounterVec {
205206
opts.Help,
206207
opts.VariableLabels,
207208
opts.ConstLabels,
209+
WithUnit(opts.Unit),
208210
)
209211
if opts.now == nil {
210212
opts.now = time.Now
@@ -349,10 +351,11 @@ type CounterFunc interface {
349351
//
350352
// Check out the ExampleGaugeFunc examples for the similar GaugeFunc.
351353
func NewCounterFunc(opts CounterOpts, function func() float64) CounterFunc {
352-
return newValueFunc(NewDesc(
354+
return newValueFunc(V2.NewDesc(
353355
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
354356
opts.Help,
355-
nil,
357+
UnconstrainedLabels(nil),
356358
opts.ConstLabels,
359+
WithUnit(opts.Unit),
357360
), CounterValue, function)
358361
}

prometheus/desc.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ type Desc struct {
4747
fqName string
4848
// help provides some helpful information about this metric.
4949
help string
50+
// unit provides the unit of this metric.
51+
unit string
5052
// constLabelPairs contains precalculated DTO label pairs based on
5153
// the constant labels.
5254
constLabelPairs []*dto.LabelPair
@@ -66,6 +68,16 @@ type Desc struct {
6668
err error
6769
}
6870

71+
// DescOpt allows setting optional fields for NewDesc.
72+
type DescOpt func(*Desc)
73+
74+
// WithUnit sets the unit for a Desc.
75+
func WithUnit(unit string) DescOpt {
76+
return func(d *Desc) {
77+
d.unit = unit
78+
}
79+
}
80+
6981
// NewDesc allocates and initializes a new Desc. Errors are recorded in the Desc
7082
// and will be reported on registration time. variableLabels and constLabels can
7183
// be nil if no such labels should be set. fqName must not be empty.
@@ -89,12 +101,16 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
89101
//
90102
// For constLabels, the label values are constant. Therefore, they are fully
91103
// specified in the Desc. See the Collector example for a usage pattern.
92-
func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, constLabels Labels) *Desc {
104+
func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, constLabels Labels, opts ...DescOpt) *Desc {
93105
d := &Desc{
94106
fqName: fqName,
95107
help: help,
96108
variableLabels: variableLabels.compile(),
97109
}
110+
111+
for _, opt := range opts {
112+
opt(d)
113+
}
98114
//nolint:staticcheck // TODO: Don't use deprecated model.NameValidationScheme.
99115
if !model.NameValidationScheme.IsValidMetricName(fqName) {
100116
d.err = fmt.Errorf("%q is not a valid metric name", fqName)
@@ -150,11 +166,13 @@ func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, const
150166
d.id = xxh.Sum64()
151167
// Sort labelNames so that order doesn't matter for the hash.
152168
sort.Strings(labelNames)
153-
// Now hash together (in this order) the help string and the sorted
169+
// Now hash together (in this order) the help string, the unit string and the sorted
154170
// label names.
155171
xxh.Reset()
156172
xxh.WriteString(help)
157173
xxh.Write(separatorByteSlice)
174+
xxh.WriteString(d.unit)
175+
xxh.Write(separatorByteSlice)
158176
for _, labelName := range labelNames {
159177
xxh.WriteString(labelName)
160178
xxh.Write(separatorByteSlice)
@@ -211,9 +229,10 @@ func (d *Desc) String() string {
211229
}
212230
}
213231
return fmt.Sprintf(
214-
"Desc{fqName: %q, help: %q, constLabels: {%s}, variableLabels: {%s}}",
232+
"Desc{fqName: %q, help: %q, unit: %q, constLabels: {%s}, variableLabels: {%s}}",
215233
d.fqName,
216234
d.help,
235+
d.unit,
217236
strings.Join(lpStrings, ","),
218237
strings.Join(vlStrings, ","),
219238
)

prometheus/desc_test.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func TestNewDescWithNilLabelValues_String(t *testing.T) {
6262
nil,
6363
nil,
6464
)
65-
if desc.String() != `Desc{fqName: "sample_label", help: "sample label", constLabels: {}, variableLabels: {}}` {
65+
if desc.String() != `Desc{fqName: "sample_label", help: "sample label", unit: "", constLabels: {}, variableLabels: {}}` {
6666
t.Errorf("String: unexpected output: %s", desc.String())
6767
}
6868
}
@@ -71,7 +71,20 @@ func TestNewInvalidDesc_String(t *testing.T) {
7171
desc := NewInvalidDesc(
7272
nil,
7373
)
74-
if desc.String() != `Desc{fqName: "", help: "", constLabels: {}, variableLabels: {}}` {
74+
if desc.String() != `Desc{fqName: "", help: "", unit: "", constLabels: {}, variableLabels: {}}` {
7575
t.Errorf("String: unexpected output: %s", desc.String())
7676
}
7777
}
78+
79+
func TestNewDescWithUnit_String(t *testing.T) {
80+
desc := V2.NewDesc(
81+
"sample_metric_bytes",
82+
"sample metric with unit",
83+
UnconstrainedLabels(nil),
84+
nil,
85+
WithUnit("bytes"),
86+
)
87+
if desc.String() != `Desc{fqName: "sample_metric_bytes", help: "sample metric with unit", unit: "bytes", constLabels: {}, variableLabels: {}}` {
88+
t.Errorf("String: unexpected output:\ngot: %s\nwant: %s", desc.String(), desc.String())
89+
}
90+
}

prometheus/example_metricvec_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ type InfoVec struct {
5353
}
5454

5555
func NewInfoVec(name, help string, labelNames []string) *InfoVec {
56-
desc := prometheus.NewDesc(name, help, labelNames, nil)
56+
desc := prometheus.V2.NewDesc(name, help, prometheus.UnconstrainedLabels(labelNames), nil)
5757
return &InfoVec{
5858
MetricVec: prometheus.NewMetricVec(desc, func(lvs ...string) prometheus.Metric {
5959
if len(lvs) != len(labelNames) {

prometheus/examples_test.go

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -310,9 +310,9 @@ func ExampleRegister() {
310310

311311
// Output:
312312
// taskCounter registered.
313-
// taskCounterVec not registered: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "worker_pool_completed_tasks_total", help: "Total number of tasks completed.", constLabels: {}, variableLabels: {worker_id}} has different label names or a different help string
313+
// taskCounterVec not registered: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "worker_pool_completed_tasks_total", help: "Total number of tasks completed.", unit: "", constLabels: {}, variableLabels: {worker_id}} has different label names or a different help string
314314
// taskCounter unregistered.
315-
// taskCounterVec not registered: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "worker_pool_completed_tasks_total", help: "Total number of tasks completed.", constLabels: {}, variableLabels: {worker_id}} has different label names or a different help string
315+
// taskCounterVec not registered: a previously registered descriptor with the same fully-qualified name as Desc{fqName: "worker_pool_completed_tasks_total", help: "Total number of tasks completed.", unit: "", constLabels: {}, variableLabels: {worker_id}} has different label names or a different help string
316316
// taskCounterVec registered.
317317
// Worker initialization failed: inconsistent label cardinality: expected 1 label values but got 2 in []string{"42", "spurious arg"}
318318
// notMyCounter is nil.
@@ -381,11 +381,12 @@ func ExampleSummaryVec() {
381381
}
382382

383383
func ExampleNewConstSummary() {
384-
desc := prometheus.NewDesc(
384+
desc := prometheus.V2.NewDesc(
385385
"http_request_duration_seconds",
386386
"A summary of the HTTP request durations.",
387-
[]string{"code", "method"},
387+
prometheus.UnconstrainedLabels([]string{"code", "method"}),
388388
prometheus.Labels{"owner": "example"},
389+
prometheus.WithUnit("seconds"),
389390
)
390391

391392
// Create a constant summary from values we got from a 3rd party telemetry system.
@@ -440,7 +441,8 @@ func ExampleHistogram() {
440441
temps := prometheus.NewHistogram(prometheus.HistogramOpts{
441442
Name: "pond_temperature_celsius",
442443
Help: "The temperature of the frog pond.", // Sorry, we can't measure how badly it smells.
443-
Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide.
444+
Unit: "celsius",
445+
Buckets: prometheus.LinearBuckets(20, 5, 5), // 5 buckets, each 5 centigrade wide.
444446
})
445447

446448
// Simulate some observations.
@@ -461,11 +463,12 @@ func ExampleHistogram() {
461463
}
462464

463465
func ExampleNewConstHistogram() {
464-
desc := prometheus.NewDesc(
466+
desc := prometheus.V2.NewDesc(
465467
"http_request_duration_seconds",
466468
"A histogram of the HTTP request durations.",
467-
[]string{"code", "method"},
469+
prometheus.UnconstrainedLabels([]string{"code", "method"}),
468470
prometheus.Labels{"owner": "example"},
471+
prometheus.WithUnit("seconds"),
469472
)
470473

471474
// Create a constant histogram from values we got from a 3rd party telemetry system.
@@ -516,11 +519,12 @@ func ExampleNewConstHistogramWithCreatedTimestamp() {
516519
}
517520

518521
func ExampleNewConstHistogram_withExemplar() {
519-
desc := prometheus.NewDesc(
522+
desc := prometheus.V2.NewDesc(
520523
"http_request_duration_seconds",
521524
"A histogram of the HTTP request durations.",
522-
[]string{"code", "method"},
525+
prometheus.UnconstrainedLabels([]string{"code", "method"}),
523526
prometheus.Labels{"owner": "example"},
527+
prometheus.WithUnit("seconds"),
524528
)
525529

526530
// Create a constant histogram from values we got from a 3rd party telemetry system.
@@ -686,10 +690,11 @@ temperature_kelvin 4.5
686690
}
687691

688692
func ExampleNewMetricWithTimestamp() {
689-
desc := prometheus.NewDesc(
693+
desc := prometheus.V2.NewDesc(
690694
"temperature_kelvin",
691695
"Current temperature in Kelvin.",
692-
nil, nil,
696+
prometheus.UnconstrainedLabels(nil), nil,
697+
prometheus.WithUnit("kelvin"),
693698
)
694699

695700
// Create a constant gauge from values we got from an external
@@ -719,10 +724,11 @@ func ExampleNewConstMetricWithCreatedTimestamp() {
719724
// Here we have a metric that is reported by an external system.
720725
// Besides providing the value, the external system also provides the
721726
// timestamp when the metric was created.
722-
desc := prometheus.NewDesc(
727+
desc := prometheus.V2.NewDesc(
723728
"time_since_epoch_seconds",
724729
"Current epoch time in seconds.",
725-
nil, nil,
730+
prometheus.UnconstrainedLabels(nil), nil,
731+
prometheus.WithUnit("seconds"),
726732
)
727733

728734
timeSinceEpochReportedByExternalSystem := time.Date(2009, time.November, 10, 23, 0, 0, 12345678, time.UTC)

prometheus/gauge.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,12 @@ type GaugeVecOpts struct {
7676
// scenarios for Gauges and Counters, where the former tends to be Set-heavy and
7777
// the latter Inc-heavy.
7878
func NewGauge(opts GaugeOpts) Gauge {
79-
desc := NewDesc(
79+
desc := V2.NewDesc(
8080
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
8181
opts.Help,
82-
nil,
82+
UnconstrainedLabels(nil),
8383
opts.ConstLabels,
84+
WithUnit(opts.Unit),
8485
)
8586
result := &gauge{desc: desc, labelPairs: desc.constLabelPairs}
8687
result.init(result) // Init self-collection.
@@ -163,6 +164,7 @@ func (v2) NewGaugeVec(opts GaugeVecOpts) *GaugeVec {
163164
opts.Help,
164165
opts.VariableLabels,
165166
opts.ConstLabels,
167+
WithUnit(opts.Unit),
166168
)
167169
return &GaugeVec{
168170
MetricVec: NewMetricVec(desc, func(lvs ...string) Metric {
@@ -302,10 +304,11 @@ type GaugeFunc interface {
302304
// value of 1. Example:
303305
// https://github.com/prometheus/common/blob/8558a5b7db3c84fa38b4766966059a7bd5bfa2ee/version/info.go#L36-L56
304306
func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc {
305-
return newValueFunc(NewDesc(
307+
return newValueFunc(V2.NewDesc(
306308
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
307309
opts.Help,
308-
nil,
310+
UnconstrainedLabels(nil),
309311
opts.ConstLabels,
312+
WithUnit(opts.Unit),
310313
), GaugeValue, function)
311314
}

prometheus/gauge_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ func TestGaugeFunc(t *testing.T) {
171171
func() float64 { return 3.1415 },
172172
)
173173

174-
if expected, got := `Desc{fqName: "test_name", help: "test help", constLabels: {a="1",b="2"}, variableLabels: {}}`, gf.Desc().String(); expected != got {
174+
if expected, got := `Desc{fqName: "test_name", help: "test help", unit: "", constLabels: {a="1",b="2"}, variableLabels: {}}`, gf.Desc().String(); expected != got {
175175
t.Errorf("expected %q, got %q", expected, got)
176176
}
177177

0 commit comments

Comments
 (0)