Skip to content

Commit

Permalink
prevent conflict unit
Browse files Browse the repository at this point in the history
Signed-off-by: Ziqi Zhao <zhaoziqi9146@gmail.com>
  • Loading branch information
fatsheep9146 committed Nov 17, 2022
1 parent d15e0db commit 0bdf0c1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
4 changes: 4 additions & 0 deletions exporters/prometheus/exporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -387,5 +387,9 @@ func (c *collector) validateMetricInfo(m metricdata.Metrics) (bool, metricdata.M
m.Description = metric.Description
}

if metric.Unit != m.Unit {
m.Unit = metric.Unit
}

return true, m
}
20 changes: 19 additions & 1 deletion exporters/prometheus/exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,14 +436,32 @@ func TestDuplicateMetrics(t *testing.T) {
},
expectedFile: "testdata/duplicate_metrics_conflict_help.txt",
},
{
name: "conflict_unit",
recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
fooA, err := meterA.SyncInt64().Counter("foo",
instrument.WithUnit(unit.Bytes),
instrument.WithDescription("meter foo"))
assert.NoError(t, err)
fooA.Add(ctx, 100, attribute.String("type", "foo"))

fooB, err := meterB.SyncInt64().Counter("foo",
instrument.WithUnit(unit.Milliseconds),
instrument.WithDescription("meter foo"))
assert.NoError(t, err)
fooB.Add(ctx, 100, attribute.String("type", "foo"))
},
options: []Option{WithoutUnits()},
expectedFile: "testdata/duplicate_metrics_conflict_unit.txt",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// initialize registry exporter
ctx := context.Background()
registry := prometheus.NewRegistry()
exporter, err := New(WithRegisterer(registry))
exporter, err := New(append(tc.options, WithRegisterer(registry))...)
require.NoError(t, err)

// initialize resource
Expand Down
11 changes: 11 additions & 0 deletions exporters/prometheus/testdata/duplicate_metrics_conflict_unit.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# HELP foo_total meter foo
# TYPE foo_total counter
foo_total{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo"} 100
foo_total{otel_scope_name="mb",otel_scope_version="v0.1.0",type="foo"} 100
# HELP otel_scope_info Instrumentation Scope metadata
# TYPE otel_scope_info gauge
otel_scope_info{otel_scope_name="ma",otel_scope_version="v0.1.0"} 1
otel_scope_info{otel_scope_name="mb",otel_scope_version="v0.1.0"} 1
# 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

0 comments on commit 0bdf0c1

Please sign in to comment.