2024-06-03 07:14:52 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.opentelemetry.io/otel/log"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkProcessor(b *testing.B) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
2024-08-01 10:13:43 +02:00
|
|
|
f func() []LoggerProviderOption
|
2024-06-03 07:14:52 +02:00
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "Simple",
|
2024-08-01 10:13:43 +02:00
|
|
|
f: func() []LoggerProviderOption {
|
|
|
|
return []LoggerProviderOption{WithProcessor(NewSimpleProcessor(noopExporter{}))}
|
2024-06-03 07:14:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "Batch",
|
2024-08-01 10:13:43 +02:00
|
|
|
f: func() []LoggerProviderOption {
|
|
|
|
return []LoggerProviderOption{WithProcessor(NewBatchProcessor(noopExporter{}))}
|
2024-06-03 07:14:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-07-11 19:51:25 +02:00
|
|
|
name: "SetTimestampSimple",
|
2024-08-01 10:13:43 +02:00
|
|
|
f: func() []LoggerProviderOption {
|
|
|
|
return []LoggerProviderOption{
|
|
|
|
WithProcessor(timestampProcessor{}),
|
|
|
|
WithProcessor(NewSimpleProcessor(noopExporter{})),
|
|
|
|
}
|
2024-06-03 07:14:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-07-11 19:51:25 +02:00
|
|
|
name: "SetTimestampBatch",
|
2024-08-01 10:13:43 +02:00
|
|
|
f: func() []LoggerProviderOption {
|
|
|
|
return []LoggerProviderOption{
|
|
|
|
WithProcessor(timestampProcessor{}),
|
|
|
|
WithProcessor(NewBatchProcessor(noopExporter{})),
|
|
|
|
}
|
2024-06-03 07:14:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-07-11 19:51:25 +02:00
|
|
|
name: "AddAttributesSimple",
|
2024-08-01 10:13:43 +02:00
|
|
|
f: func() []LoggerProviderOption {
|
|
|
|
return []LoggerProviderOption{
|
|
|
|
WithProcessor(attrAddProcessor{}),
|
|
|
|
WithProcessor(NewSimpleProcessor(noopExporter{})),
|
|
|
|
}
|
2024-06-03 07:14:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
2024-07-11 19:51:25 +02:00
|
|
|
name: "AddAttributesBatch",
|
2024-08-01 10:13:43 +02:00
|
|
|
f: func() []LoggerProviderOption {
|
|
|
|
return []LoggerProviderOption{
|
|
|
|
WithProcessor(attrAddProcessor{}),
|
|
|
|
WithProcessor(NewBatchProcessor(noopExporter{})),
|
|
|
|
}
|
2024-07-11 19:51:25 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SetAttributesSimple",
|
2024-08-01 10:13:43 +02:00
|
|
|
f: func() []LoggerProviderOption {
|
|
|
|
return []LoggerProviderOption{
|
|
|
|
WithProcessor(attrSetDecorator{}),
|
|
|
|
WithProcessor(NewSimpleProcessor(noopExporter{})),
|
|
|
|
}
|
2024-07-11 19:51:25 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "SetAttributesBatch",
|
2024-08-01 10:13:43 +02:00
|
|
|
f: func() []LoggerProviderOption {
|
|
|
|
return []LoggerProviderOption{
|
|
|
|
WithProcessor(attrSetDecorator{}),
|
|
|
|
WithProcessor(NewBatchProcessor(noopExporter{})),
|
|
|
|
}
|
2024-06-03 07:14:52 +02:00
|
|
|
},
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
b.Run(tc.name, func(b *testing.B) {
|
2024-08-01 10:13:43 +02:00
|
|
|
provider := NewLoggerProvider(tc.f()...)
|
2024-06-03 07:14:52 +02:00
|
|
|
b.Cleanup(func() { assert.NoError(b, provider.Shutdown(context.Background())) })
|
|
|
|
logger := provider.Logger(b.Name())
|
|
|
|
|
|
|
|
b.ReportAllocs()
|
|
|
|
b.RunParallel(func(pb *testing.PB) {
|
|
|
|
for pb.Next() {
|
|
|
|
r := log.Record{}
|
|
|
|
r.SetBody(log.StringValue("message"))
|
|
|
|
r.SetSeverity(log.SeverityInfo)
|
|
|
|
r.AddAttributes(
|
|
|
|
log.String("foo", "bar"),
|
|
|
|
log.Float64("float", 3.14),
|
|
|
|
log.Int("int", 123),
|
|
|
|
log.Bool("bool", true),
|
|
|
|
)
|
|
|
|
logger.Emit(context.Background(), r)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:13:43 +02:00
|
|
|
type timestampProcessor struct{}
|
2024-06-03 07:14:52 +02:00
|
|
|
|
2024-08-01 10:13:43 +02:00
|
|
|
func (p timestampProcessor) OnEmit(ctx context.Context, r *Record) error {
|
2024-06-03 07:14:52 +02:00
|
|
|
r.SetObservedTimestamp(time.Date(1988, time.November, 17, 0, 0, 0, 0, time.UTC))
|
2024-08-01 10:13:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p timestampProcessor) Enabled(context.Context, Record) bool {
|
|
|
|
return true
|
2024-06-03 07:14:52 +02:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:13:43 +02:00
|
|
|
func (p timestampProcessor) Shutdown(ctx context.Context) error {
|
|
|
|
return nil
|
2024-07-11 19:51:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:13:43 +02:00
|
|
|
func (p timestampProcessor) ForceFlush(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type attrAddProcessor struct{}
|
|
|
|
|
|
|
|
func (p attrAddProcessor) OnEmit(ctx context.Context, r *Record) error {
|
2024-07-11 19:51:25 +02:00
|
|
|
r.AddAttributes(log.String("add", "me"))
|
2024-08-01 10:13:43 +02:00
|
|
|
return nil
|
2024-07-11 19:51:25 +02:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:13:43 +02:00
|
|
|
func (p attrAddProcessor) Enabled(context.Context, Record) bool {
|
|
|
|
return true
|
2024-06-03 07:14:52 +02:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:13:43 +02:00
|
|
|
func (p attrAddProcessor) Shutdown(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p attrAddProcessor) ForceFlush(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type attrSetDecorator struct{}
|
|
|
|
|
|
|
|
func (p attrSetDecorator) OnEmit(ctx context.Context, r *Record) error {
|
2024-06-03 07:14:52 +02:00
|
|
|
r.SetAttributes(log.String("replace", "me"))
|
2024-08-01 10:13:43 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p attrSetDecorator) Enabled(context.Context, Record) bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p attrSetDecorator) Shutdown(ctx context.Context) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p attrSetDecorator) ForceFlush(ctx context.Context) error {
|
|
|
|
return nil
|
2024-06-03 07:14:52 +02:00
|
|
|
}
|