Repository navigation
fix(mlog): give an exported log record with no message a body from its span and error - #3540
Conversation
…s span and error `#[tracing::instrument(err)]` logs a failure as an event whose only field is `error`, so the OTel bridge exported it with an empty body: a search of log bodies never found it and a table of messages showed a blank line. The log export layer now wraps the bridge. For an event with an `error` field and no `message` field it notes the module and span the event came from, and a processor that runs first on the logger gives the record the body `<module>::<span name> failed: <error>`, or `<module>: <error>` with no span. The rule applies at any level. A record that has a message, or has no `error` field, is left as it was, and the `error` attribute is kept. Terminal and file logs are unchanged, and nothing runs when telemetry is off. Refs #3223 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
|
Reviewed: no findings; approval withheld, 3 threads unresolved at APPROVE withheld: 3 unresolved threads — norrietaylor crates/mlog/src/otel/body.rs, norrietaylor crates/mlog/src/otel.rs:1246, norrietaylor crates/mlog/src/otel/body.rs. Resolving the last thread releases the approval on this head. Resolve the open threads. Resolving the last one releases the approval on this head; no new review is required. Clobber: none seen. |
There was a problem hiding this comment.
The approach is sound. The thread-local handoff from BodyFromError to FillBody is synchronous on the emitting thread, and Reset clears it even on a panic. Records that have a message, or no error field, are left alone, and FillBody returns false from event_enabled, so no record is created only because of it. I found one issue. The span name comes from the per-layer-filtered context. When an #[instrument(level = "debug", err)] span is filtered out of export (the default filter is info), the body names an enclosing span from the caller, or no span at all. Several functions in the tree hit this path. Details and a fix are inline.
Generated by Claude Code
| } | ||
| put(Some(( | ||
| metadata.target(), | ||
| ctx.event_span(event).map(|s| s.name()), |
There was a problem hiding this comment.
Medium: the body can name the wrong span when the failing function's span is filtered out of export.
BodyFromError runs inside Filtered<_, ExportFilter<S>, S>, so the ctx it gets carries the export filter's id. Context::event_span on a contextual event calls lookup_current(). For a per-layer-filtered context, that skips spans the filter disabled and returns the nearest enabled ancestor, or None. The err event of #[instrument] is ERROR by default whatever the span's level, so here is the failure path:
#[tracing::instrument(level = "debug", skip_all, err(Debug))]incrates/graph/src/loader.rs:45(also:59,graph/src/wire.rs:248,diagnostics/src/procs.rs:149,180), with the defaultMINIMAL_OTEL_FILTER=info.- The debug span is disabled for the export filter. The ERROR event passes.
event_spanreturns the enclosing INFO span (saymaterializefrom another module). The exported body isgraph::loader::materialize failed: <error>. That pairs the loader's module with a caller's span and says the caller failed. With no enabled ancestor, it falls back tograph::loader: <error>, which loses the function name.
The tests don't catch this because exported() builds the stack without the Filtered wrapper that log_layer adds.
Suggested fix: take the span name from the unfiltered current span. For a contextual event, use ctx.current_span().metadata().map(|m| m.name()); Current comes from the subscriber and ignores the per-layer filter. Keep event_span only for an explicit parent:, or drop the name in that case. Then add a test that wraps BodyFromError(bridge) in .with_filter(LevelFilter::INFO) and calls a level = "debug" err function inside an info_span!.
Generated by Claude Code
There was a problem hiding this comment.
Right about the mechanism, thanks. 8b0aa05 takes the name from the subscriber's current span in place of event_span, and names a span only when it has the event's target, which is what #[instrument] gives a function and its own err event. One thing differs from the example: with the default RUST_LOG=info no layer admits debug, so the debug span is never created and the current span really is the caller's; the target check is what stops a caller from another module being named there, and the body then reads <module>: <error>. An explicit parent the filter hides names nothing. One case is left as it was: a debug-level function called from an info span in the same module still gets the caller's name, because nothing records the function's own name then; closing it needs another body format (file and line), which I have kept out of this PR. The tests now run behind .with_filter(LevelFilter::INFO), with and without a layer that admits debug: two new ones fail on the old head, two are controls.
…t filter hides it
`BodyFromError` sits behind the export filter, so the context it is given
looks spans up through that filter. `event_span` on a contextual event skips
a span the filter hides and returns the nearest ancestor it admits. The
`err` event of `#[instrument(level = "debug", err)]` is ERROR and exported
under the default `info` filter while its span is hidden, so the body named
a caller's span ("<module>::<caller> failed"), or no span at all.
A contextual event now takes the subscriber's current span, which per-layer
filters do not touch. An explicit parent is still looked up through the
filter and names nothing when hidden. When the function's span was never
created (no layer admits debug) the current span is a caller's, so a span
whose target differs from the event's is not named: the body is then
"<module>: <error>". `#[instrument]` gives its span and its event the same
target. The tests now build the stack behind a per-layer filter, as
`log_layer` does.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A `Filtered` layer is typed by the subscriber it sits on, so one value could not be stacked on both the plain registry and the registry with the extra debug layer. Each branch now applies the filter itself. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Scope matches the title. The diff adds crates/mlog/src/otel/body.rs and three small hunks in crates/mlog/src/otel.rs. No dependency or lockfile moved.
Clobber none. git log on crates/mlog/src/otel.rs shows no recent fix that this diff reverts.
Correctness I read the vendored bridge, SDK logger and tracing-subscriber context sources. The bridge emits on the calling thread, and the SDK runs the processors in order, so FillBody sees PENDING and later processors see the body. SdkLogger::event_enabled uses any, so FillBody returning false does not create or suppress a record. Context::current_span ignores the per-layer filter, and Context::span applies it, as the comment says. #[instrument(err)] records error through record_debug, so the attribute is a string named error. I found no hand-written event in crates/ that has an error field and no message, so the rule reaches only #[instrument(err)] events today. A later hand-written event of that shape inside a span of the same target gets the word failed in its body. The spool and forward processors print the body and do not redact attributes, so the body exposes nothing that the error attribute did not already expose.
The same-module caller case in the existing thread has a reply, and I did not raise it again.
Not checked I did not compile or run the tests. The secret and dependency scanners did not run, because the harness denied compound shell commands. The diff has no credential-shaped string on a manual read. The CLI version in agent.version is not known to me.
| // First, so every later processor sees the body it fills in. | ||
| let mut b = SdkLoggerProvider::builder() | ||
| .with_resource(resource) | ||
| .with_log_processor(body::FillBody); |
There was a problem hiding this comment.
low · No test covers the production wiring of this fix. The tests in otel/body.rs build their own provider and layer by hand in exported_with. Remove .with_log_processor(body::FillBody) here, or the BodyFromError(...) wrap in log_layer, and every test still passes while exported records lose the body again. The two halves work only together, and only init and log_layer join them. crates/mlog/tests/otel_requirements.rs already has installed(), which runs mlog::otel::init and log_layer, and it reads body.stringValue from the spool. Add one case there that calls a failing #[tracing::instrument(err)] function and asserts the spooled body. I did not find another place with this gap: these are the only non-test builder and bridge sites.
There was a problem hiding this comment.
Right, either half could go and nothing failed. 0597669 adds an_instrumented_failure_is_spooled_with_a_body to otel_requirements.rs: a child process under installed() calls a failing #[tracing::instrument(err)] function, and the parent asserts that exactly one spooled record carries the error and that its body is <target>::fails_to_write failed: disk full. Without the FillBody processor in init, or without the BodyFromError wrap in log_layer, the record is spooled with no body and the assertion fails.
| //! logger's first processor, then gives the record the body | ||
| //! `<target>::<span name> failed: <error>`, or `<target>: <error>` when there | ||
| //! is no span or the span is from another target. The span is the one the | ||
| //! event was emitted in even when the export filter hides it. The rule |
There was a problem hiding this comment.
nit · The module doc says the span is the one the event was emitted in even when the export filter hides it. That is true only for a contextual event whose span exists in the registry. An explicit parent that the filter hides names no span, as the comment in on_event and the test an_explicit_parent_is_named_only_when_admitted state. A span from the same target that is the caller's is also named. State the two exceptions here, or shorten the sentence to the contextual case.
There was a problem hiding this comment.
Agreed, the sentence claimed more than the code does. 0597669 states the cases in the module doc: a contextual event names the current span, also one the export filter hides, provided some layer admitted it; when none did, a span the caller entered is named if it has the event's target; an explicit parent is named only when the export filter admits it.
…_layer The body tests build their own provider and layer, so they pass with either half of the production wiring removed. This case runs a failing `#[instrument(err)]` function under the stack `init` and `log_layer` build and asserts the spooled body; it fails without the `BodyFromError` wrap in `log_layer` or without the `FillBody` processor in `init`. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A contextual event names the subscriber's current span, which is the caller's when no layer created the function's own span; it is named if it has the event's target. An explicit parent is named only when the export filter admits it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
There was a problem hiding this comment.
minimal-review — approval withheld
Verdict COMMENT — APPROVE withheld: 3 unresolved threads — norrietaylor crates/mlog/src/otel/body.rs, norrietaylor crates/mlog/src/otel.rs:1246, norrietaylor crates/mlog/src/otel/body.rs. No findings.
Panel: 3 of 3 personas ran (correctness, security, supply-chain).
What
An exported log record that has an
errorfield and no message now gets a body:<module>::<span name> failed: <error>, or<module>: <error>when there is no span.Refs #3223
Why
#[tracing::instrument(err)]logs a failure as an event whose only field iserror. The OTel bridge exported it with an empty body. A search of log bodies never found these records, and a table of messages showed a blank line for each. In a week of telemetry they were a visible share of the ERROR records.How
The log export layer wraps the bridge. For an event with an
errorfield and nomessagefield, it notes the module and the span the event came from. A processor that runs first on the logger then writes the body.What does not change
errorfield is left as it was.errorattribute is kept.The rule applies at any level, not only ERROR.
Testing
an_instrumented_failure_gets_a_body_from_its_span_and_errora_message_or_a_missing_error_is_left_aloneWorkspace lint, tests and the native end-to-end script pass on this commit.
🤖 Generated with Claude Code
Summary by CodeRabbit