You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-08-10 22:31:50 +02:00
Handle custom metric suffix in exporter/prometheus
to match otel-contrib (#6839)
Related to https://github.com/open-telemetry/opentelemetry-go/issues/6704#issuecomment-2916325796 --------- Signed-off-by: Alexandre Lamarre <alex7285@gmail.com> Co-authored-by: David Ashpole <dashpole@google.com>
This commit is contained in:
committed by
GitHub
parent
889a4862b4
commit
69613e4810
@@ -19,6 +19,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
|
|||||||
- Add metric's scope attributes as `otel_scope_[attribute]` labels in `go.opentelemetry.io/otel/exporters/prometheus`. (#5947)
|
- Add metric's scope attributes as `otel_scope_[attribute]` labels in `go.opentelemetry.io/otel/exporters/prometheus`. (#5947)
|
||||||
- Add `EventName` to `EnabledParameters` in `go.opentelemetry.io/otel/log`. (#6825)
|
- Add `EventName` to `EnabledParameters` in `go.opentelemetry.io/otel/log`. (#6825)
|
||||||
- Add `EventName` to `EnabledParameters` in `go.opentelemetry.io/otel/sdk/log`. (#6825)
|
- Add `EventName` to `EnabledParameters` in `go.opentelemetry.io/otel/sdk/log`. (#6825)
|
||||||
|
- 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)
|
||||||
|
|
||||||
### Removed
|
### Removed
|
||||||
|
|
||||||
|
@@ -425,6 +425,13 @@ func createInfoMetric(name, description string, res *resource.Resource) (prometh
|
|||||||
return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...)
|
return prometheus.NewConstMetric(desc, prometheus.GaugeValue, float64(1), values...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func unitMapGetOrDefault(unit string) string {
|
||||||
|
if promUnit, ok := unitSuffixes[unit]; ok {
|
||||||
|
return promUnit
|
||||||
|
}
|
||||||
|
return unit
|
||||||
|
}
|
||||||
|
|
||||||
var unitSuffixes = map[string]string{
|
var unitSuffixes = map[string]string{
|
||||||
// Time
|
// Time
|
||||||
"d": "days",
|
"d": "days",
|
||||||
@@ -483,7 +490,7 @@ func (c *collector) getName(m metricdata.Metrics, typ *dto.MetricType) string {
|
|||||||
if c.namespace != "" {
|
if c.namespace != "" {
|
||||||
name = c.namespace + name
|
name = c.namespace + name
|
||||||
}
|
}
|
||||||
if suffix, ok := unitSuffixes[m.Unit]; ok && !c.withoutUnits && !strings.HasSuffix(name, suffix) {
|
if suffix := unitMapGetOrDefault(m.Unit); suffix != "" && !c.withoutUnits && !strings.HasSuffix(name, suffix) {
|
||||||
name += "_" + suffix
|
name += "_" + suffix
|
||||||
}
|
}
|
||||||
if addCounterSuffix {
|
if addCounterSuffix {
|
||||||
|
@@ -95,6 +95,35 @@ func TestPrometheusExporter(t *testing.T) {
|
|||||||
counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
|
counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "counter with custom unit not tracked by ucum standards",
|
||||||
|
expectedFile: "testdata/counter_with_custom_unit_suffix.txt",
|
||||||
|
recordMetrics: func(ctx context.Context, meter otelmetric.Meter) {
|
||||||
|
opt := otelmetric.WithAttributes(
|
||||||
|
attribute.Key("A").String("B"),
|
||||||
|
attribute.Key("C").String("D"),
|
||||||
|
attribute.Key("E").Bool(true),
|
||||||
|
attribute.Key("F").Int(42),
|
||||||
|
)
|
||||||
|
counter, err := meter.Float64Counter(
|
||||||
|
"foo",
|
||||||
|
otelmetric.WithDescription("a simple counter"),
|
||||||
|
otelmetric.WithUnit("madeup"),
|
||||||
|
)
|
||||||
|
require.NoError(t, err)
|
||||||
|
counter.Add(ctx, 5, opt)
|
||||||
|
counter.Add(ctx, 10.3, opt)
|
||||||
|
counter.Add(ctx, 9, opt)
|
||||||
|
|
||||||
|
attrs2 := attribute.NewSet(
|
||||||
|
attribute.Key("A").String("D"),
|
||||||
|
attribute.Key("C").String("B"),
|
||||||
|
attribute.Key("E").Bool(true),
|
||||||
|
attribute.Key("F").Int(42),
|
||||||
|
)
|
||||||
|
counter.Add(ctx, 5, otelmetric.WithAttributeSet(attrs2))
|
||||||
|
},
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "counter that already has a total suffix",
|
name: "counter that already has a total suffix",
|
||||||
expectedFile: "testdata/counter.txt",
|
expectedFile: "testdata/counter.txt",
|
||||||
|
7
exporters/prometheus/testdata/counter_with_custom_unit_suffix.txt
vendored
Normal file
7
exporters/prometheus/testdata/counter_with_custom_unit_suffix.txt
vendored
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# HELP "foo_madeup_total" a simple counter
|
||||||
|
# TYPE "foo_madeup_total" counter
|
||||||
|
{"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
|
||||||
|
{"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
|
||||||
|
# HELP target_info Target metadata
|
||||||
|
# TYPE target_info gauge
|
||||||
|
target_info{"service.name"="prometheus_test","telemetry.sdk.language"="go","telemetry.sdk.name"="opentelemetry","telemetry.sdk.version"="latest"} 1
|
Reference in New Issue
Block a user