Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
Signed-off-by: Murphy Chen <[email protected]>
  • Loading branch information
Frapschen committed Oct 8, 2024
1 parent eb12308 commit f3fd9d9
Showing 1 changed file with 59 additions and 10 deletions.
69 changes: 59 additions & 10 deletions processor/routingprocessor/logs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,8 @@ func TestLogs_RoutingWorks_Context_Ordered(t *testing.T) {
lExpSecond := &mockLogsExporter{}
lExpThird := &mockLogsExporter{}

host := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeLogs: {
host := newMockHost(map[pipeline.Signal]map[component.ID]component.Component{
pipeline.SignalLogs: {
component.MustNewID("otlp"): defaultExp,
component.MustNewIDWithName("otlp", "first"): lExpFirst,
component.MustNewIDWithName("otlp", "second"): lExpSecond,
Expand Down Expand Up @@ -247,8 +247,8 @@ func TestLogs_RoutingWorks_ResourceAttribute_Ordered(t *testing.T) {
lExpSecond := &mockLogsExporter{}
lExpThird := &mockLogsExporter{}

host := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeLogs: {
host := newMockHost(map[pipeline.Signal]map[component.ID]component.Component{
pipeline.SignalLogs: {
component.MustNewID("otlp"): defaultExp,
component.MustNewIDWithName("otlp", "first"): lExpFirst,
component.MustNewIDWithName("otlp", "second"): lExpSecond,
Expand Down Expand Up @@ -517,8 +517,8 @@ func TestLogs_RoutingWorks_ResourceAttribute_WithOTTL_Ordered(t *testing.T) {
lExpFirst := &mockLogsExporter{}
lExpSecond := &mockLogsExporter{}

host := newMockHost(map[component.DataType]map[component.ID]component.Component{
component.DataTypeLogs: {
host := newMockHost(map[pipeline.Signal]map[component.ID]component.Component{
pipeline.SignalLogs: {
component.MustNewID("otlp"): defaultExp,
component.MustNewIDWithName("otlp", "first"): lExpFirst,
component.MustNewIDWithName("otlp", "second"): lExpSecond,
Expand All @@ -531,15 +531,15 @@ func TestLogs_RoutingWorks_ResourceAttribute_WithOTTL_Ordered(t *testing.T) {
DefaultExporters: []string{"otlp"},
Table: []RoutingTableItem{
{
Statement: `route() where resource.attributes["__otel_enabled__"] == nil`,
Statement: `route() where resource.attributes["non-matching"] != nil`,
Exporters: []string{"otlp/first"},
},
{
Statement: `delete_key(resource.attributes, "__otel_enabled__") where resource.attributes["__otel_enabled__"] == "true"`,
Statement: `route() where resource.attributes["non-matching"] == "true"`,
Exporters: []string{"otlp/first"},
},
{
Statement: `delete_key(resource.attributes, "__otel_enabled__") where resource.attributes["__otel_enabled__"] == "false"`,
Statement: `route() where resource.attributes["matching"] == "true"`,
Exporters: []string{"otlp/second"},
},
},
Expand All @@ -551,7 +551,7 @@ func TestLogs_RoutingWorks_ResourceAttribute_WithOTTL_Ordered(t *testing.T) {
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.Resource().Attributes().PutStr("matching", "true")
rl.ScopeLogs().AppendEmpty().LogRecords().AppendEmpty().Body().SetStr("this is a log")

assert.NoError(t, exp.ConsumeLogs(context.Background(), l))
Expand All @@ -565,6 +565,55 @@ func TestLogs_RoutingWorks_ResourceAttribute_WithOTTL_Ordered(t *testing.T) {
}
}

func BenchmarkLogs_Routing(t *testing.B) {
defaultExp := &mockLogsExporter{}
lExpFirst := &mockLogsExporter{}
lExpSecond := &mockLogsExporter{}

host := newMockHost(map[pipeline.Signal]map[component.ID]component.Component{
pipeline.SignalLogs: {
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{
{
Statement: `route() where resource.attributes["non-matching"] != nil`,
Exporters: []string{"otlp/first"},
},
{
Statement: `route() where resource.attributes["non-matching"] == "true"`,
Exporters: []string{"otlp/first"},
},
{
Statement: `route() where resource.attributes["matching"] == "true"`,
Exporters: []string{"otlp/second"},
},
},
})
require.NoError(t, err)
require.NoError(t, exp.Start(context.Background(), host))

mockLogs := plog.NewLogs()
for i := 0; i < 100; i++ {
rl := mockLogs.ResourceLogs().AppendEmpty()
rl.Resource().Attributes().PutStr("matching", "true")
}

for i := 0; i < t.N; i++ {
l := plog.NewLogs()
mockLogs.CopyTo(l)

assert.NoError(t, exp.ConsumeLogs(context.Background(), l))
}
}

// see https://github.com/open-telemetry/opentelemetry-collector-contrib/issues/26462
func TestLogsAttributeWithOTTLDoesNotCauseCrash(t *testing.T) {
// prepare
Expand Down

0 comments on commit f3fd9d9

Please sign in to comment.