Skip to content

Commit 69613e4

Browse files
Handle custom metric suffix in exporter/prometheus to match otel-contrib (#6839)
Related to #6704 (comment) --------- Signed-off-by: Alexandre Lamarre <[email protected]> Co-authored-by: David Ashpole <[email protected]>
1 parent 889a486 commit 69613e4

File tree

4 files changed

+45
-1
lines changed

4 files changed

+45
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1919
- Add metric's scope attributes as `otel_scope_[attribute]` labels in `go.opentelemetry.io/otel/exporters/prometheus`. (#5947)
2020
- Add `EventName` to `EnabledParameters` in `go.opentelemetry.io/otel/log`. (#6825)
2121
- Add `EventName` to `EnabledParameters` in `go.opentelemetry.io/otel/sdk/log`. (#6825)
22+
- Changed handling of `go.opentelemetry.io/otel/exporters/prometheus` metric renaming to add unit suffixes when it doesn't match one of the pre-defined values in the unit suffix map. (#6839)
2223

2324
### Removed
2425

exporters/prometheus/exporter.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,13 @@ func createInfoMetric(name, description string, res *resource.Resource) (prometh
425425
return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...)
426426
}
427427

428+
func unitMapGetOrDefault(unit string) string {
429+
if promUnit, ok := unitSuffixes[unit]; ok {
430+
return promUnit
431+
}
432+
return unit
433+
}
434+
428435
var unitSuffixes = map[string]string{
429436
// Time
430437
"d": "days",
@@ -483,7 +490,7 @@ func (c *collector) getName(m metricdata.Metrics, typ *dto.MetricType) string {
483490
if c.namespace != "" {
484491
name = c.namespace + name
485492
}
486-
if suffix, ok := unitSuffixes[m.Unit]; ok && !c.withoutUnits && !strings.HasSuffix(name, suffix) {
493+
if suffix := unitMapGetOrDefault(m.Unit); suffix != "" && !c.withoutUnits && !strings.HasSuffix(name, suffix) {
487494
name += "_" + suffix
488495
}
489496
if addCounterSuffix {

exporters/prometheus/exporter_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,35 @@ func TestPrometheusExporter(t *testing.T) {
9595
counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
9696
},
9797
},
98+
{
99+
name: "counter with custom unit not tracked by ucum standards",
100+
expectedFile: "testdata/counter_with_custom_unit_suffix.txt",
101+
recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
102+
opt := otelmetric.WithAttributes(
103+
attribute.Key("A").String("B"),
104+
attribute.Key("C").String("D"),
105+
attribute.Key("E").Bool(true),
106+
attribute.Key("F").Int(42),
107+
)
108+
counter, err := meter.Float64Counter(
109+
"foo",
110+
otelmetric.WithDescription("a simple counter"),
111+
otelmetric.WithUnit("madeup"),
112+
)
113+
require.NoError(t, err)
114+
counter.Add(ctx, 5, opt)
115+
counter.Add(ctx, 10.3, opt)
116+
counter.Add(ctx, 9, opt)
117+
118+
attrs2 := attribute.NewSet(
119+
attribute.Key("A").String("D"),
120+
attribute.Key("C").String("B"),
121+
attribute.Key("E").Bool(true),
122+
attribute.Key("F").Int(42),
123+
)
124+
counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
125+
},
126+
},
98127
{
99128
name: "counter that already has a total suffix",
100129
expectedFile: "testdata/counter.txt",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# HELP "foo_madeup_total" a simple counter
2+
# TYPE "foo_madeup_total" counter
3+
{"foo_madeup_total",A="B",C="D",E="true",F="42",otel_scope_fizz="buzz",otel_scope_name="testmeter",otel_scope_schema_url="",otel_scope_version="v0.1.0"} 24.3
4+
{"foo_madeup_total",A="D",C="B",E="true",F="42",otel_scope_fizz="buzz",otel_scope_name="testmeter",otel_scope_schema_url="",otel_scope_version="v0.1.0"} 5
5+
# HELP target_info Target metadata
6+
# TYPE target_info gauge
7+
target_info{"service.name"="prometheus_test","telemetry.sdk.language"="go","telemetry.sdk.name"="opentelemetry","telemetry.sdk.version"="latest"} 1

0 commit comments

Comments
 (0)