| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package log // import "go.opentelemetry.io/otel/sdk/log" |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"sync" |
| 9 |
|
| 10 |
"go.opentelemetry.io/otel" |
| 11 |
"go.opentelemetry.io/otel/sdk/log/internal/observ" |
| 12 |
) |
| 13 |
|
| 14 |
// Compile-time check SimpleProcessor implements Processor. |
| 15 |
var _ Processor = (*SimpleProcessor)(nil) |
| 16 |
|
| 17 |
// SimpleProcessor is an processor that synchronously exports log records. |
| 18 |
// |
| 19 |
// Use [NewSimpleProcessor] to create a SimpleProcessor. |
| 20 |
type SimpleProcessor struct { |
| 21 |
mu sync.Mutex |
| 22 |
exporter Exporter |
| 23 |
inst *observ.SLP |
| 24 |
noCmp [0]func() //nolint: unused // This is indeed used. |
| 25 |
} |
| 26 |
|
| 27 |
// NewSimpleProcessor is a simple Processor adapter. |
| 28 |
// |
| 29 |
// This Processor is not recommended for production use due to its synchronous |
| 30 |
// nature, which makes it suitable for testing, debugging, or demonstrating |
| 31 |
// other features, but can lead to slow performance and high computational |
| 32 |
// overhead. For production environments, it is recommended to use |
| 33 |
// [NewBatchProcessor] instead. However, there may be exceptions where certain |
| 34 |
// [Exporter] implementations perform better with this Processor. |
| 35 |
func NewSimpleProcessor(exporter Exporter, _ ...SimpleProcessorOption) *SimpleProcessor { |
| 36 |
slp := &SimpleProcessor{ |
| 37 |
exporter: exporter, |
| 38 |
} |
| 39 |
var err error |
| 40 |
slp.inst, err = observ.NewSLP(observ.NextSimpleProcessorID()) |
| 41 |
if err != nil { |
| 42 |
otel.Handle(err) |
| 43 |
} |
| 44 |
return slp |
| 45 |
} |
| 46 |
|
| 47 |
var simpleProcRecordsPool = sync.Pool{ |
| 48 |
New: func() any { |
| 49 |
records := make([]Record, 1) |
| 50 |
return &records |
| 51 |
}, |
| 52 |
} |
| 53 |
|
| 54 |
// Enabled returns true, indicating this Processor will process all records. |
| 55 |
func (*SimpleProcessor) Enabled(context.Context, EnabledParameters) bool { |
| 56 |
return true |
| 57 |
} |
| 58 |
|
| 59 |
// OnEmit batches provided log record. |
| 60 |
func (s *SimpleProcessor) OnEmit(ctx context.Context, r *Record) (err error) { |
| 61 |
if s.exporter == nil { |
| 62 |
return nil |
| 63 |
} |
| 64 |
|
| 65 |
s.mu.Lock() |
| 66 |
defer s.mu.Unlock() |
| 67 |
|
| 68 |
records := simpleProcRecordsPool.Get().(*[]Record) |
| 69 |
(*records)[0] = *r |
| 70 |
defer func() { |
| 71 |
simpleProcRecordsPool.Put(records) |
| 72 |
}() |
| 73 |
|
| 74 |
if s.inst != nil { |
| 75 |
defer func() { |
| 76 |
s.inst.LogProcessed(ctx, err) |
| 77 |
}() |
| 78 |
} |
| 79 |
return s.exporter.Export(ctx, *records) |
| 80 |
} |
| 81 |
|
| 82 |
// Shutdown shuts down the exporter. |
| 83 |
func (s *SimpleProcessor) Shutdown(ctx context.Context) error { |
| 84 |
if s.exporter == nil { |
| 85 |
return nil |
| 86 |
} |
| 87 |
|
| 88 |
return s.exporter.Shutdown(ctx) |
| 89 |
} |
| 90 |
|
| 91 |
// ForceFlush flushes the exporter. |
| 92 |
func (s *SimpleProcessor) ForceFlush(ctx context.Context) error { |
| 93 |
if s.exporter == nil { |
| 94 |
return nil |
| 95 |
} |
| 96 |
|
| 97 |
return s.exporter.ForceFlush(ctx) |
| 98 |
} |
| 99 |
|
| 100 |
// SimpleProcessorOption applies a configuration to a [SimpleProcessor]. |
| 101 |
type SimpleProcessorOption interface { |
| 102 |
apply() |
| 103 |
} |