Skip to content

Commit 483cccb

Browse files
committed
Yolo, add units.
Signed-off-by: bwplotka <[email protected]>
1 parent 242b076 commit 483cccb

File tree

8 files changed

+25
-6
lines changed

8 files changed

+25
-6
lines changed

prometheus/counter.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ 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+
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
)

prometheus/desc.go

+8-2
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 is a OpenMetrics unit.
51+
unit string
5052
// constLabelPairs contains precalculated DTO label pairs based on
5153
// the constant labels.
5254
constLabelPairs []*dto.LabelPair
@@ -76,7 +78,7 @@ type Desc struct {
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.
7880
func NewDesc(fqName, help string, variableLabels []string, constLabels Labels) *Desc {
79-
return V2.NewDesc(fqName, help, UnconstrainedLabels(variableLabels), constLabels)
81+
return V2.NewDesc(fqName, help, "", UnconstrainedLabels(variableLabels), constLabels)
8082
}
8183

8284
// NewDesc allocates and initializes a new Desc. Errors are recorded in the Desc
@@ -89,10 +91,14 @@ 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, unit string, variableLabels ConstrainableLabels, constLabels Labels) *Desc {
95+
if variableLabels == nil {
96+
variableLabels = UnconstrainedLabels(nil)
97+
}
9398
d := &Desc{
9499
fqName: fqName,
95100
help: help,
101+
unit: unit,
96102
variableLabels: variableLabels.compile(),
97103
}
98104
if !model.IsValidMetricName(model.LabelValue(fqName)) {

prometheus/gauge.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,10 @@ 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+
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
)

prometheus/histogram.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ type HistogramOpts struct {
371371
Namespace string
372372
Subsystem string
373373
Name string
374+
Unit string
374375

375376
// Help provides information about this Histogram.
376377
//
@@ -522,9 +523,10 @@ type HistogramVecOpts struct {
522523
// for each bucket.
523524
func NewHistogram(opts HistogramOpts) Histogram {
524525
return newHistogram(
525-
NewDesc(
526+
V2.NewDesc(
526527
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
527528
opts.Help,
529+
opts.Unit,
528530
nil,
529531
opts.ConstLabels,
530532
),
@@ -1188,6 +1190,7 @@ func (v2) NewHistogramVec(opts HistogramVecOpts) *HistogramVec {
11881190
desc := V2.NewDesc(
11891191
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
11901192
opts.Help,
1193+
opts.Unit,
11911194
opts.VariableLabels,
11921195
opts.ConstLabels,
11931196
)

prometheus/metric.go

+1
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type Opts struct {
7474
Namespace string
7575
Subsystem string
7676
Name string
77+
Unit string
7778

7879
// Help provides information about this metric.
7980
//

prometheus/registry.go

+4
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,10 @@ func processMetric(
685685
metricFamily = &dto.MetricFamily{}
686686
metricFamily.Name = proto.String(desc.fqName)
687687
metricFamily.Help = proto.String(desc.help)
688+
if desc.unit != "" {
689+
metricFamily.Unit = proto.String(desc.unit)
690+
}
691+
688692
// TODO(beorn7): Simplify switch once Desc has type.
689693
switch {
690694
case dtoMetric.Gauge != nil:

prometheus/summary.go

+1
Original file line numberDiff line numberDiff line change
@@ -576,6 +576,7 @@ func (v2) NewSummaryVec(opts SummaryVecOpts) *SummaryVec {
576576
desc := V2.NewDesc(
577577
BuildFQName(opts.Namespace, opts.Subsystem, opts.Name),
578578
opts.Help,
579+
"", // TODO
579580
opts.VariableLabels,
580581
opts.ConstLabels,
581582
)

prometheus/wrap.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ func wrapDesc(desc *Desc, prefix string, labels Labels) *Desc {
204204
constLabels[ln] = lv
205205
}
206206
// NewDesc will do remaining validations.
207-
newDesc := V2.NewDesc(prefix+desc.fqName, desc.help, desc.variableLabels, constLabels)
207+
newDesc := V2.NewDesc(prefix+desc.fqName, desc.help, desc.unit, desc.variableLabels, constLabels)
208208
// Propagate errors if there was any. This will override any errer
209209
// created by NewDesc above, i.e. earlier errors get precedence.
210210
if desc.err != nil {

0 commit comments

Comments
 (0)