Skip to content

Commit

Permalink
[processor/transform] Report all parsing errors (#24245)
Browse files Browse the repository at this point in the history
**Description:**

Follow up from
#23840
to report all errors that occur during parsing. The error isn't pretty,
but this way users know all syntax errors in their OTTL statements up
front.

I played around with trying to format this to make it more readable, but
wasn't able to come up with anything that was satisfactory.

Example config and resulting error:

```yaml
processors:
  transform:
    trace_statements:
      - context: span
        statements: 
        - ParseJSON("...")
        - false
        - set[[]]
    metric_statements:
      - context: datapoint
        statements: 
        - set(attributes, attributes)
        - true
        - set(
```

```
Error: invalid configuration: processors::transform: unable to parse OTTL statement "ParseJSON(\"...\")": editor names must start with a lowercase letter but got 'ParseJSON'; unable to parse OTTL statement "0": statement has invalid syntax: 1:1: unexpected token "0"; unable to parse OTTL statement "set[[]]": statement has invalid syntax: 1:4: unexpected token "[" (expected "(" (Value ("," Value)*)? ")" Key*); unable to parse OTTL statement "1": statement has invalid syntax: 1:1: unexpected token "1"; unable to parse OTTL statement "set(": statement has invalid syntax: 1:5: unexpected token "<EOF>" (expected ")" Key*)
```

I aggregated the errors both in the `Config` struct and on processor
startup, but realistically are highly unlikely to get an error from the
parser on startup because we've already validated the statements. I
updated it just so we're consistent between validating the config and
reporting errors from the OTTL parser.

---------

Co-authored-by: Evan Bradley <[email protected]>
  • Loading branch information
evan-bradley and evan-bradley authored Jul 14, 2023
1 parent 983815c commit 9b28f76
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 10 deletions.
20 changes: 20 additions & 0 deletions .chloggen/tp-report-all-parsing-errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Use this changelog template to create an entry for release notes.
# If your change doesn't affect end users, such as a test fix or a tooling change,
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label.

# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver)
component: processor/transform

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Report all errors from parsing OTTL statements

# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists.
issues: [24245]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
13 changes: 8 additions & 5 deletions processor/transformprocessor/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package transformprocessor // import "github.com/open-telemetry/opentelemetry-co

import (
"go.opentelemetry.io/collector/component"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand All @@ -31,6 +32,8 @@ type Config struct {
var _ component.Config = (*Config)(nil)

func (c *Config) Validate() error {
var errors error

if len(c.TraceStatements) > 0 {
pc, err := common.NewTraceParserCollection(component.TelemetrySettings{Logger: zap.NewNop()}, common.WithSpanParser(traces.SpanFunctions()), common.WithSpanEventParser(traces.SpanEventFunctions()))
if err != nil {
Expand All @@ -39,7 +42,7 @@ func (c *Config) Validate() error {
for _, cs := range c.TraceStatements {
_, err = pc.ParseContextStatements(cs)
if err != nil {
return err
errors = multierr.Append(errors, err)
}
}
}
Expand All @@ -50,9 +53,9 @@ func (c *Config) Validate() error {
return err
}
for _, cs := range c.MetricStatements {
_, err = pc.ParseContextStatements(cs)
_, err := pc.ParseContextStatements(cs)
if err != nil {
return err
errors = multierr.Append(errors, err)
}
}
}
Expand All @@ -65,10 +68,10 @@ func (c *Config) Validate() error {
for _, cs := range c.LogStatements {
_, err = pc.ParseContextStatements(cs)
if err != nil {
return err
errors = multierr.Append(errors, err)
}
}
}

return nil
return errors
}
14 changes: 13 additions & 1 deletion processor/transformprocessor/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/assert"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap/confmaptest"
"go.uber.org/multierr"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
"github.com/open-telemetry/opentelemetry-collector-contrib/processor/transformprocessor/internal/common"
Expand All @@ -22,6 +23,7 @@ func TestLoadConfig(t *testing.T) {
tests := []struct {
id component.ID
expected component.Config
errorLen int
}{
{
id: component.NewIDWithName(metadata.Type, ""),
Expand Down Expand Up @@ -108,6 +110,10 @@ func TestLoadConfig(t *testing.T) {
{
id: component.NewIDWithName(metadata.Type, "unknown_function_log"),
},
{
id: component.NewIDWithName(metadata.Type, "bad_syntax_multi_signal"),
errorLen: 3,
},
}
for _, tt := range tests {
t.Run(tt.id.String(), func(t *testing.T) {
Expand All @@ -122,7 +128,13 @@ func TestLoadConfig(t *testing.T) {
assert.NoError(t, component.UnmarshalConfig(sub, cfg))

if tt.expected == nil {
assert.Error(t, component.ValidateConfig(cfg))
err = component.ValidateConfig(cfg)
assert.Error(t, err)

if tt.errorLen > 0 {
assert.Equal(t, tt.errorLen, len(multierr.Errors(err)))
}

return
}
assert.NoError(t, component.ValidateConfig(cfg))
Expand Down
2 changes: 1 addition & 1 deletion processor/transformprocessor/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ require (
go.opentelemetry.io/collector/consumer v0.81.0
go.opentelemetry.io/collector/pdata v1.0.0-rcv0013
go.opentelemetry.io/collector/processor v0.81.0
go.uber.org/multierr v1.11.0
go.uber.org/zap v1.24.0
)

Expand Down Expand Up @@ -39,7 +40,6 @@ require (
go.opentelemetry.io/otel/metric v1.16.0 // indirect
go.opentelemetry.io/otel/trace v1.16.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
Expand Down
8 changes: 7 additions & 1 deletion processor/transformprocessor/internal/logs/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/plog"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand All @@ -27,14 +28,19 @@ func NewProcessor(contextStatements []common.ContextStatements, errorMode ottl.E
}

contexts := make([]consumer.Logs, len(contextStatements))
var errors error
for i, cs := range contextStatements {
context, err := pc.ParseContextStatements(cs)
if err != nil {
return nil, err
errors = multierr.Append(errors, err)
}
contexts[i] = context
}

if errors != nil {
return nil, errors
}

return &Processor{
contexts: contexts,
logger: settings.Logger,
Expand Down
8 changes: 7 additions & 1 deletion processor/transformprocessor/internal/metrics/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/pmetric"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand All @@ -27,14 +28,19 @@ func NewProcessor(contextStatements []common.ContextStatements, errorMode ottl.E
}

contexts := make([]consumer.Metrics, len(contextStatements))
var errors error
for i, cs := range contextStatements {
context, err := pc.ParseContextStatements(cs)
if err != nil {
return nil, err
errors = multierr.Append(errors, err)
}
contexts[i] = context
}

if errors != nil {
return nil, errors
}

return &Processor{
contexts: contexts,
logger: settings.Logger,
Expand Down
8 changes: 7 additions & 1 deletion processor/transformprocessor/internal/traces/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/consumer"
"go.opentelemetry.io/collector/pdata/ptrace"
"go.uber.org/multierr"
"go.uber.org/zap"

"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
Expand All @@ -27,14 +28,19 @@ func NewProcessor(contextStatements []common.ContextStatements, errorMode ottl.E
}

contexts := make([]consumer.Traces, len(contextStatements))
var errors error
for i, cs := range contextStatements {
context, err := pc.ParseContextStatements(cs)
if err != nil {
return nil, err
errors = multierr.Append(errors, err)
}
contexts[i] = context
}

if errors != nil {
return nil, errors
}

return &Processor{
contexts: contexts,
logger: settings.Logger,
Expand Down
17 changes: 17 additions & 0 deletions processor/transformprocessor/testdata/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,23 @@ transform/bad_syntax_trace:
- set(name, "bear" where attributes["http.path"] == "/animal"
- keep_keys(attributes, ["http.method", "http.path"])

transform/bad_syntax_multi_signal:
trace_statements:
- context: span
statements:
- set(name, "bear" where attributes["http.path"] == "/animal"
- keep_keys(attributes, ["http.method", "http.path"])
metric_statements:
- context: datapoint
statements:
- set(name, "bear" where attributes["http.path"] == "/animal"
- keep_keys(attributes, ["http.method", "http.path"])
log_statements:
- context: log
statements:
- set(body, "bear" where attributes["http.path"] == "/animal"
- keep_keys(attributes, ["http.method", "http.path"])

transform/unknown_function_log:
log_statements:
- context: log
Expand Down

0 comments on commit 9b28f76

Please sign in to comment.