Skip to content

Commit

Permalink
Config and Factory testing
Browse files Browse the repository at this point in the history
More complete example

Signed-off-by: John <john.dorman@sony.com>
  • Loading branch information
boostchicken authored and jpkrohling committed Jan 18, 2022
1 parent 382fe62 commit 2f3912a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 15 deletions.
17 changes: 17 additions & 0 deletions processor/filterprocessor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,15 +209,32 @@ In case the no metric names are provided, `matric_names` being empty, the filter

See the documentation in the [attribute processor](../attributesprocessor/README.md) for syntax

For spans, one of Services, SpanNames, Attributes, Resources or Libraries must be specified with a
non-empty value for a valid configuration.

```yaml
processors:
filter:
spans:
include:
match_type: strict
services:
- app_3
exclude:
match_type: regex
services:
- app_1
- app_2
span_names:
- hello_world
- hello/world
attributes:
- Key: container.name
Value: (app_container_1|app_container_2)
libraries:
- Name: opentelemetry
Version: 0.0-beta
resources:
- Key: container.host
Value: (localhost|127.0.0.1)
```
11 changes: 9 additions & 2 deletions processor/filterprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package filterprocessor

import (
"github.com/open-telemetry/opentelemetry-collector-contrib/internal/coreinternal/processor/filterset"
"path"
"testing"

Expand Down Expand Up @@ -286,15 +287,21 @@ func TestLoadingSpans(t *testing.T) {
}{
{
expCfg: &Config{
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "includeexclude")),
ProcessorSettings: config.NewProcessorSettings(config.NewComponentIDWithName(typeStr, "spans")),
Spans: SpanFilters{
Include: &filterconfig.MatchProperties{
Services: []string{"test"},
Config: filterset.Config{
MatchType: filterset.Strict,
},
Services: []string{"test", "test2"},
Attributes: []filterconfig.Attribute{
{Key: "should_include", Value: "(true|probably_true)"},
},
},
Exclude: &filterconfig.MatchProperties{
Config: filterset.Config{
MatchType: filterset.Regexp,
},
Attributes: []filterconfig.Attribute{
{Key: "should_exclude", Value: "(probably_false|false)"},
},
Expand Down
28 changes: 22 additions & 6 deletions processor/filterprocessor/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"context"
"fmt"
"path"
"strings"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -70,6 +71,12 @@ func TestCreateProcessors(t *testing.T) {
}, {
configName: "config_logs_record_attributes_regexp.yaml",
succeed: true,
}, {
configName: "config_traces.yaml",
succeed: true,
}, {
configName: "config_traces_invalid.yaml",
succeed: false,
},
}

Expand All @@ -87,13 +94,22 @@ func TestCreateProcessors(t *testing.T) {
factory := NewFactory()

tp, tErr := factory.CreateTracesProcessor(context.Background(), componenttest.NewNopProcessorCreateSettings(), cfg, consumertest.NewNop())
// Not implemented error
assert.NotNil(t, tErr)
assert.Nil(t, tp)

mp, mErr := factory.CreateMetricsProcessor(context.Background(), componenttest.NewNopProcessorCreateSettings(), cfg, consumertest.NewNop())
assert.Equal(t, test.succeed, mp != nil)
assert.Equal(t, test.succeed, mErr == nil)
if strings.Contains(test.configName, "traces") {
assert.Equal(t, test.succeed, tp != nil)
assert.Equal(t, test.succeed, tErr == nil)

assert.NotNil(t, mp)
assert.Nil(t, mErr)
} else {
// Should not break configs with no trace data
assert.NotNil(t, tp)
assert.Nil(t, tErr)

assert.Equal(t, test.succeed, mp != nil)
assert.Equal(t, test.succeed, mErr == nil)
}

})
}
}
Expand Down
22 changes: 18 additions & 4 deletions processor/filterprocessor/filter_processor_traces.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ type filterSpanProcessor struct {
}

func newFilterSpansProcessor(logger *zap.Logger, cfg *Config) (*filterSpanProcessor, error) {
if cfg.Spans.Include == nil && cfg.Spans.Exclude == nil {
return nil, nil
}

inc, err := createSpanMatcher(cfg.Spans.Include)
if err != nil {
return nil, err
}

exc, err := createSpanMatcher(cfg.Spans.Exclude)
if err != nil {
return nil, err
Expand All @@ -53,9 +56,20 @@ func newFilterSpansProcessor(logger *zap.Logger, cfg *Config) (*filterSpanProces
}

logger.Info(
"Span filter configured",
zap.String("include match_type", includeMatchType),
zap.String("exclude match_type", excludeMatchType),
"Span filtering configured",
zap.String("[Include] match_type", includeMatchType),
zap.Any("[Include] attributes", cfg.Spans.Include.Attributes),
zap.Any("[Include] libraries", cfg.Spans.Include.Libraries),
zap.Any("[Include] attributes", cfg.Spans.Include.Resources),
zap.Strings("[Include] services", cfg.Spans.Include.Services),
zap.Strings("[Include] span_names", cfg.Spans.Include.SpanNames),
zap.String("[Exclude] match_type", excludeMatchType),
zap.Any("[Exclude] attributes", cfg.Spans.Exclude.Attributes),
zap.Any("[Exclude] libraries", cfg.Spans.Exclude.Libraries),
zap.Any("[Exclude] resources", cfg.Spans.Exclude.Resources),
zap.Strings("[Exclude] services", cfg.Spans.Exclude.Services),
zap.Strings("[Exclude] span_names", cfg.Spans.Exclude.SpanNames),

)

return &filterSpanProcessor{
Expand Down
10 changes: 7 additions & 3 deletions processor/filterprocessor/testdata/config_traces.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,21 @@ receivers:
nop:

processors:
filter/includeexclude:
filter/spans:
spans:
# if both include and exclude are specified, include filters are applied first
# the following configuration would only allow logs with resource attributes
# "should_include" to pass through
include:
services: test
match_type: strict
services:
- test
- test2
attributes:
- key: should_include
value: "(true|probably_true)"
exclude:
match_type: regexp
attributes:
- key: should_exclude
value: "(probably_false|false)"
Expand All @@ -24,5 +28,5 @@ service:
pipelines:
traces:
receivers: [nop]
processors: [filter/includeexclude]
processors: [filter/spans]
exporters: [nop]
29 changes: 29 additions & 0 deletions processor/filterprocessor/testdata/config_traces_invalid.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
receivers:
nop:

processors:
filter/spans:
spans:
# if both include and exclude are specified, include filters are applied first
# the following configuration would only allow logs with resource attributes
# "should_include" to pass through
include:
match_type: regexp
services:
- test
attributes:
- key: should_include
value: "(true|probably_true)"
# should error since nothing is set to match on
exclude:
match_type: regexp

exporters:
nop:

service:
pipelines:
traces:
receivers: [nop]
processors: [filter/spans]
exporters: [nop]

0 comments on commit 2f3912a

Please sign in to comment.