Skip to content

Commit f079b03

Browse files
authored
sdk/log: SimpleProcessor to not panic for zero value (#5665)
1 parent f7977e0 commit f079b03

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
1616
This module is unstable and breaking changes may be introduced.
1717
See our [versioning policy](VERSIONING.md) for more information about these stability guarantees. (#5629)
1818
- Add `InstrumentationScope` field to `SpanStub` in `go.opentelemetry.io/otel/sdk/trace/tracetest`, as a replacement for the deprecated `InstrumentationLibrary`. (#5627)
19+
- Zero value of `SimpleProcessor` in `go.opentelemetry.io/otel/sdk/log` no longer panics. (#5665)
1920

2021
### Changed
2122

2223
- `Processor.OnEmit` in `go.opentelemetry.io/otel/sdk/log` now accepts a pointer to `Record` instead of a value so that the record modifications done in a processor are propagated to subsequent registered processors. (#5636)
24+
- `SimpleProcessor.Enabled` in `go.opentelemetry.io/otel/sdk/log` now returns `false` if the exporter is `nil`. (#5665)
2325

2426
### Fixed
2527

sdk/log/simple.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import (
1212
var _ Processor = (*SimpleProcessor)(nil)
1313

1414
// SimpleProcessor is an processor that synchronously exports log records.
15+
//
16+
// Use [NewSimpleProcessor] to create a SimpleProcessor.
1517
type SimpleProcessor struct {
1618
exporter Exporter
1719
}
@@ -25,10 +27,6 @@ type SimpleProcessor struct {
2527
// [NewBatchProcessor] instead. However, there may be exceptions where certain
2628
// [Exporter] implementations perform better with this Processor.
2729
func NewSimpleProcessor(exporter Exporter, _ ...SimpleProcessorOption) *SimpleProcessor {
28-
if exporter == nil {
29-
// Do not panic on nil exporter.
30-
exporter = defaultNoopExporter
31-
}
3230
return &SimpleProcessor{exporter: exporter}
3331
}
3432

@@ -41,6 +39,10 @@ var simpleProcRecordsPool = sync.Pool{
4139

4240
// OnEmit batches provided log record.
4341
func (s *SimpleProcessor) OnEmit(ctx context.Context, r *Record) error {
42+
if s.exporter == nil {
43+
return nil
44+
}
45+
4446
records := simpleProcRecordsPool.Get().(*[]Record)
4547
(*records)[0] = *r
4648
defer func() {
@@ -50,18 +52,26 @@ func (s *SimpleProcessor) OnEmit(ctx context.Context, r *Record) error {
5052
return s.exporter.Export(ctx, *records)
5153
}
5254

53-
// Enabled returns true.
55+
// Enabled returns true if the exporter is not nil.
5456
func (s *SimpleProcessor) Enabled(context.Context, Record) bool {
55-
return true
57+
return s.exporter != nil
5658
}
5759

5860
// Shutdown shuts down the expoter.
5961
func (s *SimpleProcessor) Shutdown(ctx context.Context) error {
62+
if s.exporter == nil {
63+
return nil
64+
}
65+
6066
return s.exporter.Shutdown(ctx)
6167
}
6268

6369
// ForceFlush flushes the exporter.
6470
func (s *SimpleProcessor) ForceFlush(ctx context.Context) error {
71+
if s.exporter == nil {
72+
return nil
73+
}
74+
6575
return s.exporter.ForceFlush(ctx)
6676
}
6777

sdk/log/simple_test.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,8 @@ func TestSimpleProcessorOnEmit(t *testing.T) {
5151
}
5252

5353
func TestSimpleProcessorEnabled(t *testing.T) {
54-
s := log.NewSimpleProcessor(nil)
54+
e := new(exporter)
55+
s := log.NewSimpleProcessor(e)
5556
assert.True(t, s.Enabled(context.Background(), log.Record{}))
5657
}
5758

@@ -69,6 +70,18 @@ func TestSimpleProcessorForceFlush(t *testing.T) {
6970
require.True(t, e.forceFlushCalled, "exporter ForceFlush not called")
7071
}
7172

73+
func TestSimpleProcessorEmpty(t *testing.T) {
74+
assert.NotPanics(t, func() {
75+
var s log.SimpleProcessor
76+
ctx := context.Background()
77+
record := new(log.Record)
78+
assert.NoError(t, s.OnEmit(ctx, record), "OnEmit")
79+
assert.False(t, s.Enabled(ctx, *record), "Enabled")
80+
assert.NoError(t, s.ForceFlush(ctx), "ForceFlush")
81+
assert.NoError(t, s.Shutdown(ctx), "Shutdown")
82+
})
83+
}
84+
7285
func TestSimpleProcessorConcurrentSafe(t *testing.T) {
7386
const goRoutineN = 10
7487

0 commit comments

Comments
 (0)