Skip to content

Commit 12e421a

Browse files
authored
sdk/log: move Enabled method from FilterProcessor to Processor (#7639)
Fixes #7617 Simplify the Logs SDK by unifying processor capabilities into a single interface. - Add `Enabled(ctx context.Context, p EnabledParameters) bool` to `sdk/log.Processor`. - Remove `sdk/log.FilterProcessor` interface. `Processor` implementations must now implement the `Enabled` method. Custom processors that do not filter records can implement `Enabled` to return `true`.
1 parent 5982f16 commit 12e421a

File tree

12 files changed

+108
-121
lines changed

12 files changed

+108
-121
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
2424
- Add experimental observability metrics for manual reader in `go.opentelemetry.io/otel/sdk/metric`. (#7524)
2525
- Add experimental observability metrics for periodic reader in `go.opentelemetry.io/otel/sdk/metric`. (#7571)
2626
- Support `OTEL_EXPORTER_OTLP_LOGS_INSECURE` and `OTEL_EXPORTER_OTLP_INSECURE` environmental variables in `go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp`. (#7608)
27+
- Add `Enabled` method to the `Processor` interface in `go.opentelemetry.io/otel/sdk/log`.
28+
All `Processor` implementations now include an `Enabled` method. (#7639)
2729
- The `go.opentelemetry.io/otel/semconv/v1.38.0` package.
2830
The package contains semantic conventions from the `v1.38.0` version of the OpenTelemetry Semantic Conventions.
2931
See the [migration documentation](./semconv/v1.38.0/MIGRATION.md) for information on how to upgrade from `go.opentelemetry.io/otel/semconv/v1.37.0.`(#7648)
@@ -46,6 +48,10 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
4648
### Removed
4749

4850
- Drop support for [Go 1.23]. (#7274)
51+
- Remove the `FilterProcessor` interface in `go.opentelemetry.io/otel/sdk/log`.
52+
The `Enabled` method has been added to the `Processor` interface instead.
53+
All `Processor` implementations must now implement the `Enabled` method.
54+
Custom processors that do not filter records can implement `Enabled` to return `true`. (#7639)
4955

5056
### Changed
5157

sdk/log/batch.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,11 @@ func (b *BatchProcessor) poll(interval time.Duration) (done chan struct{}) {
184184
return done
185185
}
186186

187+
// Enabled returns true, indicating this Processor will process all records.
188+
func (*BatchProcessor) Enabled(context.Context, EnabledParameters) bool {
189+
return true
190+
}
191+
187192
// OnEmit batches provided log record.
188193
func (b *BatchProcessor) OnEmit(_ context.Context, r *Record) error {
189194
if b.stopped.Load() || b.q == nil {

sdk/log/batch_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,13 @@ func TestBatchProcessor(t *testing.T) {
220220
_ = b.Shutdown(ctx)
221221
})
222222

223+
t.Run("Enabled", func(t *testing.T) {
224+
e := newTestExporter(nil)
225+
b := NewBatchProcessor(e)
226+
enabled := b.Enabled(ctx, EnabledParameters{})
227+
assert.True(t, enabled, "Enabled should return true")
228+
})
229+
223230
t.Run("OnEmit", func(t *testing.T) {
224231
const batch = 10
225232
e := newTestExporter(nil)

sdk/log/bench_test.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,15 @@ func BenchmarkProcessor(b *testing.B) {
131131

132132
type timestampProcessor struct{}
133133

134+
func (timestampProcessor) Enabled(context.Context, EnabledParameters) bool {
135+
return true
136+
}
137+
134138
func (timestampProcessor) OnEmit(_ context.Context, r *Record) error {
135139
r.SetObservedTimestamp(time.Date(1988, time.November, 17, 0, 0, 0, 0, time.UTC))
136140
return nil
137141
}
138142

139-
func (timestampProcessor) Enabled(context.Context, Record) bool {
140-
return true
141-
}
142-
143143
func (timestampProcessor) Shutdown(context.Context) error {
144144
return nil
145145
}
@@ -150,15 +150,15 @@ func (timestampProcessor) ForceFlush(context.Context) error {
150150

151151
type attrAddProcessor struct{}
152152

153+
func (attrAddProcessor) Enabled(context.Context, EnabledParameters) bool {
154+
return true
155+
}
156+
153157
func (attrAddProcessor) OnEmit(_ context.Context, r *Record) error {
154158
r.AddAttributes(log.String("add", "me"))
155159
return nil
156160
}
157161

158-
func (attrAddProcessor) Enabled(context.Context, Record) bool {
159-
return true
160-
}
161-
162162
func (attrAddProcessor) Shutdown(context.Context) error {
163163
return nil
164164
}
@@ -169,15 +169,15 @@ func (attrAddProcessor) ForceFlush(context.Context) error {
169169

170170
type attrSetDecorator struct{}
171171

172+
func (attrSetDecorator) Enabled(context.Context, EnabledParameters) bool {
173+
return true
174+
}
175+
172176
func (attrSetDecorator) OnEmit(_ context.Context, r *Record) error {
173177
r.SetAttributes(log.String("replace", "me"))
174178
return nil
175179
}
176180

177-
func (attrSetDecorator) Enabled(context.Context, Record) bool {
178-
return true
179-
}
180-
181181
func (attrSetDecorator) Shutdown(context.Context) error {
182182
return nil
183183
}

sdk/log/example_test.go

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"context"
88
"fmt"
99
"strings"
10-
"sync"
1110

1211
logapi "go.opentelemetry.io/otel/log"
1312
"go.opentelemetry.io/otel/log/global"
@@ -52,7 +51,7 @@ func Example() {
5251
}
5352

5453
// Use a processor that filters out records based on the provided context.
55-
func ExampleFilterProcessor() {
54+
func ExampleProcessor_contextFilter() {
5655
// Existing processor that emits telemetry.
5756
var processor log.Processor = log.NewBatchProcessor(nil)
5857

@@ -82,15 +81,8 @@ func WithIgnoreLogs(ctx context.Context) context.Context {
8281
// [WithIgnoreLogs] is passed to its methods.
8382
type ContextFilterProcessor struct {
8483
log.Processor
85-
86-
lazyFilter sync.Once
87-
// Support the FilterProcessor interface for the embedded processor.
88-
filter log.FilterProcessor
8984
}
9085

91-
// Compile time check.
92-
var _ log.FilterProcessor = (*ContextFilterProcessor)(nil)
93-
9486
func (p *ContextFilterProcessor) OnEmit(ctx context.Context, record *log.Record) error {
9587
if ignoreLogs(ctx) {
9688
return nil
@@ -99,12 +91,7 @@ func (p *ContextFilterProcessor) OnEmit(ctx context.Context, record *log.Record)
9991
}
10092

10193
func (p *ContextFilterProcessor) Enabled(ctx context.Context, param log.EnabledParameters) bool {
102-
p.lazyFilter.Do(func() {
103-
if f, ok := p.Processor.(log.FilterProcessor); ok {
104-
p.filter = f
105-
}
106-
})
107-
return !ignoreLogs(ctx) && (p.filter == nil || p.filter.Enabled(ctx, param))
94+
return !ignoreLogs(ctx) && p.Processor.Enabled(ctx, param)
10895
}
10996

11097
func ignoreLogs(ctx context.Context) bool {
@@ -138,6 +125,11 @@ func ExampleProcessor_eventName() {
138125
// but do support attributes.
139126
type EventNameProcessor struct{}
140127

128+
// Enabled returns true, indicating this Processor will process all records.
129+
func (*EventNameProcessor) Enabled(context.Context, log.EnabledParameters) bool {
130+
return true
131+
}
132+
141133
// OnEmit sets the EventName on log records having an "otel.event.name" string attribute.
142134
// The original attribute is not removed.
143135
func (*EventNameProcessor) OnEmit(_ context.Context, record *log.Record) error {
@@ -181,6 +173,11 @@ func ExampleProcessor_redact() {
181173
// from attributes containing "token" in the key.
182174
type RedactTokensProcessor struct{}
183175

176+
// Enabled returns true, indicating this Processor will process all records.
177+
func (*RedactTokensProcessor) Enabled(context.Context, log.EnabledParameters) bool {
178+
return true
179+
}
180+
184181
// OnEmit redacts values from attributes containing "token" in the key
185182
// by replacing them with a REDACTED value.
186183
func (*RedactTokensProcessor) OnEmit(_ context.Context, record *log.Record) error {

sdk/log/filter_processor.go

Lines changed: 0 additions & 61 deletions
This file was deleted.

sdk/log/logger.go

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ func (l *logger) Emit(ctx context.Context, r log.Record) {
5454
}
5555

5656
// Enabled returns true if at least one Processor held by the LoggerProvider
57-
// that created the logger will process param for the provided context and param.
57+
// that created the logger will process for the provided context and param.
5858
//
59-
// If it is not possible to definitively determine the param will be
59+
// If it is not possible to definitively determine the record will be
6060
// processed, true will be returned by default. A value of false will only be
6161
// returned if it can be positively verified that no Processor will process.
6262
func (l *logger) Enabled(ctx context.Context, param log.EnabledParameters) bool {
@@ -66,23 +66,13 @@ func (l *logger) Enabled(ctx context.Context, param log.EnabledParameters) bool
6666
EventName: param.EventName,
6767
}
6868

69-
// If there are more Processors than FilterProcessors,
70-
// which means not all Processors are FilterProcessors,
71-
// we cannot be sure that all Processors will drop the record.
72-
// Therefore, return true.
73-
//
74-
// If all Processors are FilterProcessors, check if any is enabled.
75-
return len(l.provider.processors) > len(l.provider.fltrProcessors) || anyEnabled(ctx, p, l.provider.fltrProcessors)
76-
}
77-
78-
func anyEnabled(ctx context.Context, param EnabledParameters, fltrs []FilterProcessor) bool {
79-
for _, f := range fltrs {
80-
if f.Enabled(ctx, param) {
69+
for _, processor := range l.provider.processors {
70+
if processor.Enabled(ctx, p) {
8171
// At least one Processor will process the Record.
8272
return true
8373
}
8474
}
85-
// No Processor will process the record
75+
// No Processor will process the record.
8676
return false
8777
}
8878

sdk/log/processor.go

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,46 @@ package log // import "go.opentelemetry.io/otel/sdk/log"
55

66
import (
77
"context"
8+
9+
"go.opentelemetry.io/otel/log"
10+
"go.opentelemetry.io/otel/sdk/instrumentation"
811
)
912

1013
// Processor handles the processing of log records.
1114
//
1215
// Any of the Processor's methods may be called concurrently with itself
1316
// or with other methods. It is the responsibility of the Processor to manage
1417
// this concurrency.
15-
//
16-
// See [FilterProcessor] for information about how a Processor can support filtering.
1718
type Processor interface {
19+
// Enabled reports whether the Processor will process for the given context
20+
// and param.
21+
//
22+
// The passed param is likely to be partial record information being
23+
// provided (e.g. a param with only the Severity set).
24+
// If a Processor needs more information than is provided, it
25+
// is said to be in an indeterminate state (see below).
26+
//
27+
// The returned value will be true when the Processor will process for the
28+
// provided context and param, and will be false if the Processor will not
29+
// process. The returned value may be true or false in an indeterminate state.
30+
// An implementation should default to returning true for an indeterminate
31+
// state, but may return false if valid reasons in particular circumstances
32+
// exist (e.g. performance, correctness).
33+
//
34+
// The param should not be held by the implementation. A copy should be
35+
// made if the param needs to be held after the call returns.
36+
//
37+
// Processor implementations are expected to re-evaluate the [Record] passed
38+
// to OnEmit. It is not expected that the caller to OnEmit will
39+
// use the result from Enabled prior to calling OnEmit.
40+
//
41+
// The SDK's Logger.Enabled returns false if all the registered processors
42+
// return false. Otherwise, it returns true.
43+
//
44+
// Implementations of this method need to be safe for a user to call
45+
// concurrently.
46+
Enabled(ctx context.Context, param EnabledParameters) bool
47+
1848
// OnEmit is called when a Record is emitted.
1949
//
2050
// OnEmit will be called independent of Enabled. Implementations need to
@@ -32,7 +62,8 @@ type Processor interface {
3262
// they were registered using WithProcessor.
3363
// Implementations may synchronously modify the record so that the changes
3464
// are visible in the next registered processor.
35-
// Notice that Record is not concurrent safe. Therefore, asynchronous
65+
//
66+
// Note that Record is not concurrent safe. Therefore, asynchronous
3667
// processing may cause race conditions. Use Record.Clone
3768
// to create a copy that shares no state with the original.
3869
OnEmit(ctx context.Context, record *Record) error
@@ -54,3 +85,10 @@ type Processor interface {
5485
// appropriate error should be returned in these situations.
5586
ForceFlush(ctx context.Context) error
5687
}
88+
89+
// EnabledParameters represents payload for [Processor]'s Enabled method.
90+
type EnabledParameters struct {
91+
InstrumentationScope instrumentation.Scope
92+
Severity log.Severity
93+
EventName string
94+
}

sdk/log/provider.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,11 @@ const (
2727
)
2828

2929
type providerConfig struct {
30-
resource *resource.Resource
31-
processors []Processor
32-
fltrProcessors []FilterProcessor
33-
attrCntLim setting[int]
34-
attrValLenLim setting[int]
35-
allowDupKeys setting[bool]
30+
resource *resource.Resource
31+
processors []Processor
32+
attrCntLim setting[int]
33+
attrValLenLim setting[int]
34+
allowDupKeys setting[bool]
3635
}
3736

3837
func newProviderConfig(opts []LoggerProviderOption) providerConfig {
@@ -65,7 +64,6 @@ type LoggerProvider struct {
6564

6665
resource *resource.Resource
6766
processors []Processor
68-
fltrProcessors []FilterProcessor
6967
attributeCountLimit int
7068
attributeValueLengthLimit int
7169
allowDupKeys bool
@@ -92,7 +90,6 @@ func NewLoggerProvider(opts ...LoggerProviderOption) *LoggerProvider {
9290
return &LoggerProvider{
9391
resource: cfg.resource,
9492
processors: cfg.processors,
95-
fltrProcessors: cfg.fltrProcessors,
9693
attributeCountLimit: cfg.attrCntLim.Value,
9794
attributeValueLengthLimit: cfg.attrValLenLim.Value,
9895
allowDupKeys: cfg.allowDupKeys.Value,
@@ -208,14 +205,9 @@ func WithResource(res *resource.Resource) LoggerProviderOption {
208205
//
209206
// For production, use [NewBatchProcessor] to batch log records before they are exported.
210207
// For testing and debugging, use [NewSimpleProcessor] to synchronously export log records.
211-
//
212-
// See [FilterProcessor] for information about how a Processor can support filtering.
213208
func WithProcessor(processor Processor) LoggerProviderOption {
214209
return loggerProviderOptionFunc(func(cfg providerConfig) providerConfig {
215210
cfg.processors = append(cfg.processors, processor)
216-
if f, ok := processor.(FilterProcessor); ok {
217-
cfg.fltrProcessors = append(cfg.fltrProcessors, f)
218-
}
219211
return cfg
220212
})
221213
}

0 commit comments

Comments
 (0)