Skip to content

cli: add --trace to trace a command - #562

Merged
JeffLuoo merged 3 commits into
google:mainfrom
JeffLuoo:cli-trace-propagation
Nov 12, 2026
Merged

JeffLuoo merged 3 commits into
google:mainfrom
JeffLuoo:cli-trace-propagation

Conversation

@JeffLuoo

@JeffLuoo JeffLuoo commented Nov 9, 2026 •

Copy link
Copy Markdown
Collaborator

ax-server cannot join a slow or failed command to the client that issued it, because the CLI sends no trace context. With --trace, an ax command starts a sampled root span, prints Trace ID: <id> to stderr, and sends a W3C traceparent on every AX RPC. ax-server (#557) and Substrate sample ParentBased. When ax-server instruments its RPC handlers and its Substrate client, they follow this decision and record the whole request.

  • Without --trace, the CLI creates no tracer and sends no trace context, so ax-server's own sampler decides for CLI traffic, as with kubectl ate.
  • RPCs are client spans under the command span, and resolving the ax-server address (which may start a port-forward) is a child span. CLI spans are exported over OTLP only when OTEL_EXPORTER_OTLP_ENDPOINT is set, in one call when the command ends, with no retry and a 2 s limit. A failed export prints a warning that names the endpoint and gives the error.
  • ax ssh returns the remote exit code instead of calling os.Exit, so the command span ends and the port-forward is cleaned up. The double Ctrl-C exit waits at most 200 ms for the export.
  • OTEL_TRACES_EXPORTER and the resource use ax-server: set up OpenTelemetry tracing, metrics, events, and JSON logs #557's rules, which move into internal/telemetry/otelenv so that ax-server and the CLI share them. Importing internal/telemetry into the CLI would add about 1.3 MB of exporters.
  • docs/observability.md has a CLI traces section.

Pins otel v1.45.0 as #557 does, and otelgrpc v0.70.0 as Substrate does. Server spans join these traces once ax-server adds the otelgrpc server handler.

Refs #13, #452.

Testing

  • go test -race ./..., go vet ./..., and go mod tidy are clean. New tests in cmd/ax/trace_test.go and internal/telemetry/otelenv cover the header with and without --trace, span parents, resource attributes, export and SDK warnings, streaming RPCs, the exporter and resource rules, one export at the end, the export limits, and exit codes. A smoke test of the built binary against a local OTLP receiver and a fake AX server showed one export with the command, resolve, and ListTasks spans, and no traceparent without --trace.

@JeffLuoo
JeffLuoo requested review from erain and rakyll November 9, 2026 19:37
@JeffLuoo
JeffLuoo force-pushed the cli-trace-propagation branch from 099112e to 80cae1b Compare November 9, 2026 19:45

@erain erain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Verified at 80cae1b: go build ./cmd/..., go vet ./..., gofmt, go mod tidy, and go test -race -count=3 ./cmd/ax/... all pass cleanly.

  • Without --trace, activeTrace stays nil, creating no TracerProvider and sending no traceparent header so ax-server makes its own sampling decision.
  • With --trace, startCommandTrace starts an AlwaysSample root span (ax <cmd>), prints Trace ID: <id> to stderr, wraps tunnel.EnsureServerURL in a resolve ax-server address child span, and configures otelgrpc.NewClientHandler with propagation.TraceContext{} only (no baggage) and metricnoop.NewMeterProvider().
  • envSpanExporter and resolveExporters match #557's exporter and resource rules while only exporting CLI spans when an OTLP endpoint is set.
  • Returning exitCodeError from runSSH cleanly runs defer cleanup() and flushes the command span before exiting with the remote exit code.

@erain erain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, the CLI side looks solid: the command span ends on every exit path, nothing changes without --trace, and ax ssh now cleans up properly. Build and tests pass on the PR head and after a local rebase.

Before merge:

  1. Please rebase on main. go.mod/go.sum conflict with #557, and after the rebase the only change there should be the otelgrpc line.
  2. ax-server on main does not extract traceparent yet (no otelgrpc server handler), so "ax-server records them" in the README is not true today. Either add the server handler here or reword the docs with a TODO.
  3. Now that #557 is in, please reuse its exporter and resource rules instead of copying resolveExporters into cmd/ax, so they can't drift.
  4. Please add a short CLI section to docs/observability.md: export only with an OTLP endpoint set (unlike ax-server), the 2 s flush limit, and service.name=ax.

Smaller points: with an unreachable collector the command waits 2 s and prints "context deadline exceeded" without the endpoint. Naming it, and printing the real error first, would help. The double Ctrl-C path in ax ssh could shorten or skip the flush, and a test for runSSH returning the remote exit code would be good.

@JeffLuoo JeffLuoo changed the title cli: trace each command and send traceparent on its RPCs cli: add --trace to trace a command Nov 10, 2026
@JeffLuoo
JeffLuoo force-pushed the cli-trace-propagation branch from 80cae1b to e91de8a Compare November 10, 2026 20:44
@JeffLuoo
JeffLuoo requested a review from erain November 10, 2026 22:05
@JeffLuoo

Copy link
Copy Markdown
Collaborator Author

Changes since this review: rebased on main, then 18b7a4e and c824970.

  1. Rebased on main. go.mod/go.sum add only otelgrpc v0.70.0.
  2. The README row and the --trace usage line no longer say that ax-server records the RPCs. The new CLI section in docs/observability.md says that ax-server does not read traceparent yet, and that a later change for Add OpenTelemetry exposition #13 adds the otelgrpc server handler. A TODO(#13) comment marks the paragraph.
  3. 18b7a4e moves the exporter and resource rules into internal/telemetry/otelenv, and ax-server and the CLI both use it. I did not import internal/telemetry into the CLI, because that adds about 1.3 MB (the metric and log exporters and the Prometheus client). ax-server keeps its behavior; TestResolveExporters moved with the code.
  4. docs/observability.md has a CLI traces section: --trace only, export only with an OTLP endpoint set (unlike ax-server), the 2 s limit, and service.name=ax.

Smaller points:

  • Export failure: the CLI keeps the ended spans and exports them in one call when the command ends, with retries off. The exporter's first retry comes after 5 s, later than the 2 s limit, so retrying only hid the real error behind a timeout. An unreachable collector now fails in about 0.01 s with one warning: Warning: could not export the command trace to http://127.0.0.1:<port>: traces export: rpc error: code = Unavailable desc = connection error: ... connect: connection refused.
  • Double Ctrl-C: that exit waits at most 200 ms for the export.
  • runSSH exit-code test: not in this PR. runSSH has no seam for the guest connection, so a test needs a new interface and a package variable in main.go. TestFinish covers the mapping from exitCodeError to the exit code. I can add the test in a follow-up.

Validation at c824970: go vet ./..., go mod tidy, and go test -race ./... (18 packages) pass. A smoke test of the built binary against a local OTLP receiver and a fake AX server showed one export with the command, resolve, and ListTasks spans, and no traceparent without --trace.

@JeffLuoo
JeffLuoo force-pushed the cli-trace-propagation branch from c824970 to 7e5e919 Compare November 11, 2026 14:54
ax-server cannot join a slow or failed command to the client that issued
it, because the CLI sends no trace context. With --trace, a command now
starts a sampled root span, prints its trace ID to stderr, and sends a
W3C traceparent on every AX RPC. ax-server and Substrate sample
ParentBased. When ax-server instruments its RPC handlers and its
Substrate client, they follow this decision and record the whole
request.

- Without --trace the CLI creates no tracer and sends no trace context,
  so ax-server keeps the sampling decision for CLI traffic.
- Each RPC is a client span under the command span, and resolving the
  ax-server address (which may start a port-forward) is a child span.
  Only traceparent is propagated, never baggage.
- CLI spans are exported over OTLP only when an OTLP endpoint is set.
  OTEL_TRACES_EXPORTER follows ax-server's rules, and the resource reads
  OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES. Export waits at most
  2 s and warns on failure; OpenTelemetry errors print as warnings.
- ax ssh returns the remote exit code instead of exiting the process,
  so the command span ends and its port-forward is cleaned up.
The ax CLI needs the same OTEL_TRACES_EXPORTER and resource rules as
ax-server. Importing internal/telemetry would link ax-server's metric
and log exporters and the Prometheus client into the CLI, which adds
about 1.3 MB. Move the rules into internal/telemetry/otelenv, which
depends only on the OpenTelemetry API and SDK:

- ResolveExporters and the Exporters set, moved without a change in
  behavior, with their tests.
- Resource, the rules of newResource: service.name, the SDK attributes,
  and OTEL_SERVICE_NAME and OTEL_RESOURCE_ATTRIBUTES on top. A partial
  resource is still returned, with the error to warn about.

ax-server keeps its behavior. TestResolveExporters moves to otelenv,
and the other ax-server tests pass without changes.
Review follow-ups for --trace:

- OTEL_TRACES_EXPORTER and the resource use otelenv, the rules
  ax-server uses, instead of a copy.
- The CLI keeps the ended spans and exports them in one call when the
  command ends, with retries off. The exporter's first retry comes after
  5 s, later than the 2 s the CLI waits, so retrying only hid the real
  error behind a timeout. An unreachable collector now fails at once,
  and the warning names the endpoint and gives the error.
- The double Ctrl-C exit of ax ssh waits at most 200 ms for the export.
- docs/observability.md has a CLI traces section: --trace only, export
  only with an OTLP endpoint set, the 2 s limit, service.name=ax, and a
  note that ax-server does not read traceparent yet (google#13).
@JeffLuoo
JeffLuoo force-pushed the cli-trace-propagation branch from 7e5e919 to e5ed83c Compare November 12, 2026 16:29
@JeffLuoo

Copy link
Copy Markdown
Collaborator Author

@erain Please review again, thanks

@erain erain self-assigned this Nov 12, 2026

@erain erain left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed again at e5ed83c. The previous blocking points are addressed: exporter and resource rules are shared through otelenv, the dependency diff is limited to otelgrpc, and the docs explain the CLI export behavior and pending server instrumentation. No blocking findings.

Local validation passed: make verify (0 issues, clean diff), go build ./cmd/ax ./cmd/ax-server ./cmd/ax-task-runner, go test -race -count=1 ./..., and go vet ./.... Postgres and E2E opt-in tiers were not enabled locally; the current PR build-and-test and E2E checks are green.

@JeffLuoo
JeffLuoo merged commit 77dec31 into google:main Nov 12, 2026
10 checks passed
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.

2 participants