From b6868b192774f31f7570ebe4901de91784987730 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Wed, 27 Mar 2024 09:54:22 -0700 Subject: [PATCH] [chore] reuse struct when parsing to avoid recursive parsing (#31727) **Description:** This is a companion PR to handle recursive state of unmarshalers with https://github.com/open-telemetry/opentelemetry-collector/pull/9750. Changing this behavior will allow the confmap.Conf object to recognize that it has already run the `Unmarshal` method on the struct, and run the mapstructure decoding of fields. --- exporter/datadogexporter/config_warnings_test.go | 5 +++-- exporter/datasetexporter/config_test.go | 3 ++- exporter/signalfxexporter/config_test.go | 2 +- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/exporter/datadogexporter/config_warnings_test.go b/exporter/datadogexporter/config_warnings_test.go index ea2220d1fdd2..ab98a6e010d6 100644 --- a/exporter/datadogexporter/config_warnings_test.go +++ b/exporter/datadogexporter/config_warnings_test.go @@ -7,6 +7,7 @@ import ( "testing" "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/confmap" ) @@ -79,7 +80,7 @@ func TestSendAggregations(t *testing.T) { t.Run(testInstance.name, func(t *testing.T) { f := NewFactory() cfg := f.CreateDefaultConfig().(*Config) - err := cfg.Unmarshal(testInstance.cfgMap) + err := component.UnmarshalConfig(testInstance.cfgMap, cfg) if err != nil || testInstance.err != "" { assert.EqualError(t, err, testInstance.err) } else { @@ -156,7 +157,7 @@ func TestPeerTags(t *testing.T) { t.Run(testInstance.name, func(t *testing.T) { f := NewFactory() cfg := f.CreateDefaultConfig().(*Config) - err := cfg.Unmarshal(testInstance.cfgMap) + err := component.UnmarshalConfig(testInstance.cfgMap, cfg) if err != nil || testInstance.err != "" { assert.EqualError(t, err, testInstance.err) } else { diff --git a/exporter/datasetexporter/config_test.go b/exporter/datasetexporter/config_test.go index a833911a6adb..07db7361c4d2 100644 --- a/exporter/datasetexporter/config_test.go +++ b/exporter/datasetexporter/config_test.go @@ -9,6 +9,7 @@ import ( "time" "github.com/stretchr/testify/assert" + "go.opentelemetry.io/collector/component" "go.opentelemetry.io/collector/config/configretry" "go.opentelemetry.io/collector/confmap" "go.opentelemetry.io/collector/exporter/exporterhelper" @@ -22,7 +23,7 @@ func TestConfigUnmarshalUnknownAttributes(t *testing.T) { "api_key": "secret", "unknown_attribute": "some value", }) - err := config.Unmarshal(configMap) + err := component.UnmarshalConfig(configMap, config) unmarshalErr := fmt.Errorf("1 error(s) decoding:\n\n* '' has invalid keys: unknown_attribute") expectedError := fmt.Errorf("cannot unmarshal config: %w", unmarshalErr) diff --git a/exporter/signalfxexporter/config_test.go b/exporter/signalfxexporter/config_test.go index 5f1c4bdc6bc7..8f135e5b77d3 100644 --- a/exporter/signalfxexporter/config_test.go +++ b/exporter/signalfxexporter/config_test.go @@ -552,7 +552,7 @@ func TestUnmarshalExcludeMetrics(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - require.NoError(t, tt.cfg.Unmarshal(confmap.NewFromStringMap(map[string]any{}))) + require.NoError(t, component.UnmarshalConfig(confmap.NewFromStringMap(map[string]any{}), tt.cfg)) assert.Len(t, tt.cfg.ExcludeMetrics, tt.excludeMetricsLen) }) }