From 5b3b0ca2bc4150b202c11049a8f7d3a42eee841a Mon Sep 17 00:00:00 2001 From: Tyler Yahn Date: Wed, 7 Aug 2024 12:33:21 -0700 Subject: [PATCH] Update the ContextFilterProcessor example --- sdk/log/example_test.go | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/sdk/log/example_test.go b/sdk/log/example_test.go index 7b697db3ea7e..bb190e0935e6 100644 --- a/sdk/log/example_test.go +++ b/sdk/log/example_test.go @@ -7,6 +7,7 @@ import ( "context" "fmt" "strings" + "sync" logapi "go.opentelemetry.io/otel/log" "go.opentelemetry.io/otel/log/global" @@ -58,7 +59,7 @@ func ExampleProcessor_filtering() { // Wrap the processor so that it ignores processing log records // when a context deriving from WithIgnoreLogs is passed // to the logging methods. - processor = &ContextFilterProcessor{processor} + processor = &ContextFilterProcessor{Processor: processor} // The created processor can then be registered with // the OpenTelemetry Logs SDK using the WithProcessor option. @@ -81,6 +82,9 @@ func WithIgnoreLogs(ctx context.Context) context.Context { // [WithIgnoreLogs] is passed to its methods. type ContextFilterProcessor struct { log.Processor + + lazyFilter sync.Once + filter log.FilterProcessor } func (p *ContextFilterProcessor) OnEmit(ctx context.Context, record *log.Record) error { @@ -91,7 +95,12 @@ func (p *ContextFilterProcessor) OnEmit(ctx context.Context, record *log.Record) } func (p *ContextFilterProcessor) Enabled(ctx context.Context, record log.Record) bool { - return !ignoreLogs(ctx) && p.Processor.Enabled(ctx, record) + p.lazyFilter.Do(func() { + if f, ok := p.Processor.(log.FilterProcessor); ok { + p.filter = f + } + }) + return !ignoreLogs(ctx) && p.filter != nil && p.filter.Enabled(ctx, record) } func ignoreLogs(ctx context.Context) bool {