|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +// Package otelslog provides [Handler], an [slog.Handler] implementation, that |
| 5 | +// can be used to bridge between the [log/slog] API and [OpenTelemetry]. |
| 6 | +// |
| 7 | +// [OpenTelemetry]: https://opentelemetry.io/docs/concepts/signals/logs/ |
| 8 | +package otelslog // import "go.opentelemetry.io/contrib/bridges/otelslog" |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "log/slog" |
| 13 | + |
| 14 | + "go.opentelemetry.io/otel/log" |
| 15 | + "go.opentelemetry.io/otel/sdk/instrumentation" |
| 16 | +) |
| 17 | + |
| 18 | +// NewLogger returns a new [slog.Logger] backed by a new [Handler]. See |
| 19 | +// [NewHandler] for details on how the backing Handler is created. |
| 20 | +func NewLogger(options ...Option) *slog.Logger { |
| 21 | + return slog.New(NewHandler(options...)) |
| 22 | +} |
| 23 | + |
| 24 | +type config struct{} |
| 25 | + |
| 26 | +// Option configures a [Handler]. |
| 27 | +type Option interface { |
| 28 | + apply(config) config |
| 29 | +} |
| 30 | + |
| 31 | +type optFunc func(config) config |
| 32 | + |
| 33 | +func (f optFunc) apply(c config) config { return f(c) } |
| 34 | + |
| 35 | +// WithInstrumentationScope returns an [Option] that configures the scope of |
| 36 | +// the [log.Logger] used by a [Handler]. |
| 37 | +// |
| 38 | +// By default if this Option is not provided, the Handler will use a default |
| 39 | +// instrumentation scope describing this bridge package. It is recommended to |
| 40 | +// provide this so log data can be associated with its source package or |
| 41 | +// module. |
| 42 | +func WithInstrumentationScope(scope instrumentation.Scope) Option { |
| 43 | + return optFunc(func(c config) config { |
| 44 | + // TODO: implement. |
| 45 | + return c |
| 46 | + }) |
| 47 | +} |
| 48 | + |
| 49 | +// WithLoggerProvider returns an [Option] that configures [log.LoggerProvider] |
| 50 | +// used by a [Handler] to create its [log.Logger]. |
| 51 | +// |
| 52 | +// By default if this Option is not provided, the Handler will use the global |
| 53 | +// LoggerProvider. |
| 54 | +func WithLoggerProvider(provider log.LoggerProvider) Option { |
| 55 | + return optFunc(func(c config) config { |
| 56 | + // TODO: implement. |
| 57 | + return c |
| 58 | + }) |
| 59 | +} |
| 60 | + |
| 61 | +// Handler is an [slog.Handler] that sends all logging records it receives to |
| 62 | +// OpenTelemetry. |
| 63 | +type Handler struct { |
| 64 | + // Ensure forward compatibility by explicitly making this not comparable. |
| 65 | + noCmp [0]func() //nolint: unused // This is indeed used. |
| 66 | + |
| 67 | + // TODO: implement. |
| 68 | +} |
| 69 | + |
| 70 | +// Compile-time check *Handler implements slog.Handler. |
| 71 | +var _ slog.Handler = (*Handler)(nil) |
| 72 | + |
| 73 | +// NewHandler returns a new [Handler] to be used as an [slog.Handler]. |
| 74 | +// |
| 75 | +// If [WithLoggerProvider] is not provided, the returned Handler will use the |
| 76 | +// global LoggerProvider. |
| 77 | +// |
| 78 | +// By default the returned Handler will use a [log.Logger] that is identified |
| 79 | +// with this bridge package information. [WithInstrumentationScope] should be |
| 80 | +// used to override this with details about the package or module the handler |
| 81 | +// will instrument. |
| 82 | +func NewHandler(options ...Option) *Handler { |
| 83 | + // TODO: implement. |
| 84 | + return &Handler{} |
| 85 | +} |
| 86 | + |
| 87 | +// Handle handles the passed record. |
| 88 | +func (h *Handler) Handle(ctx context.Context, record slog.Record) error { |
| 89 | + // TODO: implement. |
| 90 | + return nil |
| 91 | +} |
| 92 | + |
| 93 | +// Enable returns true if the Handler is enabled to log for the provided |
| 94 | +// context and Level. Otherwise, false is returned if it is not enabled. |
| 95 | +func (h *Handler) Enabled(context.Context, slog.Level) bool { |
| 96 | + // TODO: implement. |
| 97 | + return true |
| 98 | +} |
| 99 | + |
| 100 | +// WithAttrs returns a new [slog.Handler] based on h that will log using the |
| 101 | +// passed attrs. |
| 102 | +func (h *Handler) WithAttrs(attrs []slog.Attr) slog.Handler { |
| 103 | + // TODO: implement. |
| 104 | + return h |
| 105 | +} |
| 106 | + |
| 107 | +// WithGroup returns a new [slog.Handler] based on h that will log all messages |
| 108 | +// and attributes within a group of the provided name. |
| 109 | +func (h *Handler) WithGroup(name string) slog.Handler { |
| 110 | + // TODO: implement. |
| 111 | + return h |
| 112 | +} |
0 commit comments