-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
[processor/routing] Fix statement not eval in order #34999
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Use this changelog template to create an entry for release notes. | ||
|
||
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' | ||
change_type: bug_fix | ||
|
||
# The name of the component, or a single word describing the area of concern, (e.g. filelogreceiver) | ||
component: routingprocessor | ||
|
||
# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). | ||
note: Fix OTTL statement not eval in order | ||
|
||
# Mandatory: One or more tracking issues related to the change. You can use the PR number here if no issue exists. | ||
issues: [34860] | ||
|
||
# (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: | ||
|
||
# If your change doesn't affect end users or the exported elements of any package, | ||
# you should instead start your pull request title with [chore] or use the "Skip Changelog" label. | ||
# Optional: The change log or logs in which this entry should be included. | ||
# e.g. '[user]' or '[user, api]' | ||
# Include 'user' if the change is relevant to end users. | ||
# Include 'api' if there is a change to a library API. | ||
# Default: '[user]' | ||
change_logs: [] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ package routingprocessor | |
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
|
@@ -127,6 +128,63 @@ func TestLogs_RoutingWorks_Context(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestLogs_RoutingWorks_Context_Ordered(t *testing.T) { | ||
defaultExp := &mockLogsExporter{} | ||
lExpFirst := &mockLogsExporter{} | ||
lExpSecond := &mockLogsExporter{} | ||
lExpThird := &mockLogsExporter{} | ||
|
||
host := newMockHost(map[component.DataType]map[component.ID]component.Component{ | ||
component.DataTypeLogs: { | ||
component.MustNewID("otlp"): defaultExp, | ||
component.MustNewIDWithName("otlp", "first"): lExpFirst, | ||
component.MustNewIDWithName("otlp", "second"): lExpSecond, | ||
component.MustNewIDWithName("otlp", "third"): lExpThird, | ||
}, | ||
}) | ||
|
||
exp, err := newLogProcessor(noopTelemetrySettings, &Config{ | ||
FromAttribute: "X-Tenant", | ||
AttributeSource: contextAttributeSource, | ||
DefaultExporters: []string{"otlp"}, | ||
Table: []RoutingTableItem{ | ||
{ | ||
Value: "order-second", | ||
Exporters: []string{"otlp/second"}, | ||
}, | ||
{ | ||
Value: "order-first", | ||
Exporters: []string{"otlp/first"}, | ||
}, | ||
{ | ||
Value: "order-third", | ||
Exporters: []string{"otlp/third"}, | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
require.NoError(t, exp.Start(context.Background(), host)) | ||
|
||
for i := 1; i <= 5; i++ { | ||
t.Run(fmt.Sprintf("run %d time", i), func(t *testing.T) { | ||
l := plog.NewLogs() | ||
ll := l.ResourceLogs().AppendEmpty().ScopeLogs().AppendEmpty().LogRecords() | ||
ll.AppendEmpty().Body().SetStr("this is a log") | ||
|
||
assert.NoError(t, exp.ConsumeLogs( | ||
metadata.NewIncomingContext(context.Background(), metadata.New(map[string]string{ | ||
"X-Tenant": "order-third", | ||
})), | ||
l, | ||
)) | ||
assert.Empty(t, defaultExp.AllLogs()) | ||
assert.Empty(t, lExpFirst.AllLogs()) | ||
assert.Empty(t, lExpSecond.AllLogs()) | ||
assert.Len(t, lExpThird.AllLogs(), i, "log should only be routed to lExpThird exporter") | ||
}) | ||
} | ||
} | ||
|
||
func TestLogs_RoutingWorks_ResourceAttribute(t *testing.T) { | ||
defaultExp := &mockLogsExporter{} | ||
lExp := &mockLogsExporter{} | ||
|
@@ -182,6 +240,59 @@ func TestLogs_RoutingWorks_ResourceAttribute(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestLogs_RoutingWorks_ResourceAttribute_Ordered(t *testing.T) { | ||
defaultExp := &mockLogsExporter{} | ||
lExpFirst := &mockLogsExporter{} | ||
lExpSecond := &mockLogsExporter{} | ||
lExpThird := &mockLogsExporter{} | ||
|
||
host := newMockHost(map[component.DataType]map[component.ID]component.Component{ | ||
component.DataTypeLogs: { | ||
component.MustNewID("otlp"): defaultExp, | ||
component.MustNewIDWithName("otlp", "first"): lExpFirst, | ||
component.MustNewIDWithName("otlp", "second"): lExpSecond, | ||
component.MustNewIDWithName("otlp", "third"): lExpThird, | ||
}, | ||
}) | ||
|
||
exp, err := newLogProcessor(noopTelemetrySettings, &Config{ | ||
FromAttribute: "X-Tenant", | ||
AttributeSource: resourceAttributeSource, | ||
DefaultExporters: []string{"otlp"}, | ||
Table: []RoutingTableItem{ | ||
{ | ||
Value: "order-second", | ||
Exporters: []string{"otlp/second"}, | ||
}, | ||
{ | ||
Value: "order-first", | ||
Exporters: []string{"otlp/first"}, | ||
}, | ||
{ | ||
Value: "order-third", | ||
Exporters: []string{"otlp/third"}, | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
require.NoError(t, exp.Start(context.Background(), host)) | ||
|
||
for i := 1; i <= 5; i++ { | ||
t.Run(fmt.Sprintf("run %d time", i), func(t *testing.T) { | ||
l := plog.NewLogs() | ||
rl := l.ResourceLogs().AppendEmpty() | ||
rl.Resource().Attributes().PutStr("X-Tenant", "order-third") | ||
rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("this is a log") | ||
|
||
assert.NoError(t, exp.ConsumeLogs(context.Background(), l)) | ||
assert.Empty(t, defaultExp.AllLogs()) | ||
assert.Empty(t, lExpFirst.AllLogs()) | ||
assert.Empty(t, lExpSecond.AllLogs()) | ||
assert.Len(t, lExpThird.AllLogs(), i, "log should only be routed to lExpThird exporter") | ||
}) | ||
} | ||
} | ||
|
||
func TestLogs_RoutingWorks_ResourceAttribute_DropsRoutingAttribute(t *testing.T) { | ||
defaultExp := &mockLogsExporter{} | ||
lExp := &mockLogsExporter{} | ||
|
@@ -400,6 +511,59 @@ func TestLogsAreCorrectlySplitPerResourceAttributeWithOTTL(t *testing.T) { | |
}) | ||
} | ||
|
||
func TestLogs_RoutingWorks_ResourceAttribute_WithOTTL_Ordered(t *testing.T) { | ||
defaultExp := &mockLogsExporter{} | ||
lExpFirst := &mockLogsExporter{} | ||
lExpSecond := &mockLogsExporter{} | ||
|
||
host := newMockHost(map[component.DataType]map[component.ID]component.Component{ | ||
component.DataTypeLogs: { | ||
component.MustNewID("otlp"): defaultExp, | ||
component.MustNewIDWithName("otlp", "first"): lExpFirst, | ||
component.MustNewIDWithName("otlp", "second"): lExpSecond, | ||
}, | ||
}) | ||
|
||
exp, err := newLogProcessor(noopTelemetrySettings, &Config{ | ||
FromAttribute: "__otel_enabled__", | ||
AttributeSource: resourceAttributeSource, | ||
DefaultExporters: []string{"otlp"}, | ||
Table: []RoutingTableItem{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I find this test a bit confusing: the first two conditions will never be true for different reasons and the third is only resulting in true if the deletion happens based on the value of the key used in the where clause. The statements could be just simple route statements?
Or even better:
And in the main test code, have this attribute instead:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jpkrohling The test is designed to run 5 times, and I expected it to produce the same result each time. This is how expected statements work. If the statements are executed randomly ( BWT, the statements is from the issue's description. It can demonstrate that statements are executed in order or randomly will affect the routing results. so I used it in the test. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Running them 5 times is fine, my concern is about the readability of the test. While the statements on the issue description were enough to reproduce the problem, the tests should be easily understandable to our future selves without having to come back to this issue to get the full picture, IMO. |
||
{ | ||
Statement: `route() where resource.attributes["__otel_enabled__"] == nil`, | ||
Exporters: []string{"otlp/first"}, | ||
}, | ||
{ | ||
Statement: `delete_key(resource.attributes, "__otel_enabled__") where resource.attributes["__otel_enabled__"] == "true"`, | ||
Exporters: []string{"otlp/first"}, | ||
}, | ||
{ | ||
Statement: `delete_key(resource.attributes, "__otel_enabled__") where resource.attributes["__otel_enabled__"] == "false"`, | ||
Exporters: []string{"otlp/second"}, | ||
}, | ||
}, | ||
}) | ||
require.NoError(t, err) | ||
require.NoError(t, exp.Start(context.Background(), host)) | ||
|
||
for i := 1; i <= 5; i++ { | ||
t.Run(fmt.Sprintf("run %d time", i), func(t *testing.T) { | ||
l := plog.NewLogs() | ||
rl := l.ResourceLogs().AppendEmpty() | ||
rl.Resource().Attributes().PutStr("__otel_enabled__", "false") | ||
rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("this is a log") | ||
|
||
assert.NoError(t, exp.ConsumeLogs(context.Background(), l)) | ||
|
||
assert.Empty(t, defaultExp.AllLogs()) | ||
assert.Empty(t, lExpFirst.AllLogs()) | ||
|
||
// we expect Statement eval in order and log should only be routed to lExpSecond exporter | ||
assert.Len(t, lExpSecond.AllLogs(), i, "log should only be routed to lExpSecond exporter") | ||
}) | ||
} | ||
} | ||
|
||
// see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/26462 | ||
func TestLogsAttributeWithOTTLDoesNotCauseCrash(t *testing.T) { | ||
// prepare | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,7 @@ type router[E component.Component, K any] struct { | |
table []RoutingTableItem | ||
|
||
defaultExporters []E | ||
routes map[string]routingItem[E, K] | ||
routes []routingItem[E, K] | ||
} | ||
|
||
// newRouter creates a new router instance with its type parameter constrained | ||
|
@@ -44,12 +44,11 @@ func newRouter[E component.Component, K any]( | |
|
||
table: table, | ||
defaultExporterIDs: defaultExporterIDs, | ||
|
||
routes: make(map[string]routingItem[E, K]), | ||
} | ||
} | ||
|
||
type routingItem[E component.Component, K any] struct { | ||
key string | ||
exporters []E | ||
statement *ottl.Statement[K] | ||
} | ||
|
@@ -90,17 +89,14 @@ func (r *router[E, K]) registerDefaultExporters(available map[component.ID]compo | |
// registerRouteExporters registers route exporters using the provided | ||
// available exporters map to check if they were available. | ||
func (r *router[E, K]) registerRouteExporters(available map[component.ID]component.Component) error { | ||
var routes []routingItem[E, K] | ||
for _, item := range r.table { | ||
statement, err := r.getStatementFrom(item) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
route, ok := r.routes[key(item)] | ||
if !ok { | ||
route.statement = statement | ||
} | ||
|
||
var exporters []E | ||
for _, name := range item.Exporters { | ||
e, err := r.extractExporter(name, available) | ||
if errors.Is(err, errExporterNotFound) { | ||
|
@@ -109,10 +105,16 @@ func (r *router[E, K]) registerRouteExporters(available map[component.ID]compone | |
if err != nil { | ||
return err | ||
} | ||
route.exporters = append(route.exporters, e) | ||
exporters = append(exporters, e) | ||
} | ||
r.routes[key(item)] = route | ||
routes = append(routes, routingItem[E, K]{ | ||
key: key(item), | ||
exporters: exporters, | ||
statement: statement, | ||
}) | ||
} | ||
r.routes = routes | ||
|
||
return nil | ||
} | ||
|
||
|
@@ -171,9 +173,11 @@ func (r *router[E, K]) extractExporter(name string, available map[component.ID]c | |
} | ||
|
||
func (r *router[E, K]) getExporters(key string) []E { | ||
e, ok := r.routes[key] | ||
if !ok { | ||
return r.defaultExporters | ||
for _, route := range r.routes { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder what are the performance implications of this change. This code is in the hot path. If you are using this change in production already, are you able to provide some insights here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't tested it in production yet, but according to the changed code, it does not break the loop behavior. Whether There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not concerned about the behavior, I'm concerned about the performance. I just double-checked, and this doesn't seem like it's in the hot path, called only on the Start path. In any case, it would make me more comfortable if we had a benchmark comparing how it was before and how it is now. |
||
if route.key == key { | ||
return route.exporters | ||
} | ||
} | ||
return e.exporters | ||
|
||
return r.defaultExporters | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
was this change intended?