Skip to content

ax-server: set up OpenTelemetry tracing, metrics, events, and JSON logs - #557

Merged
erain merged 2 commits into
google:mainfrom
erain:otel-bootstrap
Nov 10, 2026
Merged

erain merged 2 commits into
google:mainfrom
erain:otel-bootstrap

Conversation

@erain

@erain erain commented Nov 5, 2026 •

Copy link
Copy Markdown
Collaborator

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 CreateTask spent 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 the OTEL_* variables. ax-server and Substrate telemetry therefore behave the same way and join on the same attributes.

What ax-server now does

  • Traces export over OTLP/gRPC.
    • Sampling: 10% of new traces (ParentBased(TraceIDRatio(0.1))), and a caller's sampling decision is followed.
    • Propagation: only W3C trace context; incoming baggage is not trusted.
  • Metrics are pushed over OTLP, and also served for Prometheus on :9090/metrics.
    • OTEL_METRICS_EXPORTER controls only the push: otlp (the default) is on; prometheus or none is off. The new --metrics-addr flag controls only /metrics, and an empty value turns it off.
    • /metrics also serves Go and process metrics. They are not pushed.
    • The SDK's default cardinality limit applies (2000 attribute sets per instrument); OTEL_GO_X_CARDINALITY_LIMIT overrides it.
  • Events go through the new telemetry.Emit. With OTEL_LOGS_EXPORTER unset or none (the default, as in Substrate), an event is written to stdout as a JSON log line. With otlp, it is sent as an OTLP log record instead. With otlp,console, it goes to both. An event is never dropped because OTLP is off.
  • Logs are now JSON on stdout. A line logged while a span is active carries trace_id, span_id, and trace_flags. Log lines are not exported over OTLP.
  • SDK errors are logged through slog instead of stderr.

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_SAMPLER itself, because the SDK falls back to sampling every trace. A typo such as parentbased_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 new ax-otel-config ConfigMap with envFrom. They also set OTEL_RESOURCE_ATTRIBUTES from the downward API: k8s.namespace.name, k8s.pod.name, k8s.pod.uid, and service.instance.id set to the pod UID, as Substrate does.

  • deploy/ax-server.yaml ships 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 /metrics and events go to stdout. Connecting a collector means editing the ConfigMap; docs/observability.md shows how.
  • test/e2e/manifests/ax-server.yaml points at the collector that Substrate's kind overlay deploys (opentelemetry-collector.otel-system.svc:4317), with OTEL_LOGS_EXPORTER: otlp,console.
  • Port 9090 is exposed as metrics on the Service; probes and the API stay on :8080.

internal/axattr fixes the attribute names AX will put on spans and events. For actor identity it reuses Substrate's ate.atespace and ate.actor.uid, and it keeps those and the other identity attributes off metrics. docs/observability.md documents 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:

  • ax-server's logs change from slog text to JSON.
  • ax-server also listens on :9090.

Validation

  • Unit tests: go test ./internal/telemetry (also with -race -count=3) covers:

    • spans, a metric, and an event delivered over OTLP to an in-process gRPC collector, with the ax-server resource;
    • OTEL_*_EXPORTER=none sending nothing, while spans are still sampled and /metrics still serves;
    • events written to stdout by default, OTLP only with otlp, and both with otlp,console;
    • the default sampler: about 10% of 4,000 root spans sampled, and children follow a sampled or unsampled parent;
    • invalid sampler settings (a typo, a missing ratio, a ratio of 10) keeping the 10% default, plus a table of sampler and exporter values;
    • only traceparent/tracestate propagated;
    • /metrics serving 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 when OTEL_GO_X_CARDINALITY_LIMIT=100;
    • the log handler adding trace_id/span_id/trace_flags only when a span is active.

    go test ./..., go vet ./..., gofmt, and go mod tidy pass.

  • Live cluster:

    • Setup: a GKE 1.37 cluster with Agent Substrate v1.0.0-rc.1 and a gVisor worker pool, running ax-server from this branch with the E2E manifest. The ConfigMap pointed it at an OpenTelemetry Collector with a debug exporter, with a 10 s metric push interval.
    • Result: the full E2E suite passed (8/8). All 86 log lines ax-server wrote during the run were JSON, with no SDK errors or warnings, while it pushed metrics every 10 s. The downward-API resource reached the SDK:
    {"time":"2026-11-06T21:14:02.543509374Z","level":"INFO","msg":"tracing initialized","export":true}
    {"time":"2026-11-06T21:14:02.543842658Z","level":"INFO","msg":"metrics initialized","export":true}
    {"time":"2026-11-06T21:14:02.544556911Z","level":"INFO","msg":"OTLP logs export initialized","exporter":"console,otlp"}
    
    $ curl -s localhost:9090/metrics | grep ^target_info
    target_info{k8s_namespace_name="ax-system",k8s_pod_name="ax-server-5cf64fb5f-lzm9g",k8s_pod_uid="7918ee31-…",service_instance_id="7918ee31-…",service_name="ax-server",telemetry_sdk_language="go",telemetry_sdk_name="opentelemetry",telemetry_sdk_version="1.45.0"} 1
    

    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.

@erain
erain requested a review from JeffLuoo November 5, 2026 21:28
@erain
erain marked this pull request as ready for review November 5, 2026 21:29

@JeffLuoo JeffLuoo 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.

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:

  1. Keep the 10% default when OTEL_TRACES_SAMPLER has an unknown value (telemetry.go L161).
  2. Send metrics over OTLP, as #13 asks (telemetry.go L179).
  3. Put the OTEL_* settings and the resource attributes in the manifests (deploy/ax-server.yaml).
  4. Decide the default of the logs exporter, and a stdout fallback for events (telemetry.go L202).

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.

Comment thread internal/telemetry/telemetry.go Resolved
Comment thread docs/observability.md Resolved
Comment thread internal/telemetry/telemetry.go Resolved
Comment thread deploy/ax-server.yaml Resolved
Comment thread test/e2e/manifests/ax-server.yaml Resolved
Comment thread internal/telemetry/telemetry.go Resolved
Comment thread internal/telemetry/telemetry.go Resolved
Comment thread internal/telemetry/log.go Resolved
Comment thread internal/axattr/axattr.go Resolved
Comment thread internal/telemetry/telemetry_test.go Resolved

@JeffLuoo JeffLuoo 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.

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.

Comment thread docs/observability.md Resolved
Comment thread internal/telemetry/event.go Resolved

@JeffLuoo JeffLuoo 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.

Please also rebase

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.
@erain
erain merged commit dcdfd0a into google:main Nov 10, 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