Skip to content

Python adapter: report test runs as environment test, and detect the running commit and release - #56

Merged
sagnik11 merged 3 commits into
mainfrom
feat/python-test-env-and-commit
Nov 15, 2026
Merged

sagnik11 merged 3 commits into
mainfrom
feat/python-test-env-and-commit

Conversation

@sagnik11

@sagnik11 sagnik11 commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

What users saw

  • A Python service's test suite (pytest, Django's manage.py test) that ran with Autter tracing on, and with no environment configured, reported its errors and request counts as production. Test failures showed up next to real production issues.
  • Requests made through Starlette's or FastAPI's TestClient carried nothing that said "test client": the user agent testclient that would have told is dropped by the adapter's privacy filter (since 1.19.0) before the spans leave the process, so the ingester cannot see it either.
  • Python events carried no commit, so the platform could not link a Runtime issue to the commit and pull request that was running. A service that set no release showed no release at all.

Cause

init_tracing() used Resource.create() as it was: nothing detected a test run, the running commit or a release.

What changes (all in adapters/python/autter_tracing.py)

init_tracing() fills in resource attributes the service did not set. An explicit value always wins, from OTEL_RESOURCE_ATTRIBUTES or a passed resource=.

  • Test runs. Under pytest (PYTEST_VERSION or PYTEST_CURRENT_TEST set, or pytest imported) or Django's manage.py test, a resource with neither deployment.environment.name nor deployment.environment gets deployment.environment=test. One info line is logged when this applies. Turn it off with AUTTER_DETECT_TEST_ENV=false or init_tracing(detect_test_env=False).
  • Test-client requests. RedactingSpanExporter adds autter.test_client=true to SERVER spans whose raw user agent (http.user_agent / user_agent.original) is exactly testclient, or whose host (server.address, net.host.name, http.host, http.server_name) is exactly testserver. It reads them before the identifying-attribute drop, and the user agent is still dropped. The attribute keys come from real recordings: FastAPI TestClient with the ASGI instrumentation in the old, stable and dup HTTP conventions, and Django's test Client. Limit: in the stable HTTP conventions, Django's instrumentation records no host, so a Django test-client request is not marked there. The manage.py test environment detection still covers that case. The mark is a hint for people reading the trace only: a caller can send any user agent or host, so nothing (alerting, paging, the environment) may act on it. The environment is decided from the process alone (pytest, manage.py test), never from a request.
  • Commit. deployment.commit is set from the first of AUTTER_COMMIT, GITHUB_SHA, VERCEL_GIT_COMMIT_SHA, CF_PAGES_COMMIT_SHA, GIT_SHA, GIT_COMMIT, COMMIT_SHA, SOURCE_COMMIT, RENDER_GIT_COMMIT, RAILWAY_GIT_COMMIT_SHA, HEROKU_SLUG_COMMIT, SOURCE_VERSION (the order runtime-node and the source-map CLI use) that holds 7 to 40 hexadecimal characters (lower-cased). An invalid value is skipped and the next variable is tried. Only environment variables are read: no git, no subprocess.
  • Release. When service.version is absent, it is set to AUTTER_RELEASE, else to the commit (the full SHA). If an explicit deployment.commit is set, the release defaults to that commit. Turn this off with AUTTER_DETECT_RELEASE=false or init_tracing(detect_release=False). Behaviour change: a service with no release now reports its commit as the release.
  • Request counts. All of this is applied before the request-count meter provider's resource is derived, so test traffic's request counts are also stored as test and carry the same commit and release.

New public names: TEST_CLIENT_ATTRIBUTE, COMMIT_ENV_VARS, detect_commit(), detect_test_runner(). New init_tracing() keyword arguments: detect_test_env, detect_release.

Tests: new adapters/python/test_deployment_detection.py covers runner detection and near misses, explicit-environment precedence, both opt-outs for each feature, commit variable order and hex validation, release precedence, metric resource parity, the test-client mark and its near misses, and a real child pytest run. Every test there sets the variables it needs and clears the rest, so the suite gives the same results under unittest, under pytest, and on CI where GITHUB_SHA is set. pytest is added to requirements-test.txt for the child-run check. The suite itself still runs under unittest and never imports pytest in its own process. Run locally on opentelemetry-sdk 1.45 / Python 3.14 and 1.20 / Python 3.9, under unittest and pytest, and with the stdlib only.

Docs and changelog are left for the docs PR (RT-E).

🤖 Generated with Claude Code


Autofix with [code]smith
Open in [code]smith · Need help on this PR? Tag @codesmith-bot with what you need.

sagnik11 and others added 3 commits November 15, 2026 10:46
…ing commit and release

init_tracing() now fills in resource attributes the service did not set:

- deployment.environment=test under pytest or Django's `manage.py test`,
  only when the resource has neither deployment.environment.name nor
  deployment.environment. Off with AUTTER_DETECT_TEST_ENV=false or
  init_tracing(detect_test_env=False). Logs one info line when it applies.
- deployment.commit from the first of AUTTER_COMMIT, GIT_SHA, GIT_COMMIT,
  COMMIT_SHA, SOURCE_COMMIT, VERCEL_GIT_COMMIT_SHA, GITHUB_SHA,
  RENDER_GIT_COMMIT, RAILWAY_GIT_COMMIT_SHA, CF_PAGES_COMMIT_SHA,
  HEROKU_SLUG_COMMIT, SOURCE_VERSION holding 7-40 hex characters.
  Environment variables only, never git.
- service.version, when absent, from AUTTER_RELEASE, else the full commit.
  Off with AUTTER_DETECT_RELEASE=false or init_tracing(detect_release=False).

Applied before the request-count meter provider's resource is derived, so
request counts carry the same environment, commit and release as spans.

RedactingSpanExporter marks SERVER spans whose raw user agent is exactly
`testclient` or whose host is exactly `testserver` with
autter.test_client=true, read before the user agent is dropped.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…int only

Commit variables are now tried in the order runtime-node and the
source-map CLI use: AUTTER_COMMIT, GITHUB_SHA, VERCEL_GIT_COMMIT_SHA,
CF_PAGES_COMMIT_SHA, GIT_SHA, GIT_COMMIT, COMMIT_SHA, SOURCE_COMMIT,
RENDER_GIT_COMMIT, RAILWAY_GIT_COMMIT_SHA, HEROKU_SLUG_COMMIT,
SOURCE_VERSION. Same rules: trimmed, 7-40 hex, invalid values skipped.

The autter.test_client comment now says it is a hint that nothing may act
on (a caller can send any user agent or host); the environment comes from
the process alone. A test checks a test-client request outside a test run
leaves the environment unset.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@sagnik11
sagnik11 merged commit 1d29bb7 into main Nov 15, 2026
18 checks passed
sagnik11 added a commit that referenced this pull request Nov 15, 2026
…ommit detection, test environments and queue helpers (#57)

- @autter/runtime-next/client: initAutterBrowser turns on same-origin
  trace propagation by default (tracePropagation: false opts out;
  tracePropagationTargets adds cross-origin APIs). Third-party origins and
  the tracker's own endpoint never get a traceparent. Does nothing outside a
  browser. An enableTracePropagation/disableTracePropagation call made before
  initAutterBrowser keeps its effect. Plain @autter/runtime-browser stays
  opt-in; its dist is unchanged.
- runtime-cli: `sourcemaps setup` prints the trace propagation step (with
  the CORS note) for browser setups.
- runtime-node: log records' resource carries deployment.commit, like the
  trace resource, for initAutterServer and initAutterLogging. Added in the
  OTLP log sink, so runtime-core's encoder (shared with runtime-edge) and
  runtime-edge's dist stay unchanged.
- Docs: Getting started (release/commit defaults, enableTracePropagation,
  test runs), Integrations (FastAPI and Django CORS, Python detection and
  opt-outs), Requests and errors, Architecture (sink `commit`), package
  READMEs. CHANGELOG [Unreleased] for #53-#56 and this change.

Co-authored-by: Claude Opus 5.5 <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.

1 participant