Skip to content

Commit 6349728

Browse files
committed
start adding support for units
Signed-off-by: Arianna Vespri <[email protected]>
1 parent 80d3f0b commit 6349728

26 files changed

+113
-24
lines changed

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,7 @@ require (
3030
)
3131

3232
exclude github.com/prometheus/client_golang v1.12.1
33+
34+
replace github.com/prometheus/common => github.com/vesari/common v0.0.0-20231121140958-784d3aad4c30
35+
36+
replace github.com/prometheus/client_model => github.com/vesari/client_model v0.0.0-20231118150741-19723af61627

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,17 +33,17 @@ github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRW
3333
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
3434
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
3535
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
36-
github.com/prometheus/client_model v0.5.0 h1:VQw1hfvPvk3Uv6Qf29VrPF32JB6rtbgI6cYPYQjL0Qw=
37-
github.com/prometheus/client_model v0.5.0/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
38-
github.com/prometheus/common v0.45.0 h1:2BGz0eBc2hdMDLnO/8n0jeB3oPrt2D08CekT0lneoxM=
39-
github.com/prometheus/common v0.45.0/go.mod h1:YJmSTw9BoKxJplESWWxlbyttQR4uaEcGyv9MZjVOJsY=
4036
github.com/prometheus/procfs v0.12.0 h1:jluTpSng7V9hY0O2R9DzzJHYb2xULk9VTR1V1R/k6Bo=
4137
github.com/prometheus/procfs v0.12.0/go.mod h1:pcuDEFsWDnvcgNzo4EEweacyhjeA9Zk3cnaOZAZEfOo=
4238
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
4339
github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjRBZyWFQ=
4440
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
4541
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
4642
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
43+
github.com/vesari/client_model v0.0.0-20231118150741-19723af61627 h1:pJsecGZFkJIN3ipp7fS8hLDK0Dcnk2JVxXds3Dev3dQ=
44+
github.com/vesari/client_model v0.0.0-20231118150741-19723af61627/go.mod h1:dTiFglRmd66nLR9Pv9f0mZi7B7fk5Pm3gvsjB5tr+kI=
45+
github.com/vesari/common v0.0.0-20231121140958-784d3aad4c30 h1:WaoSRlUM9CilXfZwJ5WIsFJKLnwJ6/WZjyT1oiiRO7s=
46+
github.com/vesari/common v0.0.0-20231121140958-784d3aad4c30/go.mod h1:F2CatmaM24xsPNg4EqskJKMF+Tl27WbRF+Exgus+vOw=
4747
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
4848
golang.org/x/net v0.0.0-20190603091049-60506f45cf65/go.mod h1:HSz+uSET+XFnRR8LxR5pz3Of3rY3CfYBVs4xY44aLks=
4949
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=

prometheus/build_info_collector.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ func NewBuildInfoCollector() Collector {
3030
NewDesc(
3131
"go_build_info",
3232
"Build information about the main Go module.",
33+
"",
3334
nil, Labels{"path": path, "version": version, "checksum": sum},
3435
),
3536
GaugeValue, 1)}

prometheus/collectors/dbstats_collector.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,46 +46,55 @@ func NewDBStatsCollector(db *sql.DB, dbName string) prometheus.Collector {
4646
maxOpenConnections: prometheus.NewDesc(
4747
fqName("max_open_connections"),
4848
"Maximum number of open connections to the database.",
49+
"",
4950
nil, prometheus.Labels{"db_name": dbName},
5051
),
5152
openConnections: prometheus.NewDesc(
5253
fqName("open_connections"),
5354
"The number of established connections both in use and idle.",
55+
"",
5456
nil, prometheus.Labels{"db_name": dbName},
5557
),
5658
inUseConnections: prometheus.NewDesc(
5759
fqName("in_use_connections"),
5860
"The number of connections currently in use.",
61+
"",
5962
nil, prometheus.Labels{"db_name": dbName},
6063
),
6164
idleConnections: prometheus.NewDesc(
6265
fqName("idle_connections"),
6366
"The number of idle connections.",
67+
"",
6468
nil, prometheus.Labels{"db_name": dbName},
6569
),
6670
waitCount: prometheus.NewDesc(
6771
fqName("wait_count_total"),
6872
"The total number of connections waited for.",
73+
"",
6974
nil, prometheus.Labels{"db_name": dbName},
7075
),
7176
waitDuration: prometheus.NewDesc(
7277
fqName("wait_duration_seconds_total"),
7378
"The total time blocked waiting for a new connection.",
79+
"seconds",
7480
nil, prometheus.Labels{"db_name": dbName},
7581
),
7682
maxIdleClosed: prometheus.NewDesc(
7783
fqName("max_idle_closed_total"),
7884
"The total number of connections closed due to SetMaxIdleConns.",
85+
"",
7986
nil, prometheus.Labels{"db_name": dbName},
8087
),
8188
maxIdleTimeClosed: prometheus.NewDesc(
8289
fqName("max_idle_time_closed_total"),
8390
"The total number of connections closed due to SetConnMaxIdleTime.",
91+
"",
8492
nil, prometheus.Labels{"db_name": dbName},
8593
),
8694
maxLifetimeClosed: prometheus.NewDesc(
8795
fqName("max_lifetime_closed_total"),
8896
"The total number of connections closed due to SetConnMaxLifetime.",
97+
"",
8998
nil, prometheus.Labels{"db_name": dbName},
9099
),
91100
}

prometheus/counter.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func NewCounter(opts CounterOpts) Counter {
8888
desc := NewDesc(
8989
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
9090
opts.Help,
91+
opts.Unit,
9192
nil,
9293
opts.ConstLabels,
9394
)
@@ -203,6 +204,7 @@ func (v2) NewCounterVec(opts CounterVecOpts) *CounterVec {
203204
desc := V2.NewDesc(
204205
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
205206
opts.Help,
207+
opts.Unit,
206208
opts.VariableLabels,
207209
opts.ConstLabels,
208210
)
@@ -352,6 +354,7 @@ func NewCounterFunc(opts CounterOpts, function func() float64) CounterFunc {
352354
return newValueFunc(NewDesc(
353355
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
354356
opts.Help,
357+
opts.Unit,
355358
nil,
356359
opts.ConstLabels,
357360
), CounterValue, function)

prometheus/desc.go

Lines changed: 10 additions & 5 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
@@ -75,8 +77,8 @@ type Desc struct {
7577
//
7678
// For constLabels, the label values are constant. Therefore, they are fully
7779
// specified in the Desc. See the Collector example for a usage pattern.
78-
func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *Desc {
79-
return V2.NewDesc(fqName, help, UnconstrainedLabels(variableLabels), constLabels)
80+
func NewDesc(fqName, help string, unit string, variableLabels []string, constLabels Labels) *Desc {
81+
return V2.NewDesc(fqName, help, unit, UnconstrainedLabels(variableLabels), constLabels)
8082
}
8183

8284
// NewDesc allocates and initializes a new Desc. Errors are recorded in the Desc
@@ -89,10 +91,11 @@ func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *
8991
//
9092
// For constLabels, the label values are constant. Therefore, they are fully
9193
// specified in the Desc. See the Collector example for a usage pattern.
92-
func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, constLabels Labels) *Desc {
94+
func (v2) NewDesc(fqName, help string, unit string, variableLabels ConstrainableLabels, constLabels Labels) *Desc {
9395
d := &Desc{
9496
fqName: fqName,
9597
help: help,
98+
unit: unit,
9699
variableLabels: variableLabels.compile(),
97100
}
98101
if !model.IsValidMetricName(model.LabelValue(fqName)) {
@@ -149,10 +152,11 @@ func (v2) NewDesc(fqName, help string, variableLabels ConstrainableLabels, const
149152
d.id = xxh.Sum64()
150153
// Sort labelNames so that order doesn't matter for the hash.
151154
sort.Strings(labelNames)
152-
// Now hash together (in this order) the help string and the sorted
155+
// Now hash together (in this order) the help string, the unit string and the sorted
153156
// label names.
154157
xxh.Reset()
155158
xxh.WriteString(help)
159+
xxh.WriteString(unit)
156160
xxh.Write(separatorByteSlice)
157161
for _, labelName := range labelNames {
158162
xxh.WriteString(labelName)
@@ -198,9 +202,10 @@ func (d *Desc) String() string {
198202
}
199203
}
200204
return fmt.Sprintf(
201-
"Desc{fqName: %q, help: %q, constLabels: {%s}, variableLabels: {%s}}",
205+
"Desc{fqName: %q, help: %q, unit: %q, constLabels: {%s}, variableLabels: {%s}}",
202206
d.fqName,
203207
d.help,
208+
d.unit,
204209
strings.Join(lpStrings, ","),
205210
strings.Join(vlStrings, ","),
206211
)

prometheus/desc_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ func TestNewDescInvalidLabelValues(t *testing.T) {
2121
desc := NewDesc(
2222
"sample_label",
2323
"sample label",
24+
"",
2425
nil,
2526
Labels{"a": "\xFF"},
2627
)

prometheus/example_clustermanager_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,13 @@ var (
6666
oomCountDesc = prometheus.NewDesc(
6767
"clustermanager_oom_crashes_total",
6868
"Number of OOM crashes.",
69+
"",
6970
[]string{"host"}, nil,
7071
)
7172
ramUsageDesc = prometheus.NewDesc(
7273
"clustermanager_ram_usage_bytes",
7374
"RAM usage as reported to the cluster manager.",
75+
"bytes",
7476
[]string{"host"}, nil,
7577
)
7678
)

prometheus/example_metricvec_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ type InfoVec struct {
5252
*prometheus.MetricVec
5353
}
5454

55-
func NewInfoVec(name, help string, labelNames []string) *InfoVec {
56-
desc := prometheus.NewDesc(name, help, labelNames, nil)
55+
func NewInfoVec(name, help, unit string, labelNames []string) *InfoVec {
56+
desc := prometheus.NewDesc(name, help, unit, labelNames, nil)
5757
return &InfoVec{
5858
MetricVec: prometheus.NewMetricVec(desc, func(lvs ...string) prometheus.Metric {
5959
if len(lvs) != len(labelNames) {
@@ -110,6 +110,7 @@ func ExampleMetricVec() {
110110
infoVec := NewInfoVec(
111111
"library_version_info",
112112
"Versions of the libraries used in this binary.",
113+
"",
113114
[]string{"library", "version"},
114115
)
115116

prometheus/examples_test.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,9 +308,9 @@ func ExampleRegister() {
308308

309309
// Output:
310310
// taskCounter registered.
311-
// 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
311+
// 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
312312
// taskCounter unregistered.
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
// taskCounterVec registered.
315315
// Worker initialization failed: inconsistent label cardinality: expected 1 label values but got 2 in []string{"42", "spurious arg"}
316316
// notMyCounter is nil.
@@ -382,6 +382,7 @@ func ExampleNewConstSummary() {
382382
desc := prometheus.NewDesc(
383383
"http_request_duration_seconds",
384384
"A summary of the HTTP request durations.",
385+
"seconds",
385386
[]string{"code", "method"},
386387
prometheus.Labels{"owner": "example"},
387388
)
@@ -433,6 +434,7 @@ func ExampleNewConstHistogram() {
433434
desc := prometheus.NewDesc(
434435
"http_request_duration_seconds",
435436
"A histogram of the HTTP request durations.",
437+
"seconds",
436438
[]string{"code", "method"},
437439
prometheus.Labels{"owner": "example"},
438440
)
@@ -460,6 +462,7 @@ func ExampleNewConstHistogram_WithExemplar() {
460462
desc := prometheus.NewDesc(
461463
"http_request_duration_seconds",
462464
"A histogram of the HTTP request durations.",
465+
"seconds",
463466
[]string{"code", "method"},
464467
prometheus.Labels{"owner": "example"},
465468
)
@@ -630,6 +633,7 @@ func ExampleNewMetricWithTimestamp() {
630633
desc := prometheus.NewDesc(
631634
"temperature_kelvin",
632635
"Current temperature in Kelvin.",
636+
"kelvin",
633637
nil, nil,
634638
)
635639

@@ -663,6 +667,7 @@ func ExampleNewConstMetricWithCreatedTimestamp() {
663667
desc := prometheus.NewDesc(
664668
"time_since_epoch_seconds",
665669
"Current epoch time in seconds.",
670+
"seconds",
666671
nil, nil,
667672
)
668673

prometheus/expvar_collector_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,19 @@ func ExampleNewExpvarCollector() {
2929
"memstats": prometheus.NewDesc(
3030
"expvar_memstats",
3131
"All numeric memstats as one metric family. Not a good role-model, actually... ;-)",
32+
"",
3233
[]string{"type"}, nil,
3334
),
3435
"lone-int": prometheus.NewDesc(
3536
"expvar_lone_int",
3637
"Just an expvar int as an example.",
38+
"",
3739
nil, nil,
3840
),
3941
"http-request-map": prometheus.NewDesc(
4042
"expvar_http_request_total",
4143
"How many http requests processed, partitioned by status code and http method.",
44+
"",
4245
[]string{"code", "method"}, nil,
4346
),
4447
})

prometheus/gauge.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ func NewGauge(opts GaugeOpts) Gauge {
7979
desc := NewDesc(
8080
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
8181
opts.Help,
82+
opts.Unit,
8283
nil,
8384
opts.ConstLabels,
8485
)
@@ -161,6 +162,7 @@ func (v2) NewGaugeVec(opts GaugeVecOpts) *GaugeVec {
161162
desc := V2.NewDesc(
162163
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
163164
opts.Help,
165+
opts.Unit,
164166
opts.VariableLabels,
165167
opts.ConstLabels,
166168
)
@@ -305,6 +307,7 @@ func NewGaugeFunc(opts GaugeOpts, function func() float64) GaugeFunc {
305307
return newValueFunc(NewDesc(
306308
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
307309
opts.Help,
310+
opts.Unit,
308311
nil,
309312
opts.ConstLabels,
310313
), GaugeValue, function)

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)