Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optionally add OM unit #1392

Closed
wants to merge 28 commits into from
Closed
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d74a7fb
start adding support for units
vesari Nov 26, 2023
5d8c6de
do not assume units for now
vesari Nov 30, 2023
f78b666
again, do not assume units
vesari Nov 30, 2023
e876012
assume the units
vesari Dec 2, 2023
b1f5a86
add units in more places
vesari Dec 2, 2023
788d53b
fix dependencies in go.mod
vesari Dec 2, 2023
1692495
Merge remote-tracking branch 'prometheus/main' into add-unit
vesari Jan 19, 2024
5971215
Update version of common in go.mod
vesari Jan 20, 2024
efb64f3
Start modifying handler
vesari Jan 27, 2024
fafe45b
Merge remote-tracking branch 'prometheus/main' into add-unit
vesari Feb 14, 2024
51861d5
Use different common
vesari Mar 7, 2024
1362436
Merge remote-tracking branch 'prometheus/main' into add-unit
vesari Mar 7, 2024
a2bfd3a
Use the newly named func for OM encoding
vesari Mar 14, 2024
7900714
Merge remote-tracking branch 'prometheus/main' into add-unit
vesari Mar 14, 2024
19d7102
Fix examples and comments
vesari Mar 14, 2024
031e0d3
Fix go.mod
vesari Mar 14, 2024
fd60a57
Adapt go.mod as much as possible
vesari Mar 14, 2024
f7e2848
Use go 1.20
vesari Mar 15, 2024
5f58996
Use go 1.21
vesari Mar 15, 2024
ac9d1d8
Upgrade github workflows to go 1.21 needed for common
vesari Mar 15, 2024
3db440e
Delete test funcs which are unused according to lint
vesari Mar 15, 2024
7752d37
Upgrade to Go 1.21 in Makefile and workflows for tests
vesari Mar 15, 2024
47c8cf1
Upgrade Go version to 1.21 in tutorial
vesari Mar 15, 2024
7312bd5
Merge remote-tracking branch 'prometheus/main' into add-unit
vesari Apr 13, 2024
3644321
Adapt to forthcoming new handlers options, reinstate support for old Go
vesari Apr 22, 2024
dca67a5
Merge remote-tracking branch 'prometheus/main' into add-unit
vesari Apr 22, 2024
ab34546
Merge remote-tracking branch 'prometheus/main' into add-unit
vesari May 7, 2024
717d3d3
Update prometheus/promhttp/http.go
vesari May 9, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:

strategy:
matrix:
go_version: ["1.20", "1.21", "1.22"]
go_version: ["1.21", "1.22"]

steps:
- name: Checkout code
Expand All @@ -42,5 +42,5 @@ jobs:
CI: true

- name: Run style and unused
if: ${{ matrix.go_version == '1.20' }}
if: ${{ matrix.go_version == '1.21' }}
run: make style unused
2 changes: 1 addition & 1 deletion .github/workflows/golangci-lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: install Go
uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0
with:
go-version: 1.20.x
go-version: 1.21.x
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next time, this probably should be done in a separate PR 🤗

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why this change was even needed 😬, could you explain why you decided to bump the go version here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as with the go.mod below. Just some change I should have reverted, sorry.

- name: Install snmp_exporter/generator dependencies
run: sudo apt-get update && sudo apt-get -y install libsnmp-dev
if: github.repository == 'prometheus/snmp_exporter'
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test: deps common-test
test-short: deps common-test-short

.PHONY: generate-go-collector-test-files
VERSIONS := 1.20 1.21 1.22
VERSIONS := 1.21 1.22
generate-go-collector-test-files:
for GO_VERSION in $(VERSIONS); do \
docker run \
Expand Down
2 changes: 2 additions & 0 deletions examples/exemplars/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ func main() {
requestDurations := prometheus.NewHistogram(prometheus.HistogramOpts{
Name: "http_request_duration_seconds",
Help: "A histogram of the HTTP request durations in seconds.",
Unit: "seconds",
Buckets: prometheus.ExponentialBuckets(0.1, 1.5, 5),
})

Expand Down Expand Up @@ -62,6 +63,7 @@ func main() {
registry,
promhttp.HandlerOpts{
EnableOpenMetrics: true,
WithUnit: true,
}),
)
// To test: curl -H 'Accept: application/openmetrics-text' localhost:8080/metrics
Expand Down
10 changes: 5 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
module github.com/prometheus/client_golang

go 1.20
go 1.21

require (
github.com/beorn7/perks v1.0.1
github.com/cespare/xxhash/v2 v2.2.0
github.com/davecgh/go-spew v1.1.1
github.com/json-iterator/go v1.1.12
github.com/prometheus/client_model v0.6.0
github.com/prometheus/common v0.48.0
github.com/prometheus/common v0.50.0
github.com/prometheus/procfs v0.12.0
golang.org/x/sys v0.17.0
golang.org/x/sys v0.18.0
google.golang.org/protobuf v1.33.0
)

Expand All @@ -21,8 +21,8 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f // indirect
golang.org/x/net v0.20.0 // indirect
golang.org/x/oauth2 v0.16.0 // indirect
golang.org/x/net v0.22.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
Expand Down
19 changes: 11 additions & 8 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg
github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand All @@ -33,24 +34,25 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/prometheus/client_model v0.6.0 h1:k1v3CzpSRUTrKMppY35TLwPvxHqBu0bYgxZzqGIgaos=
github.com/prometheus/client_model v0.6.0/go.mod h1:NTQHnmxFpouOD0DpvP4XujX3CdOAGQPoaGhyTchlyt8=
github.com/prometheus/common v0.48.0 h1:QO8U2CdOzSn1BBsmXJXduaaW+dY/5QLjfB8svtSzKKE=
github.com/prometheus/common v0.48.0/go.mod h1:0/KsvlIEfPQCQ5I2iNSAWKPZziNCvRs5EC6ILDTlAPc=
github.com/prometheus/common v0.50.0 h1:YSZE6aa9+luNa2da6/Tik0q0A5AbR+U003TItK57CPQ=
github.com/prometheus/common v0.50.0/go.mod h1:wHFBCEVWVmHMUpg7pYcOm2QUR/ocQdYSJVQJKnHc3xQ=
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
golang.org/x/net v0.20.0 h1:aCL9BSgETF1k+blQaYUBx9hJ9LOGP3gAVemcZlf1Kpo=
golang.org/x/net v0.20.0/go.mod h1:z8BVo6PvndSri0LbOE3hAn0apkU+1YvI6E70E9jsnvY=
golang.org/x/oauth2 v0.16.0 h1:aDkGMBSYxElaoP81NpoUoz2oo2R2wHdZpGToUxfyQrQ=
golang.org/x/oauth2 v0.16.0/go.mod h1:hqZ+0LWXsiVoZpeld6jVt06P3adbS2Uu911W1SsJv2o=
golang.org/x/net v0.22.0 h1:9sGLhx7iRIHEiX0oAJ3MRZMUCElJgy7Br1nO+AMN3Tc=
golang.org/x/net v0.22.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/oauth2 v0.18.0 h1:09qnuIAgzdx1XplqJvW6CQqMCtGZykZWcXzPMPUusvI=
golang.org/x/oauth2 v0.18.0/go.mod h1:Wf7knwG0MPoWIMMBgFlEaSUDaKskp0dCfrlJRJXbBi8=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand All @@ -65,5 +67,6 @@ google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGm
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
1 change: 1 addition & 0 deletions prometheus/build_info_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewBuildInfoCollector() Collector {
NewDesc(
"go_build_info",
"Build information about the main Go module.",
"",
nil, Labels{"path": path, "version": version, "checksum": sum},
),
GaugeValue, 1)}
Expand Down
9 changes: 9 additions & 0 deletions prometheus/collectors/dbstats_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,46 +46,55 @@ func NewDBStatsCollector(db *sql.DB, dbName string) prometheus.Collector {
maxOpenConnections: prometheus.NewDesc(
fqName("max_open_connections"),
"Maximum number of open connections to the database.",
"",
nil, prometheus.Labels{"db_name": dbName},
),
openConnections: prometheus.NewDesc(
fqName("open_connections"),
"The number of established connections both in use and idle.",
"",
nil, prometheus.Labels{"db_name": dbName},
),
inUseConnections: prometheus.NewDesc(
fqName("in_use_connections"),
"The number of connections currently in use.",
"",
nil, prometheus.Labels{"db_name": dbName},
),
idleConnections: prometheus.NewDesc(
fqName("idle_connections"),
"The number of idle connections.",
"",
nil, prometheus.Labels{"db_name": dbName},
),
waitCount: prometheus.NewDesc(
fqName("wait_count_total"),
"The total number of connections waited for.",
"",
nil, prometheus.Labels{"db_name": dbName},
),
waitDuration: prometheus.NewDesc(
fqName("wait_duration_seconds_total"),
"The total time blocked waiting for a new connection.",
"seconds",
nil, prometheus.Labels{"db_name": dbName},
),
maxIdleClosed: prometheus.NewDesc(
fqName("max_idle_closed_total"),
"The total number of connections closed due to SetMaxIdleConns.",
"",
nil, prometheus.Labels{"db_name": dbName},
),
maxIdleTimeClosed: prometheus.NewDesc(
fqName("max_idle_time_closed_total"),
"The total number of connections closed due to SetConnMaxIdleTime.",
"",
nil, prometheus.Labels{"db_name": dbName},
),
maxLifetimeClosed: prometheus.NewDesc(
fqName("max_lifetime_closed_total"),
"The total number of connections closed due to SetConnMaxLifetime.",
"",
nil, prometheus.Labels{"db_name": dbName},
),
}
Expand Down
23 changes: 0 additions & 23 deletions prometheus/collectors/go_collector_go121_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,26 +149,3 @@ func withSchedulerMetrics() []string {
"go_threads",
}
}

func withDebugMetrics() []string {
return []string{
"go_godebug_non_default_behavior_execerrdot_events_total",
"go_godebug_non_default_behavior_gocachehash_events_total",
"go_godebug_non_default_behavior_gocachetest_events_total",
"go_godebug_non_default_behavior_gocacheverify_events_total",
"go_godebug_non_default_behavior_http2client_events_total",
"go_godebug_non_default_behavior_http2server_events_total",
"go_godebug_non_default_behavior_installgoroot_events_total",
"go_godebug_non_default_behavior_jstmpllitinterp_events_total",
"go_godebug_non_default_behavior_multipartmaxheaders_events_total",
"go_godebug_non_default_behavior_multipartmaxparts_events_total",
"go_godebug_non_default_behavior_multipathtcp_events_total",
"go_godebug_non_default_behavior_panicnil_events_total",
"go_godebug_non_default_behavior_randautoseed_events_total",
"go_godebug_non_default_behavior_tarinsecurepath_events_total",
"go_godebug_non_default_behavior_tlsmaxrsasize_events_total",
"go_godebug_non_default_behavior_x509sha1_events_total",
"go_godebug_non_default_behavior_x509usefallbackroots_events_total",
"go_godebug_non_default_behavior_zipinsecurepath_events_total",
}
}
30 changes: 0 additions & 30 deletions prometheus/collectors/go_collector_go122_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,33 +164,3 @@ func withSchedulerMetrics() []string {
"go_threads",
}
}

func withDebugMetrics() []string {
return []string{
"go_godebug_non_default_behavior_execerrdot_events_total",
"go_godebug_non_default_behavior_gocachehash_events_total",
"go_godebug_non_default_behavior_gocachetest_events_total",
"go_godebug_non_default_behavior_gocacheverify_events_total",
"go_godebug_non_default_behavior_gotypesalias_events_total",
"go_godebug_non_default_behavior_http2client_events_total",
"go_godebug_non_default_behavior_http2server_events_total",
"go_godebug_non_default_behavior_httplaxcontentlength_events_total",
"go_godebug_non_default_behavior_httpmuxgo121_events_total",
"go_godebug_non_default_behavior_installgoroot_events_total",
"go_godebug_non_default_behavior_jstmpllitinterp_events_total",
"go_godebug_non_default_behavior_multipartmaxheaders_events_total",
"go_godebug_non_default_behavior_multipartmaxparts_events_total",
"go_godebug_non_default_behavior_multipathtcp_events_total",
"go_godebug_non_default_behavior_panicnil_events_total",
"go_godebug_non_default_behavior_randautoseed_events_total",
"go_godebug_non_default_behavior_tarinsecurepath_events_total",
"go_godebug_non_default_behavior_tls10server_events_total",
"go_godebug_non_default_behavior_tlsmaxrsasize_events_total",
"go_godebug_non_default_behavior_tlsrsakex_events_total",
"go_godebug_non_default_behavior_tlsunsafeekm_events_total",
"go_godebug_non_default_behavior_x509sha1_events_total",
"go_godebug_non_default_behavior_x509usefallbackroots_events_total",
"go_godebug_non_default_behavior_x509usepolicies_events_total",
"go_godebug_non_default_behavior_zipinsecurepath_events_total",
}
}
3 changes: 3 additions & 0 deletions prometheus/counter.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ func NewCounter(opts CounterOpts) Counter {
desc := NewDesc(
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,
opts.Unit,
nil,
opts.ConstLabels,
)
Expand Down Expand Up @@ -203,6 +204,7 @@ func (v2) NewCounterVec(opts CounterVecOpts) *CounterVec {
desc := V2.NewDesc(
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,
opts.Unit,
opts.VariableLabels,
opts.ConstLabels,
)
Expand Down Expand Up @@ -352,6 +354,7 @@ func NewCounterFunc(opts CounterOpts, function func() float64) CounterFunc {
return newValueFunc(NewDesc(
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
opts.Help,
opts.Unit,
nil,
opts.ConstLabels,
), CounterValue, function)
Expand Down
15 changes: 10 additions & 5 deletions prometheus/desc.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type Desc struct {
fqName string
// help provides some helpful information about this metric.
help string
// unit provides the unit of this metric.
unit string
// constLabelPairs contains precalculated DTO label pairs based on
// the constant labels.
constLabelPairs []*dto.LabelPair
Expand Down Expand Up @@ -75,8 +77,8 @@ type Desc struct {
//
// For constLabels, the label values are constant. Therefore, they are fully
// specified in the Desc. See the Collector example for a usage pattern.
func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *Desc {
return V2.NewDesc(fqName, help, UnconstrainedLabels(variableLabels), constLabels)
func NewDesc(fqName, help, unit string, variableLabels []string, constLabels Labels) *Desc {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unfortunately this is a breaking change. We can't change this function.

Not sure how to solve it. Probably NewDescWithUnit, or changing v2 only would make sense.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, we can't make this change.

I wonder what would be a good option though, maybe something like below?

func (d *Desc) WithUnit(u string) *Desc {
  d.unit = u
  return d
}

A new function NewDescWithUnit also sounds doable, but honestly, a v2 would be the cleaner option. I'm not sure how viable v2 is though

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I'll revert the breaking changes and explore the alternatives you both suggested.

return V2.NewDesc(fqName, help, unit, UnconstrainedLabels(variableLabels), constLabels)
}

// NewDesc allocates and initializes a new Desc. Errors are recorded in the Desc
Expand All @@ -89,10 +91,11 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
//
// For constLabels, the label values are constant. Therefore, they are fully
// specified in the Desc. See the Collector example for a usage pattern.
func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, constLabels Labels) *Desc {
func (v2) NewDesc(fqName, help, unit string, variableLabels ConstrainableLabels, constLabels Labels) *Desc {
d := &Desc{
fqName: fqName,
help: help,
unit: unit,
variableLabels: variableLabels.compile(),
}
if !model.IsValidMetricName(model.LabelValue(fqName)) {
Expand Down Expand Up @@ -149,10 +152,11 @@ func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, const
d.id = xxh.Sum64()
// Sort labelNames so that order doesn't matter for the hash.
sort.Strings(labelNames)
// Now hash together (in this order) the help string and the sorted
// Now hash together (in this order) the help string, the unit string and the sorted
// label names.
xxh.Reset()
xxh.WriteString(help)
xxh.WriteString(unit)
xxh.Write(separatorByteSlice)
for _, labelName := range labelNames {
xxh.WriteString(labelName)
Expand Down Expand Up @@ -198,9 +202,10 @@ func (d *Desc) String() string {
}
}
return fmt.Sprintf(
"Desc{fqName: %q, help: %q, constLabels: {%s}, variableLabels: {%s}}",
"Desc{fqName: %q, help: %q, unit: %q, constLabels: {%s}, variableLabels: {%s}}",
d.fqName,
d.help,
d.unit,
strings.Join(lpStrings, ","),
strings.Join(vlStrings, ","),
)
Expand Down
1 change: 1 addition & 0 deletions prometheus/desc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func TestNewDescInvalidLabelValues(t *testing.T) {
desc := NewDesc(
"sample_label",
"sample label",
"",
nil,
Labels{"a": "\xFF"},
)
Expand Down
2 changes: 2 additions & 0 deletions prometheus/example_clustermanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,13 @@ var (
oomCountDesc = prometheus.NewDesc(
"clustermanager_oom_crashes_total",
"Number of OOM crashes.",
"",
[]string{"host"}, nil,
)
ramUsageDesc = prometheus.NewDesc(
"clustermanager_ram_usage_bytes",
"RAM usage as reported to the cluster manager.",
"bytes",
[]string{"host"}, nil,
)
)
Expand Down
5 changes: 3 additions & 2 deletions prometheus/example_metricvec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ type InfoVec struct {
*prometheus.MetricVec
}

func NewInfoVec(name, help string, labelNames []string) *InfoVec {
desc := prometheus.NewDesc(name, help, labelNames, nil)
func NewInfoVec(name, help, unit string, labelNames []string) *InfoVec {
desc := prometheus.NewDesc(name, help, unit, labelNames, nil)
Comment on lines -55 to +56
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar problem here, we can't change Public function's signatures

return &InfoVec{
MetricVec: prometheus.NewMetricVec(desc, func(lvs ...string) prometheus.Metric {
if len(lvs) != len(labelNames) {
Expand Down Expand Up @@ -110,6 +110,7 @@ func ExampleMetricVec() {
infoVec := NewInfoVec(
"library_version_info",
"Versions of the libraries used in this binary.",
"",
[]string{"library", "version"},
)

Expand Down
Loading
Loading