Skip to content

Commit

Permalink
enhance logic
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 21, 2022
1 parent 40ed283 commit a1a652a
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 65 deletions.
45 changes: 38 additions & 7 deletions exporters/prometheus/exporter.go
Expand Up @@ -423,17 +423,48 @@ const (
)

func validateCollision(mf *dto.MetricFamily, mfs map[string]*dto.MetricFamily) collisionType {
emf, exist := mfs[mf.GetName()]
if !exist {
return noCollision
newName := mf.GetName()
newType := mf.GetType()
newNameWithoutSuffix := ""

emf, exist := mfs[newName]
if exist {
if emf.GetType() != mf.GetType() {
return typeCollision
}

if emf.GetHelp() != mf.GetHelp() {
return helpCollision
}
}

switch {
case strings.HasSuffix(newName, "_count"):
newNameWithoutSuffix = newName[:len(newName)-6]
case strings.HasSuffix(newName, "_sum"):
newNameWithoutSuffix = newName[:len(newName)-4]
case strings.HasSuffix(newName, "_bucket"):
newNameWithoutSuffix = newName[:len(newName)-7]
}

if emf.GetType() != mf.GetType() {
return typeCollision
if newNameWithoutSuffix != "" {
if emf, exist = mfs[newNameWithoutSuffix]; exist {
if emf.GetType() == dto.MetricType_HISTOGRAM {
return typeCollision
}
}
}

if emf.GetHelp() != mf.GetHelp() {
return helpCollision
if newType == dto.MetricType_HISTOGRAM {
if _, exist = mfs[newName+"_count"]; exist {
return typeCollision
}
if _, exist = mfs[newName+"_sum"]; exist {
return typeCollision
}
if _, exist = mfs[newName+"_bucket"]; exist {
return typeCollision
}
}

return noCollision
Expand Down
82 changes: 24 additions & 58 deletions exporters/prometheus/exporter_test.go
Expand Up @@ -403,7 +403,7 @@ func TestDuplicateMetrics(t *testing.T) {
expectedFile string
}{
{
name: "all_same",
name: "no_conflict",
recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
fooA, err := meterA.SyncInt64().Counter("foo",
instrument.WithUnit(unit.Bytes),
Expand All @@ -417,7 +417,7 @@ func TestDuplicateMetrics(t *testing.T) {
assert.NoError(t, err)
fooB.Add(ctx, 100, attribute.String("type", "foo"))
},
expectedFile: "testdata/duplicate_metrics_all_same.txt",
expectedFile: "testdata/no_conflict.txt",
},
{
name: "conflict_help",
Expand All @@ -434,7 +434,7 @@ func TestDuplicateMetrics(t *testing.T) {
assert.NoError(t, err)
fooB.Add(ctx, 100, attribute.String("type", "foo"))
},
expectedFile: "testdata/duplicate_metrics_conflict_help.txt",
expectedFile: "testdata/conflict_help.txt",
},
{
name: "conflict_unit",
Expand All @@ -452,10 +452,10 @@ func TestDuplicateMetrics(t *testing.T) {
fooB.Add(ctx, 100, attribute.String("type", "foo"))
},
options: []Option{WithoutUnits()},
expectedFile: "testdata/duplicate_metrics_conflict_unit.txt",
expectedFile: "testdata/conflict_unit.txt",
},
{
name: "conflict_type",
name: "conflict_type_counter",
recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
counter, err := meterA.SyncInt64().Counter("foo",
instrument.WithUnit(unit.Bytes),
Expand All @@ -470,7 +470,25 @@ func TestDuplicateMetrics(t *testing.T) {
gauge.Add(ctx, 200, attribute.String("type", "foo"))
},
options: []Option{WithoutUnits()},
expectedFile: "testdata/duplicate_metrics_conflict_type.txt",
expectedFile: "testdata/conflict_type_counter.txt",
},
{
name: "conflict_type_histogram",
recordMetrics: func(ctx context.Context, meterA, meterB otelmetric.Meter) {
histogram, err := meterA.SyncInt64().Histogram("foo",
instrument.WithUnit(unit.Bytes),
instrument.WithDescription("meter foo"))
assert.NoError(t, err)
histogram.Record(ctx, 100, attribute.String("type", "foo"))

gauge, err := meterA.SyncInt64().UpDownCounter("foo_count",
instrument.WithUnit(unit.Bytes),
instrument.WithDescription("meter foo"))
assert.NoError(t, err)
gauge.Add(ctx, 200, attribute.String("type", "foo"))
},
options: []Option{WithoutUnits()},
expectedFile: "testdata/conflict_type_histogram.txt",
},
}

Expand Down Expand Up @@ -511,56 +529,4 @@ func TestDuplicateMetrics(t *testing.T) {
require.NoError(t, err)
})
}

// foo, err := provider.Meter("meterfoo", otelmetric.WithInstrumentationVersion("v0.1.0")).
// SyncInt64().Counter(
// "foo",
// instrument.WithUnit(unit.Milliseconds),
// instrument.WithDescription("meter foo"))
// assert.NoError(t, err)
// foo.Add(ctx, 100, attribute.String("type", "foo"))

// fooWithSameInfo, err := provider.Meter("meterfoo", otelmetric.WithInstrumentationVersion("v0.1.0")).
// SyncInt64().Counter(
// "foo",
// instrument.WithUnit(unit.Milliseconds),
// instrument.WithDescription("meter foo"))
// assert.NoError(t, err)
// fooWithSameInfo.Add(ctx, 100, attribute.String("type", "foo"))

// families, err := registry.Gather()
// if err != nil {
// t.Fatalf("gather failed:%v", err)
// return
// } else {
// t.Logf("gather succeded\n")
// for i := range families {
// t.Logf("Name:[%v] Help:[%v] Type:[%v], Points:[%v]", *families[i].Name, *families[i].Help, families[i].Type.String(), len(families[i].Metric))
// }
// }

// fooWithConflictedType, err := provider.Meter("meterfoo", otelmetric.WithInstrumentationVersion("v0.1.0")).
// SyncInt64().Histogram(
// "foo",
// instrument.WithUnit(unit.Milliseconds),
// instrument.WithDescription("meter foo"))
// require.NoError(t, err)
// fooWithConflictedType.Record(ctx, 1)
// families, err = registry.Gather()
// if err != nil {
// t.Fatalf("gather failed:%v", err)
// return
// } else {
// t.Logf("gather succeded\n")
// for i := range families {
// t.Logf("Name:[%v] Help:[%v] Type:[%v], Points:[%v]", *families[i].Name, *families[i].Help, families[i].Type.String(), len(families[i].Metric))
// }
// }

// file, err := os.Open("testdata/multi_scopes.txt")
// require.NoError(t, err)
// t.Cleanup(func() { require.NoError(t, file.Close()) })

// err = testutil.GatherAndCompare(registry, file)
// require.NoError(t, err)
}
26 changes: 26 additions & 0 deletions exporters/prometheus/testdata/conflict_type_histogram.txt
@@ -0,0 +1,26 @@
# HELP foo meter foo
# TYPE foo histogram
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="0"} 0
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="5"} 0
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="10"} 0
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="25"} 0
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="50"} 0
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="75"} 0
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="100"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="250"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="500"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="750"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="1000"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="2500"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="5000"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="7500"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="10000"} 1
foo_bucket{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo",le="+Inf"} 1
foo_sum{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo"} 100
foo_count{otel_scope_name="ma",otel_scope_version="v0.1.0",type="foo"} 1
# 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
# 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 a1a652a

Please sign in to comment.