mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-01-07 23:02:15 +02:00
23f7b41e0a
Fixes https://github.com/open-telemetry/opentelemetry-go/issues/5769 Related spec PR: https://github.com/open-telemetry/opentelemetry-specification/pull/4203 Remark: A follow-up in contrib is required and afterwards here to fix the `example/dice`. Benchstat results for `sdk/log` (`log` has no benchmarks related to Enabled): ``` goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/sdk/log cpu: Intel(R) Core(TM) i9-10885H CPU @ 2.40GHz │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ BatchProcessorOnEmit-16 398.8n ± 10% 395.6n ± 12% ~ (p=0.971 n=10) Processor/Simple-16 882.2n ± 8% 869.8n ± 9% ~ (p=0.811 n=10) Processor/Batch-16 1.478µ ± 3% 1.485µ ± 5% ~ (p=0.646 n=10) Processor/SetTimestampSimple-16 847.8n ± 1% 844.6n ± 3% ~ (p=0.247 n=10) Processor/SetTimestampBatch-16 1.480µ ± 3% 1.473µ ± 4% ~ (p=0.700 n=10) Processor/AddAttributesSimple-16 930.0n ± 1% 933.8n ± 1% ~ (p=0.172 n=10) Processor/AddAttributesBatch-16 1.624µ ± 2% 1.639µ ± 2% ~ (p=0.839 n=10) Processor/SetAttributesSimple-16 903.4n ± 1% 895.1n ± 1% ~ (p=0.190 n=10) Processor/SetAttributesBatch-16 1.554µ ± 4% 1.529µ ± 3% ~ (p=0.159 n=10) LoggerNewRecord/5_attributes-16 346.0n ± 2% 343.3n ± 2% ~ (p=0.448 n=10) LoggerNewRecord/10_attributes-16 1.608µ ± 6% 1.503µ ± 2% -6.53% (p=0.007 n=10) LoggerEnabled-16 34.305n ± 8% 6.706n ± 1% -80.45% (p=0.000 n=10) LoggerProviderLogger-16 636.9n ± 10% 605.8n ± 3% ~ (p=0.105 n=10) WalkAttributes/1_attributes-16 5.363n ± 3% 4.540n ± 14% -15.34% (p=0.002 n=10) WalkAttributes/10_attributes-16 5.436n ± 7% 4.461n ± 2% -17.95% (p=0.000 n=10) WalkAttributes/100_attributes-16 5.126n ± 9% 4.465n ± 1% -12.90% (p=0.000 n=10) WalkAttributes/1000_attributes-16 5.316n ± 9% 4.502n ± 5% -15.32% (p=0.002 n=10) SetAddAttributes/SetAttributes-16 220.5n ± 18% 192.6n ± 11% -12.67% (p=0.007 n=10) SetAddAttributes/AddAttributes-16 165.3n ± 21% 127.3n ± 22% -22.96% (p=0.011 n=10) SimpleProcessorOnEmit-16 2.159n ± 9% 2.167n ± 9% ~ (p=0.739 n=10) geomean 178.3n 154.5n -13.31% │ old.txt │ new.txt │ │ B/s │ B/s vs base │ BatchProcessorOnEmit-16 76.52Mi ± 11% 77.14Mi ± 14% ~ (p=0.971 n=10) │ old.txt │ new.txt │ │ B/op │ B/op vs base │ BatchProcessorOnEmit-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/Simple-16 417.0 ± 0% 417.0 ± 0% ~ (p=1.000 n=10) ¹ Processor/Batch-16 1.093Ki ± 1% 1.088Ki ± 1% ~ (p=0.254 n=10) Processor/SetTimestampSimple-16 417.0 ± 0% 417.0 ± 0% ~ (p=1.000 n=10) ¹ Processor/SetTimestampBatch-16 1.095Ki ± 1% 1.084Ki ± 2% ~ (p=0.361 n=10) Processor/AddAttributesSimple-16 417.0 ± 0% 417.0 ± 0% ~ (p=1.000 n=10) ¹ Processor/AddAttributesBatch-16 1.085Ki ± 1% 1.086Ki ± 1% ~ (p=1.000 n=10) Processor/SetAttributesSimple-16 465.0 ± 0% 465.0 ± 0% ~ (p=1.000 n=10) ¹ Processor/SetAttributesBatch-16 1.129Ki ± 1% 1.125Ki ± 1% ~ (p=0.084 n=10) LoggerNewRecord/5_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ LoggerNewRecord/10_attributes-16 610.0 ± 0% 610.0 ± 0% ~ (p=1.000 n=10) ¹ LoggerEnabled-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ LoggerProviderLogger-16 359.0 ± 6% 346.0 ± 3% ~ (p=0.117 n=10) WalkAttributes/1_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ WalkAttributes/10_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ WalkAttributes/100_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ WalkAttributes/1000_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SetAddAttributes/SetAttributes-16 48.00 ± 0% 48.00 ± 0% ~ (p=1.000 n=10) ¹ SetAddAttributes/AddAttributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SimpleProcessorOnEmit-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ geomean ² -0.27% ² ¹ all samples are equal ² summaries must be >0 to compute geomean │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ BatchProcessorOnEmit-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/Simple-16 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/Batch-16 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/SetTimestampSimple-16 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/SetTimestampBatch-16 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/AddAttributesSimple-16 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/AddAttributesBatch-16 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/SetAttributesSimple-16 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ Processor/SetAttributesBatch-16 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ LoggerNewRecord/5_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ LoggerNewRecord/10_attributes-16 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ LoggerEnabled-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ LoggerProviderLogger-16 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ WalkAttributes/1_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ WalkAttributes/10_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ WalkAttributes/100_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ WalkAttributes/1000_attributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SetAddAttributes/SetAttributes-16 1.000 ± 0% 1.000 ± 0% ~ (p=1.000 n=10) ¹ SetAddAttributes/AddAttributes-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ SimpleProcessorOnEmit-16 0.000 ± 0% 0.000 ± 0% ~ (p=1.000 n=10) ¹ geomean ² +0.00% ² ¹ all samples are equal ² summaries must be >0 to compute geomean ```
159 lines
4.7 KiB
Go
159 lines
4.7 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package log_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
|
|
logapi "go.opentelemetry.io/otel/log"
|
|
"go.opentelemetry.io/otel/log/global"
|
|
"go.opentelemetry.io/otel/sdk/log"
|
|
)
|
|
|
|
// Initialize OpenTelemetry Logs SDK and setup logging using a log bridge.
|
|
func Example() {
|
|
// Create an exporter that will emit log records.
|
|
// E.g. use go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp
|
|
// to send logs using OTLP over HTTP:
|
|
// exporter, err := otlploghttp.New(ctx)
|
|
var exporter log.Exporter
|
|
|
|
// Create a log record processor pipeline.
|
|
processor := log.NewBatchProcessor(exporter)
|
|
|
|
// Create a logger provider.
|
|
// You can pass this instance directly when creating a log bridge.
|
|
provider := log.NewLoggerProvider(
|
|
log.WithProcessor(processor),
|
|
)
|
|
|
|
// Handle shutdown properly so that nothing leaks.
|
|
defer func() {
|
|
err := provider.Shutdown(context.Background())
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}()
|
|
|
|
// Register as global logger provider so that it can be used via global.Meter
|
|
// and accessed using global.GetMeterProvider.
|
|
// Most log bridges use the global logger provider as default.
|
|
// If the global logger provider is not set then a no-op implementation
|
|
// is used, which fails to generate data.
|
|
global.SetLoggerProvider(provider)
|
|
|
|
// Use a bridge so that you can emit logs using your Go logging library of preference.
|
|
// E.g. use go.opentelemetry.io/contrib/bridges/otelslog so that you can use log/slog:
|
|
// slog.SetDefault(otelslog.NewLogger("my/pkg/name", otelslog.WithLoggerProvider(provider)))
|
|
}
|
|
|
|
// Use a processor that filters out records based on the provided context.
|
|
func ExampleProcessor_filtering() {
|
|
// Existing processor that emits telemetry.
|
|
var processor log.Processor = log.NewBatchProcessor(nil)
|
|
|
|
// Wrap the processor so that it ignores processing log records
|
|
// when a context deriving from WithIgnoreLogs is passed
|
|
// to the logging methods.
|
|
processor = &ContextFilterProcessor{Processor: processor}
|
|
|
|
// The created processor can then be registered with
|
|
// the OpenTelemetry Logs SDK using the WithProcessor option.
|
|
_ = log.NewLoggerProvider(
|
|
log.WithProcessor(processor),
|
|
)
|
|
}
|
|
|
|
type key struct{}
|
|
|
|
var ignoreLogsKey key
|
|
|
|
// WithIgnoreLogs returns a context which is used by [ContextFilterProcessor]
|
|
// to filter out log records.
|
|
func WithIgnoreLogs(ctx context.Context) context.Context {
|
|
return context.WithValue(ctx, ignoreLogsKey, true)
|
|
}
|
|
|
|
// ContextFilterProcessor filters out logs when a context deriving from
|
|
// [WithIgnoreLogs] is passed to its methods.
|
|
type ContextFilterProcessor struct {
|
|
log.Processor
|
|
|
|
lazyFilter sync.Once
|
|
// Use the experimental FilterProcessor interface
|
|
// (go.opentelemetry.io/otel/sdk/log/internal/x).
|
|
filter filter
|
|
}
|
|
|
|
type filter interface {
|
|
Enabled(ctx context.Context, param logapi.EnabledParameters) bool
|
|
}
|
|
|
|
func (p *ContextFilterProcessor) OnEmit(ctx context.Context, record *log.Record) error {
|
|
if ignoreLogs(ctx) {
|
|
return nil
|
|
}
|
|
return p.Processor.OnEmit(ctx, record)
|
|
}
|
|
|
|
func (p *ContextFilterProcessor) Enabled(ctx context.Context, param logapi.EnabledParameters) bool {
|
|
p.lazyFilter.Do(func() {
|
|
if f, ok := p.Processor.(filter); ok {
|
|
p.filter = f
|
|
}
|
|
})
|
|
return !ignoreLogs(ctx) && (p.filter == nil || p.filter.Enabled(ctx, param))
|
|
}
|
|
|
|
func ignoreLogs(ctx context.Context) bool {
|
|
_, ok := ctx.Value(ignoreLogsKey).(bool)
|
|
return ok
|
|
}
|
|
|
|
// Use a processor which redacts sensitive data from some attributes.
|
|
func ExampleProcessor_redact() {
|
|
// Existing processor that emits telemetry.
|
|
var processor log.Processor = log.NewBatchProcessor(nil)
|
|
|
|
// Add a processor so that it redacts values from token attributes.
|
|
redactProcessor := &RedactTokensProcessor{}
|
|
|
|
// The created processor can then be registered with
|
|
// the OpenTelemetry Logs SDK using the WithProcessor option.
|
|
_ = log.NewLoggerProvider(
|
|
// Order is important here. Redact before handing to the processor.
|
|
log.WithProcessor(redactProcessor),
|
|
log.WithProcessor(processor),
|
|
)
|
|
}
|
|
|
|
// RedactTokensProcessor is a [log.Processor] decorator that redacts values
|
|
// from attributes containing "token" in the key.
|
|
type RedactTokensProcessor struct{}
|
|
|
|
// OnEmit redacts values from attributes containing "token" in the key
|
|
// by replacing them with a REDACTED value.
|
|
func (p *RedactTokensProcessor) OnEmit(ctx context.Context, record *log.Record) error {
|
|
record.WalkAttributes(func(kv logapi.KeyValue) bool {
|
|
if strings.Contains(strings.ToLower(kv.Key), "token") {
|
|
record.AddAttributes(logapi.String(kv.Key, "REDACTED"))
|
|
}
|
|
return true
|
|
})
|
|
return nil
|
|
}
|
|
|
|
// Shutdown returns nil.
|
|
func (p *RedactTokensProcessor) Shutdown(ctx context.Context) error {
|
|
return nil
|
|
}
|
|
|
|
// ForceFlush returns nil.
|
|
func (p *RedactTokensProcessor) ForceFlush(ctx context.Context) error {
|
|
return nil
|
|
}
|