Skip to content

refactor!: extract protocol trace context only from Diagnostic - #312

Merged
tisonkun merged 4 commits into
mainfrom
fix/trace-context-tuple
Dec 1, 2026
Merged

tisonkun merged 4 commits into
mainfrom
fix/trace-context-tuple

Conversation

@tisonkun

@tisonkun tisonkun commented Dec 1, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Extract protocol trace correlation fields in OpentelemetryLog (LogRecord::set_trace_context) and GoogleCloudLoggingLayout (logging.googleapis.com/{trace,spanId,trace_sampled}) exclusively from Diagnostic ("trace_id", "span_id", "sampled") entries instead of inspecting or consuming Record::key_values(), and remove the redundant "trace_sampled" diagnostic key alias.

Previously, merging trace keys across both Diagnostic and Record::key_values() allowed domain event attributes (such as logging an asynchronous job or statement entity's persisted "trace_id" while finalizing or cancelling it from another span) to forge cross-trace (record.trace_id, diag.span_id) pairs, clear an active span's trace correlation when the domain field was None or non-hex, and strip "trace_id" from exported OTLP attributes or Cloud Logging payload fields. With trace correlation scoped strictly to Diagnostic, domain "sampled" fields in Record::key_values() cannot collide with diagnostic sampling flags either, so a separate "trace_sampled" diagnostic alias is no longer needed.

Changes

  • logforth-append-opentelemetry:
    • Populate LogRecord::set_trace_context only from valid Diagnostic ("trace_id", "span_id") pairs (32- and 16-digit non-zero hex) and optional boolean "sampled".
    • Emit all Record::key_values() entries directly as ordinary SdkLogRecord attributes without consuming trace-named keys.
  • logforth-layout-google-cloud-logging:
    • Populate logging.googleapis.com/{trace,spanId,trace_sampled} only from valid Diagnostic ("trace_id", "span_id", "sampled") entries when trace_project_id is configured, copying validated diagnostic hex IDs onto fixed-size stack buffers without intermediate span_id String allocations.
    • Route all Record::key_values() entries to logging.googleapis.com/labels or top-level jsonPayload as ordinary fields.
  • Docs & Integration Tests:
    • Update DESIGN.md, CHANGELOG.md, crate READMEs, and rustdoc in OpentelemetryLog and GoogleCloudLoggingLayout to document the canonical ("trace_id", "span_id", "sampled") diagnostic keys.
    • Add integration tests covering active sampled/unsampled spans alongside explicit record trace attributes, label routing, and composed diagnostic shadowing.

Previously, `OpentelemetryLog` and `GoogleCloudLoggingLayout` merged
`trace_id`, `span_id`, and `sampled` / `trace_sampled` field-by-field
across `Diagnostic` and `Record::key_values()`. When a log record carried
a standalone business `"trace_id"` (without `"span_id"`) or a non-hex ID
inside an active span, `OpentelemetryLog` either paired the record's
`trace_id` with the diagnostic's `span_id` and `sampled` flag or cleared
the diagnostic trace context altogether, and `GoogleCloudLoggingLayout`
emitted a mismatched `(trace, spanId, trace_sampled)` tuple while
dropping the standalone `"trace_id"` from `jsonPayload` and `labels`.

Extract trace context per source instead of mixing IDs across sources:
- Explicit `Record::key_values()` override diagnostic trace context as a
  unit (without inheriting diagnostic sampling flags) only when both a
  valid 32-digit non-zero hex `trace_id` and a valid 16-digit non-zero
  hex `span_id` are present.
- Standalone or malformed IDs in `Record::key_values()` remain ordinary
  attributes, `jsonPayload` fields, or `labels` while leaving any active
  diagnostic trace tuple intact.
- Records carrying neither `"trace_id"` nor `"span_id"` continue to
  allow boolean `"sampled"` / `"trace_sampled"` overrides on an active
  diagnostic trace.

Signed-off-by: tison <wander4096@gmail.com>
Signed-off-by: tison <wander4096@gmail.com>
Populate `OpentelemetryLog` `LogRecord::set_trace_context` and
`GoogleCloudLoggingLayout` `logging.googleapis.com/{trace,spanId,trace_sampled}`
exclusively from `Diagnostic` context instead of inspecting or consuming
`Record::key_values()`.

Explicit record key-values represent domain event attributes (such as a
persisted entity's `trace_id`) rather than the active execution span of the
caller emitting the log, so keeping `Record::key_values()` untouched preserves
domain attributes consistently across sinks and avoids complex cross-source
override rules in code and documentation.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun tisonkun changed the title fix: prevent cross-source trace tuple mixing in OTel and GCP outputs refactor!: extract protocol trace context only from Diagnostic Dec 1, 2026
Standardize well-known trace correlation keys in `Diagnostic` on
`("trace_id", "span_id", "sampled")` across `OpentelemetryLog`,
`GoogleCloudLoggingLayout`, and `DESIGN.md` (`GoogleCloudLoggingLayout`
continues to emit the boolean `"sampled"` flag under Google Cloud's
`"logging.googleapis.com/trace_sampled"` JSON field).

Because protocol trace correlation is extracted only from `Diagnostic`
rather than `Record::key_values()`, business `"sampled"` attributes in
log records no longer collide with diagnostic sampling flags, making a
second `"trace_sampled"` diagnostic alias unnecessary.

Signed-off-by: tison <wander4096@gmail.com>
@tisonkun
tisonkun merged commit f9c5b56 into main Dec 1, 2026
10 checks passed
@tisonkun
tisonkun deleted the fix/trace-context-tuple branch December 1, 2026 07:21
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.

1 participant