From 06e14870bbdc0aec4936df49f74419f40b054e2a Mon Sep 17 00:00:00 2001 From: Sagnik Ghosh Date: Sun, 15 Nov 2026 10:46:41 +0530 Subject: [PATCH 1/2] Python adapter: report test runs as environment test, detect the running 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 --- adapters/python/autter_tracing.py | 166 ++++++- adapters/python/requirements-test.txt | 9 +- adapters/python/test_deployment_detection.py | 457 +++++++++++++++++++ 3 files changed, 630 insertions(+), 2 deletions(-) create mode 100644 adapters/python/test_deployment_detection.py diff --git a/adapters/python/autter_tracing.py b/adapters/python/autter_tracing.py index 406bdc0..aac001d 100644 --- a/adapters/python/autter_tracing.py +++ b/adapters/python/autter_tracing.py @@ -53,6 +53,26 @@ spans. :func:`init_tracing` registers its meter provider as the global one: an app that registers its own afterwards should turn counting off. +Test runs, commit and release. :func:`init_tracing` fills in three resource +attributes the service did not set itself (an explicit value always wins): + +* ``deployment.environment = test`` when the process runs under a test + runner (pytest, or Django's ``manage.py test``) and the resource has no + environment at all (neither ``deployment.environment.name`` nor + ``deployment.environment``). Off with ``AUTTER_DETECT_TEST_ENV=false`` or + ``init_tracing(detect_test_env=False)``. A request made through + Starlette's or FastAPI's ``TestClient`` (user agent ``testclient``, host + ``testserver``), or through Django's test ``Client`` where the + instrumentation records its host ``testserver`` (old HTTP semantic + conventions), is marked ``autter.test_client = true`` on its server span, + whatever the environment. +* ``deployment.commit``: the running commit, from the first of + :data:`COMMIT_ENV_VARS` holding 7 to 40 hexadecimal characters + (environment variables only: no git, no subprocess). +* ``service.version`` (the release) when it is absent: ``AUTTER_RELEASE``, + else the detected commit, in full. Off with ``AUTTER_DETECT_RELEASE=false`` + or ``init_tracing(detect_release=False)``. + Copy this file and ``redact.py`` into your project. Needs ``opentelemetry-sdk`` (tested with 1.20, 1.27 and 1.41 on Python 3.9, 1.45 on Python 3.14) and, for :func:`init_tracing`'s default exporter, ``opentelemetry-exporter-otlp-proto-http``. @@ -62,6 +82,8 @@ import logging import os +import re +import sys import threading import time import weakref @@ -127,7 +149,11 @@ __all__ = [ "TAIL_RETAINED_ATTRIBUTE", "METRIC_COUNTED_ATTRIBUTE", + "TEST_CLIENT_ATTRIBUTE", + "COMMIT_ENV_VARS", "REQUEST_COUNT_RESOURCE_ATTRIBUTES", + "detect_commit", + "detect_test_runner", "CountRequests", "request_meter_provider", "RecordUnsampled", @@ -159,6 +185,43 @@ _REQUEST_INSTRUMENT = "autter.http.server.duration" _HTTP_SERVER_DURATIONS = ("http.server.duration", "http.server.request.duration") +#: On a server span of a request made by a test client: Starlette's and +#: FastAPI's ``TestClient`` (user agent ``testclient``, host ``testserver``) +#: or Django's test ``Client`` (host ``testserver``). Read from the raw +#: attributes, before the user agent is dropped. Same key as the ingester's. +TEST_CLIENT_ATTRIBUTE = "autter.test_client" +_TEST_CLIENT_USER_AGENT = "testclient" +_TEST_CLIENT_HOST = "testserver" +_USER_AGENT_KEYS = ("http.user_agent", "user_agent.original") +_HOST_KEYS = ("server.address", "net.host.name", "http.host", "http.server_name") + +_ENVIRONMENT_KEYS = ("deployment.environment.name", "deployment.environment") +_TEST_ENVIRONMENT = "test" +DETECT_TEST_ENV_ENV = "AUTTER_DETECT_TEST_ENV" + +#: Environment variables that carry the running commit, in the order they are +#: tried (the list @autter/runtime-node uses). The first one holding 7 to 40 +#: hexadecimal characters is the commit; other values are ignored. +COMMIT_ENV_VARS = ( + "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", +) +_COMMIT_RE = re.compile(r"[0-9a-fA-F]{7,40}") +_COMMIT_ATTRIBUTE = "deployment.commit" +_RELEASE_ATTRIBUTE = "service.version" +RELEASE_ENV = "AUTTER_RELEASE" +DETECT_RELEASE_ENV = "AUTTER_DETECT_RELEASE" + MAX_SPANS_PER_TRACE = 256 MAX_BUFFERED_SPANS = 5_000 TRACE_TTL_SECONDS = 30.0 @@ -550,6 +613,86 @@ def _env_flag(name: str, default: bool = False) -> bool: return default +def _from_test_client(span: ReadableSpan) -> bool: + """A server span whose raw user agent is exactly ``testclient`` or whose + host is exactly ``testserver``: a request a test client made.""" + if span.kind is not SpanKind.SERVER: + return False + attributes = span.attributes or {} + return any(attributes.get(key) == _TEST_CLIENT_USER_AGENT for key in _USER_AGENT_KEYS) or any( + attributes.get(key) == _TEST_CLIENT_HOST for key in _HOST_KEYS + ) + + +def detect_test_runner( + environ: Optional[Any] = None, modules: Optional[Any] = None, argv: Optional[Sequence[str]] = None +) -> Optional[str]: + """The test runner this process runs under, or ``None``: ``"pytest"`` + (``PYTEST_VERSION`` or ``PYTEST_CURRENT_TEST`` set, or ``pytest`` + imported) or ``"manage.py test"`` (Django's test command). Defaults to + ``os.environ``, ``sys.modules`` and ``sys.argv``.""" + environ = os.environ if environ is None else environ + modules = sys.modules if modules is None else modules + argv = sys.argv if argv is None else argv + if environ.get("PYTEST_VERSION") or environ.get("PYTEST_CURRENT_TEST") or "pytest" in modules: + return "pytest" + if len(argv) >= 2 and os.path.basename(str(argv[0])) == "manage.py" and argv[1] == "test": + return "manage.py test" + return None + + +def detect_commit(environ: Optional[Any] = None) -> Optional[str]: + """The running commit (lower case): the first of :data:`COMMIT_ENV_VARS` + whose value is 7 to 40 hexadecimal characters, or ``None``. Reads + environment variables only (default ``os.environ``): never git.""" + environ = os.environ if environ is None else environ + for name in COMMIT_ENV_VARS: + value = (environ.get(name) or "").strip() + if _COMMIT_RE.fullmatch(value): + return value.lower() + return None + + +def _has_value(attributes: Any, key: str) -> bool: + value = attributes.get(key) + return value is not None and value != "" + + +def _with_deployment(resource: Resource, detect_test_env: Optional[bool], detect_release: Optional[bool]) -> Resource: + """``resource`` with the test environment, the running commit and the + release added where it has none (an explicit value always wins).""" + attributes = resource.attributes + added: dict = {} + if not any(_has_value(attributes, key) for key in _ENVIRONMENT_KEYS): + if detect_test_env if detect_test_env is not None else _env_flag(DETECT_TEST_ENV_ENV, True): + runner = detect_test_runner() + if runner is not None: + added["deployment.environment"] = _TEST_ENVIRONMENT + LOGGER.info( + "[autter-runtime] test run detected (%s): reporting deployment.environment=test. Set " + "deployment.environment, or AUTTER_DETECT_TEST_ENV=false, to report another environment.", + runner, + ) + if _has_value(attributes, _COMMIT_ATTRIBUTE): + # Set explicitly (OTEL_RESOURCE_ATTRIBUTES): kept as it is, and the + # release defaults to it (when it is a commit), never to a variable + # that may disagree. + explicit = str(attributes[_COMMIT_ATTRIBUTE]).strip() + commit = explicit.lower() if _COMMIT_RE.fullmatch(explicit) else None + else: + commit = detect_commit() + if commit is not None: + added[_COMMIT_ATTRIBUTE] = commit + if not _has_value(attributes, _RELEASE_ATTRIBUTE): + if detect_release if detect_release is not None else _env_flag(DETECT_RELEASE_ENV, True): + release = (os.environ.get(RELEASE_ENV) or "").strip() or commit + if release: + added[_RELEASE_ATTRIBUTE] = release + if not added: + return resource + return Resource({**attributes, **added}, getattr(resource, "schema_url", None)) + + def _keys_to_drop(keep: Any, drop: Optional[Sequence[str]]) -> frozenset: """``keep`` / ``drop`` as given, else from the environment.""" if keep is None: @@ -604,6 +747,9 @@ def clean(attributes: Any) -> Any: return redact_attributes(kept) if redact else kept attributes = clean(span.attributes) + if _from_test_client(span): + # From the raw user agent and host, before the user agent is dropped. + attributes = {**(attributes or {}), TEST_CLIENT_ATTRIBUTE: True} if hasher is not None and (span.attributes or {}).get(CLIENT_IP_HASH_ATTRIBUTE) is None: # From the raw address, before it was dropped; a hash already on the # span (set upstream) is left alone. @@ -719,6 +865,8 @@ def init_tracing( drop_attributes: Optional[Sequence[str]] = None, hash_client_ip: Optional[bool] = None, client_ip_salt: Optional[str] = None, + detect_test_env: Optional[bool] = None, + detect_release: Optional[bool] = None, ) -> TracerProvider: """A TracerProvider that head-samples as configured and always exports errors, registered as the global provider. @@ -729,7 +877,20 @@ def init_tracing( ``OTEL_EXPORTER_OTLP_ENDPOINT`` (or ``…_TRACES_ENDPOINT``) and ``OTEL_EXPORTER_OTLP_HEADERS``. * ``resource``: default ``Resource.create()``, which reads - ``OTEL_SERVICE_NAME`` and ``OTEL_RESOURCE_ATTRIBUTES``. + ``OTEL_SERVICE_NAME`` and ``OTEL_RESOURCE_ATTRIBUTES``. What it does + not set, the three below fill in; its own values always win. + * ``detect_test_env`` (default: on, unless ``AUTTER_DETECT_TEST_ENV=false``; + ``False`` turns it off): under a test runner (:func:`detect_test_runner`: + pytest, Django's ``manage.py test``), a resource with neither + ``deployment.environment.name`` nor ``deployment.environment`` gets + ``deployment.environment=test``, logged once at info level. + * ``deployment.commit``: the running commit, :func:`detect_commit` (the + first of :data:`COMMIT_ENV_VARS` holding 7 to 40 hexadecimal + characters; environment variables only), when the resource has none. + * ``detect_release`` (default: on, unless ``AUTTER_DETECT_RELEASE=false``; + ``False`` turns it off): a resource without ``service.version`` gets + ``AUTTER_RELEASE``, else the commit (the full SHA, which the platform + resolves to the commit and its pull request). * ``redact``: scrub secrets and PII in :class:`RedactingSpanExporter`. * ``keep_identifying_attributes`` (default ``AUTTER_KEEP_IDENTIFYING_ATTRIBUTES``: ``true`` or a comma-separated @@ -785,6 +946,9 @@ def init_tracing( return existing if resource is None: resource = Resource.create() + # Before the meter provider's resource is derived from it, so request + # counts carry the same environment, commit and release as the spans. + resource = _with_deployment(resource, detect_test_env, detect_release) dropped = _keys_to_drop(keep_identifying_attributes, drop_attributes) resource = _without_identifying_resource(resource, dropped) meter_provider = None diff --git a/adapters/python/requirements-test.txt b/adapters/python/requirements-test.txt index 16eae4a..3ee4d4a 100644 --- a/adapters/python/requirements-test.txt +++ b/adapters/python/requirements-test.txt @@ -1,6 +1,13 @@ -# For test_autter_tracing.py (the other adapter tests are stdlib only). Pinned 2026-10-21. +# For test_autter_tracing.py and test_deployment_detection.py (the other adapter tests are stdlib only). +# Pinned 2026-10-21; pytest (and what it needs) 2026-11-15, for the check that a real pytest run reports +# environment test (the suite itself runs under unittest). # autter_tracing.py itself is tested with opentelemetry-sdk 1.20, 1.27, 1.41 and 1.45. opentelemetry-api==1.45.1 opentelemetry-sdk==1.45.1 opentelemetry-semantic-conventions==0.66b1 typing_extensions==4.16.0 +iniconfig==2.3.1 +packaging==26.3 +pluggy==1.7.0 +Pygments==2.21.0 +pytest==9.2.0 diff --git a/adapters/python/test_deployment_detection.py b/adapters/python/test_deployment_detection.py new file mode 100644 index 0000000..a302f20 --- /dev/null +++ b/adapters/python/test_deployment_detection.py @@ -0,0 +1,457 @@ +"""Test runs, the running commit and the release in ``init_tracing()``, and +the ``autter.test_client`` mark on test-client requests (autter_tracing.py). +Needs opentelemetry-sdk; skipped without it. + +Every test here sets the environment variables it depends on and clears the +rest (the commit variables, the opt-outs, ``PYTEST_*``), so the suite gives +the same answers under unittest, under pytest and on CI (which sets +``GITHUB_SHA``). Test-runner detection is patched where a test needs a +process that is (or is not) a test run. + +Run: python3 -m unittest discover -s adapters/python -v +""" + +from __future__ import annotations + +import importlib.util +import os +import subprocess +import sys +import tempfile +import textwrap +import unittest +from unittest import mock + +sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) + +try: + from opentelemetry.sdk.metrics import Histogram + from opentelemetry.sdk.metrics.export import AggregationTemporality, MetricExporter, MetricExportResult + from opentelemetry.sdk.resources import Resource + from opentelemetry.sdk.trace import TracerProvider + from opentelemetry.sdk.trace.export import SimpleSpanProcessor + from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter + from opentelemetry.sdk.trace.sampling import ALWAYS_ON + from opentelemetry.trace import SpanKind +except ImportError as error: # pragma: no cover - depends on the environment + raise unittest.SkipTest(f"opentelemetry-sdk is not installed ({error})") + +import autter_tracing # noqa: E402 +from autter_tracing import ( # noqa: E402 + COMMIT_ENV_VARS, + TEST_CLIENT_ATTRIBUTE, + RedactingSpanExporter, + detect_commit, + detect_test_runner, + init_tracing, +) + +HERE = os.path.dirname(os.path.abspath(__file__)) +SHA = "4f2c9e1a7b3d5f60718293a4b5c6d7e8f9012345" +OTHER_SHA = "9c1e4f2a7b3d" + +#: Cleared before every test, then set from the test's own values. +_CONTROLLED = ( + *COMMIT_ENV_VARS, + "AUTTER_RELEASE", + "AUTTER_DETECT_RELEASE", + "AUTTER_DETECT_TEST_ENV", + "AUTTER_COUNT_REQUESTS", + "OTEL_RESOURCE_ATTRIBUTES", + "OTEL_SERVICE_NAME", + "PYTEST_VERSION", + "PYTEST_CURRENT_TEST", +) + + +class _Env: + """``os.environ`` with only ``values`` set among :data:`_CONTROLLED`; + restored on exit.""" + + def __init__(self, **values: str): + self._values = values + self._patch = mock.patch.dict(os.environ, {}) + + def __enter__(self): + self._patch.__enter__() + for name in _CONTROLLED: + os.environ.pop(name, None) + os.environ.update(self._values) + return self + + def __exit__(self, *exc): + return self._patch.__exit__(*exc) + + +class CapturingMetricExporter(MetricExporter): + def __init__(self): + super().__init__(preferred_temporality={Histogram: AggregationTemporality.DELTA}) + self.batches = [] + + def export(self, metrics_data, timeout_millis=10_000, **kwargs): + self.batches.append(metrics_data) + return MetricExportResult.SUCCESS + + def force_flush(self, timeout_millis=10_000): + return True + + def shutdown(self, timeout_millis=30_000, **kwargs): + pass + + def resources(self): + return [rm.resource.attributes for batch in self.batches for rm in batch.resource_metrics] + + +def _start(env=None, runner=None, **kwargs): + """init_tracing() with ``env`` (everything else in :data:`_CONTROLLED` + unset) in a process whose test runner is ``runner`` (``None``: not a test + run). Returns the provider.""" + with _Env(**(env or {})), mock.patch.object(autter_tracing, "detect_test_runner", return_value=runner): + kwargs.setdefault("count_requests", False) + return init_tracing(exporter=InMemorySpanExporter(), set_global=False, **kwargs) + + +def _resource(env=None, runner=None, **kwargs): + provider = _start(env, runner, **kwargs) + attributes = dict(provider.resource.attributes) + provider.shutdown() + return attributes + + +class TestRunnerDetection(unittest.TestCase): + def test_pytest_is_detected_from_its_variables_and_its_module(self): + no_modules = {} + self.assertEqual(detect_test_runner({"PYTEST_VERSION": "8.4.2"}, no_modules, ["app.py"]), "pytest") + self.assertEqual( + detect_test_runner({"PYTEST_CURRENT_TEST": "test_app.py::test_x (call)"}, no_modules, ["app.py"]), "pytest" + ) + self.assertEqual(detect_test_runner({}, {"pytest": object()}, ["app.py"]), "pytest") + + def test_django_manage_py_test_is_detected(self): + self.assertEqual(detect_test_runner({}, {}, ["manage.py", "test"]), "manage.py test") + self.assertEqual(detect_test_runner({}, {}, ["/srv/app/manage.py", "test", "shop"]), "manage.py test") + + def test_near_misses_are_not_a_test_run(self): + for environ, modules, argv in ( + ({}, {}, ["manage.py", "runserver"]), + ({}, {}, ["manage.py"]), + ({}, {}, ["manage.py", "testserver"]), # Django's dev server with fixtures + ({}, {}, ["notmanage.py", "test"]), + ({}, {}, ["gunicorn", "app:app"]), + ({}, {"_pytest": object(), "pytest_asyncio": object()}, ["app.py"]), + ({"PYTEST_VERSION": "", "PYTEST_CURRENT_TEST": ""}, {}, ["app.py"]), + ({"PYTEST_ADDOPTS": "-q"}, {}, ["app.py"]), + ({}, {}, []), + ): + with self.subTest(environ=environ, modules=sorted(modules), argv=argv): + self.assertIsNone(detect_test_runner(environ, modules, argv)) + + def test_defaults_read_the_process(self): + with _Env(PYTEST_CURRENT_TEST="x"): + self.assertEqual(detect_test_runner(), "pytest") + + +class TestEnvironment(unittest.TestCase): + def test_a_test_run_without_an_environment_reports_test_and_says_so_once(self): + with self.assertLogs("autter.runtime", "INFO") as logs: + resource = _resource(runner="pytest") + self.assertEqual(resource["deployment.environment"], "test") + lines = [line for line in logs.output if "test run detected" in line] + self.assertEqual(len(lines), 1) + self.assertIn("INFO", lines[0]) + self.assertIn("pytest", lines[0]) + + def test_a_passed_resource_without_an_environment_also_reports_test(self): + resource = _resource(runner="manage.py test", resource=Resource.create({"service.name": "shop"})) + self.assertEqual(resource["deployment.environment"], "test") + self.assertEqual(resource["service.name"], "shop") + + def test_an_explicit_environment_always_wins(self): + for env, resource, expected in ( + ({"OTEL_RESOURCE_ATTRIBUTES": "deployment.environment=staging"}, None, + {"deployment.environment": "staging"}), + ({"OTEL_RESOURCE_ATTRIBUTES": "deployment.environment.name=production"}, None, + {"deployment.environment.name": "production"}), + ({}, Resource.create({"deployment.environment": "production"}), {"deployment.environment": "production"}), + ({}, Resource.create({"deployment.environment.name": "preview"}), {"deployment.environment.name": "preview"}), + ): + with self.subTest(env=env, expected=expected): + with self.assertLogs("autter.runtime", "DEBUG") as logs: + autter_tracing.LOGGER.debug("start") + attributes = _resource(env, runner="pytest", resource=resource) + found = {k: attributes[k] for k in ("deployment.environment", "deployment.environment.name") if k in attributes} + self.assertEqual(found, expected) + self.assertFalse(any("test run detected" in line for line in logs.output)) + + def test_not_a_test_run_adds_no_environment(self): + resource = _resource(runner=None) + self.assertNotIn("deployment.environment", resource) + self.assertNotIn("deployment.environment.name", resource) + + def test_opt_outs(self): + for env, kwargs in ( + ({"AUTTER_DETECT_TEST_ENV": "false"}, {}), + ({"AUTTER_DETECT_TEST_ENV": "0"}, {}), + ({}, {"detect_test_env": False}), + ({"AUTTER_DETECT_TEST_ENV": "true"}, {"detect_test_env": False}), # the argument wins + ): + with self.subTest(env=env, kwargs=kwargs): + self.assertNotIn("deployment.environment", _resource(env, runner="pytest", **kwargs)) + # And the argument turns it on over the variable. + self.assertEqual( + _resource({"AUTTER_DETECT_TEST_ENV": "false"}, runner="pytest", detect_test_env=True)[ + "deployment.environment" + ], + "test", + ) + + def test_request_counts_carry_the_same_environment_commit_and_release(self): + metric_exporter = CapturingMetricExporter() + provider = _start({"GIT_SHA": SHA}, runner="pytest", count_requests=True, metric_exporter=metric_exporter) + attrs = {"http.method": "GET", "http.route": "/ok", "http.status_code": 200} + with provider.get_tracer("test").start_as_current_span("GET /ok", kind=SpanKind.SERVER, attributes=attrs): + pass + provider.force_flush() + provider.shutdown() + metric_resources = metric_exporter.resources() + self.assertTrue(metric_resources, "the request was counted") + for key, value in (("deployment.environment", "test"), ("deployment.commit", SHA), ("service.version", SHA)): + self.assertEqual(provider.resource.attributes[key], value) + for metric_resource in metric_resources: # older SDKs export once more on shutdown + self.assertEqual(metric_resource[key], value) + self.assertIs(metric_resources[0]["autter.metrics_wired"], True) + + +class Commit(unittest.TestCase): + def test_variables_are_tried_in_order(self): + self.assertEqual( + COMMIT_ENV_VARS, + ( + "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", + ), + ) + for index, name in enumerate(COMMIT_ENV_VARS): + with self.subTest(name=name): + environ = {name: SHA} + # A later variable never overrides it. + environ.update({later: OTHER_SHA for later in COMMIT_ENV_VARS[index + 1:]}) + self.assertEqual(detect_commit(environ), SHA) + + def test_only_7_to_40_hex_characters_count(self): + self.assertEqual(detect_commit({"GIT_SHA": "abc1234"}), "abc1234") + self.assertEqual(detect_commit({"GIT_SHA": " " + SHA.upper() + "\n"}), SHA) + for value in ( + "abc123", # 6 + SHA + "a", # 41 + "abc123g", # not hex + "refs/heads/main", # CodeBuild's SOURCE_VERSION on a branch build + "v1.2.3", + "0x" + SHA[:38], + "abc 1234", + "", + ): + with self.subTest(value=value): + self.assertIsNone(detect_commit({"SOURCE_VERSION": value})) + # An invalid value is skipped, not fatal: the next variable is used. + self.assertEqual(detect_commit({"AUTTER_COMMIT": "main", "GITHUB_SHA": SHA}), SHA) + + def test_init_tracing_sets_deployment_commit(self): + self.assertEqual(_resource({"VERCEL_GIT_COMMIT_SHA": SHA})["deployment.commit"], SHA) + self.assertNotIn("deployment.commit", _resource({})) + self.assertNotIn("deployment.commit", _resource({"GIT_SHA": "not-a-sha"})) + + def test_an_explicit_commit_wins(self): + resource = _resource({"OTEL_RESOURCE_ATTRIBUTES": f"deployment.commit={OTHER_SHA}", "GIT_SHA": SHA}) + self.assertEqual(resource["deployment.commit"], OTHER_SHA) + self.assertEqual(resource["service.version"], OTHER_SHA) + + def test_commit_detection_never_runs_git(self): + with mock.patch.object(subprocess, "Popen", side_effect=AssertionError("subprocess")), mock.patch.object( + subprocess, "run", side_effect=AssertionError("subprocess") + ), mock.patch.object(os, "system", side_effect=AssertionError("os.system")): + self.assertNotIn("deployment.commit", _resource({})) + self.assertEqual(_resource({"GIT_SHA": SHA})["deployment.commit"], SHA) + + +class Release(unittest.TestCase): + def test_autter_release_then_the_full_commit(self): + self.assertEqual(_resource({"AUTTER_RELEASE": "2026.11.15", "GIT_SHA": SHA})["service.version"], "2026.11.15") + self.assertEqual(_resource({"GIT_SHA": SHA})["service.version"], SHA) + self.assertEqual(_resource({"AUTTER_RELEASE": " ", "GIT_SHA": SHA})["service.version"], SHA) + self.assertEqual(_resource({"AUTTER_RELEASE": "2026.11.15"})["service.version"], "2026.11.15") + self.assertNotIn("service.version", _resource({})) + + def test_an_explicit_release_always_wins(self): + env = {"AUTTER_RELEASE": "from-env", "GIT_SHA": SHA} + self.assertEqual( + _resource({**env, "OTEL_RESOURCE_ATTRIBUTES": "service.version=4.2.0"})["service.version"], "4.2.0" + ) + resource = _resource(env, resource=Resource.create({"service.version": "4.2.1"})) + self.assertEqual(resource["service.version"], "4.2.1") + # The commit is still reported. + self.assertEqual(resource["deployment.commit"], SHA) + + def test_opt_outs(self): + for env, kwargs in ( + ({"AUTTER_DETECT_RELEASE": "false"}, {}), + ({}, {"detect_release": False}), + ({"AUTTER_DETECT_RELEASE": "true"}, {"detect_release": False}), # the argument wins + ): + with self.subTest(env=env, kwargs=kwargs): + resource = _resource({"AUTTER_RELEASE": "r1", "GIT_SHA": SHA, **env}, **kwargs) + self.assertNotIn("service.version", resource) + self.assertEqual(resource["deployment.commit"], SHA, "the commit is reported either way") + + +def _server_span(attributes, kind=SpanKind.SERVER, **exporter_options): + exporter = InMemorySpanExporter() + provider = TracerProvider(sampler=ALWAYS_ON) + provider.add_span_processor(SimpleSpanProcessor(RedactingSpanExporter(exporter, **exporter_options))) + with mock.patch.dict(os.environ, {"AUTTER_KEEP_IDENTIFYING_ATTRIBUTES": "", "AUTTER_DROP_ATTRIBUTES": ""}): + with provider.get_tracer("test").start_as_current_span("GET /x", kind=kind, attributes=attributes): + pass + provider.shutdown() + [span] = exporter.get_finished_spans() + return span + + +# What OpenTelemetry's ASGI instrumentation (0.66b1) records for a request +# made through Starlette's / FastAPI's TestClient, in the old and the stable +# HTTP semantic conventions (recorded with the fastapi recipe's pins). +TESTCLIENT_OLD = { + "http.scheme": "http", "http.host": "testserver", "net.host.port": 80, "http.flavor": "1.1", + "http.target": "/x", "http.url": "http://testserver/x", "http.method": "GET", + "http.server_name": "testserver", "http.user_agent": "testclient", "net.peer.ip": "testclient", + "net.peer.port": 50000, "http.route": "/x", "http.status_code": 200, +} +TESTCLIENT_STABLE = { + "url.scheme": "http", "server.address": "testserver", "server.port": 80, "network.protocol.version": "1.1", + "url.path": "/x", "http.request.method": "GET", "user_agent.original": "testclient", + "client.address": "testclient", "client.port": 50000, "http.route": "/x", "http.response.status_code": 200, +} + +# What OpenTelemetry's Django instrumentation (0.66b2) records for a request +# made through Django's test Client, old HTTP semantic conventions. (With the +# stable ones it records no host at all, so nothing marks the request; a +# ``manage.py test`` run still reports environment ``test``.) +DJANGO_CLIENT_OLD = { + "http.method": "GET", "http.server_name": "testserver", "http.scheme": "http", "net.host.port": 80, + "http.url": "http://testserver/x", "net.peer.ip": "127.0.0.1", "http.flavor": "1.1", "http.route": "x", + "http.status_code": 200, +} + + +class TestClientMark(unittest.TestCase): + def test_testclient_requests_are_marked_and_the_user_agent_still_dropped(self): + for attributes in (TESTCLIENT_OLD, TESTCLIENT_STABLE, DJANGO_CLIENT_OLD): + with self.subTest(keys=sorted(attributes)[:2]): + span = _server_span(attributes) + self.assertIs(span.attributes[TEST_CLIENT_ATTRIBUTE], True) + self.assertNotIn("http.user_agent", span.attributes) + self.assertNotIn("user_agent.original", span.attributes) + self.assertNotIn("client.address", span.attributes) + + def test_each_signal_alone_marks_the_span(self): + cases = [{"http.method": "GET", key: "testclient"} for key in ("http.user_agent", "user_agent.original")] + cases += [ + {"http.method": "GET", key: "testserver"} + for key in ("server.address", "net.host.name", "http.host", "http.server_name") + ] + for attributes in cases: + with self.subTest(attributes=attributes): + self.assertIs(_server_span(attributes).attributes[TEST_CLIENT_ATTRIBUTE], True) + + def test_marked_also_with_redaction_off_and_the_user_agent_kept(self): + span = _server_span({"http.user_agent": "testclient"}, redact=False) + self.assertIs(span.attributes[TEST_CLIENT_ATTRIBUTE], True) + kept = _server_span({"http.user_agent": "testclient"}, keep_identifying_attributes=True) + self.assertIs(kept.attributes[TEST_CLIENT_ATTRIBUTE], True) + self.assertEqual(kept.attributes["http.user_agent"], "testclient") + + def test_near_misses_are_not_marked(self): + for attributes, kind in ( + ({"http.user_agent": "testclient/1.0"}, SpanKind.SERVER), + ({"http.user_agent": "TestClient"}, SpanKind.SERVER), + ({"http.user_agent": " testclient"}, SpanKind.SERVER), + ({"http.user_agent": "python-httpx/0.28 testclient"}, SpanKind.SERVER), + ({"user_agent.original": "Werkzeug/3.1.3"}, SpanKind.SERVER), # Flask's test client + ({"http.host": "testserver:8000"}, SpanKind.SERVER), + ({"http.host": "testserver.example.com"}, SpanKind.SERVER), + ({"server.address": "localhost"}, SpanKind.SERVER), + ({"server.address": "TESTSERVER"}, SpanKind.SERVER), + ({"http.url": "http://testserver/x"}, SpanKind.SERVER), # not a host key + ({"net.peer.ip": "testclient"}, SpanKind.SERVER), # the client, not the user agent + ({"peer.service": "testserver", "http.user_agent": "Mozilla/5.0"}, SpanKind.SERVER), + # A client span calling a test server (an outgoing call), and an internal one. + ({"http.user_agent": "testclient", "server.address": "testserver"}, SpanKind.CLIENT), + ({"http.user_agent": "testclient"}, SpanKind.INTERNAL), + ): + with self.subTest(attributes=attributes, kind=kind): + self.assertNotIn(TEST_CLIENT_ATTRIBUTE, _server_span(attributes, kind).attributes) + + def test_init_tracing_marks_retained_error_spans(self): + exporter = InMemorySpanExporter() + with _Env(OTEL_TRACES_SAMPLER="always_off"), mock.patch.object( + autter_tracing, "detect_test_runner", return_value=None + ): + provider = init_tracing(exporter=exporter, set_global=False, count_requests=False) + with self.assertRaises(ValueError): + with provider.get_tracer("test").start_as_current_span("GET /x", kind=SpanKind.SERVER, attributes=TESTCLIENT_OLD): + raise ValueError("boom") + provider.force_flush() + [span] = exporter.get_finished_spans() + self.assertIs(span.attributes[TEST_CLIENT_ATTRIBUTE], True) + self.assertIs(span.attributes[autter_tracing.TAIL_RETAINED_ATTRIBUTE], True) + self.assertNotIn("http.user_agent", span.attributes) + provider.shutdown() + + +_PYTEST_PROBE = textwrap.dedent( + """ + import os, sys + sys.path.insert(0, {here!r}) + from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter + from autter_tracing import init_tracing + + def test_reports_test(): + provider = init_tracing(exporter=InMemorySpanExporter(), set_global=False, count_requests=False) + assert provider.resource.attributes["deployment.environment"] == "test" + + def test_explicit_environment_wins(): + os.environ["OTEL_RESOURCE_ATTRIBUTES"] = "deployment.environment=production" + provider = init_tracing(exporter=InMemorySpanExporter(), set_global=False, count_requests=False) + assert provider.resource.attributes["deployment.environment"] == "production" + """ +) + + +class UnderPytest(unittest.TestCase): + """Detection inside a real pytest run (a child process; this suite itself + may run under unittest). Skipped when pytest is not installed.""" + + def test_a_real_pytest_run_reports_test(self): + # find_spec, not import: importing pytest here would make every later + # init_tracing() in this process a detected test run. + if importlib.util.find_spec("pytest") is None: + self.skipTest("pytest is not installed") + with tempfile.TemporaryDirectory() as tmp: + path = os.path.join(tmp, "test_probe.py") + with open(path, "w", encoding="utf-8") as fh: + fh.write(_PYTEST_PROBE.format(here=HERE)) + env = {k: v for k, v in os.environ.items() if k not in _CONTROLLED} + result = subprocess.run( + [sys.executable, "-m", "pytest", "-q", "-p", "no:cacheprovider", path], + cwd=tmp, + env=env, + capture_output=True, + text=True, + timeout=120, + ) + self.assertEqual(result.returncode, 0, result.stdout + result.stderr) + self.assertIn("2 passed", result.stdout) + + +if __name__ == "__main__": + unittest.main() From d5907fd1705ddff37dcb9b8ca69c80910fcab249 Mon Sep 17 00:00:00 2001 From: Sagnik Ghosh Date: Sun, 15 Nov 2026 10:52:14 +0530 Subject: [PATCH 2/2] Python adapter: shared commit variable order; test-client mark is a hint 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 --- adapters/python/autter_tracing.py | 13 ++++++++----- adapters/python/test_deployment_detection.py | 8 ++++++-- 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/adapters/python/autter_tracing.py b/adapters/python/autter_tracing.py index aac001d..60f01f8 100644 --- a/adapters/python/autter_tracing.py +++ b/adapters/python/autter_tracing.py @@ -188,7 +188,10 @@ #: On a server span of a request made by a test client: Starlette's and #: FastAPI's ``TestClient`` (user agent ``testclient``, host ``testserver``) #: or Django's test ``Client`` (host ``testserver``). Read from the raw -#: attributes, before the user agent is dropped. Same key as the ingester's. +#: attributes, before the user agent is dropped. A hint for people reading +#: the trace, nothing more: a caller can send any user agent or host, so +#: nothing may act on it (alerting, paging, the environment). The +#: environment comes from the process alone (:func:`detect_test_runner`). TEST_CLIENT_ATTRIBUTE = "autter.test_client" _TEST_CLIENT_USER_AGENT = "testclient" _TEST_CLIENT_HOST = "testserver" @@ -200,19 +203,19 @@ DETECT_TEST_ENV_ENV = "AUTTER_DETECT_TEST_ENV" #: Environment variables that carry the running commit, in the order they are -#: tried (the list @autter/runtime-node uses). The first one holding 7 to 40 +#: tried (the order @autter/runtime-node and the source-map CLI use). The first one holding 7 to 40 #: hexadecimal characters is the commit; other values are ignored. COMMIT_ENV_VARS = ( "AUTTER_COMMIT", + "GITHUB_SHA", + "VERCEL_GIT_COMMIT_SHA", + "CF_PAGES_COMMIT_SHA", "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", ) diff --git a/adapters/python/test_deployment_detection.py b/adapters/python/test_deployment_detection.py index a302f20..7899238 100644 --- a/adapters/python/test_deployment_detection.py +++ b/adapters/python/test_deployment_detection.py @@ -227,8 +227,8 @@ def test_variables_are_tried_in_order(self): self.assertEqual( COMMIT_ENV_VARS, ( - "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", + "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", ), ) @@ -405,6 +405,10 @@ def test_init_tracing_marks_retained_error_spans(self): self.assertIs(span.attributes[TEST_CLIENT_ATTRIBUTE], True) self.assertIs(span.attributes[autter_tracing.TAIL_RETAINED_ATTRIBUTE], True) self.assertNotIn("http.user_agent", span.attributes) + # A hint only: a test-client request never decides the environment + # (a caller could fake it); outside a test run there is none. + self.assertNotIn("deployment.environment", span.resource.attributes) + self.assertNotIn("deployment.environment.name", span.resource.attributes) provider.shutdown()