-
Notifications
You must be signed in to change notification settings - Fork 579
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement slog Handler Enabled (#5315)
* Implement slog Handler Enabled * Comment MinSeverity field * Run go mod tidy
- Loading branch information
Showing
4 changed files
with
74 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} |