Skip to content

Commit

Permalink
Implement slog Handler Enabled (#5315)
Browse files Browse the repository at this point in the history
* Implement slog Handler Enabled

* Comment MinSeverity field

* Run go mod tidy
  • Loading branch information
MrAlias authored Mar 20, 2024
1 parent aba0417 commit 10c1eea
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 9 deletions.
6 changes: 5 additions & 1 deletion bridges/otelslog/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@ module go.opentelemetry.io/contrib/bridges/otelslog
go 1.21

require (
go.opentelemetry.io/otel/log v0.0.1-alpha
github.com/stretchr/testify v1.9.0
go.opentelemetry.io/otel/log v0.0.1-alpha.0.20240319182811-335f4de960ff
go.opentelemetry.io/otel/sdk v1.24.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
10 changes: 6 additions & 4 deletions bridges/otelslog/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,19 @@ github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
go.opentelemetry.io/otel v1.24.0 h1:0LAOdjNmQeSTzGBzduGe/rU4tZhMwL5rWgtp9Ku5Jfo=
go.opentelemetry.io/otel v1.24.0/go.mod h1:W7b9Ozg4nkF5tWI5zsXkaKKDjdVjpD4oAt9Qi/MArHo=
go.opentelemetry.io/otel/log v0.0.1-alpha h1:Gy4SxFnkHv2wmmzv//sblb4/PoCYVtuZbdFY/XamvHM=
go.opentelemetry.io/otel/log v0.0.1-alpha/go.mod h1:fg1zxLfxAyzlCLyULJTWXUbFVYyOwQZD/DgtGm7VvgA=
go.opentelemetry.io/otel/log v0.0.1-alpha.0.20240319182811-335f4de960ff h1:WMikyBC7alFcyvvVj22Spm8ad72hjUJTS5BQ4YlBDXY=
go.opentelemetry.io/otel/log v0.0.1-alpha.0.20240319182811-335f4de960ff/go.mod h1:ToOZ06+agH/C+P2+bp6Ea/CLMDviyMVUNUQaKTB1ieg=
go.opentelemetry.io/otel/metric v1.24.0 h1:6EhoGWWK28x1fbpA4tYTOWBkPefTDQnb8WSGXlc88kI=
go.opentelemetry.io/otel/metric v1.24.0/go.mod h1:VYhLe1rFfxuTXLgj4CBiyz+9WYBA8pNGJgDcSFRKBco=
go.opentelemetry.io/otel/sdk v1.24.0 h1:YMPPDNymmQN3ZgczicBY3B6sf9n62Dlj9pWD3ucgoDw=
go.opentelemetry.io/otel/sdk v1.24.0/go.mod h1:KVrIYw6tEubO9E96HQpcmpTKDVn9gdv35HoYiQWGDFg=
go.opentelemetry.io/otel/trace v1.24.0 h1:CsKnnL4dUAr/0llH9FKuc698G04IrpWV0MQA/Y1YELI=
go.opentelemetry.io/otel/trace v1.24.0/go.mod h1:HPc3Xr/cOApsBI154IU0OI0HJexz+aw5uPdbs3UCjNU=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
10 changes: 6 additions & 4 deletions bridges/otelslog/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Handler struct {
// Ensure forward compatibility by explicitly making this not comparable.
noCmp [0]func() //nolint: unused // This is indeed used.

// TODO: implement.
logger log.Logger
}

// Compile-time check *Handler implements slog.Handler.
Expand Down Expand Up @@ -92,9 +92,11 @@ func (h *Handler) Handle(ctx context.Context, record slog.Record) error {

// Enable returns true if the Handler is enabled to log for the provided
// context and Level. Otherwise, false is returned if it is not enabled.
func (h *Handler) Enabled(context.Context, slog.Level) bool {
// TODO: implement.
return true
func (h *Handler) Enabled(ctx context.Context, l slog.Level) bool {
var record log.Record
const sevOffset = slog.Level(log.SeverityDebug) - slog.LevelDebug
record.SetSeverity(log.Severity(l + sevOffset))
return h.logger.Enabled(ctx, record)
}

// WithAttrs returns a new [slog.Handler] based on h that will log using the
Expand Down
57 changes: 57 additions & 0 deletions bridges/otelslog/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package otelslog

import (
"context"
"log/slog"
"testing"

"github.com/stretchr/testify/assert"

"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/embedded"
)

// embeddedLogger is a type alias so the embedded.Logger type doesn't conflict
// with the Logger method of the recorder when it is embedded.
type embeddedLogger = embedded.Logger // nolint:unused // Used below.

// recorder records all [log.Record]s it is ased to emit.
type recorder struct {
embedded.LoggerProvider
embeddedLogger // nolint:unused // Used to embed embedded.Logger.

// MinSeverity is the minimum severity the recorder will return true for
// when Enabled is called (unless enableKey is set).
MinSeverity log.Severity
}

func (r *recorder) Logger(string, ...log.LoggerOption) log.Logger { return r }

func (r *recorder) Emit(context.Context, log.Record) {
}

type enablerKey uint

var enableKey enablerKey

func (r *recorder) Enabled(ctx context.Context, record log.Record) bool {
return ctx.Value(enableKey) != nil || record.Severity() >= r.MinSeverity
}

func TestHandlerEnabled(t *testing.T) {
r := new(recorder)
r.MinSeverity = log.SeverityInfo

h := NewHandler(WithLoggerProvider(r))
h.logger = r.Logger("") // TODO: Remove when #5311 merged.

ctx := context.Background()
assert.False(t, h.Enabled(ctx, slog.LevelDebug), "level conversion: permissive")
assert.True(t, h.Enabled(ctx, slog.LevelInfo), "level conversion: restrictive")

ctx = context.WithValue(ctx, enableKey, true)
assert.True(t, h.Enabled(ctx, slog.LevelDebug), "context not passed")
}

0 comments on commit 10c1eea

Please sign in to comment.