2024-06-27 11:55:29 +02:00
|
|
|
// Copyright The OpenTelemetry Authors
|
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
|
|
package log_test
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2024-07-06 13:07:29 +02:00
|
|
|
"fmt"
|
2024-06-27 11:55:29 +02:00
|
|
|
"strings"
|
|
|
|
|
|
2024-07-06 13:07:29 +02:00
|
|
|
logapi "go.opentelemetry.io/otel/log"
|
|
|
|
|
"go.opentelemetry.io/otel/log/global"
|
|
|
|
|
"go.opentelemetry.io/otel/sdk/log"
|
2024-06-27 11:55:29 +02:00
|
|
|
)
|
|
|
|
|
|
2024-07-06 13:07:29 +02:00
|
|
|
// 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)))
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-01 15:49:12 +02:00
|
|
|
// Use a processor that filters out records based on the provided context.
|
|
|
|
|
func ExampleProcessor_filtering() {
|
|
|
|
|
// Existing processor that emits telemetry.
|
2024-07-06 13:07:29 +02:00
|
|
|
var processor log.Processor = log.NewBatchProcessor(nil)
|
2024-07-01 15:49:12 +02:00
|
|
|
|
|
|
|
|
// 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}
|
|
|
|
|
|
|
|
|
|
// The created processor can then be registered with
|
|
|
|
|
// the OpenTelemetry Logs SDK using the WithProcessor option.
|
2024-07-06 13:07:29 +02:00
|
|
|
_ = log.NewLoggerProvider(
|
|
|
|
|
log.WithProcessor(processor),
|
2024-07-01 15:49:12 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type key struct{}
|
|
|
|
|
|
|
|
|
|
var igoreLogsKey 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, igoreLogsKey, true)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ContextFilterProcessor filters out logs when a context deriving from
|
|
|
|
|
// [WithIgnoreLogs] is passed to its methods.
|
|
|
|
|
type ContextFilterProcessor struct {
|
2024-07-06 13:07:29 +02:00
|
|
|
log.Processor
|
2024-07-01 15:49:12 +02:00
|
|
|
}
|
|
|
|
|
|
2024-08-01 10:13:43 +02:00
|
|
|
func (p *ContextFilterProcessor) OnEmit(ctx context.Context, record *log.Record) error {
|
2024-07-01 15:49:12 +02:00
|
|
|
if ignoreLogs(ctx) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return p.Processor.OnEmit(ctx, record)
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 13:07:29 +02:00
|
|
|
func (p *ContextFilterProcessor) Enabled(ctx context.Context, record log.Record) bool {
|
2024-07-01 15:49:12 +02:00
|
|
|
return !ignoreLogs(ctx) && p.Processor.Enabled(ctx, record)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ignoreLogs(ctx context.Context) bool {
|
|
|
|
|
_, ok := ctx.Value(igoreLogsKey).(bool)
|
|
|
|
|
return ok
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-27 11:55:29 +02:00
|
|
|
// Use a processor which redacts sensitive data from some attributes.
|
|
|
|
|
func ExampleProcessor_redact() {
|
|
|
|
|
// Existing processor that emits telemetry.
|
2024-07-06 13:07:29 +02:00
|
|
|
var processor log.Processor = log.NewBatchProcessor(nil)
|
2024-06-27 11:55:29 +02:00
|
|
|
|
2024-08-01 10:13:43 +02:00
|
|
|
// Add a processor so that it redacts values from token attributes.
|
|
|
|
|
redactProcessor := &RedactTokensProcessor{}
|
2024-06-27 11:55:29 +02:00
|
|
|
|
|
|
|
|
// The created processor can then be registered with
|
|
|
|
|
// the OpenTelemetry Logs SDK using the WithProcessor option.
|
2024-07-06 13:07:29 +02:00
|
|
|
_ = log.NewLoggerProvider(
|
2024-08-01 10:13:43 +02:00
|
|
|
// Order is important here. Redact before handing to the processor.
|
|
|
|
|
log.WithProcessor(redactProcessor),
|
2024-07-06 13:07:29 +02:00
|
|
|
log.WithProcessor(processor),
|
2024-06-27 11:55:29 +02:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-06 13:07:29 +02:00
|
|
|
// RedactTokensProcessor is a [log.Processor] decorator that redacts values
|
2024-06-27 11:55:29 +02:00
|
|
|
// from attributes containing "token" in the key.
|
2024-08-01 10:13:43 +02:00
|
|
|
type RedactTokensProcessor struct{}
|
2024-06-27 11:55:29 +02:00
|
|
|
|
|
|
|
|
// OnEmit redacts values from attributes containing "token" in the key
|
|
|
|
|
// by replacing them with a REDACTED value.
|
2024-08-01 10:13:43 +02:00
|
|
|
func (p *RedactTokensProcessor) OnEmit(ctx context.Context, record *log.Record) error {
|
2024-07-06 13:07:29 +02:00
|
|
|
record.WalkAttributes(func(kv logapi.KeyValue) bool {
|
2024-06-27 11:55:29 +02:00
|
|
|
if strings.Contains(strings.ToLower(kv.Key), "token") {
|
2024-07-06 13:07:29 +02:00
|
|
|
record.AddAttributes(logapi.String(kv.Key, "REDACTED"))
|
2024-06-27 11:55:29 +02:00
|
|
|
}
|
|
|
|
|
return true
|
|
|
|
|
})
|
2024-08-01 10:13:43 +02:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Enabled returns true.
|
|
|
|
|
func (p *RedactTokensProcessor) Enabled(context.Context, log.Record) bool {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 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
|
2024-06-27 11:55:29 +02:00
|
|
|
}
|