Skip to content

Commit

Permalink
[chore] Change processor config tests to unmarshal config only for th…
Browse files Browse the repository at this point in the history
…at component. (part 1) (open-telemetry#13298)

* [chore] change processor config tests to unmarshal config only for that component.

Updates open-telemetry#13224

- attributesprocessor
- cumulativetodeltaprocessor
- deltatorateprocessor
- filterprocessor
- groupbyattrsprocessor
- groupbytraceprocessor

* [chore] Address comments.

* [chore] run "make gotidy"

build-and-test / checks (pull_request) Failing after 3m — checks

Following instructions:
```
go.mod/go.sum deps changes detected, please run "make gotidy" and commit the changes in this PR.
```
  • Loading branch information
kovrus committed Aug 15, 2022
1 parent fb1e263 commit 038c562
Show file tree
Hide file tree
Showing 36 changed files with 1,481 additions and 1,866 deletions.
335 changes: 181 additions & 154 deletions processor/attributesprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,197 +18,224 @@ import (
"path/filepath"
"testing"

"go.opentelemetry.io/collector/confmap/confmaptest"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component/componenttest"
"go.opentelemetry.io/collector/config"
"go.opentelemetry.io/collector/service/servicetest"

"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/attraction"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filterconfig"
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filterset"
)

func TestLoadingConfig(t *testing.T) {
factories, err := componenttest.NopFactories()
assert.NoError(t, err)

factory := NewFactory()
factories.Processors[typeStr] = factory
cfg, err := servicetest.LoadConfigAndValidate(filepath.Join("testdata", "config.yaml"), factories)
assert.NoError(t, err)
require.NotNil(t, cfg)

p0 := cfg.Processors[config.NewComponentIDWithName(typeStr, "insert")]
assert.Equal(t, p0, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "insert")),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "attribute1", Value: 123, Action: attraction.INSERT},
{Key: "string key", FromAttribute: "anotherkey", Action: attraction.INSERT},
func TestLoadConfig(t *testing.T) {
t.Parallel()

tests := []struct {
id config.ComponentID
expected config.Processor
}{
{
id: config.NewComponentIDWithName(typeStr, "insert"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "attribute1", Value: 123, Action: attraction.INSERT},
{Key: "string key", FromAttribute: "anotherkey", Action: attraction.INSERT},
},
},
},
},
})

p1 := cfg.Processors[config.NewComponentIDWithName(typeStr, "update")]
assert.Equal(t, p1, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "update")),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "boo", FromAttribute: "foo", Action: attraction.UPDATE},
{Key: "db.secret", Value: "redacted", Action: attraction.UPDATE},
{
id: config.NewComponentIDWithName(typeStr, "update"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "boo", FromAttribute: "foo", Action: attraction.UPDATE},
{Key: "db.secret", Value: "redacted", Action: attraction.UPDATE},
},
},
},
},
})

p2 := cfg.Processors[config.NewComponentIDWithName(typeStr, "upsert")]
assert.Equal(t, p2, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "upsert")),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "region", Value: "planet-earth", Action: attraction.UPSERT},
{Key: "new_user_key", FromAttribute: "user_key", Action: attraction.UPSERT},
{
id: config.NewComponentIDWithName(typeStr, "upsert"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "region", Value: "planet-earth", Action: attraction.UPSERT},
{Key: "new_user_key", FromAttribute: "user_key", Action: attraction.UPSERT},
},
},
},
},
})

p3 := cfg.Processors[config.NewComponentIDWithName(typeStr, "delete")]
assert.Equal(t, p3, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "delete")),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "credit_card", Action: attraction.DELETE},
{Key: "duplicate_key", Action: attraction.DELETE},
{
id: config.NewComponentIDWithName(typeStr, "delete"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "credit_card", Action: attraction.DELETE},
{Key: "duplicate_key", Action: attraction.DELETE},
},
},
},
},
})

p4 := cfg.Processors[config.NewComponentIDWithName(typeStr, "hash")]
assert.Equal(t, p4, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "hash")),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "user.email", Action: attraction.HASH},
{
id: config.NewComponentIDWithName(typeStr, "hash"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "user.email", Action: attraction.HASH},
},
},
},
},
})

p5 := cfg.Processors[config.NewComponentIDWithName(typeStr, "excludemulti")]
assert.Equal(t, p5, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "excludemulti")),
MatchConfig: filterconfig.MatchConfig{
Exclude: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Strict),
Services: []string{"svcA", "svcB"},
Attributes: []filterconfig.Attribute{
{Key: "env", Value: "dev"},
{Key: "test_request"},
{
id: config.NewComponentIDWithName(typeStr, "excludemulti"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
MatchConfig: filterconfig.MatchConfig{
Exclude: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Strict),
Services: []string{"svcA", "svcB"},
Attributes: []filterconfig.Attribute{
{Key: "env", Value: "dev"},
{Key: "test_request"},
},
},
},
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "credit_card", Action: attraction.DELETE},
{Key: "duplicate_key", Action: attraction.DELETE},
},
},
},
},
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "credit_card", Action: attraction.DELETE},
{Key: "duplicate_key", Action: attraction.DELETE},
{
id: config.NewComponentIDWithName(typeStr, "includeservices"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
MatchConfig: filterconfig.MatchConfig{
Include: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Regexp),
Services: []string{"auth.*", "login.*"},
},
},
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "credit_card", Action: attraction.DELETE},
{Key: "duplicate_key", Action: attraction.DELETE},
},
},
},
},
})

p6 := cfg.Processors[config.NewComponentIDWithName(typeStr, "includeservices")]
assert.Equal(t, p6, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "includeservices")),
MatchConfig: filterconfig.MatchConfig{
Include: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Regexp),
Services: []string{"auth.*", "login.*"},
{
id: config.NewComponentIDWithName(typeStr, "selectiveprocessing"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
MatchConfig: filterconfig.MatchConfig{
Include: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Strict),
Services: []string{"svcA", "svcB"},
},
Exclude: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Strict),
Attributes: []filterconfig.Attribute{
{Key: "redact_trace", Value: false},
},
},
},
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "credit_card", Action: attraction.DELETE},
{Key: "duplicate_key", Action: attraction.DELETE},
},
},
},
},
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "credit_card", Action: attraction.DELETE},
{Key: "duplicate_key", Action: attraction.DELETE},
{
id: config.NewComponentIDWithName(typeStr, "complex"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "operation", Value: "default", Action: attraction.INSERT},
{Key: "svc.operation", FromAttribute: "operation", Action: attraction.UPSERT},
{Key: "operation", Action: attraction.DELETE},
},
},
},
},
})

p7 := cfg.Processors[config.NewComponentIDWithName(typeStr, "selectiveprocessing")]
assert.Equal(t, p7, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "selectiveprocessing")),
MatchConfig: filterconfig.MatchConfig{
Include: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Strict),
Services: []string{"svcA", "svcB"},
},
Exclude: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Strict),
Attributes: []filterconfig.Attribute{
{Key: "redact_trace", Value: false},
{
id: config.NewComponentIDWithName(typeStr, "example"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "db.table", Action: attraction.DELETE},
{Key: "redacted_span", Value: true, Action: attraction.UPSERT},
{Key: "copy_key", FromAttribute: "key_original", Action: attraction.UPDATE},
{Key: "account_id", Value: 2245, Action: attraction.INSERT},
{Key: "account_password", Action: attraction.DELETE},
},
},
},
},
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "credit_card", Action: attraction.DELETE},
{Key: "duplicate_key", Action: attraction.DELETE},
{
id: config.NewComponentIDWithName(typeStr, "regexp"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
MatchConfig: filterconfig.MatchConfig{
Include: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Regexp),
Services: []string{"auth.*"},
},
Exclude: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Regexp),
SpanNames: []string{"login.*"},
},
},
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "password", Action: attraction.UPDATE, Value: "obfuscated"},
{Key: "token", Action: attraction.DELETE},
},
},
},
},
})

p8 := cfg.Processors[config.NewComponentIDWithName(typeStr, "complex")]
assert.Equal(t, p8, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "complex")),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "operation", Value: "default", Action: attraction.INSERT},
{Key: "svc.operation", FromAttribute: "operation", Action: attraction.UPSERT},
{Key: "operation", Action: attraction.DELETE},
{
id: config.NewComponentIDWithName(typeStr, "convert"),
expected: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentID(typeStr)),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "http.status_code", Action: attraction.CONVERT, ConvertedType: "int"},
},
},
},
},
})
}

p9 := cfg.Processors[config.NewComponentIDWithName(typeStr, "example")]
assert.Equal(t, p9, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "example")),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "db.table", Action: attraction.DELETE},
{Key: "redacted_span", Value: true, Action: attraction.UPSERT},
{Key: "copy_key", FromAttribute: "key_original", Action: attraction.UPDATE},
{Key: "account_id", Value: 2245, Action: attraction.INSERT},
{Key: "account_password", Action: attraction.DELETE},
},
},
})
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
require.NoError(t, err)

p10 := cfg.Processors[config.NewComponentIDWithName(typeStr, "regexp")]
assert.Equal(t, p10, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "regexp")),
MatchConfig: filterconfig.MatchConfig{
Include: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Regexp),
Services: []string{"auth.*"},
},
Exclude: &filterconfig.MatchProperties{
Config: *createConfig(filterset.Regexp),
SpanNames: []string{"login.*"},
},
},
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "password", Action: attraction.UPDATE, Value: "obfuscated"},
{Key: "token", Action: attraction.DELETE},
},
},
})
factory := NewFactory()
cfg := factory.CreateDefaultConfig()

p11 := cfg.Processors[config.NewComponentIDWithName(typeStr, "convert")]
assert.Equal(t, p11, &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "convert")),
Settings: attraction.Settings{
Actions: []attraction.ActionKeyValue{
{Key: "http.status_code", Action: attraction.CONVERT, ConvertedType: "int"},
},
},
})
sub, err := cm.Sub(tt.id.String())
require.NoError(t, err)
require.NoError(t, config.UnmarshalProcessor(sub, cfg))

assert.NoError(t, cfg.Validate())
assert.Equal(t, tt.expected, cfg)
})
}
}
Loading

0 comments on commit 038c562

Please sign in to comment.