Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

otellogr: Fix nil context panic #6527

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

### Fixed

- Use `context.Background()` as default context instead of nil in `go.opentelemetry.io/contrib/bridges/otellogr`. (#6527)
- Fix error logged by Jaeger remote sampler on empty or unset `OTEL_TRACES_SAMPLER_ARG` environment variable (#6511)
- Relax minimum Go version to 1.22.0 in various modules. (#6595)

Expand Down
3 changes: 2 additions & 1 deletion bridges/otellogr/logsink.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ func NewLogSink(name string, options ...Option) *LogSink {
logger: c.provider.Logger(name, opts...),
levelSeverity: c.levelSeverity,
opts: opts,
ctx: context.Background(),
flc1125 marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down Expand Up @@ -240,7 +241,7 @@ func (l *LogSink) Info(level int, msg string, keysAndValues ...any) {

// Init receives optional information about the logr library this
// implementation does not use it.
func (l *LogSink) Init(info logr.RuntimeInfo) {
func (l *LogSink) Init(logr.RuntimeInfo) {
// We don't need to do anything here.
// CallDepth is used to calculate the caller's PC.
// PC is dropped as part of the conversion to the OpenTelemetry log.Record.
Expand Down
34 changes: 34 additions & 0 deletions bridges/otellogr/logsink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,40 @@ func TestLogSink(t *testing.T) {
}
}

// fix https://github.com/open-telemetry/opentelemetry-go-contrib/issues/6509
func TestLogSinkCtxInInfo(t *testing.T) {
rec := logtest.NewRecorder()
ls := NewLogSink("name", WithLoggerProvider(rec))
l := logr.New(ls)
ctx := context.WithValue(context.Background(), "key", "value") // nolint:revive,staticcheck

tests := []struct {
name string
keyValues []any
wantCtxFunc func(ctx context.Context)
}{
{"default", nil, func(ctx context.Context) {
assert.Equal(t, context.Background(), ctx)
}},
{"default with context", []any{"ctx", ctx}, func(ctx context.Context) {
assert.Equal(t, "value", ctx.Value("key"))
}},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
defer rec.Reset()

l.Info("msg", tt.keyValues...)
require.Len(t, rec.Result(), 1)

got := rec.Result()[0]
require.Len(t, got.Records, 1)
tt.wantCtxFunc(got.Records[0].Context())
})
}
}

func TestLogSinkEnabled(t *testing.T) {
enabledFunc := func(ctx context.Context, param log.EnabledParameters) bool {
return param.Severity == log.SeverityInfo
Expand Down
Loading