-
Notifications
You must be signed in to change notification settings - Fork 232
Add decoupleafterbatch converter to ensure decouple processor follows batch processor #1255
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
tylerbenson
merged 22 commits into
open-telemetry:main
from
nslaughter:enhancement/decouple-first
Apr 22, 2024
Merged
Changes from 19 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3d7dde0
Always put decouple processor first in pipeline
nslaughter 728e681
Add converter to derive processors from a base
nslaughter f4b76d3
Remove scratchpad code
nslaughter ffab596
implement rules and test
nslaughter 25e2092
update tests
nslaughter d543b2c
improve tests for reviewers
nslaughter e2dd81c
Merge branch 'open-telemetry:main' into enhancement/decouple-first
nslaughter 4c51018
fix toggle for append predicate
nslaughter 38e8f13
Fix typo in function comment
nslaughter e35b344
Document converter and auto-configuration
nslaughter b536046
Document converter and auto-configuration
nslaughter a10b9e1
rm errant test
nslaughter 65ce10f
Add tests to clarify decouple->batch ill-formed chain
nslaughter ec45851
Fix typo in test case description
nslaughter 7b236f3
Improve name of predicate/helper
nslaughter 1b21b0b
Update collector/processor/decoupleprocessor/README.md
nslaughter a818179
gofmt -s -w .
nslaughter 9b72b9e
restructure tests to extend coverage
nslaughter d88a4e2
go mod tidy
nslaughter d8d5062
Update collector/internal/confmap/converter/decoupleafterbatchconvert…
nslaughter 5038809
Add auto-config explaination to Collector
nslaughter 9db6767
Merge branch 'main' into enhancement/decouple-first
nslaughter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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
11 changes: 11 additions & 0 deletions
11
collector/internal/confmap/converter/decoupleafterbatchconverter/README.md
This file contains hidden or 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,11 @@ | ||
| # DecoupleAfterBatch Converter | ||
|
|
||
| The `DecoupleAfterBatch` converter automatically modifies the collector's configuration for the Lambda distribution. Its purpose is to ensure that a decouple processor is always present after a batch processor in a pipeline, in order to prevent potential data loss due to the Lambda environment being frozen. | ||
|
|
||
| ## Behavior | ||
|
|
||
| The converter scans the collector's configuration and makes the following adjustments: | ||
|
|
||
| 1. If a pipeline contains a batch processor with no decouple processor defined after it, the converter will automatically add a decouple processor to the pipeline immediately after the batch processor. | ||
|
|
||
| 2. If a pipeline contains a batch processor with a decouple processor already defined after it or there is no batch processor defined, the converter will not make any changes to the pipeline configuration. | ||
nslaughter marked this conversation as resolved.
Show resolved
Hide resolved
|
||
118 changes: 118 additions & 0 deletions
118
collector/internal/confmap/converter/decoupleafterbatchconverter/converter.go
This file contains hidden or 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,118 @@ | ||
| // 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. | ||
|
|
||
| // The decoupleafterbatchconverter implements the Converter for mutating Collector | ||
| // configurations to ensure the decouple processor is placed after the batch processor. | ||
| // This is logically implemented by appending the decouple processor to the end of | ||
| // processor chains where a batch processor is found unless another decouple processor | ||
| // was seen. | ||
| package decoupleafterbatchconverter | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "strings" | ||
|
|
||
| "go.opentelemetry.io/collector/confmap" | ||
| ) | ||
|
|
||
| const ( | ||
| serviceKey = "service" | ||
| pipelinesKey = "pipelines" | ||
| processorsKey = "processors" | ||
| batchProcessor = "batch" | ||
| decoupleProcessor = "decouple" | ||
| ) | ||
|
|
||
| type converter struct{} | ||
|
|
||
| // New returns a confmap.Converter that ensures the decoupleprocessor is placed first in the pipeline. | ||
| func New() confmap.Converter { | ||
| return &converter{} | ||
| } | ||
|
|
||
| func (c converter) Convert(_ context.Context, conf *confmap.Conf) error { | ||
| serviceVal := conf.Get(serviceKey) | ||
| service, ok := serviceVal.(map[string]interface{}) | ||
| if !ok { | ||
| return nil | ||
| } | ||
|
|
||
| pipelinesVal, ok := service[pipelinesKey] | ||
| if !ok { | ||
| return nil | ||
| } | ||
|
|
||
| pipelines, ok := pipelinesVal.(map[string]interface{}) | ||
| if !ok { | ||
| return nil | ||
| } | ||
|
|
||
| // accumulates updates over the pipelines and applies them | ||
| // once all pipeline configs are processed | ||
| updates := make(map[string]interface{}) | ||
| for telemetryType, pipelineVal := range pipelines { | ||
| pipeline, ok := pipelineVal.(map[string]interface{}) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| processorsVal, ok := pipeline[processorsKey] | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| processors, ok := processorsVal.([]interface{}) | ||
| if !ok { | ||
| continue | ||
| } | ||
|
|
||
| // accumulate config updates | ||
| if shouldAppendDecouple(processors) { | ||
| processors = append(processors, decoupleProcessor) | ||
| updates[fmt.Sprintf("%s::%s::%s::%s", serviceKey, pipelinesKey, telemetryType, processorsKey)] = processors | ||
| break | ||
| } | ||
|
|
||
| } | ||
|
|
||
| // apply all updates | ||
| if len(updates) > 0 { | ||
| if err := conf.Merge(confmap.NewFromStringMap(updates)); err != nil { | ||
| return err | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| // The shouldAppendDecouple is the filter predicate for the Convert function action. It tells whether | ||
| // (bool) there was a decouple processor after the last | ||
| // batch processor, which Convert uses to decide whether to append the decouple processor. | ||
| func shouldAppendDecouple(processors []interface{}) bool { | ||
| var shouldAppendDecouple bool | ||
| for _, processorVal := range processors { | ||
| processor, ok := processorVal.(string) | ||
| if !ok { | ||
| continue | ||
| } | ||
| processorBaseName := strings.Split(processor, "/")[0] | ||
| if processorBaseName == batchProcessor { | ||
| shouldAppendDecouple = true | ||
| } else if processorBaseName == decoupleProcessor { | ||
| shouldAppendDecouple = false | ||
| } | ||
| } | ||
| return shouldAppendDecouple | ||
| } |
153 changes: 153 additions & 0 deletions
153
collector/internal/confmap/converter/decoupleafterbatchconverter/converter_test.go
This file contains hidden or 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,153 @@ | ||
| // 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 decoupleafterbatchconverter | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "go.opentelemetry.io/collector/confmap" | ||
|
|
||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| func TestConvert(t *testing.T) { | ||
| // Since this really tests differences in input, it's easier to read cases | ||
| // without the repeated definition of other fields in the config. | ||
| baseConf := func(input []interface{}) *confmap.Conf { | ||
| return confmap.NewFromStringMap(map[string]interface{}{ | ||
| "service": map[string]interface{}{ | ||
| "pipelines": map[string]interface{}{ | ||
| "traces": map[string]interface{}{ | ||
| "processors": input, | ||
| }, | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| testCases := []struct { | ||
| name string | ||
| input *confmap.Conf | ||
| expected *confmap.Conf | ||
| err error | ||
| }{ | ||
| // This test is first, because it illustrates the difference in making the rule that when | ||
| // batch is present the converter appends decouple processor to the end of chain versus | ||
| // the approach of this code which is to do this only when the last instance of batch | ||
| // is not followed by decouple processor. | ||
| { | ||
| name: "batch then decouple in middle of chain", | ||
| input: baseConf([]interface{}{"processor1", "batch", "decouple", "processor2"}), | ||
| expected: baseConf([]interface{}{"processor1", "batch", "decouple", "processor2"}), | ||
| }, | ||
| { | ||
| name: "no service", | ||
| input: confmap.New(), | ||
| expected: confmap.New(), | ||
| }, | ||
| { | ||
| name: "no pipelines", | ||
| input: confmap.NewFromStringMap( | ||
| map[string]interface{}{ | ||
| "service": map[string]interface{}{ | ||
| "extensions": map[string]interface{}{}, | ||
| }, | ||
| }, | ||
| ), | ||
| expected: confmap.NewFromStringMap( | ||
| map[string]interface{}{ | ||
| "service": map[string]interface{}{ | ||
| "extensions": map[string]interface{}{}, | ||
| }, | ||
| }, | ||
| ), | ||
| }, | ||
| { | ||
| name: "no processors in chain", | ||
| input: confmap.NewFromStringMap( | ||
| map[string]interface{}{ | ||
| "service": map[string]interface{}{ | ||
| "extensions": map[string]interface{}{}, | ||
| "pipelines": map[string]interface{}{ | ||
| "traces": map[string]interface{}{}, | ||
| }, | ||
| }, | ||
| }, | ||
| ), | ||
| expected: confmap.NewFromStringMap(map[string]interface{}{ | ||
| "service": map[string]interface{}{ | ||
| "extensions": map[string]interface{}{}, | ||
| "pipelines": map[string]interface{}{ | ||
| "traces": map[string]interface{}{}, | ||
| }, | ||
| }, | ||
| }, | ||
| ), | ||
| }, | ||
| { | ||
| name: "batch processor in singleton chain", | ||
| input: baseConf([]interface{}{"batch"}), | ||
| expected: baseConf([]interface{}{"batch", "decouple"}), | ||
| }, | ||
| { | ||
| name: "batch processor present twice", | ||
| input: baseConf([]interface{}{"batch", "processor1", "batch"}), | ||
| expected: baseConf([]interface{}{"batch", "processor1", "batch", "decouple"}), | ||
| }, | ||
|
|
||
| { | ||
| name: "batch processor not present", | ||
| input: baseConf([]interface{}{"processor1", "processor2"}), | ||
| expected: baseConf([]interface{}{"processor1", "processor2"}), | ||
| }, | ||
| { | ||
| name: "batch sandwiched between input no decouple", | ||
| input: baseConf([]interface{}{"processor1", "batch", "processor2"}), | ||
| expected: baseConf([]interface{}{"processor1", "batch", "processor2", "decouple"}), | ||
| }, | ||
|
|
||
| { | ||
| name: "batch and decouple input already present in correct position", | ||
| input: baseConf([]interface{}{"processor1", "batch", "processor2", "decouple"}), | ||
| expected: baseConf([]interface{}{"processor1", "batch", "processor2", "decouple"}), | ||
| }, | ||
| { | ||
| name: "decouple and batch", | ||
| input: baseConf([]interface{}{"decouple", "batch"}), | ||
| expected: baseConf([]interface{}{"decouple", "batch", "decouple"}), | ||
| }, | ||
| { | ||
| name: "decouple then batch mixed with others in the pipelinefirst then batch somewhere", | ||
| input: baseConf([]interface{}{"processor1", "decouple", "processor2", "batch", "processor3"}), | ||
| expected: baseConf([]interface{}{"processor1", "decouple", "processor2", "batch", "processor3", "decouple"}), | ||
| }, | ||
| } | ||
|
|
||
| for _, tc := range testCases { | ||
| t.Run(tc.name, func(t *testing.T) { | ||
| conf := tc.input | ||
| expected := tc.expected | ||
|
|
||
| c := New() | ||
| err := c.Convert(context.Background(), conf) | ||
| if err != tc.err { | ||
| t.Errorf("unexpected error converting: %v", err) | ||
| } | ||
| if diff := cmp.Diff(expected.ToStringMap(), conf.ToStringMap()); diff != "" { | ||
| t.Errorf("Convert() mismatch: (-want +got):\n%s", diff) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.