Skip to content

feat: inject logger and tracer into worker context#98

Open
marnickvda wants to merge 2 commits into
mainfrom
feat/inject-logger-tracer-into-worker-context
Open

feat: inject logger and tracer into worker context#98
marnickvda wants to merge 2 commits into
mainfrom
feat/inject-logger-tracer-into-worker-context

Conversation

@marnickvda

@marnickvda marnickvda commented Jun 19, 2026

Copy link
Copy Markdown
Contributor

inject logger and tracer into worker context

Description

Background workers were started with the bare server context, while HTTP handlers get the server's logger and tracer attached per-request via fctx.Middleware. As a result, any worker calling fctx.Logger(ctx) or fctx.Tracer(ctx) silently fell back to the no-op logger/tracer, so worker logs and spans were dropped.

runWorker now mirrors the HTTP path: it attaches the server's logger (and tracer, when configured) to the worker context before invoking the worker, so background processes get the same observability as request handlers.

Types of Changes

  • Bug fix (non-breaking change which fixes an issue)

Tasks

  • Inject s.Logger into the worker context in runWorker
  • Inject s.Tracer into the worker context when configured
  • Cover both with tests

Review

  • Tests — TestRunWorkerInjectsLogger (observer-backed, asserts the worker log is captured rather than dropped) and TestRunWorkerInjectsTracer (asserts the worker sees the configured tracer, not the no-op fallback); go test ./... passes
  • Documentation
  • CHANGELOG

Deployment Notes

None.

Workers received the bare server context, so fctx.Logger and fctx.Tracer
fell back to their no-op implementations and background process logs and
spans were silently dropped. Mirror the HTTP path (fctx.Middleware) by
attaching the server's logger and tracer to the worker context before
running each worker.
@marnickvda marnickvda requested a review from a team as a code owner June 19, 2026 14:48
Comment thread worker.go
// that one worker cannot crash the process or affect the others.
func (s *server) runWorker(ctx context.Context, w Worker) {
ctx = fctx.WithLogger(ctx, s.Logger)
if s.Tracer != nil {

@AdrianCio AdrianCio Jun 22, 2026

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

I'd remove the if, and just overwrite whatever's there, just to avoid cases where it might get initialized with something blank (non-nil) and you don't get your own trace injected correctly.

I don't think there's a case where we don't want our tracer to be injected 🤔

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.

2 participants