trace/logging: write trace_id and span_id as OpenTelemetry names them
Maintainers usually reply within 1 day
Nobody has claimed this yet.
Assessment
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Newbie friendliness
- 72/100
Research direction
Start by reading the trace/logging.Handler implementation and its existing tests; the issue does not name specific file paths. Check how it currently adds traceId, then inspect the available trace context APIs in the Go OpenTelemetry trace package. Done means logs use trace_id, include span_id for a valid active span, and retain the correlation ID fallback when no valid span exists.
Written by the indexing model from the issue text.
Description
Current Limitation
trace/logging.Handler adds the request's correlation ID to log lines, but the output doesn't follow the OpenTelemetry convention for trace context in logs, and it can't add a span ID:
- Field name. It writes
traceId. The OTel spec for non-OTLP log formats (Trace Context in non-OTLP Log Formats) names the fieldstrace_id,span_idandtrace_flags. Collectors and backends (the Collector'sfilelogreceiver, Loki/Grafana derived fields, OpenObserve) pick up those names when linking logs to traces. WithtraceId, every consumer needs its own parsing rule. - No span ID. The handler reads only
trace.GetTraceID(ctx). When the context carries an active OTel span, its span ID never reaches the log line, so a line can be linked to a trace but not to the span (the hop or operation) that wrote it. - Can disagree with the span's trace ID. The value comes from
TraceMiddleware, not from the OTel span. Unless a service adds something like the customIDGeneratorin OpenNSW/nsw-srilanka#866, thetraceIdin the logs and the trace ID of the spans are different values for the same request.
Suggested Improvement
Make trace/logging.Handler write trace context as the OTel convention describes:
- Rename the field from
traceIdtotrace_id. - Prefer the active OTel span. If
oteltrace.SpanContextFromContext(ctx).IsValid(), add:trace_id:sc.TraceID().String()(32 lowercase hex characters)span_id:sc.SpanID().String()(16 lowercase hex characters)- optionally
trace_flags:sc.TraceFlags()as 2 hex characters
- Fall back to
trace.GetTraceID(ctx)astrace_idwhen there's no valid span, as today.
func (h *Handler) Handle(ctx context.Context, record slog.Record) error {
if sc := oteltrace.SpanContextFromContext(ctx); sc.IsValid() {
record.AddAttrs(
slog.String("trace_id", sc.TraceID().String()),
slog.String("span_id", sc.SpanID().String()),
)
} else if traceID := trace.GetTraceID(ctx); traceID != "" {
record.AddAttrs(slog.String("trace_id", traceID))
}
return h.Handler.Handle(ctx, record)
}
- Services without OTel tracing keep working unchanged, apart from the field name.
- This adds a dependency on
go.opentelemetry.io/otel/trace(API only, no SDK) to thetracemodule. - Renaming the field is a breaking change for anything that queries
traceIdin logs. Release it as a new minor version and note it in the release notes.
Out of scope: the traceId / correlationId names in httputil error responses, and where TraceMiddleware gets its ID from (#349).
Version
github.com/OpenNSW/core/trace v0.2.0
Additional Context
- Needed by openndx/openndx-core#702 (adopt
core/trace,trace_idon log lines) and openndx/openndx-core#711 (OTel tracing,span_idon log lines). openndx-core's logging standard (openndx/openndx-core#708) specifiestrace_id. - Related to #349: once
TraceMiddlewaretakes its ID from the active span, step 3's fallback only applies to services without OTel tracing, and logs and spans always share one trace ID.
- Dominant language
- Go
- Stars
- 6
- Forks
- 8
- Avg merge
- 21h 36m
- Merged PRs (30d)
- 42
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from OpenNSW/core
-
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 1 day
-
payment: test fixtures hardcode real gateway names (govpay/lankapay) instead of generic placeholdersOpen
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 1 day
-
nsw-sl cleanup Type/Improvement
Difficulty 3/5 1-2 days Newbie friendliness 48/100
Maintainers usually reply within 1 day
-
remote: let the host inject a logger, and log with one consistent policyPossibly taken @mushrafmim claimed this 5 days ago. OpenType/Improvement
Difficulty 4/5 3-5 days Newbie friendliness 22/100
Maintainers usually reply within 1 day
Similar issues
-
☢️ Bug v3
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day
-
area/import-export area/projects concern/ux
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
go-vikunja/vikunja#4455 · 1 comment ·
Maintainers usually reply within 2 days
-
Dependabot PRs no longer merge automaticallyPossibly taken @dwisiswant0 claimed this today. Open
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
projectdiscovery/urlfinder#160 ·
Maintainers usually reply within 1 day
-
database
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
chnm/legal-modernism#450 ·
Maintainers usually reply within 1 day
-
tech-debt
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
Maintainers usually reply within 1 day