Repository navigation
ax-server: set up OpenTelemetry tracing, metrics, events, and JSON logs - #557
Conversation
JeffLuoo
left a comment
There was a problem hiding this comment.
I compared this PR with the OpenTelemetry setup in Agent Substrate (internal/serverboot at 94e0a26a). Most of the SDK setup is the same: the versions, the resource, the default sampler, the propagator, the error handler, the batch processors, the JSON logs with trace IDs, and the separate metrics listener. The tests are good, and the log handler calls r.Clone() before it adds attributes, as the log/slog docs say.
This PR is part of #13, but later PRs will build on the configuration, the manifests, and the Providers API that it adds. Thus I recommend four changes before merge:
- Keep the 10% default when
OTEL_TRACES_SAMPLERhas an unknown value (telemetry.goL161). - Send metrics over OTLP, as #13 asks (
telemetry.goL179). - Put the
OTEL_*settings and the resource attributes in the manifests (deploy/ax-server.yaml). - Decide the default of the logs exporter, and a stdout fallback for events (
telemetry.goL202).
The other comments do not block the merge.
Validation at 2b1f206: go build ./cmd/ax-server, go vet and gofmt on the changed packages, and go mod tidy show no problems. go test -race -count=3 ./internal/telemetry passes. I did not run the e2e suite.
JeffLuoo
left a comment
There was a problem hiding this comment.
I checked b2ff786 against each reply. All 11 comments are fixed, and the four items that I asked to fix before merge are done. At b2ff786, go build, go vet ./..., gofmt, go mod tidy, and go test ./... pass, and go test -race -count=3 ./internal/telemetry passes too. I also tested the new behavior: a sampler typo keeps 10%, the OTLP metric push exports at each interval when nothing records (as the doc says), and an event goes to stdout when OTLP is off.
The two new comments do not block the merge.
A correction to the reply on the sampler: the SDK still parses OTEL_TRACES_SAMPLER in NewTracerProvider, before it applies WithSampler. With a typo, the sampled share is 10%, but the SDK also logs unsupported sampler through the error handler. Thus an operator sees two warnings, and the SDK warning does not say that ax-server kept the default. Substrate has the same second warning. No change is necessary.
ax-server had no telemetry: plain-text logs and nothing to scrape or collect. Set up the OpenTelemetry SDK in a new internal/telemetry package and wire it into ax-server. - Traces export over OTLP/gRPC. New traces are sampled at 10% (ParentBased(TraceIDRatio)), a caller's decision is followed, and only W3C trace context is propagated, not baggage. - A logger provider exports structured events over OTLP/gRPC. ax-server's own log lines stay on stdout. - Metrics are served for Prometheus on :9090/metrics (--metrics-addr), with Go and process metrics and a cap of 100 attribute sets per instrument. Probes and the API stay on :8080. - Logs are JSON, with trace_id and span_id when a span is active. - SDK errors go to slog instead of stderr. Exporters and sampling follow the standard OTEL_* environment variables. internal/axattr names the attributes AX emits, reusing Substrate's ate.atespace and ate.actor.uid so records join on them. The OTel versions match Agent Substrate's (otel v1.45.0, semconv v1.43.0). Nothing records spans or emits events yet; RPC instrumentation and task state events build on this.
…d events to stdout Address review on the OpenTelemetry setup: - Resolve OTEL_TRACES_SAMPLER and OTEL_*_EXPORTER in ax-server, as Agent Substrate does. An unknown or invalid value is logged and keeps the default, so a typo no longer samples every trace or turns export off. - Add an OTLP periodic metric reader. OTEL_METRICS_EXPORTER controls only the push; --metrics-addr still controls /metrics. - Default OTEL_LOGS_EXPORTER to none and accept console. Add telemetry.Emit, which writes an event to stdout when OTLP logs are off, or beside OTLP with console, so events are not dropped. - Keep the SDK's default cardinality limit (2000), so OTEL_GO_X_CARDINALITY_LIMIT applies again. - Add trace_flags to log lines. - Read OTEL_* settings from an ax-otel-config ConfigMap, and set the k8s.* resource attributes and service.instance.id from the downward API. The production manifest ships with OTLP push off; the E2E manifest points at the collector from Substrate's kind overlay. - List ate.atespace among the attributes kept off metrics.
b2ff786 to
345d8d5
Compare
ax-server has no telemetry. It writes plain-text logs and serves nothing to scrape or collect, so an operator can't see where a slow
CreateTaskspent its time, how often RPCs fail, or which log lines belong together. This PR adds the OpenTelemetry setup that RPC instrumentation and task state events will build on. Part of #13.The setup follows Agent Substrate's
internal/serverboot, including its versions (otel v1.45.0, semconv v1.43.0) and its rules for theOTEL_*variables. ax-server and Substrate telemetry therefore behave the same way and join on the same attributes.What ax-server now does
ParentBased(TraceIDRatio(0.1))), and a caller's sampling decision is followed.:9090/metrics.OTEL_METRICS_EXPORTERcontrols only the push:otlp(the default) is on;prometheusornoneis off. The new--metrics-addrflag controls only/metrics, and an empty value turns it off./metricsalso serves Go and process metrics. They are not pushed.OTEL_GO_X_CARDINALITY_LIMIToverrides it.telemetry.Emit. WithOTEL_LOGS_EXPORTERunset ornone(the default, as in Substrate), an event is written to stdout as a JSON log line. Withotlp, it is sent as an OTLP log record instead. Withotlp,console, it goes to both. An event is never dropped because OTLP is off.trace_id,span_id, andtrace_flags. Log lines are not exported over OTLP.Settings ax-server can't use. As the OpenTelemetry specification asks, an unknown or invalid value is logged and ignored, which keeps the default. ax-server resolves
OTEL_TRACES_SAMPLERitself, because the SDK falls back to sampling every trace. A typo such asparentbased_traceidratoi, or a ratio sampler whose argument is missing or out of range, keeps the 10% default. An unknown exporter name keeps the default exporter instead of turning export off.Manifests. Both manifests read the
OTEL_*settings from a newax-otel-configConfigMap withenvFrom. They also setOTEL_RESOURCE_ATTRIBUTESfrom the downward API:k8s.namespace.name,k8s.pod.name,k8s.pod.uid, andservice.instance.idset to the pod UID, as Substrate does.deploy/ax-server.yamlships with OTLP push off (OTEL_TRACES_EXPORTER: none,OTEL_METRICS_EXPORTER: prometheus). An install without a collector therefore logs no export errors and loses nothing: metrics stay on/metricsand events go to stdout. Connecting a collector means editing the ConfigMap;docs/observability.mdshows how.test/e2e/manifests/ax-server.yamlpoints at the collector that Substrate's kind overlay deploys (opentelemetry-collector.otel-system.svc:4317), withOTEL_LOGS_EXPORTER: otlp,console.9090is exposed asmetricson the Service; probes and the API stay on:8080.internal/axattrfixes the attribute names AX will put on spans and events. For actor identity it reuses Substrate'sate.atespaceandate.actor.uid, and it keeps those and the other identity attributes off metrics.docs/observability.mddocuments all of this for operators.Nothing records spans, metrics, or events yet; that starts with the RPC instrumentation and task state event PRs.
Behavior changes:
:9090.Validation
Unit tests:
go test ./internal/telemetry(also with-race -count=3) covers:ax-serverresource;OTEL_*_EXPORTER=nonesending nothing, while spans are still sampled and/metricsstill serves;otlp, and both withotlp,console;traceparent/tracestatepropagated;/metricsserving an instrument, Go and process metrics, and the resource; 300 attribute sets with no overflow under the default limit, and 100 series with the overflow series whenOTEL_GO_X_CARDINALITY_LIMIT=100;trace_id/span_id/trace_flagsonly when a span is active.go test ./...,go vet ./..., gofmt, andgo mod tidypass.Live cluster:
Nothing records data yet, so the collector received no data points. OTLP delivery of each signal is covered by the unit test against the in-process collector, not by the live run.
This change was written with help from an AI coding assistant.