Skip to main content

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​

  1. The API server starts the trace in the otelecho middleware. It opens job.publish when it stores and publishes the job. The nats-client adds the trace context to the headers of the NATS message.

  2. The agent gets the message in a nats.process span, which continues the trace from the headers. It opens job.process under it.

  3. The API server opens job.collect while it waits for the answers of the agents. It sends the trace back in the traceresponse header of the response.

  4. 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.

SpanServiceAttributes that tell you the most
the commandosapi-cliThe name is the command
GET /api/...osapi-controllerhttp.route, http.response.status_code, osapi.target
job.publishosapi-controllerjob.id, job.type, osapi.target, messaging.destination.name
job.collectosapi-controllerjob.expected, job.answered, job.timed_out; a job.timeout event names each silent agent
response.encodeosapi-controllerThe time to encode the answer as JSON
response.writeosapi-controllerhttp.response.body.size. The time to write the answer; a slow network or client makes it long
validate.target, target.resolveosapi-controllerosapi.target. A refresh of the agent cache that checks or resolves a target
job.list, job.get, job.summary, job.retry, job.deleteosapi-controllerjob.count or job.id. A read or change of the job bucket, with its NATS calls under it
agent.list, agent.get, agent.timelineosapi-controllerosapi.agent.count or osapi.target. A read of the agent registry
enrollment.list, .accept, .reject, .removeosapi-controllerosapi.agent.count or osapi.agent.hostname
audit.list, audit.get, audit.exportosapi-controllerosapi.audit.count or osapi.audit.id
traces.readosapi-controllerosapi.trace.id, osapi.trace.spans. The read of a trace from Jaeger
job.processosapi-agentosapi.agent.hostname, osapi.agent.machine_id, host.arch, job.status, job.changed
behavior.<name>.<method>osapi-agentosapi.behavior.name, osapi.behavior.method, osapi.behavior.outcome
exec <program>osapi-agentprocess.executable.name, osapi.exec.sudo, process.exit.code. The arguments are not recorded
file.write, file.remove, file.chmodosapi-agentfile.path, and file.mode and file.size where they apply. The content is not recorded
kv.<op>, objectstore.<op>, stream.<op>bothdb.operation.name, db.namespace (the bucket or the stream), nats.kv.key or nats.object.name
nats.publish, nats.processbothmessaging.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, agent or cli.
  • STATUS is the job status on a job.process, changed or unchanged on a behavior write, and ok or error on the other spans.
  • DETAIL is what the span acted on, as key=value pairs, so each value says what it is: host= on a job.process, the inputs that a behavior records, path= of a file, op= and target= of a job, size= of the answer. A failed span leads with error=. A value with a space is quoted, as in logfmt. --json gives each span's whole attributes map 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​

  1. Copy the trace ID from the summary line of the command.
  2. Put it in the search box at the top of Jaeger. Jaeger opens the trace.
  3. Each job.process shows its host in osapi.agent.hostname. A red span failed. Open it, and read the error in its status.
  4. Compare job.expected and job.answered on job.collect. When they are different, the job.timeout events 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 job
  • osapi.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:

  1. Run all three processes with --debug (or telemetry.tracing.enabled: true)
  2. Execute a command: osapi client node hostname
  3. Find the trace_id in any component's log output
  4. 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:

exporterBehavior
(unset)Log correlation only. trace_id is in the structured logs, but osapi exports no spans
stdoutRaw OpenTelemetry span JSON, printed directly to stdout without the structured logger. Useful to find a problem one time, but the output is large
otlpSpans 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.

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
Today, the OTLP exporter uses a gRPC connection without TLS and without

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.