-
Notifications
You must be signed in to change notification settings - Fork 156
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add converter to update batch processor
when sapm exporter enables token_access_passthrough use this converter to add include_metadata with the right header key
- Loading branch information
1 parent
8026a6f
commit 6f85316
Showing
8 changed files
with
153 additions
and
1 deletion.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
.../testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_include_metadata.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
receivers: | ||
sapm: | ||
include_metadata: true | ||
|
||
processors: | ||
batch: |
6 changes: 6 additions & 0 deletions
6
.../include_metadata_on_sapm_token_passthrough/agent_config_w_include_metadata_disabled.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
receivers: | ||
sapm: | ||
include_metadata: false | ||
|
||
processors: | ||
batch: |
9 changes: 9 additions & 0 deletions
9
...testdata/include_metadata_on_sapm_token_passthrough/agent_config_wo_include_metadata.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
receivers: | ||
sapm: | ||
|
||
|
||
processors: | ||
batch: | ||
send_batch_size: 10000 | ||
timeout: 10s | ||
|
9 changes: 9 additions & 0 deletions
9
.../include_metadata_on_sapm_token_passthrough/expected_agent_config_w_include_metadata.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
receivers: | ||
sapm: | ||
include_metadata: true | ||
|
||
processors: | ||
batch: | ||
metadata_keys: | ||
- X-SF-Token | ||
|
52 changes: 52 additions & 0 deletions
52
internal/configconverter/update_batchproc_on_token_passthrough.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 include_metadata set to true | ||
if in.IsSet("receivers::sapm::include_metadata") { | ||
if accessTokenPassthrough, ok := in.Get("receivers::sapm::include_metadata").(bool); ok && accessTokenPassthrough { | ||
// 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) | ||
} |
69 changes: 69 additions & 0 deletions
69
internal/configconverter/update_batchproc_on_token_passthrough_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// 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 TestUpdateBatchProcOnIncludeMetadata(t *testing.T) { | ||
cfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_include_metadata.yaml") | ||
require.NotNil(t, cfgMap) | ||
require.NoError(t, err) | ||
|
||
expectedCfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/expected_agent_config_w_include_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 TestNoIncludeMetadata(t *testing.T) { | ||
cfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_wo_include_metadata.yaml") | ||
require.NotNil(t, cfgMap) | ||
require.NoError(t, err) | ||
|
||
expectedCfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_wo_include_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 TestIncludeMetadataDisabled(t *testing.T) { | ||
cfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_include_metadata_disabled.yaml") | ||
require.NotNil(t, cfgMap) | ||
require.NoError(t, err) | ||
|
||
expectedCfgMap, err := confmaptest.LoadConf("testdata/include_metadata_on_sapm_token_passthrough/agent_config_w_include_metadata_disabled.yaml") | ||
require.NoError(t, err) | ||
require.NotNil(t, cfgMap) | ||
|
||
err = UpdateBatchProcOnTokenPassthrough(context.Background(), cfgMap) | ||
require.NoError(t, err) | ||
|
||
assert.Equal(t, expectedCfgMap, cfgMap) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters