Skip to content

Commit

Permalink
add converter to update batch processor
Browse files Browse the repository at this point in the history
when sapm exporter enables token_access_passthrough
use this converter to add include_metadata with the
right header key
  • Loading branch information
asreehari-splunk committed Sep 19, 2024
1 parent 8026a6f commit fd1749a
Show file tree
Hide file tree
Showing 7 changed files with 170 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
receivers:
sapm:
access_token_passthrough: true

processors:
batch:
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
receivers:
sapm:
access_token_passthrough: true

processors:
batch:
metadata_keys:
- X-SF-Token

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
receivers:
sapm:


processors:
batch:
send_batch_size: 10000
timeout: 10s

64 changes: 64 additions & 0 deletions internal/configconverter/update_batchproc_on_token_passthrough.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// 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.

// 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]interface{}{}

// Check for sapm receiver with access_token_passthrough
if in.IsSet("receivers::sapm::access_token_passthrough") && in.Get("receivers::sapm::access_token_passthrough").(bool) {
// Add metadata_keys to batch processor
switch batchProcessor := in.Get("processors::batch").(type) {
case nil:
out["processors::batch"] = map[string]interface{}{
"metadata_keys": []interface{}{"X-SF-Token"},
}
case map[string]interface{}:
batchProcessor["metadata_keys"] = []interface{}{"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)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
// 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.

// 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)
}
1 change: 1 addition & 0 deletions internal/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
23 changes: 13 additions & 10 deletions internal/settings/settings_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

Expand Down Expand Up @@ -337,12 +337,18 @@ func TestSetDefaultEnvVarsSetsInterfaceFromConfigOption(t *testing.T) {
func TestSetDefaultFeatureGatesRespectsOverrides(t *testing.T) {
t.Cleanup(setRequiredEnvVars(t))
for _, args := range [][]string{
{"--feature-gates", "some-gate", "--feature-gates", "telemetry.useOtelForInternalMetrics", "--feature-gates",
"another-gate"},
{"--feature-gates", "some-gate", "--feature-gates", "+telemetry.useOtelForInternalMetrics",
"--feature-gates", "another-gate"},
{"--feature-gates", "some-gate", "--feature-gates", "-telemetry.useOtelForInternalMetrics",
"--feature-gates", "another-gate"},
{
"--feature-gates", "some-gate", "--feature-gates", "telemetry.useOtelForInternalMetrics", "--feature-gates",
"another-gate",
},
{
"--feature-gates", "some-gate", "--feature-gates", "+telemetry.useOtelForInternalMetrics",
"--feature-gates", "another-gate",
},
{
"--feature-gates", "some-gate", "--feature-gates", "-telemetry.useOtelForInternalMetrics",
"--feature-gates", "another-gate",
},
} {
t.Run(strings.Join(args, " "), func(t *testing.T) {
settings, err := New(args)
Expand All @@ -353,7 +359,6 @@ func TestSetDefaultFeatureGatesRespectsOverrides(t *testing.T) {
}

func TestSetSoftMemLimitWithoutGoMemLimitEnvVar(t *testing.T) {

// if GOLIMIT is not set, we expect soft limit to be 90% of the total memory env var or 90% of default total memory 512 Mib.
t.Cleanup(setRequiredEnvVars(t))
require.NoError(t, os.Setenv(MemTotalEnvVar, "200"))
Expand All @@ -367,7 +372,6 @@ func TestSetSoftMemLimitWithoutGoMemLimitEnvVar(t *testing.T) {
require.NoError(t, err)
require.NotNil(t, settings)
require.Equal(t, int64(482344960), debug.SetMemoryLimit(-1))

}

func TestUseConfigPathsFromEnvVar(t *testing.T) {
Expand Down Expand Up @@ -556,7 +560,6 @@ func TestConfigArgEnvURIForm(t *testing.T) {
require.NoError(t, err)
require.Equal(t, []string{"env:SOME_ENV_VAR"}, settings.configPaths.value)
require.Equal(t, settings.configPaths.value, settings.ResolverURIs())

}

func TestCheckRuntimeParams_MemTotal(t *testing.T) {
Expand Down

0 comments on commit fd1749a

Please sign in to comment.