-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update
minsev
to allow dynamic severities (#6116)
Similar to [`slog`](https://pkg.go.dev/log/slog#hdr-Levels), allow the `minsev.LogProcessor` to be configured with dynamic severity resolution. --------- Co-authored-by: Robert Pająk <[email protected]>
- Loading branch information
Showing
6 changed files
with
304 additions
and
18 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright The OpenTelemetry Authors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package minsev // import "go.opentelemetry.io/contrib/processors/minsev" | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
"strings" | ||
"sync" | ||
|
||
"go.opentelemetry.io/otel/log" | ||
) | ||
|
||
const key = "OTEL_LOG_LEVEL" | ||
|
||
var getSeverity = sync.OnceValue(func() log.Severity { | ||
conv := map[string]log.Severity{ | ||
"": log.SeverityInfo, // Default to SeverityInfo for unset. | ||
"debug": log.SeverityDebug, | ||
"info": log.SeverityInfo, | ||
"warn": log.SeverityWarn, | ||
"error": log.SeverityError, | ||
} | ||
// log.SeverityUndefined for unknown values. | ||
return conv[strings.ToLower(os.Getenv(key))] | ||
}) | ||
|
||
type EnvSeverity struct{} | ||
|
||
func (EnvSeverity) Severity() log.Severity { return getSeverity() } | ||
|
||
func ExampleSeveritier() { | ||
// Mock an environment variable setup that would be done externally. | ||
_ = os.Setenv(key, "error") | ||
|
||
p := NewLogProcessor(&processor{}, EnvSeverity{}) | ||
|
||
ctx, params := context.Background(), log.EnabledParameters{} | ||
params.SetSeverity(log.SeverityDebug) | ||
fmt.Println(p.Enabled(ctx, params)) | ||
|
||
params.SetSeverity(log.SeverityError) | ||
fmt.Println(p.Enabled(ctx, params)) | ||
|
||
// Output: | ||
// false | ||
// true | ||
} |
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
Oops, something went wrong.