Skip to content

Job logger trace correlation#3782

Merged
DrJosh9000 merged 3 commits intobuildkite:mainfrom
aapzu:job-logger-trace-correlation
Mar 25, 2026
Merged

Job logger trace correlation#3782
DrJosh9000 merged 3 commits intobuildkite:mainfrom
aapzu:job-logger-trace-correlation

Conversation

@aapzu
Copy link
Copy Markdown
Contributor

@aapzu aapzu commented Mar 24, 2026

Description

When both logging to stdout and tracing are on, it's difficult to connect the logs to the traces. That can be done by using build and log ids, but even that won't help with the exact span (e.g. if it was a hook or the step itself). This PR adds trace_id and span_id fields to the json formatted logs (if tracing is enabled), which are extracted from the job's traceparent header.

To be able to independently test the new code, the inline job logger code from job_runner.go was extracted into dedicated files to make it independently testable.

Context

relates to #3770

Changes

  • agent/json_job_logger.go: A new file with the extracted json logging code. Otherwise it's the same as what previously was in job_runner.go, but with the added traceparent field extraction.
  • agent/job_logger.go: Extracted jobLogger from job_runner.go. Either initializes a JsonJobLogger or a TextJobLogger based on the log_format configuration. Due to its simplicity, this file also contains the TextJobLogger implementation.
  • agent/job_runner.go: Extracted jobLogger to job_logger.go.
  • agent/json_job_logger_test.go: tests for traceparent field extraction as well as for the other fields that are added to the logs.
  • agent/job_logger_test.go: tests for format dispatching behaviour.

Testing

  • Tests have run locally (with go test ./...). Buildkite employees may check this if the pipeline has run automatically.
  • Code is formatted (with go tool gofumpt -extra -w .)

Disclosures / Credits

Claude Code wrote the implementation and tests. I directed the design decisions and reviewed every single piece of the code written by it.

@aapzu aapzu requested review from a team as code owners March 24, 2026 11:03
aapzu and others added 2 commits March 24, 2026 16:12
Moves the inline job logger out of job_runner.go into json_job_logger.go
and job_logger.go. JsonJobLogger handles structured JSON output with job
metadata fields (org, pipeline, branch, etc.). JobLogger dispatches to
JsonJobLogger for JSON format or passes bytes through directly for text.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Parses the job's traceparent header and includes trace_id and span_id
as structured log fields for trace correlation. Adds tests for the
traceparent parsing and all structured job fields.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@aapzu aapzu force-pushed the job-logger-trace-correlation branch from b00d8e6 to e4238fb Compare March 24, 2026 14:12
Rename JsonJobLogger to JSONJobLogger, NewJsonJobLogger to NewJSONJobLogger, etc
https://google.github.io/styleguide/go/decisions#initialisms

Use cmp.Diff for comparing maps
https://google.github.io/styleguide/go/decisions#full-structure-comparisons

Avoid nil checking and type assertions by unmarshaling JSON to
map[string]string instead of map[string]any, and
take advantage of zero value for string to avoid missing map value checks
Copy link
Copy Markdown
Contributor

@DrJosh9000 DrJosh9000 left a comment

Choose a reason for hiding this comment

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

Thanks @aapzu, this is good. I've pushed some readability improvements, LGTM

@DrJosh9000 DrJosh9000 merged commit 9b50c93 into buildkite:main Mar 25, 2026
1 check passed
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