Skip to content

pkg/logger: add SugaredLogger.WithCtx & CtxKeyVals#1942

Merged
jmank88 merged 1 commit intomainfrom
logger-ctx
Mar 30, 2026
Merged

pkg/logger: add SugaredLogger.WithCtx & CtxKeyVals#1942
jmank88 merged 1 commit intomainfrom
logger-ctx

Conversation

@jmank88
Copy link
Copy Markdown
Contributor

@jmank88 jmank88 commented Mar 30, 2026

This PR introduces SuagaredLogger.WithCtx (and the supporting func CtxKeyVals) for extracting otel and custom values from a context for logging.

Comment thread pkg/logger/logger.go
Comment on lines +320 to +324
kvs = append(kvs, "trace_id", spanCtx.TraceID().String())
kvs = append(kvs, "trace_flags", spanCtx.TraceFlags().String())
}
if spanCtx.HasSpanID() {
kvs = append(kvs, "span_id", spanCtx.SpanID().String())
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This logic is already present in our otel zap adapter. If we put slog in front of everything (and use the context variants), then we could unify the duplicate logic there instead. In the mean time, any redundancy shouldn't hurt.

@github-actions
Copy link
Copy Markdown

⚠️ API Diff Results - github.com/smartcontractkit/chainlink-common

⚠️ Breaking Changes (4)

pkg/capabilities.RequestMetadata (1)
  • ExecutionTimestamp — 🗑️ Removed
pkg/capabilities/pb.(*RequestMetadata) (1)
  • GetExecutionTimestamp — 🗑️ Removed
pkg/capabilities/pb.RequestMetadata (1)
  • ExecutionTimestamp — 🗑️ Removed
pkg/logger.SugaredLogger (1)
  • WithCtx — ➕ Added

✅ Compatible Changes (2)

pkg/logger (1)
  • CtxKeyVals — ➕ Added
pkg/logger.SugaredLogger (1)
  • WithCtx — ➕ Added

📄 View full apidiff report

@jmank88 jmank88 marked this pull request as ready for review March 30, 2026 13:47
@jmank88 jmank88 requested a review from a team as a code owner March 30, 2026 13:47
Comment thread pkg/logger/logger.go
// Example: l.With(CtxKeyVals(ctx, "keyFoo", ctxKeyFoo, "keyBar", ctxKeyBar)...)
// See: [SugaredLogger.WithCtx]
func CtxKeyVals(ctx context.Context, keyvals ...any) []any {
var kvs []any
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Future expansion:

Suggested change
var kvs []any
var kvs []any
v := ctx.Value(loggerKVKey)
if v != nil {
kvs = slices.Clone(v.([]any))
}
func ContextWith(context.Context, kvs ...any) context.Context {
	var kvs []any
	v := ctx.Value(loggerKVKey)
	if v != nil {
		//TODO technically just need the order right, but should de-dupe the keys
		kvs = append(kvs, v.([]any)...)
	}
	return context.With(loggerKVKey, kvs)
}

@jmank88 jmank88 added this pull request to the merge queue Mar 30, 2026
Merged via the queue into main with commit 877b798 Mar 30, 2026
31 of 32 checks passed
@jmank88 jmank88 deleted the logger-ctx branch March 30, 2026 16:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants