Skip to main content

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.

EnvValuesDefault
LOG_FORMATjson, text (logfmt, for reading by eye)json on every environment, dev included
LOG_LEVELdebug, 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.

KeyMeaningHow a Go call site sets it
eventStable dotted event name. This is the record message: slog.InfoContext(ctx, "run.started", ...)the message
serviceEmitting servicestamped by obslog.Init
org_idTenant orgobslog.OrgID
team_idTeam / org unitobslog.TeamID
run_idWorkflow runobslog.RunID
session_idAgent sessionobslog.SessionID
segmentWorker segmentobslog.Segment
hop_keyA2A hopobslog.HopKey
agent_refAgent referenceobslog.AgentRef
approval_idApproval / gate parkobslog.ApprovalID
tool_use_idTool callobslog.ToolUseID
trace_idOTel trace (32 hex)stamped from the span in ctx
span_idOTel span (16 hex)stamped from the span in ctx
outcomeResult of the stepobslog.Outcome
duration_msElapsed time, whole msobslog.Duration
errError message, err.Error() verbatimobslog.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. Every slog.*Context(ctx, ...) below it carries those fields. The context-free slog.Info cannot carry them, so run-scoped code must use the Context variants.
  • 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") writes run_id=a), then the context bindings, then the OTel span. Within the context, a later ContextWith of a key replaces the earlier one.
  • Reserved top-level keys are dropped, not re-keyed. msg, time, level, source, event and service are written only by the logger. A caller's attribute with one of these names, at the call site or in logger.With, is dropped silently. It is not renamed to <key>_attr. So a caller cannot overwrite the event name. Inside a WithGroup these names are ordinary keys.
  • An empty value never unbinds. ContextWith(ctx, obslog.OrgID("")) leaves an earlier org_id bound. To log a run without a field, build the context without it.
  • err is not redacted. obslog.Err writes err.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_id that the collector's tail sampling dropped. If the trace is missing in Tempo, that does not mean the line is wrong.
  • Do not open WithGroup on 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 service and level only. The collector may add its own target or env resource label. Take service from one source: the service field on the line is authoritative. Map the compose com.docker.compose.service label 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_id alone would create a stream per run.
  • event is 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's filelog → json_parser must therefore leave these as log attributes. It must never promote them to resource attributes or list them in loki.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.

#eventEvent-specific keysEmitted by
1run.startednoneorchestrator (G)
2run.finishedstate, running_ms, cost_usd, cost_unknown_norchestrator (G)
3segment.startedworker_idworker (F)
4segment.endedoutcome, duration_ms, tokensworker (F)
5hop.decideddecision (allow/deny/defer), ruleorchestrator (G)
6gate.parkedgate, timeout_atorchestrator (G)
7gate.resumeddecision, wait_ms, worker_idorchestrator (G)
8gateway.requestsurface (model/mcp), endpoint_id, dialect, model, upstream_status, outcome, deciding_gate, cost_status, tokens, latency_msmodel-gateway + mcpproxy (E)
9start.refusedreason, and run_id is requiredworker (F)
10run.recoveredprior_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.