Distributed tracing
osapi uses OpenTelemetry to send one trace ID
through each component of a request. When tracing is enabled, you can follow a
request from the CLI to the API server to the agent. Filter the logs on
trace_id.
How it works
-
The API server starts the trace in the
otelechomiddleware. It opensjob.publishwhen it stores and publishes the job. The nats-client adds the trace context to the headers of the NATS message. -
The agent gets the message in a
nats.processspan, which continues the trace from the headers. It opensjob.processunder it. -
The API server opens
job.collectwhile it waits for the answers of the agents. It sends the trace back in thetraceresponseheader of the response. -
The CLI prints the trace ID at the end of the summary line, after the job ID:
3 hosts: 2 ok, 1 skipped job 8adc7e9f-040c-4d5b-8cb1-2e834158f746 trace 26e448c9dbe1c4a8d216a98c675079c9
The CLI needs no tracing configuration for this, and no access to the tracing
backend. Only the controller and the agents send spans. The Go SDK gives the
same ID: resp.TraceID().
A person who develops osapi can also let the CLI send its own span. Give the CLI
telemetry.tracing with an exporter. Then the trace starts at the command,
for example osapi client node package list, and the controller continues it.
The trace ID is the same in each component. Without an exporter, the CLI sends
no trace context, so that a trace has no parent that never arrives.
The span tree
A request to one host, or a broadcast such as --target _all, makes this tree.
A broadcast has one job.process for each agent:
osapi-controller GET /api/node/:hostname/package
├─ job.publish
│ ├─ kv.put (the job)
│ └─ nats.publish
│ ├─ osapi-agent nats.process (web-01)
│ │ └─ job.process
│ │ ├─ kv.get (the job)
│ │ ├─ exec dpkg-query
│ │ └─ kv.put (the answer)
│ └─ osapi-agent nats.process (web-02)
│ └─ job.process
│ └─ ...
├─ job.collect
│ └─ kv.watch (the answers)
├─ response.encode (the answer as JSON)
└─ response.write (the answer to the client, and its size)
A behavior adds behavior.<name>.<method> under job.process, and its commands
and file changes are under that span. A behavior that it requires is a span of
its own under it, for example behavior.managedfile.update under
behavior.sysctl.create. osapi dsl behavior get --name <name> lists the spans
that a behavior can make. The tree above leaves out the kv.put of each status
event.
| Span | Service | Attributes that tell you the most |
|---|---|---|
| the command | osapi-cli | The name is the command |
GET /api/... | osapi-controller | http.route, http.response.status_code, osapi.target |
job.publish | osapi-controller | job.id, job.type, osapi.target, messaging.destination.name |
job.collect | osapi-controller | job.expected, job.answered, job.timed_out; a job.timeout event names each silent agent |
response.encode | osapi-controller | The time to encode the answer as JSON |
response.write | osapi-controller | http.response.body.size. The time to write the answer; a slow network or client makes it long |
validate.target, target.resolve | osapi-controller | osapi.target. A refresh of the agent cache that checks or resolves a target |
job.list, job.get, job.summary, job.retry, job.delete | osapi-controller | job.count or job.id. A read or change of the job bucket, with its NATS calls under it |
agent.list, agent.get, agent.timeline | osapi-controller | osapi.agent.count or osapi.target. A read of the agent registry |
enrollment.list, .accept, .reject, .remove | osapi-controller | osapi.agent.count or osapi.agent.hostname |
audit.list, audit.get, audit.export | osapi-controller | osapi.audit.count or osapi.audit.id |
traces.read | osapi-controller | osapi.trace.id, osapi.trace.spans. The read of a trace from Jaeger |
job.process | osapi-agent | osapi.agent.hostname, osapi.agent.machine_id, host.arch, job.status, job.changed |
behavior.<name>.<method> | osapi-agent | osapi.behavior.name, osapi.behavior.method, osapi.behavior.outcome |
exec <program> | osapi-agent | process.executable.name, osapi.exec.sudo, process.exit.code. The arguments are not recorded |
file.write, file.remove, file.chmod | osapi-agent | file.path, and file.mode and file.size where they apply. The content is not recorded |
kv.<op>, objectstore.<op>, stream.<op> | both | db.operation.name, db.namespace (the bucket or the stream), nats.kv.key or nats.object.name |
nats.publish, nats.process | both | messaging.destination.name, the subject |
Each span has service.version, the version of the binary that made it.
The NATS spans come from nats-client,
which makes one for each call to NATS inside a trace. A call outside a trace,
such as a heartbeat, makes no span, but the call is in the
osapi_nats_client_operation_duration_seconds metric. A file is written only
when its content changed, so a file.write span is a change; the behavior span
has osapi.behavior.changed.
A span has the error status when its work failed: a failed job, a command that
failed, or a job.collect with no answer. A host that skips a job is not an
error. Its job.process has job.status skipped and a job.skipped event
with the reason.
The health endpoints and the files of the UI have no spans. A probe and the UI call them each few seconds, and their traces would hide the traces of the jobs.
Read a trace from the CLI or the UI
You can read a trace without access to the tracing backend. The controller reads it for you:
osapi client telemetry trace get --id 26e448c9dbe1c4a8d216a98c675079c9
Trace:
ID: 26e448c9dbe1c4a8d216a98c675079c9
Started: 2026-11-15 15:18:49
Duration: 20ms
Services: osapi-controller, osapi-agent
NATS calls left out: 11
NATS call time: 20ms
Spans (1):
SPAN ON DURATION STATUS DETAIL
POST /api/node/:hostname/sysctl controller 20ms ok status=200
├─ job.publish controller 2ms ok op=behavior.sysctl.create target=debian-1
│ └─ job.process agent 11ms completed host=debian-1
│ └─ behavior.sysctl.create agent 5ms changed key=vm.swappiness
│ ├─ behavior.managedfile.update agent 1ms changed
│ │ └─ file.write agent 147µs ok path=/etc/sysctl.d/osapi-vm.swappiness.conf
│ └─ exec sysctl agent 3ms ok
└─ job.collect controller 12ms ok
- ON is what ran the span, the role of the service that made it:
controller,agentorcli. - STATUS is the job status on a
job.process,changedorunchangedon a behavior write, andokorerroron the other spans. - DETAIL is what the span acted on, as
key=valuepairs, so each value says what it is:host=on ajob.process, the inputs that a behavior records,path=of a file,op=andtarget=of a job,size=of the answer. A failed span leads witherror=. A value with a space is quoted, as in logfmt.--jsongives each span's wholeattributesmap instead.
The NATS calls are the bookkeeping of the job queue: the agent reads the job,
writes its status as it goes, and writes its answer. The controller leaves them
out of the tree and counts them. --all, the all query parameter of the API,
shows them, and says what each key is for, for example status: started,
response or file record, instead of the key.
The controller makes the tree, so the CLI, the UI and the SDK show the same one.
The answer has the summary of the trace, then each root span with its children.
Each span has its ON, STATUS and DETAIL, and the span as the backend has it,
with its attributes. Spans (1) counts the root spans.
The Traces page of the UI, in the Admin menu, shows the same tree with a
timing bar for each span, and a check box that shows the NATS calls.
/admin/traces/<id> opens one trace.
The controller reads the trace from the query API of Jaeger at
telemetry.tracing.url, the address where a person opens Jaeger. Without that
setting, the command answers that no tracing backend is configured. The backend
gets the spans a few seconds after the request, so a trace that ran a moment ago
can be not found. Run the command again after a few seconds. The command needs
trace:read, which each built-in role has. A token with a target scope gets a
trace only when each target that the trace records is in its scope. Refer to
Target scope.
Read a trace in Jaeger
- Copy the trace ID from the summary line of the command.
- Put it in the search box at the top of Jaeger. Jaeger opens the trace.
- Each
job.processshows its host inosapi.agent.hostname. A red span failed. Open it, and read the error in its status. - Compare
job.expectedandjob.answeredonjob.collect. When they are different, thejob.timeoutevents name the agents that did not answer.
To find traces without an ID, open Search. Set Service to
osapi-controller, and set Tags to one of these:
job.id=<the job ID>, for one jobosapi.agent.hostname=web-01, for the work of one host
An audit entry has trace_id. Put it in the search box of Jaeger to open the
trace of that request.
Debugging with traces
To trace a request end-to-end:
- Run all three processes with
--debug(ortelemetry.tracing.enabled: true) - Execute a command:
osapi client node hostname - Find the
trace_idin any component's log output - Filter all logs:
grep trace_id=<hex>across API server and agent output
This log output shows the same trace ID in each component:
# API Server
INF publishing job request job_id=abc123 trace_id=e0fd287f... span_id=1a2b3c...
INF received job response job_id=abc123 trace_id=e0fd287f... span_id=1a2b3c...
# Agent
INF processing job job_id=abc123 trace_id=e0fd287f... span_id=4d5e6f...
INF job processing completed job_id=abc123 trace_id=e0fd287f... span_id=4d5e6f...
Configuration
Tracing is off by default. The exporter field sets where osapi sends the
spans:
exporter | Behavior |
|---|---|
| (unset) | Log correlation only. trace_id is in the structured logs, but osapi exports no spans |
stdout | Raw OpenTelemetry span JSON, printed directly to stdout without the structured logger. Useful to find a problem one time, but the output is large |
otlp | Spans sent via gRPC to a tracing backend (Jaeger, Tempo, etc.) |
The --debug flag auto-enables tracing with no exporter (log correlation
only). It does not enable the stdout exporter.
Log correlation (recommended default)
Enable tracing so that trace_id and span_id are in the structured log lines.
osapi exports no spans. To follow a request, search the logs with grep.
telemetry:
tracing:
enabled: true
OTLP export (production)
Send spans to a tracing backend that you run, for example
Jaeger or
Grafana Tempo. The otlp_endpoint is the gRPC
address of that backend.
telemetry:
tracing:
enabled: true
exporter: otlp
otlp_endpoint: localhost:4317
# Where a person opens Jaeger. The controller also reads traces from it
# for osapi client telemetry trace get and the Traces page.
url: http://jaeger:16686
authentication. This works for local and internal backends. It does not work for cloud services that need TLS and API keys, for example Grafana Cloud, Honeycomb and Datadog. Authenticated OTLP is planned. :::
Refer to Configuration for the full reference.
Related
- Configuration: full configuration reference
- Architecture: system design overview
- Job System: how async job processing works