Skip to content

Commit 7de7f54

Browse files
committed
Add explanatory comments
Signed-off-by: Arianna Vespri <[email protected]>
1 parent dac5aec commit 7de7f54

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

expfmt/openmetrics_create.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,14 @@ import (
5252
// its type will be set to `unknown` in that case to avoid invalid OpenMetrics
5353
// output.
5454
//
55-
// - The `# UNIT` line is optional, but if populated, the unit has to be present in the metric
56-
// name as its suffix (see https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#unit)
55+
// - According to the OM specs, the `# UNIT` line is optional, but if populated,
56+
// the unit has to be present in the metric name as its suffix:
57+
// (see https://github.com/OpenObservability/OpenMetrics/blob/main/specification/OpenMetrics.md#unit).
58+
// However, in order to accomodate any potential scenario where such a change in the
59+
// metric name is not desirable, the users are here given the choice of either explicitly
60+
// opt in, in case they wish for the unit to be included in the output AND in the metric name
61+
// as a suffix (see the description of the ToOpenMetricsWithUnit function below),
62+
// or not to opt in, in case they don't want for any of that to happen.
5763
//
5864
// - No support for the following (optional) features: `_created`
5965
// line, info type, stateset type, gaugehistogram type.
@@ -66,11 +72,16 @@ import (
6672

6773
type ToOpenMetrics struct {
6874
withUnit bool
69-
// withCreated bool can be added in the future
75+
// withCreated bool can be added in the future.
7076
}
7177

7278
type ToOpenMetricsOption = func(*ToOpenMetrics)
7379

80+
// ToOpenMetricsWithUnit is meant to be called as an optional argument in the MetricFamilyToOpenMetrics
81+
// function. It enables a set unit to be written to the output and to be added to the metric name, (if
82+
// it's not there already), as a suffix. Without opting in this way, the unit will not be added to the
83+
// metric name and, on top of that, the unit will not be passed onto the output, even if it were declared
84+
// in the *dto.MetricFamily struct, i.e. even if in.Unit !=nil.
7485
func ToOpenMetricsWithUnit() ToOpenMetricsOption {
7586
return func(om *ToOpenMetrics) {
7687
om.withUnit = true

expfmt/openmetrics_create_test.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,34 @@ func TestCreateOpenMatricsWithUnit(t *testing.T) {
545545
out: `# HELP name_seconds doc string
546546
# TYPE name_seconds counter
547547
# UNIT name_seconds seconds
548+
`,
549+
},
550+
// 3: No metric plus unit wrong unit in name. // Can this happen at all?
551+
{
552+
in: &dto.MetricFamily{
553+
Name: proto.String("name_milliseconds_total"),
554+
Help: proto.String("doc string"),
555+
Type: dto.MetricType_COUNTER.Enum(),
556+
Unit: proto.String("seconds"),
557+
Metric: []*dto.Metric{},
558+
},
559+
out: `# HELP name_milliseconds_seconds doc string
560+
# TYPE name_milliseconds_seconds counter
561+
# UNIT name_milliseconds_seconds seconds
562+
`,
563+
},
564+
// 3: No metric plus unit already in name.
565+
{
566+
in: &dto.MetricFamily{
567+
Name: proto.String("name_seconds_total"),
568+
Help: proto.String("doc string"),
569+
Type: dto.MetricType_COUNTER.Enum(),
570+
Unit: proto.String("seconds"),
571+
Metric: []*dto.Metric{},
572+
},
573+
out: `# HELP name_seconds doc string
574+
# TYPE name_seconds counter
575+
# UNIT name_seconds seconds
548576
`,
549577
},
550578
}

0 commit comments

Comments
 (0)