diff --git a/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_tkn_passthrough.yaml b/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_tkn_passthrough.yaml new file mode 100644 index 0000000000..66655e7110 --- /dev/null +++ b/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_tkn_passthrough.yaml @@ -0,0 +1,6 @@ +receivers: + sapm: + access_token_passthrough: true + +processors: + batch: diff --git a/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_tkn_passthrough_and_metadata.yaml b/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_tkn_passthrough_and_metadata.yaml new file mode 100644 index 0000000000..646dfbb71f --- /dev/null +++ b/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_tkn_passthrough_and_metadata.yaml @@ -0,0 +1,9 @@ +receivers: + sapm: + access_token_passthrough: true + +processors: + batch: + metadata_keys: + - X-SF-Token + \ No newline at end of file diff --git a/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_wo_tkn_passthrough.yaml b/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_wo_tkn_passthrough.yaml new file mode 100644 index 0000000000..c0ce4c578e --- /dev/null +++ b/internal/configconverter/testdata/include_metadata_on_sapm_token_passthrough/agent_config_wo_tkn_passthrough.yaml @@ -0,0 +1,9 @@ +receivers: + sapm: + + +processors: + batch: + send_batch_size: 10000 + timeout: 10s + \ No newline at end of file diff --git a/internal/configconverter/update_batchproc_on_token_passthrough.go b/internal/configconverter/update_batchproc_on_token_passthrough.go new file mode 100644 index 0000000000..3270d234dd --- /dev/null +++ b/internal/configconverter/update_batchproc_on_token_passthrough.go @@ -0,0 +1,52 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package configconverter + +import ( + "context" + "fmt" + + "go.opentelemetry.io/collector/confmap" +) + +func UpdateBatchProcOnTokenPassthrough(_ context.Context, in *confmap.Conf) error { + if in == nil { + return nil + } + + out := map[string]any{} + + // Check for sapm receiver with access_token_passthrough + if in.IsSet("receivers::sapm::access_token_passthrough") { + if _, ok := in.Get("receivers::sapm::access_token_passthrough").(bool); ok { + // Add metadata_keys to batch processor + switch batchProcessor := in.Get("processors::batch").(type) { + case nil: + out["processors::batch"] = map[string]any{ + "metadata_keys": []interface{}{"X-SF-Token"}, + } + case map[string]interface{}: + batchProcessor["metadata_keys"] = []any{"X-SF-Token"} + out["processors::batch"] = batchProcessor + default: + return fmt.Errorf("unexpected type for processors::batch: %T", batchProcessor) + } + } + } + + // Merge the modified configuration back into the original Conf + modifiedConf := confmap.NewFromStringMap(out) + return in.Merge(modifiedConf) +} diff --git a/internal/configconverter/update_batchproc_on_token_passthrough_test.go b/internal/configconverter/update_batchproc_on_token_passthrough_test.go new file mode 100644 index 0000000000..130c06aaa2 --- /dev/null +++ b/internal/configconverter/update_batchproc_on_token_passthrough_test.go @@ -0,0 +1,54 @@ +// Copyright The OpenTelemetry Authors +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package configconverter + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" + "go.opentelemetry.io/collector/confmap/confmaptest" +) + +func TestUpdateBatchProcOnTokenPassthrough(t *testing.T) { + cfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_tkn_passthrough.yaml") + require.NotNil(t, cfgMap) + require.NoError(t, err) + + expectedCfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_tkn_passthrough_and_metadata.yaml") + require.NoError(t, err) + require.NotNil(t, cfgMap) + + err = UpdateBatchProcOnTokenPassthrough(context.Background(), cfgMap) + require.NoError(t, err) + + assert.Equal(t, expectedCfgMap, cfgMap) +} + +func TestNoTokenAccessPassthrough(t *testing.T) { + cfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_wo_tkn_passthrough.yaml") + require.NotNil(t, cfgMap) + require.NoError(t, err) + + expectedCfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_wo_tkn_passthrough.yaml") + require.NoError(t, err) + require.NotNil(t, cfgMap) + + err = UpdateBatchProcOnTokenPassthrough(context.Background(), cfgMap) + require.NoError(t, err) + + assert.Equal(t, expectedCfgMap, cfgMap) +} diff --git a/internal/settings/settings.go b/internal/settings/settings.go index 2ba2d154a5..a6ecc4c0b9 100644 --- a/internal/settings/settings.go +++ b/internal/settings/settings.go @@ -206,6 +206,7 @@ func (s *Settings) ConfMapConverterFactories() []confmap.ConverterFactory { if !s.noConvertConfig { confMapConverterFactories = append( confMapConverterFactories, + configconverter.ConverterFactoryFromFunc(configconverter.UpdateBatchProcOnTokenPassthrough), configconverter.ConverterFactoryFromFunc(configconverter.NormalizeGcp), configconverter.ConverterFactoryFromFunc(configconverter.DisableKubeletUtilizationMetrics), configconverter.ConverterFactoryFromFunc(configconverter.DisableExcessiveInternalMetrics), diff --git a/internal/settings/settings_test.go b/internal/settings/settings_test.go index 53dfba7698..a1ac91f5cd 100644 --- a/internal/settings/settings_test.go +++ b/internal/settings/settings_test.go @@ -156,7 +156,7 @@ func TestNewSettingsConvertConfig(t *testing.T) { require.Equal(t, []string(nil), settings.discoveryProperties) require.Equal(t, []string{configPath, anotherConfigPath}, settings.ResolverURIs()) - require.Equal(t, 6, len(settings.ConfMapConverterFactories())) + require.Equal(t, 7, len(settings.ConfMapConverterFactories())) require.Equal(t, []string{"--feature-gates", "foo", "--feature-gates", "-bar"}, settings.ColCoreArgs()) }