Observability log contract
Design: #3477, devops-engineer design comment 6959886725, approved by the founder (comment 6981311017).
Owner of the Go side: internal/obslog (piece D). The Python worker follows the same keys through structlog
contextvars (piece F).
Setup
Every Go service calls obslog.Init("<service>") first thing in main. Wired today: context-engine,
agent-orchestrator, model-gateway.
| Env | Values | Default |
|---|---|---|
LOG_FORMAT | json, text (logfmt, for reading by eye) | json on every environment, dev included |
LOG_LEVEL | debug, info, warn, error (slog syntax, case-insensitive; warn+2 works) | info |
An unrecognised value falls back to the default and logs one obslog.config WARN line saying so.
ENVIRONMENT no longer chooses the format or level. Before #3477, dev wrote logfmt text at DEBUG; set
LOG_LEVEL=debug to get the DEBUG lines back.
Tools that parse these logs must accept both outputs. scripts/check-model-gateway-rollback.py (check 5,
drain-not-cut) reads the drain timestamps from either time=… or "time":"…". Log collection (piece A)
expects JSON.
The 16 fields
These keys mean the same thing in every service, and a service never uses another name for one of these
concepts. The set is pinned by internal/obslog/testdata/fields.golden.
| Key | Meaning | How a Go call site sets it |
|---|---|---|
event | Stable dotted event name. This is the record message: slog.InfoContext(ctx, "run.started", ...) | the message |
service | Emitting service | stamped by obslog.Init |
org_id | Tenant org | obslog.OrgID |
team_id | Team / org unit | obslog.TeamID |
run_id | Workflow run | obslog.RunID |
session_id | Agent session | obslog.SessionID |
segment | Worker segment | obslog.Segment |
hop_key | A2A hop | obslog.HopKey |
agent_ref | Agent reference | obslog.AgentRef |
approval_id | Approval / gate park | obslog.ApprovalID |
tool_use_id | Tool call | obslog.ToolUseID |
trace_id | OTel trace (32 hex) | stamped from the span in ctx |
span_id | OTel span (16 hex) | stamped from the span in ctx |
outcome | Result of the step | obslog.Outcome |
duration_ms | Elapsed time, whole ms | obslog.Duration |
err | Error message, err.Error() verbatim | obslog.Err (nil is dropped) |
Rules the handler enforces:
- Absent fields are omitted, never
"". A contract key whose value is the empty string is dropped. - Bind once, log everywhere.
ctx = obslog.ContextWith(ctx, obslog.RunID(id), obslog.OrgID(org))at entry. Everyslog.*Context(ctx, ...)below it carries those fields. The context-freeslog.Infocannot carry them, so run-scoped code must use theContextvariants. - No key is written twice; the first value wins. The order is: values bound with
logger.With(...), then the call-site attributes in argument order (RunID("a"), RunID("b")writesrun_id=a), then the context bindings, then the OTel span. Within the context, a laterContextWithof a key replaces the earlier one. - Reserved top-level keys are dropped, not re-keyed.
msg,time,level,source,eventandserviceare written only by the logger. A caller's attribute with one of these names, at the call site or inlogger.With, is dropped silently. It is not renamed to<key>_attr. So a caller cannot overwrite the event name. Inside aWithGroupthese names are ordinary keys. - An empty value never unbinds.
ContextWith(ctx, obslog.OrgID(""))leaves an earlierorg_idbound. To log a run without a field, build the context without it. erris not redacted.obslog.Errwriteserr.Error()verbatim. Never pass an error that can carry a secret: a connection string or DSN, a signed or pre-signed URL, a bearer/API/agent token, or a header dump. Wrap it first so that only a safe message remains. The redaction seam is tracked in #4601.- Trace ids are stamped for any valid span, sampled or not. A line can carry a
trace_idthat the collector's tail sampling dropped. If the trace is missing in Tempo, that does not mean the line is wrong. - Do not open
WithGroupon a contract logger. The context-stamped fields would land inside the group.
Loki labels (binding on piece A)
Ruling by the principal architect on #4599 (review 5958978201):
- Stream labels (indexed) are
serviceandlevelonly. The collector may add its own target or env resource label. Takeservicefrom one source: theservicefield on the line is authoritative. Map the composecom.docker.compose.servicelabel onto it rather than emitting both. - Never use as labels:
run_id,session_id,trace_id,span_id,org_id,team_id,hop_key,tool_use_id,approval_id,agent_ref,segment,outcome,duration_ms,err. Every distinct value is one Loki stream:run_idalone would create a stream per run. eventis not a label either until the event names are lint-bounded (#4601). Until then, free-text messages make its set unbounded.- Correlation ids go in as structured metadata:
run_id,org_id,trace_id,session_id,approval_id. With Loki's native OTLP ingest, log-record attributes become structured metadata and only resource attributes become labels. The collector'sfilelog→json_parsermust therefore leave these as log attributes. It must never promote them to resource attributes or list them inloki.attribute.labels/default_labels_enabled. The Run explorer then queries{service=~".+"} | run_id="<id>"without| json.
The ten INFO events
Every one of these is logged at INFO with the contract fields known at that point (at least run_id and
org_id once a run exists), plus the event-specific keys below. Event-specific keys are not contract
fields; they are fixed per event here.
| # | event | Event-specific keys | Emitted by |
|---|---|---|---|
| 1 | run.started | none | orchestrator (G) |
| 2 | run.finished | state, running_ms, cost_usd, cost_unknown_n | orchestrator (G) |
| 3 | segment.started | worker_id | worker (F) |
| 4 | segment.ended | outcome, duration_ms, tokens | worker (F) |
| 5 | hop.decided | decision (allow/deny/defer), rule | orchestrator (G) |
| 6 | gate.parked | gate, timeout_at | orchestrator (G) |
| 7 | gate.resumed | decision, wait_ms, worker_id | orchestrator (G) |
| 8 | gateway.request | surface (model/mcp), endpoint_id, dialect, model, upstream_status, outcome, deciding_gate, cost_status, tokens, latency_ms | model-gateway + mcpproxy (E) |
| 9 | start.refused | reason, and run_id is required | worker (F) |
| 10 | run.recovered | prior_worker, age (lease reclaim / sweeper) | orchestrator + W2 (G) |
Status: piece D ships the package and the three service wirings. None of the ten events is emitted yet.
They arrive with pieces E, F and G on #3477. Wiring the nine other initLogger binaries, plus a guard, is
#4600.