Repository navigation
cli: add --trace to trace a command - #562
Conversation
099112e to
80cae1b
Compare
erain
left a comment
There was a problem hiding this comment.
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,activeTracestaysnil, creating noTracerProviderand sending notraceparentheader soax-servermakes its own sampling decision. - With
--trace,startCommandTracestarts anAlwaysSampleroot span (ax <cmd>), printsTrace ID: <id>tostderr, wrapstunnel.EnsureServerURLin aresolve ax-server addresschild span, and configuresotelgrpc.NewClientHandlerwithpropagation.TraceContext{}only (no baggage) andmetricnoop.NewMeterProvider(). envSpanExporterandresolveExportersmatch#557's exporter and resource rules while only exporting CLI spans when an OTLP endpoint is set.- Returning
exitCodeErrorfromrunSSHcleanly runsdefer cleanup()and flushes the command span before exiting with the remote exit code.
erain
left a comment
There was a problem hiding this comment.
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:
- Please rebase on main.
go.mod/go.sumconflict with #557, and after the rebase the only change there should be theotelgrpcline. - ax-server on main does not extract
traceparentyet (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. - Now that #557 is in, please reuse its exporter and resource rules instead of copying
resolveExportersintocmd/ax, so they can't drift. - 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, andservice.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.
80cae1b to
e91de8a
Compare
|
Changes since this review: rebased on
Smaller points:
Validation at c824970: |
c824970 to
7e5e919
Compare
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).
7e5e919 to
e5ed83c
Compare
|
@erain Please review again, thanks |
erain
left a comment
There was a problem hiding this comment.
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.
ax-server cannot join a slow or failed command to the client that issued it, because the CLI sends no trace context. With
--trace, anaxcommand starts a sampled root span, printsTrace ID: <id>to stderr, and sends a W3Ctraceparenton 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.--trace, the CLI creates no tracer and sends no trace context, so ax-server's own sampler decides for CLI traffic, as withkubectl ate.OTEL_EXPORTER_OTLP_ENDPOINTis 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 sshreturns the remote exit code instead of callingos.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_EXPORTERand the resource use ax-server: set up OpenTelemetry tracing, metrics, events, and JSON logs #557's rules, which move intointernal/telemetry/otelenvso that ax-server and the CLI share them. Importinginternal/telemetryinto the CLI would add about 1.3 MB of exporters.docs/observability.mdhas a CLI traces section.Pins
otel v1.45.0as #557 does, andotelgrpc v0.70.0as Substrate does. Server spans join these traces once ax-server adds theotelgrpcserver handler.Refs #13, #452.
Testing
go test -race ./...,go vet ./..., andgo mod tidyare clean. New tests incmd/ax/trace_test.goandinternal/telemetry/otelenvcover 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, andListTasksspans, and notraceparentwithout--trace.