Skip to content

feat(observability): OpenTelemetry tracing for the API and the sandbox manager (#489) - #494

Merged
ArthurCRodrigues merged 3 commits into
mainfrom
feat/489
Nov 21, 2026
Merged

ArthurCRodrigues merged 3 commits into
mainfrom
feat/489

Conversation

@jaoppb

@jaoppb jaoppb commented Nov 21, 2026

Copy link
Copy Markdown
Collaborator

Closes #489
Part of webtech-network/prisma-infrastructure#92

Stacked on #490 (#487, structured JSON logs). Review this PR's own commits; the base is feat/487.

What

OpenTelemetry tracing for autograder-api and autograder-sandbox-manager. Tracing is off unless OTEL_EXPORTER_OTLP_ENDPOINT is set (it is a no-op in tests). When on, each service exports OTLP over HTTP/protobuf, batched in a background thread. The standard OTEL_* variables apply; the service name defaults to SERVICE_NAME.

  • Propagation: FastAPI instrumentation in both services continues an incoming W3C traceparent. The requests-based sandbox client sends its own traceparent from client spans named by the control API's route template. Only W3C trace context is propagated; baggage is never read or forwarded.
  • API extras: SQLAlchemy statements (placeholders, never bound values) and the AI provider's httpx calls are traced. A client span never starts a trace, so the idle worker's queue polls produce none. Health, readiness and /metrics are not traced.
  • Redaction: no exported span carries a raw path, query string, URL, client address, or exception message or stack trace. That includes the status description, where the SDK puts "<type>: <message>". Server spans keep the route template; errors keep their type only.
  • Durable job correlation (migration 012): each submission stores the accepting request's X-Request-ID and its acceptance span's traceparent (when sampled). Each grading attempt is its own trace, rooted at grading.attempt, which links to that acceptance span and records the outcome status and published error code.
  • Logs: lines carry trace_id/span_id while a span context is active. During an attempt, request_id is the accepting request's id, plus an attempt_id field.
  • Dependencies: opentelemetry-api becomes a base dependency, because the Action image imports the sandbox client. The SDK, OTLP exporter and FastAPI instrumentation are in the sandbox extra; the SQLAlchemy and httpx instrumentations are in the server extra.

Changes that affect operators and log consumers

  • request_id semantics change (log consumers, INF-1 Alloy, INF-3 dashboards): [Observability][AG-1] Sanitize Logs & Emit Structured JSON Logs from autograder-sandbox-manager #487 used the attempt id as the worker's request_id. Now request_id groups a submission's acceptance and all its attempts. Isolate one attempt with attempt_id (API lines) or trace_id (each attempt is its own trace, different from the accepting request's).
  • Migration 012: run alembic -c web/alembic.ini upgrade head before starting this build. To roll back: stop the API, back up, run downgrade 011 with this image, then start the previous image. python -m web.database.schema adopt now expects 012's columns to be absent (ADDED_AFTER_ADOPTION).
  • INF-5: set OTEL_EXPORTER_OTLP_ENDPOINT=http://alloy:4318 and OTEL_RESOURCE_ATTRIBUTES=deployment.environment.name=<env>.

The release (1.1.0 version bump) is a separate PR after #487, #488 and #489 merge; no tag is cut here.

Verification

  • Unit and SQLite web tests: 2227 passed. PostgreSQL tier (tests/web -m postgres, including migrations and adoption): 35 passed.
  • make contracts and make docs (strict) pass. Pylint in a clean checkout: rc 0.
  • New tests: tests/unit/sandbox/test_tracing.py and tests/web/test_tracing.py (propagation, redaction including status, sampling, probes, httpx, DB spans without values, attempt links and retries, create_app wiring), the migration 012 test, and a suite-wide guard (tests/conftest.py) that keeps tracing off unless a test turns it on.
  • Multi-lens review (code-quality, docs-quality, security, api-docs, data, observability, testing, devops, performance) approved in round 3.

🤖 Generated with Claude Code

@jaoppb
jaoppb added this pull request to stack #495 November 21, 2026 20:45
Copilot AI balanced review requested due to automatic review settings November 21, 2026 22:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.

Base automatically changed from feat/487 to main November 21, 2026 23:07
…x manager (#489)

Tracing is off unless OTEL_EXPORTER_OTLP_ENDPOINT is set; then each service
exports OTLP/HTTP spans batched in the background, configured by the
standard OTEL_* variables (OTEL_SERVICE_NAME defaults to SERVICE_NAME,
sampler parentbased_always_on).

- FastAPI instrumentation in both services continues an incoming W3C
  traceparent; the sandbox client sends its own (client spans named by the
  control API's route template). The API also traces SQLAlchemy statements
  (placeholders only) and the AI provider's httpx calls. A client span never
  starts a trace, so the idle worker's polls produce none. Probes are not
  traced.
- Spans never carry raw paths, query strings, URLs, client addresses or
  exception messages: a redacting exporter keeps the route template and the
  exception type.
- Migration 012 stores the accepting request's X-Request-ID and its span's
  traceparent on the submission. Each grading attempt is its own trace,
  rooted at grading.attempt and linked to that span; its log lines carry
  the accepting request id plus attempt_id. Adoption of create_all
  databases expects 012's columns absent.
- Log lines carry trace_id/span_id while a span is active.
- opentelemetry-api becomes a base dependency (the sandbox client
  propagates context); the SDK, exporter and instrumentations live in the
  sandbox and server extras.
…ew (#489)

- Spans no longer export error messages through the status description
  (SDK, FastAPI and SQLAlchemy put "<type>: <message>" there): the
  redactor keeps the status code only, and the attempt and sandbox client
  spans record errors by type. Spans that need no redaction are exported
  unchanged.
- Only W3C trace context is propagated (never baggage), so a caller's
  baggage cannot reach the AI provider.
- A sandbox 4xx is a handled answer, not a failed span; grading.attempt
  records its outcome status and published error code.
- Both lifespans export the remaining batched spans after the drain.
- service.name from OTEL_RESOURCE_ATTRIBUTES is honoured; OTEL_SERVICE_NAME
  is read by the SDK only.
- Module-level sampler and exporter classes, one tracer per provider, one
  unmatched-route constant, adoption reports unexpected columns as text.
- Tests: tracing is off suite-wide unless a test turns it on, the
  create_app wiring, API probes, httpx spans, sampler overrides, unsampled
  and invalid caller contexts, retries, every control route's template,
  migration 012 on existing rows.
- Docs: one Logs and traces section for both services, what request_id
  now groups for log consumers, the 012 rollout order.
…wnership (#489)

- Rolling back runs downgrade 011 with this release's image before the
  previous image starts: the previous image neither knows revision 012
  nor serves a database past its head.
- record_attempt_outcome lives with the API's tracing; the API lifespan
  calls shutdown_api_tracing, which also removes the process-wide httpx and
  SQLAlchemy instrumentation.
- The redactor always copies: one set of rules, no parallel predicate.
- The tracing_setup docstring names service.name in
  OTEL_RESOURCE_ATTRIBUTES and the tracecontext-only propagation.
@ArthurCRodrigues
ArthurCRodrigues merged commit a809995 into main Nov 21, 2026
8 checks passed
@ArthurCRodrigues
ArthurCRodrigues deleted the feat/489 branch November 21, 2026 23:32
ArthurCRodrigues added a commit that referenced this pull request Nov 21, 2026
Conflicts with the tracing change (#494): CHANGELOG.md (both entries),
pyproject.toml and uv.lock (both dependency sets, lock regenerated with
uv lock), sandbox_manager/api.py (tracing instrumentation, then the metrics
middleware), web/service/worker.py (both recordings of the attempt outcome).

tests/unit/sandbox/test_tracing.py: the control API now has GET /metrics,
which the remote client never calls, so the route template check skips it
like /health.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
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.

[Observability][AG-3] Instrument autograder-api & autograder-sandbox-manager with OpenTelemetry + Cut Release

3 participants