Skip to content

feat: add span_id and service_name to monitoring logs#5633

Open
Incheonkirin wants to merge 1 commit into
bentoml:mainfrom
Incheonkirin:feat/monitoring-trace-context
Open

feat: add span_id and service_name to monitoring logs#5633
Incheonkirin wants to merge 1 commit into
bentoml:mainfrom
Incheonkirin:feat/monitoring-trace-context

Conversation

@Incheonkirin

Copy link
Copy Markdown

Summary

Monitoring records already include trace_id, but not span_id or the service name. This makes it impossible to correlate a monitoring log line to a specific span (or service) within a distributed trace without manually calling mon.log(...) in user code. Closes #4367.

Changes

Add span_id and service_name as preserved columns in both monitor backends, populated from the existing trace_context — exactly how trace_id is already handled:

  • DefaultMonitor (monitoring/default.py) — file/JSON logs
  • OTLPMonitor (monitoring/otlp.py) — OTLP export

Before:

{"trace_id": "...", "request_id": "...", "timestamp": "...", "...": "..."}

After:

{"trace_id": "...", "span_id": "...", "service_name": "...", "request_id": "...", "timestamp": "...", "...": "..."}

Notes

  • Crash-safe: span_id falls back to 0 when no span is active, identical to the existing trace_id behavior; service_name is None when unset (same pattern as request_id).
  • No new dependencies; mirrors the existing preserved-column pattern, so users no longer need to log trace context manually.

The monitoring data already includes `trace_id` but not `span_id` or the
service name, so log records cannot be correlated to a specific span or service
within a trace — users had to add these manually via `mon.log(...)`.

Add `span_id` and `service_name` as preserved columns in both the default
(file) monitor and the OTLP monitor, populated from the existing
`trace_context` (mirroring how `trace_id` is already handled). Both values are
crash-safe: `span_id` falls back to 0 when no span is active, exactly like the
existing `trace_id` column.

Closes bentoml#4367

Signed-off-by: Mingi Jeong <incheonkirin@users.noreply.github.com>
@Incheonkirin Incheonkirin requested a review from a team as a code owner June 5, 2026 09:24
@Incheonkirin Incheonkirin requested review from parano and removed request for a team June 5, 2026 09:24
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.

enhancement: Include the trace_id, span_id, and service name in the monitoring log

1 participant