Skip to content

Commit af1fe4c

Browse files
committed
add unit to opts
1 parent c4f69f0 commit af1fe4c

24 files changed

+101
-16
lines changed

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: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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)

0 commit comments

Comments
 (0)