mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-28 21:09:17 +02:00
002c0a4c03
Closes #5425 Our current log `Processor` interface contains more functionality than the [OTel spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/logs/sdk.md#logrecordprocessor-operations). The additional functionality allows processors to report back to the API if a Record should be constructed and emitted or not, which is quite helpful[^1][^2][^3][^4][^5]. This removes the `Enabled` method from the `Processor` type. It adds this functionality a new optional and experimental `FilterProcessor` interface type. The logger and provider are updated to check for this optional interface to be implemented with the configured processors and uses them to back the `Logger.Enabled` method, preserving existing functionality. By making this change: - The `Processor` interface is now compliant with the OTel spec and does not contain any additional unspecified behavior. - All `Processor` implementations are no longer required to implement an `Enabled` method. The default, when they do not implement this method, is to assume they are enabled. ### Benchmark ```terminal goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/sdk/log cpu: Intel(R) Core(TM) i7-8550U CPU @ 1.80GHz │ old.txt │ new7.txt │ │ sec/op │ sec/op vs base │ LoggerEnabled-8 133.30n ± 3% 32.36n ± 3% -75.72% (p=0.000 n=10) │ old.txt │ new7.txt │ │ B/op │ B/op vs base │ LoggerEnabled-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal │ old.txt │ new7.txt │ │ allocs/op │ allocs/op vs base │ LoggerEnabled-8 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ ¹ all samples are equal ``` This is a significant performance improvement due to the `Record` no longer being converted from the API version to the SDK version. [^1]: https://pkg.go.dev/go.opentelemetry.io/contrib/processors/minsev [^2]: https://pkg.go.dev/go.opentelemetry.io/otel/sdk/log#BatchProcessor.Enabled [^3]: https://pkg.go.dev/go.opentelemetry.io/otel/sdk/log#SimpleProcessor.Enabled [^4]:af75717ac4/bridges/otelslog/handler.go (L206-L211)
[^5]:d0309ddd8c/bridges/otelzap/core.go (L142-L146)
--------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Sam Xie <sam@samxie.me>
141 lines
2.8 KiB
Go
141 lines
2.8 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package log_test
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"go.opentelemetry.io/otel/sdk/log"
|
|
)
|
|
|
|
type exporter struct {
|
|
records []log.Record
|
|
|
|
exportCalled bool
|
|
shutdownCalled bool
|
|
forceFlushCalled bool
|
|
}
|
|
|
|
func (e *exporter) Export(_ context.Context, r []log.Record) error {
|
|
e.records = r
|
|
e.exportCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (e *exporter) Shutdown(context.Context) error {
|
|
e.shutdownCalled = true
|
|
return nil
|
|
}
|
|
|
|
func (e *exporter) ForceFlush(context.Context) error {
|
|
e.forceFlushCalled = true
|
|
return nil
|
|
}
|
|
|
|
func TestSimpleProcessorOnEmit(t *testing.T) {
|
|
e := new(exporter)
|
|
s := log.NewSimpleProcessor(e)
|
|
|
|
r := new(log.Record)
|
|
r.SetSeverityText("test")
|
|
_ = s.OnEmit(context.Background(), r)
|
|
|
|
require.True(t, e.exportCalled, "exporter Export not called")
|
|
assert.Equal(t, []log.Record{*r}, e.records)
|
|
}
|
|
|
|
func TestSimpleProcessorShutdown(t *testing.T) {
|
|
e := new(exporter)
|
|
s := log.NewSimpleProcessor(e)
|
|
_ = s.Shutdown(context.Background())
|
|
require.True(t, e.shutdownCalled, "exporter Shutdown not called")
|
|
}
|
|
|
|
func TestSimpleProcessorForceFlush(t *testing.T) {
|
|
e := new(exporter)
|
|
s := log.NewSimpleProcessor(e)
|
|
_ = s.ForceFlush(context.Background())
|
|
require.True(t, e.forceFlushCalled, "exporter ForceFlush not called")
|
|
}
|
|
|
|
type writerExporter struct {
|
|
io.Writer
|
|
}
|
|
|
|
func (e *writerExporter) Export(_ context.Context, records []log.Record) error {
|
|
for _, r := range records {
|
|
_, _ = io.WriteString(e.Writer, r.Body().String())
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (e *writerExporter) Shutdown(context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
func (e *writerExporter) ForceFlush(context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
func TestSimpleProcessorEmpty(t *testing.T) {
|
|
assert.NotPanics(t, func() {
|
|
var s log.SimpleProcessor
|
|
ctx := context.Background()
|
|
record := new(log.Record)
|
|
assert.NoError(t, s.OnEmit(ctx, record), "OnEmit")
|
|
assert.NoError(t, s.ForceFlush(ctx), "ForceFlush")
|
|
assert.NoError(t, s.Shutdown(ctx), "Shutdown")
|
|
})
|
|
}
|
|
|
|
func TestSimpleProcessorConcurrentSafe(t *testing.T) {
|
|
const goRoutineN = 10
|
|
|
|
var wg sync.WaitGroup
|
|
wg.Add(goRoutineN)
|
|
|
|
r := new(log.Record)
|
|
r.SetSeverityText("test")
|
|
ctx := context.Background()
|
|
e := &writerExporter{new(strings.Builder)}
|
|
s := log.NewSimpleProcessor(e)
|
|
for i := 0; i < goRoutineN; i++ {
|
|
go func() {
|
|
defer wg.Done()
|
|
|
|
_ = s.OnEmit(ctx, r)
|
|
_ = s.Shutdown(ctx)
|
|
_ = s.ForceFlush(ctx)
|
|
}()
|
|
}
|
|
|
|
wg.Wait()
|
|
}
|
|
|
|
func BenchmarkSimpleProcessorOnEmit(b *testing.B) {
|
|
r := new(log.Record)
|
|
r.SetSeverityText("test")
|
|
ctx := context.Background()
|
|
s := log.NewSimpleProcessor(nil)
|
|
|
|
b.ReportAllocs()
|
|
b.ResetTimer()
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
var out error
|
|
|
|
for pb.Next() {
|
|
out = s.OnEmit(ctx, r)
|
|
}
|
|
|
|
_ = out
|
|
})
|
|
}
|