Skip to content

Commit b82d79a

Browse files
authored
Add skeleton of slog bridge (#5292)
* Add slog bridge skeleton * Apply feedback Add LoggerProvider as an option. Rename New to NewHandler. Add the New function to return an *slog.Logger. * Rename New to NewLogger
1 parent 6137d41 commit b82d79a

5 files changed

Lines changed: 163 additions & 0 deletions

File tree

.github/dependabot.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ updates:
100100
schedule:
101101
interval: weekly
102102
day: sunday
103+
- package-ecosystem: gomod
104+
directory: /bridges/otelslog
105+
labels:
106+
- dependencies
107+
- go
108+
- Skip Changelog
109+
schedule:
110+
interval: weekly
111+
day: sunday
103112
- package-ecosystem: gomod
104113
directory: /bridges/prometheus
105114
labels:

bridges/otelslog/go.mod

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
module go.opentelemetry.io/contrib/bridges/otelslog
2+
3+
go 1.21
4+
5+
require (
6+
go.opentelemetry.io/otel/log v0.0.1-alpha
7+
go.opentelemetry.io/otel/sdk v1.24.0
8+
)
9+
10+
require (
11+
github.com/go-logr/logr v1.4.1 // indirect
12+
github.com/go-logr/stdr v1.2.2 // indirect
13+
go.opentelemetry.io/otel v1.24.0 // indirect
14+
go.opentelemetry.io/otel/metric v1.24.0 // indirect
15+
go.opentelemetry.io/otel/trace v1.24.0 // indirect
16+
)

bridges/otelslog/go.sum

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
2+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
3+
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
4+
github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ=
5+
github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
6+
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
7+
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
8+
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
9+
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
10+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
11+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
12+
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
13+
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
14+
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
15+
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
16+
go.opentelemetry.io/otel/log v0.0.1-alpha h1:Gy4SxFnkHv2wmmzv//sblb4/PoCYVtuZbdFY/XamvHM=
17+
go.opentelemetry.io/otel/log v0.0.1-alpha/go.mod h1:fg1zxLfxAyzlCLyULJTWXUbFVYyOwQZD/DgtGm7VvgA=
18+
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
19+
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
20+
go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw=
21+
go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg=
22+
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
23+
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
24+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
25+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

bridges/otelslog/handler.go

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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+
}

versions.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ module-sets:
7575
modules:
7676
- go.opentelemetry.io/contrib/config
7777
excluded-modules:
78+
- go.opentelemetry.io/contrib/bridges/slog
7879
- go.opentelemetry.io/contrib/instrgen
7980
- go.opentelemetry.io/contrib/instrgen/driver
8081
- go.opentelemetry.io/contrib/instrgen/testdata/interface

0 commit comments

Comments
 (0)