Skip to content

Commit e4c38a0

Browse files
authored
Remove logic adding unit to metrics name (#877)
* Remove withUnit flag and automatic adding of unit to metric name, change tests accordingly Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * Remove redundant tests and comments for OM creation Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> * fix fmt number in test Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it> --------- Signed-off-by: Arianna Vespri <arianna.vespri@yahoo.it>
1 parent adea628 commit e4c38a0

3 files changed

Lines changed: 16 additions & 75 deletions

File tree

expfmt/encode_test.go

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,7 @@ foo_metric 1.234
248248
metric: metric1,
249249
format: FmtOpenMetrics_0_0_1,
250250
expOut: `# TYPE foo_metric unknown
251+
# UNIT foo_metric seconds
251252
foo_metric 1.234
252253
`,
253254
},
@@ -256,24 +257,16 @@ foo_metric 1.234
256257
metric: metric1,
257258
format: FmtOpenMetrics_1_0_0,
258259
expOut: `# TYPE foo_metric unknown
260+
# UNIT foo_metric seconds
259261
foo_metric 1.234
260262
`,
261263
},
262-
// 7: Simple Counter FmtOpenMetrics_0_0_1 unit opted in
263-
{
264-
metric: metric1,
265-
format: FmtOpenMetrics_0_0_1,
266-
options: []EncoderOption{WithUnit()},
267-
expOut: `# TYPE foo_metric_seconds unknown
268-
# UNIT foo_metric_seconds seconds
269-
foo_metric_seconds 1.234
270-
`,
271-
},
272-
// 8: Simple Counter FmtOpenMetrics_1_0_0 unit opted out
264+
// 7: Simple Counter FmtOpenMetrics_1_0_0
273265
{
274266
metric: metric1,
275267
format: FmtOpenMetrics_1_0_0,
276268
expOut: `# TYPE foo_metric unknown
269+
# UNIT foo_metric seconds
277270
foo_metric 1.234
278271
`,
279272
},

expfmt/openmetrics_create.go

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ import (
3030

3131
type encoderOption struct {
3232
withCreatedLines bool
33-
withUnit bool
3433
}
3534

3635
type EncoderOption func(*encoderOption)
@@ -51,17 +50,6 @@ func WithCreatedLines() EncoderOption {
5150
}
5251
}
5352

54-
// WithUnit is an EncoderOption enabling a set unit to be written to the output
55-
// and to be added to the metric name, if it's not there already, as a suffix.
56-
// Without opting in this way, the unit will not be added to the metric name and,
57-
// on top of that, the unit will not be passed onto the output, even if it
58-
// were declared in the *dto.MetricFamily struct, i.e. even if in.Unit !=nil.
59-
func WithUnit() EncoderOption {
60-
return func(t *encoderOption) {
61-
t.withUnit = true
62-
}
63-
}
64-
6553
// MetricFamilyToOpenMetrics converts a MetricFamily proto message into the
6654
// OpenMetrics text format and writes the resulting lines to 'out'. It returns
6755
// the number of bytes written and any error encountered. The output will have
@@ -99,15 +87,6 @@ func WithUnit() EncoderOption {
9987
// its type will be set to `unknown` in that case to avoid invalid OpenMetrics
10088
// output.
10189
//
102-
// - According to the OM specs, the `# UNIT` line is optional, but if populated,
103-
// the unit has to be present in the metric name as its suffix:
104-
// (see https://github.com/prometheus/OpenMetrics/blob/v1.0.0/specification/OpenMetrics.md#unit).
105-
// However, in order to accommodate any potential scenario where such a change in the
106-
// metric name is not desirable, the users are here given the choice of either explicitly
107-
// opt in, in case they wish for the unit to be included in the output AND in the metric name
108-
// as a suffix (see the description of the WithUnit function above),
109-
// or not to opt in, in case they don't want for any of that to happen.
110-
//
11190
// - No support for the following (optional) features: info type,
11291
// stateset type, gaugehistogram type.
11392
//
@@ -151,9 +130,6 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
151130
if metricType == dto.MetricType_COUNTER && strings.HasSuffix(compliantName, "_total") {
152131
compliantName = name[:len(name)-6]
153132
}
154-
if toOM.withUnit && in.Unit != nil && !strings.HasSuffix(compliantName, "_"+*in.Unit) {
155-
compliantName = compliantName + "_" + *in.Unit
156-
}
157133

158134
// Comments, first HELP, then TYPE.
159135
if in.Help != nil {
@@ -217,7 +193,7 @@ func MetricFamilyToOpenMetrics(out io.Writer, in *dto.MetricFamily, options ...E
217193
if err != nil {
218194
return written, err
219195
}
220-
if toOM.withUnit && in.Unit != nil {
196+
if in.Unit != nil {
221197
n, err = w.WriteString("# UNIT ")
222198
written += n
223199
if err != nil {

expfmt/openmetrics_create_test.go

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ summary_name_created{name_1="value 1",name_2="value 2"} 12345.6
399399
},
400400
},
401401
},
402-
options: []EncoderOption{WithCreatedLines(), WithUnit()},
402+
options: []EncoderOption{WithCreatedLines()},
403403
out: `# HELP request_duration_microseconds The response latency.
404404
# TYPE request_duration_microseconds histogram
405405
# UNIT request_duration_microseconds microseconds
@@ -449,6 +449,7 @@ request_duration_microseconds_created 12345.6
449449
},
450450
out: `# HELP request_duration_microseconds The response latency.
451451
# TYPE request_duration_microseconds histogram
452+
# UNIT request_duration_microseconds microseconds
452453
request_duration_microseconds_bucket{le="100.0"} 123
453454
request_duration_microseconds_bucket{le="120.0"} 412
454455
request_duration_microseconds_bucket{le="144.0"} 592
@@ -613,13 +614,12 @@ foos_total 42.0
613614
Unit: proto.String("seconds"),
614615
Metric: []*dto.Metric{},
615616
},
616-
options: []EncoderOption{WithUnit()},
617617
out: `# HELP name_seconds doc string
618618
# TYPE name_seconds counter
619619
# UNIT name_seconds seconds
620620
`,
621621
},
622-
// 15: Histogram plus unit, but unit not opted in.
622+
// 15: Histogram plus unit
623623
{
624624
in: &dto.MetricFamily{
625625
Name: proto.String("request_duration_microseconds"),
@@ -659,6 +659,7 @@ foos_total 42.0
659659
},
660660
out: `# HELP request_duration_microseconds The response latency.
661661
# TYPE request_duration_microseconds histogram
662+
# UNIT request_duration_microseconds microseconds
662663
request_duration_microseconds_bucket{le="100.0"} 123
663664
request_duration_microseconds_bucket{le="120.0"} 412
664665
request_duration_microseconds_bucket{le="144.0"} 592
@@ -668,35 +669,7 @@ request_duration_microseconds_sum 1.7560473e+06
668669
request_duration_microseconds_count 2693
669670
`,
670671
},
671-
// 16: No metric, unit opted in, no unit in name.
672-
{
673-
in: &dto.MetricFamily{
674-
Name: proto.String("name_total"),
675-
Help: proto.String("doc string"),
676-
Type: dto.MetricType_COUNTER.Enum(),
677-
Unit: proto.String("seconds"),
678-
Metric: []*dto.Metric{},
679-
},
680-
options: []EncoderOption{WithUnit()},
681-
out: `# HELP name_seconds doc string
682-
# TYPE name_seconds counter
683-
# UNIT name_seconds seconds
684-
`,
685-
},
686-
// 17: No metric, unit opted in, BUT unit == nil.
687-
{
688-
in: &dto.MetricFamily{
689-
Name: proto.String("name_total"),
690-
Help: proto.String("doc string"),
691-
Type: dto.MetricType_COUNTER.Enum(),
692-
Metric: []*dto.Metric{},
693-
},
694-
options: []EncoderOption{WithUnit()},
695-
out: `# HELP name doc string
696-
# TYPE name counter
697-
`,
698-
},
699-
// 18: Counter, timestamp given, unit opted in, _total suffix.
672+
// 16: Counter, timestamp given, unit given, _total suffix.
700673
{
701674
in: &dto.MetricFamily{
702675
Name: proto.String("some_measure_total"),
@@ -737,15 +710,14 @@ request_duration_microseconds_count 2693
737710
},
738711
},
739712
},
740-
options: []EncoderOption{WithUnit()},
741-
out: `# HELP some_measure_seconds some testing measurement
742-
# TYPE some_measure_seconds counter
743-
# UNIT some_measure_seconds seconds
744-
some_measure_seconds_total{labelname="val1",basename="basevalue"} 42.0
745-
some_measure_seconds_total{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06
713+
out: `# HELP some_measure some testing measurement
714+
# TYPE some_measure counter
715+
# UNIT some_measure seconds
716+
some_measure_total{labelname="val1",basename="basevalue"} 42.0
717+
some_measure_total{labelname="val2",basename="basevalue"} 0.23 1.23456789e+06
746718
`,
747719
},
748-
// 11: Gauge histogram.
720+
// 17: Gauge histogram.
749721
{
750722
in: &dto.MetricFamily{
751723
Name: proto.String("name"),

0 commit comments

Comments
 (0)