diff --git a/CHANGELOG.md b/CHANGELOG.md index f28a537f062..6e8f2a1c5d5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm - The `IsEmpty` method is added to the `Instrument` type in `go.opentelemetry.io/otel/sdk/metric`. This method is used to check if an `Instrument` instance is a zero-value. (#5431) - Store and provide the emitted `context.Context` in `ScopeRecords` of `go.opentelemetry.io/otel/sdk/log/logtest`. (#5468) +- `SimpleProcessor.OnEmit` in `go.opentelemetry.io/otel/sdk/log` no longer allocates a slice which makes it possible to have a zero-allocation log processing using `SimpleProcessor`. (#5493) ### Changed diff --git a/sdk/log/simple.go b/sdk/log/simple.go index c7aa14b8706..fc5690b22d5 100644 --- a/sdk/log/simple.go +++ b/sdk/log/simple.go @@ -5,6 +5,7 @@ package log // import "go.opentelemetry.io/otel/sdk/log" import ( "context" + "sync" ) // Compile-time check SimpleProcessor implements Processor. @@ -30,9 +31,22 @@ func NewSimpleProcessor(exporter Exporter, _ ...SimpleProcessorOption) *SimplePr return &SimpleProcessor{exporter: exporter} } +var simpleProcRecordsPool = sync.Pool{ + New: func() any { + records := make([]Record, 1) + return &records + }, +} + // OnEmit batches provided log record. func (s *SimpleProcessor) OnEmit(ctx context.Context, r Record) error { - return s.exporter.Export(ctx, []Record{r}) + records := simpleProcRecordsPool.Get().(*[]Record) + (*records)[0] = r + defer func() { + simpleProcRecordsPool.Put(records) + }() + + return s.exporter.Export(ctx, *records) } // Enabled returns true.