From d790f80a26114664b9f7f56baee5b2ef1341cfc3 Mon Sep 17 00:00:00 2001 From: Dmitry Anoshin Date: Tue, 16 Apr 2024 08:48:27 -0700 Subject: [PATCH] [chore] [mdatagen] Fix generated tests for include/exclude capability Unblocking https://github.com/open-telemetry/opentelemetry-collector-contrib/pull/32394, again --- .../metadata/generated_metrics_test.go | 58 +++++++++---------- .../internal/metadata/testdata/config.yaml | 16 +++++ cmd/mdatagen/templates/metrics_test.go.tmpl | 40 ++++++------- .../templates/testdata/config.yaml.tmpl | 2 + 4 files changed, 67 insertions(+), 49 deletions(-) diff --git a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics_test.go b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics_test.go index 6860fe3c866..b6214e5d894 100644 --- a/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics_test.go +++ b/cmd/mdatagen/internal/samplereceiver/internal/metadata/generated_metrics_test.go @@ -14,43 +14,43 @@ import ( "go.opentelemetry.io/collector/receiver/receivertest" ) -type metricsSet int +type testDataSet int const ( - metricsSetDefault metricsSet = iota - metricsSetAll - metricsSetNone + testDataSetDefault testDataSet = iota + testDataSetAll + testDataSetNone ) func TestMetricsBuilder(t *testing.T) { tests := []struct { - name string - metricsSet metricsSet - resAttrsConfigured bool - expectEmpty bool + name string + metricsSet testDataSet + resAttrsSet testDataSet + expectEmpty bool }{ { name: "default", }, { - name: "all_set", - metricsSet: metricsSetAll, - resAttrsConfigured: true, + name: "all_set", + metricsSet: testDataSetAll, + resAttrsSet: testDataSetAll, }, { - name: "none_set", - metricsSet: metricsSetNone, - resAttrsConfigured: true, - expectEmpty: true, + name: "none_set", + metricsSet: testDataSetNone, + resAttrsSet: testDataSetNone, + expectEmpty: true, }, { - name: "filter_set_include", - resAttrsConfigured: true, + name: "filter_set_include", + resAttrsSet: testDataSetAll, }, { - name: "filter_set_exclude", - resAttrsConfigured: true, - expectEmpty: true, + name: "filter_set_exclude", + resAttrsSet: testDataSetAll, + expectEmpty: true, }, } for _, test := range tests { @@ -63,31 +63,31 @@ func TestMetricsBuilder(t *testing.T) { mb := NewMetricsBuilder(loadMetricsBuilderConfig(t, test.name), settings, WithStartTime(start)) expectedWarnings := 0 - if test.metricsSet == metricsSetDefault { + if test.metricsSet == testDataSetDefault { assert.Equal(t, "[WARNING] Please set `enabled` field explicitly for `default.metric`: This metric will be disabled by default soon.", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } - if test.metricsSet == metricsSetDefault || test.metricsSet == metricsSetAll { + if test.metricsSet == testDataSetDefault || test.metricsSet == testDataSetAll { assert.Equal(t, "[WARNING] `default.metric.to_be_removed` should not be enabled: This metric is deprecated and will be removed soon.", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } - if test.metricsSet == metricsSetAll || test.metricsSet == metricsSetNone { + if test.metricsSet == testDataSetAll || test.metricsSet == testDataSetNone { assert.Equal(t, "[WARNING] `optional.metric` should not be configured: This metric is deprecated and will be removed soon.", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } - if test.metricsSet == metricsSetAll || test.metricsSet == metricsSetNone { + if test.metricsSet == testDataSetAll || test.metricsSet == testDataSetNone { assert.Equal(t, "[WARNING] `optional.metric.empty_unit` should not be configured: This metric is deprecated and will be removed soon.", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } - if test.metricsSet == metricsSetDefault { + if test.resAttrsSet == testDataSetDefault { assert.Equal(t, "[WARNING] Please set `enabled` field explicitly for `string.resource.attr_disable_warning`: This resource_attribute will be disabled by default soon.", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } - if test.resAttrsConfigured { + if test.resAttrsSet == testDataSetAll || test.resAttrsSet == testDataSetNone { assert.Equal(t, "[WARNING] `string.resource.attr_remove_warning` should not be configured: This resource_attribute is deprecated and will be removed soon.", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } - if test.metricsSet == metricsSetDefault || test.metricsSet == metricsSetAll { + if test.resAttrsSet == testDataSetDefault || test.resAttrsSet == testDataSetAll { assert.Equal(t, "[WARNING] `string.resource.attr_to_be_removed` should not be enabled: This resource_attribute is deprecated and will be removed soon.", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } @@ -137,10 +137,10 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, res, rm.Resource()) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() - if test.metricsSet == metricsSetDefault { + if test.metricsSet == testDataSetDefault { assert.Equal(t, defaultMetricsCount, ms.Len()) } - if test.metricsSet == metricsSetAll { + if test.metricsSet == testDataSetAll { assert.Equal(t, allMetricsCount, ms.Len()) } validatedMetrics := make(map[string]bool) diff --git a/cmd/mdatagen/internal/samplereceiver/internal/metadata/testdata/config.yaml b/cmd/mdatagen/internal/samplereceiver/internal/metadata/testdata/config.yaml index 24a348ce4b5..3786910e06d 100644 --- a/cmd/mdatagen/internal/samplereceiver/internal/metadata/testdata/config.yaml +++ b/cmd/mdatagen/internal/samplereceiver/internal/metadata/testdata/config.yaml @@ -60,52 +60,68 @@ none_set: filter_set_include: resource_attributes: map.resource.attr: + enabled: true include: - regexp: ".*" optional.resource.attr: + enabled: true include: - regexp: ".*" slice.resource.attr: + enabled: true include: - regexp: ".*" string.enum.resource.attr: + enabled: true include: - regexp: ".*" string.resource.attr: + enabled: true include: - regexp: ".*" string.resource.attr_disable_warning: + enabled: true include: - regexp: ".*" string.resource.attr_remove_warning: + enabled: true include: - regexp: ".*" string.resource.attr_to_be_removed: + enabled: true include: - regexp: ".*" filter_set_exclude: resource_attributes: map.resource.attr: + enabled: true exclude: - regexp: ".*" optional.resource.attr: + enabled: true exclude: - strict: "optional.resource.attr-val" slice.resource.attr: + enabled: true exclude: - regexp: ".*" string.enum.resource.attr: + enabled: true exclude: - strict: "one" string.resource.attr: + enabled: true exclude: - strict: "string.resource.attr-val" string.resource.attr_disable_warning: + enabled: true exclude: - strict: "string.resource.attr_disable_warning-val" string.resource.attr_remove_warning: + enabled: true exclude: - strict: "string.resource.attr_remove_warning-val" string.resource.attr_to_be_removed: + enabled: true exclude: - strict: "string.resource.attr_to_be_removed-val" diff --git a/cmd/mdatagen/templates/metrics_test.go.tmpl b/cmd/mdatagen/templates/metrics_test.go.tmpl index 13c4ac95272..bbd48e4af85 100644 --- a/cmd/mdatagen/templates/metrics_test.go.tmpl +++ b/cmd/mdatagen/templates/metrics_test.go.tmpl @@ -14,19 +14,19 @@ import ( ) -type metricsSet int +type testDataSet int const ( - metricsSetDefault metricsSet = iota - metricsSetAll - metricsSetNone + testDataSetDefault testDataSet = iota + testDataSetAll + testDataSetNone ) func TestMetricsBuilder(t *testing.T) { tests := []struct { name string - metricsSet metricsSet - resAttrsConfigured bool + metricsSet testDataSet + resAttrsSet testDataSet expectEmpty bool }{ { @@ -34,23 +34,23 @@ func TestMetricsBuilder(t *testing.T) { }, { name: "all_set", - metricsSet: metricsSetAll, - resAttrsConfigured: true, + metricsSet: testDataSetAll, + resAttrsSet: testDataSetAll, }, { name: "none_set", - metricsSet: metricsSetNone, - resAttrsConfigured: true, + metricsSet: testDataSetNone, + resAttrsSet: testDataSetNone, expectEmpty: true, }, {{- if .ResourceAttributes }} { name: "filter_set_include", - resAttrsConfigured: true, + resAttrsSet: testDataSetAll, }, { name: "filter_set_exclude", - resAttrsConfigured: true, + resAttrsSet: testDataSetAll, expectEmpty: true, }, {{- end }} @@ -67,19 +67,19 @@ func TestMetricsBuilder(t *testing.T) { expectedWarnings := 0 {{- range $name, $metric := .Metrics }} {{- if and $metric.Enabled $metric.Warnings.IfEnabled }} - if test.metricsSet == metricsSetDefault || test.metricsSet == metricsSetAll { + if test.metricsSet == testDataSetDefault || test.metricsSet == testDataSetAll { assert.Equal(t, "[WARNING] `{{ $name }}` should not be enabled: {{ $metric.Warnings.IfEnabled }}", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } {{- end }} {{- if $metric.Warnings.IfEnabledNotSet }} - if test.metricsSet == metricsSetDefault { + if test.metricsSet == testDataSetDefault { assert.Equal(t, "[WARNING] Please set `enabled` field explicitly for `{{ $name }}`: {{ $metric.Warnings.IfEnabledNotSet }}", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } {{- end }} {{- if $metric.Warnings.IfConfigured }} - if test.metricsSet == metricsSetAll || test.metricsSet == metricsSetNone { + if test.metricsSet == testDataSetAll || test.metricsSet == testDataSetNone { assert.Equal(t, "[WARNING] `{{ $name }}` should not be configured: {{ $metric.Warnings.IfConfigured }}", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } @@ -87,19 +87,19 @@ func TestMetricsBuilder(t *testing.T) { {{- end }} {{- range $name, $attr := .ResourceAttributes }} {{- if and $attr.Enabled $attr.Warnings.IfEnabled }} - if test.metricsSet == metricsSetDefault || test.metricsSet == metricsSetAll { + if test.resAttrsSet == testDataSetDefault || test.resAttrsSet == testDataSetAll { assert.Equal(t, "[WARNING] `{{ $name }}` should not be enabled: {{ $attr.Warnings.IfEnabled }}", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } {{- end }} {{- if $attr.Warnings.IfEnabledNotSet }} - if test.metricsSet == metricsSetDefault { + if test.resAttrsSet == testDataSetDefault { assert.Equal(t, "[WARNING] Please set `enabled` field explicitly for `{{ $name }}`: {{ $attr.Warnings.IfEnabledNotSet }}", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } {{- end }} {{- if $attr.Warnings.IfConfigured }} - if test.resAttrsConfigured { + if test.resAttrsSet == testDataSetAll || test.resAttrsSet == testDataSetNone { assert.Equal(t, "[WARNING] `{{ $name }}` should not be configured: {{ $attr.Warnings.IfConfigured }}", observedLogs.All()[expectedWarnings].Message) expectedWarnings++ } @@ -146,10 +146,10 @@ func TestMetricsBuilder(t *testing.T) { assert.Equal(t, res, rm.Resource()) assert.Equal(t, 1, rm.ScopeMetrics().Len()) ms := rm.ScopeMetrics().At(0).Metrics() - if test.metricsSet == metricsSetDefault { + if test.metricsSet == testDataSetDefault { assert.Equal(t, defaultMetricsCount, ms.Len()) } - if test.metricsSet == metricsSetAll { + if test.metricsSet == testDataSetAll { assert.Equal(t, allMetricsCount, ms.Len()) } validatedMetrics := make(map[string]bool) diff --git a/cmd/mdatagen/templates/testdata/config.yaml.tmpl b/cmd/mdatagen/templates/testdata/config.yaml.tmpl index 862aa0aaf1f..a587b803cdc 100644 --- a/cmd/mdatagen/templates/testdata/config.yaml.tmpl +++ b/cmd/mdatagen/templates/testdata/config.yaml.tmpl @@ -34,6 +34,7 @@ filter_set_include: resource_attributes: {{- range $name, $attr := .ResourceAttributes }} {{ $name }}: + enabled: true include: - regexp: ".*" {{- end }} @@ -41,6 +42,7 @@ filter_set_exclude: resource_attributes: {{- range $name, $attr := .ResourceAttributes }} {{ $name }}: + enabled: true exclude: {{- if eq $attr.Type.String "Str" }} - strict: {{ $attr.TestValue }}