Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

feat(ci): emit per-test timings to PostHog DevEx for analysis (#57216)

Raúl Negrón-Otero2a8224365a7b
diff --git a/.github/scripts/report_test_timings.py b/.github/scripts/report_test_timings.py
new file mode 100644
index 000000000000..2bf1332874e1
--- /dev/null
+++ b/.github/scripts/report_test_timings.py
@@ -0,0 +1,493 @@
+#!/usr/bin/env -S uv run --script
+# /// script
+# requires-python = ">=3.11"
+# dependencies = [
+# "defusedxml~=0.7",
+# "opentelemetry-api~=1.27",
+# "opentelemetry-sdk~=1.27",
+# "opentelemetry-exporter-otlp-proto-http~=1.27",
+# ]
+# ///
+"""Emit OTLP traces from Backend CI JUnit XML artifacts.
+
+Reads `junit-results-*` artifacts (downloaded by the workflow) and emits one
+trace per workflow run shaped:
+
+ ci-backend (root)
+ └── <segment>-<group> (shard)
+ ├── <pytest nodeid> (test)
+ └── ...
+
+Trace ID is deterministic per (run_id, run_attempt) so each workflow attempt
+gets its own trace. Failures and errors mark spans Status.ERROR.
+
+This script must NEVER fail the workflow: any unexpected error is logged and
+the process exits 0. The workflow job is also `continue-on-error: true` as a
+second belt for setup and artifact-download failures.
+"""
+
+from __future__ import annotations
+
+import os
+import sys
+import json
+import hashlib
+import logging
+import secrets
+import argparse
+from dataclasses import dataclass
+from datetime import UTC, datetime, timedelta
+from pathlib import Path
+from typing import Any
+
+import defusedxml.ElementTree as ET # XXE-safe stdlib drop-in
+from opentelemetry import trace
+from opentelemetry.exporter.otlp.proto.http.trace_exporter import OTLPSpanExporter
+from opentelemetry.sdk.resources import Resource
+from opentelemetry.sdk.trace import TracerProvider
+from opentelemetry.sdk.trace.export import BatchSpanProcessor
+from opentelemetry.sdk.trace.id_generator import IdGenerator
+from opentelemetry.trace import Status, StatusCode
+
+logger = logging.getLogger("report_test_timings")
+
+DEFAULT_OTLP_ENDPOINT = "https://us.i.posthog.com/i/v1/traces"
+SERVICE_NAME = "ci-backend"
+INSTRUMENTATION_NAME = "posthog-ci-test-timings"
+INSTRUMENTATION_VERSION = "0.1.0"
+# ~150 KB serialized at this size — well under capture-logs' 2 MiB body limit.
+SPAN_BATCH_SIZE = 1000
+
+
+@dataclass(frozen=True)
+class TestCase:
+ nodeid: str
+ classname: str
+ name: str
+ duration_seconds: float
+ start: datetime
+ end: datetime
+ outcome: str # passed | failed | error | skipped | rerun_passed
+ attempts: int # 1 + number of pytest-rerunfailures retries before final outcome
+
+
+@dataclass(frozen=True)
+class ArtifactInfo:
+ path: Path
+ suite: str
+ segment: str
+ group: int | None
+ total: int | None
+
+
+@dataclass(frozen=True)
+class Shard:
+ info: ArtifactInfo
+ junit_filename: str
+ start: datetime
+ end: datetime
+ testcase_seconds: float
+ overhead_seconds: float
+ tests: list[TestCase]
+
+
+# ---------- artifact directory parsing ----------
+
+
+def split_artifact_name(name_parts: list[str]) -> tuple[str, str]:
+ if not name_parts:
+ return "", ""
+ if name_parts[0] == "backend" and len(name_parts) > 1:
+ return "backend", "-".join(name_parts[1:])
+ return "-".join(name_parts), "-".join(name_parts)
+
+
+def derive_suite_segment_and_group(artifact_dir_name: str) -> tuple[str, str, int | None]:
+ """Parse `junit-results-backend-core-29` → ("backend", "core", 29).
+
+ Trailing numeric token is the shard group. Slug values are kept as-is so
+ span attributes match workflow artifact names.
+ """
+ suffix = artifact_dir_name.removeprefix("junit-results-")
+ parts = suffix.split("-")
+ group = int(parts[-1]) if len(parts) > 1 and parts[-1].isdigit() else None
+ name_parts = parts[:-1] if group is not None else parts
+ suite, segment = split_artifact_name(name_parts)
+ return suite, segment, group
+
+
+def collect_artifact_infos(artifacts_root: Path) -> list[ArtifactInfo]:
+ artifact_dirs = sorted(d for d in artifacts_root.iterdir() if d.is_dir()) or [artifacts_root]
+ parsed = [(d, *derive_suite_segment_and_group(d.name)) for d in artifact_dirs]
+
+ groups_by_shard_key: dict[tuple[str, str], set[int]] = {}
+ for _, suite, segment, group in parsed:
+ if group is not None:
+ groups_by_shard_key.setdefault((suite, segment), set()).add(group)
+
+ shard_totals = {
+ key: max(groups) if groups == set(range(1, max(groups) + 1)) else None
+ for key, groups in groups_by_shard_key.items()
+ }
+
+ return [
+ ArtifactInfo(path=d, suite=suite, segment=segment, group=group, total=shard_totals.get((suite, segment)))
+ for d, suite, segment, group in parsed
+ ]
+
+
+# ---------- junit parsing ----------
+
+
+def classify_testcase(testcase: Any) -> tuple[str, int]:
+ """Return (outcome, attempts) from a single `<testcase>` element.
+
+ pytest-rerunfailures emits prior attempts as `<rerunFailure>` /
+ `<rerunError>` siblings before the final outcome child. Walk children once.
+ """
+ rerun_count = 0
+ final_outcome: str | None = None
+ for child in testcase:
+ tag = child.tag
+ if tag.startswith("rerun"):
+ rerun_count += 1
+ elif tag in ("failure", "error", "skipped") and final_outcome is None:
+ final_outcome = "failed" if tag == "failure" else tag
+ if final_outcome is None:
+ final_outcome = "rerun_passed" if rerun_count else "passed"
+ return final_outcome, 1 + rerun_count
+
+
+def to_nodeid(classname: str, name: str) -> str:
+ """`posthog.hogql.test.test_resolver.TestResolver`, `test_x` → `posthog/hogql/test/test_resolver/TestResolver::test_x`.
+
+ JUnit drops the `.py` and the file/class boundary; this is a best-effort
+ reconstruction that's stable for grouping in queries.
+ """
+ return f"{classname.replace('.', '/')}::{name}" if classname else name
+
+
+def parse_iso_utc(value: str) -> datetime | None:
+ """Parse an ISO 8601 timestamp (pytest emits naive); treat as UTC."""
+ if not value:
+ return None
+ try:
+ dt = datetime.fromisoformat(value)
+ except ValueError:
+ return None
+ return dt.replace(tzinfo=UTC) if dt.tzinfo is None else dt.astimezone(UTC)
+
+
+def parse_shard(xml_path: Path, info: ArtifactInfo) -> Shard | None:
+ """One Shard per junit XML file. Tolerant of malformed input."""
+ try:
+ root = ET.parse(xml_path).getroot()
+ except ET.ParseError as exc:
+ logger.warning("failed to parse %s: %s", xml_path, exc)
+ return None
+
+ suite_elem = root if root.tag == "testsuite" else root.find("testsuite")
+ if suite_elem is None:
+ return None
+
+ start = parse_iso_utc(suite_elem.get("timestamp", ""))
+ if start is None:
+ return None
+ try:
+ wall_seconds = float(suite_elem.get("time", "0"))
+ except ValueError:
+ wall_seconds = 0.0
+
+ tests: list[TestCase] = []
+ cursor = start
+ for tc in suite_elem.iter("testcase"):
+ classname = tc.get("classname", "")
+ name = tc.get("name", "")
+ outcome, attempts = classify_testcase(tc)
+ try:
+ duration = float(tc.get("time", "0"))
+ except ValueError:
+ duration = 0.0
+ test_start = cursor
+ test_end = cursor + timedelta(seconds=duration)
+ cursor = test_end
+ tests.append(
+ TestCase(
+ nodeid=to_nodeid(classname, name),
+ classname=classname,
+ name=name,
+ duration_seconds=duration,
+ start=test_start,
+ end=test_end,
+ outcome=outcome,
+ attempts=attempts,
+ )
+ )
+
+ end = start + timedelta(seconds=wall_seconds)
+ testcase_seconds = sum(t.duration_seconds for t in tests)
+ return Shard(
+ info=info,
+ junit_filename=xml_path.name,
+ start=start,
+ end=end,
+ testcase_seconds=testcase_seconds,
+ overhead_seconds=max(0.0, wall_seconds - testcase_seconds),
+ tests=tests,
+ )
+
+
+def collect_shards(artifacts_root: Path) -> list[Shard]:
+ shards: list[Shard] = []
+ for artifact in collect_artifact_infos(artifacts_root):
+ for xml_path in sorted(artifact.path.rglob("junit*.xml")):
+ shard = parse_shard(xml_path, artifact)
+ if shard is not None:
+ shards.append(shard)
+ return shards
+
+
+# ---------- threshold filter ----------
+
+
+def should_emit(test: TestCase, min_duration_seconds: float) -> bool:
+ """Emit signal-bearing testcases: failures, errors, reruns, or above the duration threshold."""
+ if test.outcome in ("failed", "error"):
+ return True
+ if test.attempts > 1:
+ return True
+ return test.duration_seconds >= min_duration_seconds
+
+
+def filter_shards(shards: list[Shard], min_duration_seconds: float) -> list[Shard]:
+ """Prune sub-threshold passing tests; preserve shard wall-time bounds and order."""
+ return [
+ Shard(
+ info=s.info,
+ junit_filename=s.junit_filename,
+ start=s.start,
+ end=s.end,
+ testcase_seconds=s.testcase_seconds,
+ overhead_seconds=s.overhead_seconds,
+ tests=[t for t in s.tests if should_emit(t, min_duration_seconds)],
+ )
+ for s in shards
+ ]
+
+
+# ---------- workflow context ----------
+
+
+def get_pull_request_number() -> int | None:
+ event_path = os.environ.get("GITHUB_EVENT_PATH", "")
+ if event_path:
+ try:
+ payload = json.loads(Path(event_path).read_text())
+ except (OSError, json.JSONDecodeError):
+ payload = {}
+ number = payload.get("number")
+ if isinstance(number, int):
+ return number
+ ref_parts = os.environ.get("GITHUB_REF", "").split("/")
+ if len(ref_parts) >= 3 and ref_parts[0] == "refs" and ref_parts[1] == "pull" and ref_parts[2].isdigit():
+ return int(ref_parts[2])
+ return None
+
+
+def get_run_url() -> str:
+ repo = os.environ.get("GITHUB_REPOSITORY", "")
+ run_id = os.environ.get("GITHUB_RUN_ID", "")
+ if not repo or not run_id:
+ return ""
+ server = os.environ.get("GITHUB_SERVER_URL", "https://github.com")
+ return f"{server}/{repo}/actions/runs/{run_id}"
+
+
+def workflow_resource_attributes() -> dict[str, str | int]:
+ """Resource attributes attached to every span — pre-aggregation context for the run."""
+ keys = ("WORKFLOW", "RUN_ID", "RUN_NUMBER", "RUN_ATTEMPT", "REF", "SHA", "ACTOR", "REPOSITORY")
+ attrs: dict[str, str | int] = {f"ci.{k.lower()}": os.environ.get(f"GITHUB_{k}", "") for k in keys}
+ attrs["ci.event_name"] = os.environ.get("GITHUB_EVENT_NAME", "")
+ attrs["ci.head_ref"] = os.environ.get("GITHUB_HEAD_REF", "")
+ attrs["ci.base_ref"] = os.environ.get("GITHUB_BASE_REF", "")
+ pr_number = get_pull_request_number()
+ if pr_number is not None:
+ attrs["ci.pr_number"] = pr_number
+ attrs["ci.run_url"] = get_run_url()
+ return {k: v for k, v in attrs.items() if v != ""}
+
+
+# ---------- OTLP export ----------
+
+
+def deterministic_trace_id(run_id: str, run_attempt: str) -> int:
+ """One trace ID per (run_id, run_attempt). Reruns of the same attempt collide intentionally."""
+ digest = hashlib.sha256(f"{run_id}:{run_attempt}".encode()).digest()
+ return int.from_bytes(digest[:16], "big") # OTLP trace IDs are 128-bit (16 bytes).
+
+
+class _FixedTraceIdGenerator(IdGenerator):
+ """Force every span in the run to share a deterministic trace ID."""
+
+ def __init__(self, trace_id: int) -> None:
+ self._trace_id = trace_id
+
+ def generate_trace_id(self) -> int:
+ return self._trace_id
+
+ def generate_span_id(self) -> int:
+ span_id = secrets.randbits(64)
+ while span_id == 0:
+ span_id = secrets.randbits(64)
+ return span_id
+
+
+def _to_ns(dt: datetime) -> int:
+ return int(dt.timestamp() * 1_000_000_000)
+
+
+def emit_traces(shards: list[Shard], endpoint: str, token: str) -> None:
+ """Build root → shard → test span hierarchy and ship via OTLP HTTP."""
+ run_id = os.environ.get("GITHUB_RUN_ID", "0")
+ run_attempt = os.environ.get("GITHUB_RUN_ATTEMPT", "1")
+ trace_id = deterministic_trace_id(run_id, run_attempt)
+
+ resource = Resource.create({"service.name": SERVICE_NAME, **workflow_resource_attributes()})
+ provider = TracerProvider(resource=resource, id_generator=_FixedTraceIdGenerator(trace_id))
+ exporter = OTLPSpanExporter(endpoint=endpoint, headers={"Authorization": f"Bearer {token}"})
+ provider.add_span_processor(BatchSpanProcessor(exporter, max_export_batch_size=SPAN_BATCH_SIZE))
+ tracer = provider.get_tracer(INSTRUMENTATION_NAME, INSTRUMENTATION_VERSION)
+
+ if not shards:
+ provider.shutdown()
+ return
+
+ root_start = min(s.start for s in shards)
+ root_end = max(s.end for s in shards)
+ root_span = tracer.start_span(SERVICE_NAME, start_time=_to_ns(root_start))
+ root_has_error = False
+ with trace.use_span(root_span, end_on_exit=False):
+ for shard in shards:
+ if _emit_shard_span(tracer, shard):
+ root_has_error = True
+
+ if root_has_error:
+ root_span.set_status(Status(StatusCode.ERROR))
+ root_span.end(end_time=_to_ns(root_end))
+ provider.shutdown()
+
+
+def _emit_shard_span(tracer: trace.Tracer, shard: Shard) -> bool:
+ """Emit shard span and its test children. Returns True iff any child has Error."""
+ info = shard.info
+ shard_name = f"{info.segment}-{info.group}" if info.group is not None else info.segment
+ shard_span = tracer.start_span(shard_name, start_time=_to_ns(shard.start))
+ shard_span.set_attribute("shard.suite", info.suite)
+ shard_span.set_attribute("shard.segment", info.segment)
+ if info.group is not None:
+ shard_span.set_attribute("shard.group", info.group)
+ if info.total is not None:
+ shard_span.set_attribute("shard.total", info.total)
+ shard_span.set_attribute("shard.junit_filename", shard.junit_filename)
+ shard_span.set_attribute("shard.testcase_seconds", shard.testcase_seconds)
+ shard_span.set_attribute("shard.overhead_seconds", shard.overhead_seconds)
+
+ has_error = False
+ with trace.use_span(shard_span, end_on_exit=False):
+ # Pytest runs serially within a shard (no `-n` flag — confirmed in pytest.ini),
+ # so parse-time cumulative durations give non-overlapping per-test windows
+ # that stay stable even after threshold filtering.
+ for test in shard.tests:
+ test_span = tracer.start_span(test.nodeid, start_time=_to_ns(test.start))
+ test_span.set_attribute("test.outcome", test.outcome)
+ test_span.set_attribute("test.attempts", test.attempts)
+ test_span.set_attribute("test.classname", test.classname)
+ test_span.set_attribute("test.name", test.name)
+ if test.outcome in ("failed", "error"):
+ test_span.set_status(Status(StatusCode.ERROR))
+ has_error = True
+ test_span.end(end_time=_to_ns(test.end))
+
+ if has_error:
+ shard_span.set_status(Status(StatusCode.ERROR))
+ shard_span.end(end_time=_to_ns(shard.end))
+ return has_error
+
+
+# ---------- CLI ----------
+
+
+def parse_args(argv: list[str]) -> argparse.Namespace:
+ parser = argparse.ArgumentParser(description=__doc__.splitlines()[0] if __doc__ else "")
+ parser.add_argument("artifacts_root", type=Path, help="directory of downloaded junit-results-* artifacts")
+ parser.add_argument(
+ "--min-duration-seconds",
+ type=float,
+ default=0.5,
+ help="drop sub-threshold passing testcases (failures/reruns kept regardless)",
+ )
+ parser.add_argument(
+ "--otlp-endpoint",
+ default=os.environ.get("POSTHOG_OTLP_TRACES_ENDPOINT", DEFAULT_OTLP_ENDPOINT),
+ help=f"OTLP /v1/traces endpoint (default: $POSTHOG_OTLP_TRACES_ENDPOINT or {DEFAULT_OTLP_ENDPOINT})",
+ )
+ parser.add_argument("--dry-run", action="store_true", help="parse and summarize, do not emit")
+ return parser.parse_args(argv)
+
+
+def main(argv: list[str] | None = None) -> int:
+ logging.basicConfig(level=logging.INFO, format="%(message)s")
+ args = parse_args(argv if argv is not None else sys.argv[1:])
+
+ if not args.artifacts_root.exists():
+ logger.error("artifacts_root does not exist: %s", args.artifacts_root)
+ return 0
+
+ try:
+ shards = collect_shards(args.artifacts_root)
+ except Exception:
+ logger.exception("failed to collect shards")
+ return 0
+
+ pre_filter = sum(len(s.tests) for s in shards)
+ shards = filter_shards(shards, args.min_duration_seconds)
+ post_filter = sum(len(s.tests) for s in shards)
+ logger.info(
+ "collected %d shards, %d testcases (%d after %.2fs threshold filter)",
+ len(shards),
+ pre_filter,
+ post_filter,
+ args.min_duration_seconds,
+ )
+
+ if not shards:
+ return 0
+
+ if args.dry_run or os.environ.get("DRY_RUN") == "1":
+ for shard in shards[:3]:
+ logger.info(
+ " %s/%s shard %s: %d tests, %.1fs wall",
+ shard.info.suite,
+ shard.info.segment,
+ shard.info.group,
+ len(shard.tests),
+ (shard.end - shard.start).total_seconds(),
+ )
+ return 0
+
+ token = os.environ.get("POSTHOG_DEVEX_PROJECT_API_TOKEN", "")
+ if not token:
+ logger.warning("POSTHOG_DEVEX_PROJECT_API_TOKEN not set; skipping emit")
+ return 0
+
+ try:
+ emit_traces(shards, args.otlp_endpoint, token)
+ logger.info("emitted %d testcase spans to %s", post_filter, args.otlp_endpoint)
+ except Exception:
+ logger.exception("failed to emit traces")
+
+ return 0
+
+
+if __name__ == "__main__":
+ sys.exit(main())
diff --git a/.github/scripts/test_report_test_timings.py b/.github/scripts/test_report_test_timings.py
new file mode 100644
index 000000000000..11e93b4b6c5e
--- /dev/null
+++ b/.github/scripts/test_report_test_timings.py
@@ -0,0 +1,268 @@
+from __future__ import annotations
+
+import sys
+import json
+import textwrap
+import importlib.util
+from collections.abc import Iterator
+from contextlib import contextmanager
+from datetime import UTC, datetime, timedelta
+from pathlib import Path
+
+import pytest
+
+SCRIPT_PATH = Path(__file__).with_name("report_test_timings.py")
+SPEC = importlib.util.spec_from_file_location("report_test_timings", SCRIPT_PATH)
+assert SPEC is not None and SPEC.loader is not None
+report_test_timings = importlib.util.module_from_spec(SPEC)
+# Register before exec so @dataclass can resolve the module via sys.modules.
+sys.modules["report_test_timings"] = report_test_timings
+SPEC.loader.exec_module(report_test_timings)
+
+
+def _testcase(
+ *,
+ outcome: str = "passed",
+ attempts: int = 1,
+ duration: float = 1.0,
+ start: datetime | None = None,
+ name: str = "t",
+) -> report_test_timings.TestCase:
+ test_start = start if start is not None else datetime(2026, 5, 4, 10, 0, 0, tzinfo=UTC)
+ return report_test_timings.TestCase(
+ nodeid=f"m::{name}",
+ classname="m",
+ name=name,
+ duration_seconds=duration,
+ start=test_start,
+ end=test_start + timedelta(seconds=duration),
+ outcome=outcome,
+ attempts=attempts,
+ )
+
+
+# ---------- artifact name parsing ----------
+
+
+ "dir_name,expected",
+ [
+ ("junit-results-backend-core-29", ("backend", "core", 29)),
+ ("junit-results-async-migrations", ("async-migrations", "async-migrations", None)),
+ ("junit-results-llm-gateway", ("llm-gateway", "llm-gateway", None)),
+ ],
+)
+def test_derive_suite_segment_and_group(dir_name: str, expected: tuple[str, str, int | None]) -> None:
+ assert report_test_timings.derive_suite_segment_and_group(dir_name) == expected
+
+
+# ---------- shard parsing end-to-end ----------
+
+
+def _write_shard_xml(artifact_dir: Path, *, filename: str, timestamp: str, time: str, body: str) -> None:
+ artifact_dir.mkdir(parents=True, exist_ok=True)
+ (artifact_dir / filename).write_text(
+ textwrap.dedent(
+ f"""\
+ <?xml version="1.0"?>
+ <testsuites>
+ <testsuite name="pytest" timestamp="{timestamp}" time="{time}">
+ {body}
+ </testsuite>
+ </testsuites>
+ """
+ )
+ )
+
+
+def test_collect_shards_builds_test_windows_and_overhead(tmp_path: Path) -> None:
+ for group in range(1, 7):
+ (tmp_path / f"junit-results-backend-core-{group}").mkdir()
+ _write_shard_xml(
+ tmp_path / "junit-results-backend-core-7",
+ filename="junit-core.xml",
+ timestamp="2026-05-04T10:00:00.000000",
+ time="10.0",
+ body="""\
+ <testcase classname="pkg.test_a.TestA" name="test_fast" time="0.1"/>
+ <testcase classname="pkg.test_a.TestA" name="test_slow" time="2.0"/>
+ <testcase classname="pkg.test_a.TestA" name="test_rerun" time="0.2">
+ <rerunFailure message="x"/>
+ </testcase>
+ <testcase classname="pkg.test_a.TestA" name="test_fail" time="0.1"><failure message="x"/></testcase>
+ """,
+ )
+
+ shards = report_test_timings.collect_shards(tmp_path)
+
+ assert len(shards) == 1
+ shard = shards[0]
+ assert shard.info.suite == "backend"
+ assert shard.info.segment == "core"
+ assert shard.info.group == 7
+ assert shard.info.total == 7
+ assert shard.start == datetime(2026, 5, 4, 10, 0, 0, tzinfo=UTC)
+ assert (shard.end - shard.start).total_seconds() == pytest.approx(10.0)
+ assert shard.testcase_seconds == pytest.approx(2.4)
+ assert shard.overhead_seconds == pytest.approx(7.6)
+ assert shard.junit_filename == "junit-core.xml"
+ assert [t.name for t in shard.tests] == ["test_fast", "test_slow", "test_rerun", "test_fail"]
+ assert shard.tests[0].nodeid == "pkg/test_a/TestA::test_fast"
+ assert shard.tests[0].start == shard.start
+ assert shard.tests[0].end == datetime(2026, 5, 4, 10, 0, 0, 100000, tzinfo=UTC)
+ assert shard.tests[1].start == shard.tests[0].end
+ assert shard.tests[2].start == datetime(2026, 5, 4, 10, 0, 2, 100000, tzinfo=UTC)
+ assert shard.tests[2].outcome == "rerun_passed"
+ assert shard.tests[2].attempts == 2
+ assert shard.tests[3].outcome == "failed"
+
+
+# ---------- threshold filter ----------
+
+
+ "outcome,attempts,duration,threshold,expected",
+ [
+ ("passed", 1, 0.1, 0.5, False), # sub-threshold pass: dropped
+ ("passed", 1, 1.0, 0.5, True), # above threshold: kept
+ ("failed", 1, 0.0, 0.5, True), # failure: kept regardless of duration
+ ("error", 1, 0.0, 0.5, True), # error: kept regardless of duration
+ ("rerun_passed", 3, 0.1, 0.5, True), # rerun: kept regardless of duration
+ ],
+)
+def test_should_emit(outcome: str, attempts: int, duration: float, threshold: float, expected: bool) -> None:
+ test = _testcase(outcome=outcome, attempts=attempts, duration=duration)
+ assert report_test_timings.should_emit(test, threshold) is expected
+
+
+def test_filter_shards_preserves_parse_time_test_windows(tmp_path: Path) -> None:
+ _write_shard_xml(
+ tmp_path / "junit-results-backend-core-1",
+ filename="junit-core.xml",
+ timestamp="2026-05-04T10:00:00",
+ time="10.0",
+ body="""\
+ <testcase classname="pkg.t.T" name="test_fast_before" time="0.1"/>
+ <testcase classname="pkg.t.T" name="test_slow" time="2.0"/>
+ <testcase classname="pkg.t.T" name="test_rerun" time="0.2"><rerunFailure message="x"/></testcase>
+ <testcase classname="pkg.t.T" name="test_fail" time="0.1"><failure message="x"/></testcase>
+ """,
+ )
+
+ filtered = report_test_timings.filter_shards(
+ report_test_timings.collect_shards(tmp_path),
+ min_duration_seconds=0.5,
+ )
+
+ assert [test.name for test in filtered[0].tests] == ["test_slow", "test_rerun", "test_fail"]
+ assert filtered[0].tests[0].start == datetime(2026, 5, 4, 10, 0, 0, 100000, tzinfo=UTC)
+ assert filtered[0].tests[0].end == datetime(2026, 5, 4, 10, 0, 2, 100000, tzinfo=UTC)
+ assert filtered[0].tests[1].start == datetime(2026, 5, 4, 10, 0, 2, 100000, tzinfo=UTC)
+ assert filtered[0].tests[2].start == datetime(2026, 5, 4, 10, 0, 2, 300000, tzinfo=UTC)
+
+
+def test_emit_shard_span_uses_stored_test_windows(monkeypatch: pytest.MonkeyPatch) -> None:
+ class FakeSpan:
+ def __init__(self, name: str, start_time: int) -> None:
+ self.name = name
+ self.start_time = start_time
+ self.end_time: int | None = None
+ self.attributes: dict[str, str | int | float] = {}
+
+ def set_attribute(self, key: str, value: str | int | float) -> None:
+ self.attributes[key] = value
+
+ def set_status(self, status: object) -> None:
+ pass
+
+ def end(self, end_time: int) -> None:
+ self.end_time = end_time
+
+ class FakeTracer:
+ def __init__(self) -> None:
+ self.spans: list[FakeSpan] = []
+
+ def start_span(self, name: str, start_time: int) -> FakeSpan:
+ span = FakeSpan(name, start_time)
+ self.spans.append(span)
+ return span
+
+ @contextmanager
+ def use_span(span: FakeSpan, end_on_exit: bool = False) -> Iterator[None]:
+ yield
+
+ start = datetime(2026, 5, 4, 10, 0, 0, tzinfo=UTC)
+ shard = report_test_timings.Shard(
+ info=report_test_timings.ArtifactInfo(
+ path=Path("junit-results-backend-core-1"),
+ suite="backend",
+ segment="core",
+ group=1,
+ total=1,
+ ),
+ junit_filename="junit-core.xml",
+ start=start,
+ end=start + timedelta(seconds=10),
+ testcase_seconds=2.1,
+ overhead_seconds=7.9,
+ tests=[
+ _testcase(name="slow", duration=2.0, start=start + timedelta(seconds=0.1)),
+ _testcase(
+ name="fail",
+ outcome="failed",
+ duration=0.1,
+ start=start + timedelta(seconds=2.3),
+ ),
+ ],
+ )
+ tracer = FakeTracer()
+ monkeypatch.setattr(report_test_timings.trace, "use_span", use_span)
+
+ has_error = report_test_timings._emit_shard_span(tracer, shard)
+
+ assert has_error is True
+ assert [span.name for span in tracer.spans] == ["core-1", "m::slow", "m::fail"]
+ assert tracer.spans[1].start_time == report_test_timings._to_ns(start + timedelta(seconds=0.1))
+ assert tracer.spans[1].end_time == report_test_timings._to_ns(start + timedelta(seconds=2.1))
+ assert tracer.spans[2].start_time == report_test_timings._to_ns(start + timedelta(seconds=2.3))
+ assert tracer.spans[2].end_time == report_test_timings._to_ns(start + timedelta(seconds=2.4))
+ assert tracer.spans[0].attributes["shard.testcase_seconds"] == pytest.approx(2.1)
+ assert tracer.spans[0].attributes["shard.overhead_seconds"] == pytest.approx(7.9)
+
+
+# ---------- workflow context ----------
+
+
+def test_workflow_resource_attributes_includes_query_and_drilldown_fields(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ event_path = tmp_path / "event.json"
+ event_path.write_text(json.dumps({"number": 57216}))
+ monkeypatch.setenv("GITHUB_EVENT_PATH", str(event_path))
+ monkeypatch.setenv("GITHUB_EVENT_NAME", "pull_request")
+ monkeypatch.setenv("GITHUB_HEAD_REF", "worktree-per-test-telemetry-junit")
+ monkeypatch.setenv("GITHUB_BASE_REF", "master")
+ monkeypatch.setenv("GITHUB_SERVER_URL", "https://github.com")
+ monkeypatch.setenv("GITHUB_REPOSITORY", "PostHog/posthog")
+ monkeypatch.setenv("GITHUB_RUN_ID", "25218527467")
+
+ attrs = report_test_timings.workflow_resource_attributes()
+
+ assert attrs["ci.event_name"] == "pull_request"
+ assert attrs["ci.head_ref"] == "worktree-per-test-telemetry-junit"
+ assert attrs["ci.base_ref"] == "master"
+ assert attrs["ci.pr_number"] == 57216
+ assert attrs["ci.run_url"] == "https://github.com/PostHog/posthog/actions/runs/25218527467"
+
+
+# ---------- trace id ----------
+
+
+def test_deterministic_trace_id_is_stable_across_processes() -> None:
+ a = report_test_timings.deterministic_trace_id("25218527467", "1")
+ b = report_test_timings.deterministic_trace_id("25218527467", "1")
+ assert a == b
+ # Different attempt -> different trace id (so reruns of the same workflow run id are isolated).
+ assert report_test_timings.deterministic_trace_id("25218527467", "2") != a
+ # Result fits in 128 bits.
+ assert 0 <= a < 2**128
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index 143bd7cc3e67..78406df49b1a 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -1375,7 +1375,7 @@ jobs:
shell: bash
run: | # async_migrations covered in ci-async-migrations.yml
set +e
- pytest -v --tb=short --reuse-db ${{
+ pytest -v --tb=short --reuse-db -o junit_duration_report=call ${{
matrix.compat
&& env.CLICKHOUSE_COMPAT_PYTEST_TARGETS
|| (
@@ -1442,7 +1442,7 @@ jobs:
MODAL_TOKEN_SECRET: ${{ needs.changes.outputs.tasks_temporal == 'true' && secrets.MODAL_TOKEN_SECRET || '' }}
run: |
set +e
- pytest -v --tb=short --reuse-db --timeout=600 --timeout-method=thread posthog/temporal products/batch_exports/backend/tests/temporal products/tasks/backend/temporal -m "not async_migrations" \
+ pytest -v --tb=short --reuse-db -o junit_duration_report=call --timeout=600 --timeout-method=thread posthog/temporal products/batch_exports/backend/tests/temporal products/tasks/backend/temporal -m "not async_migrations" \
--splits ${{ matrix.concurrency }} --group ${{ matrix.group }} \
--durations=100 --durations-min=1.0 --store-durations \
--splitting-algorithm=duration_based_chunks \
@@ -1926,3 +1926,58 @@ jobs:
github-token: ${{ steps.telemetry-app-token.outputs.token }}
status-job: 'Django Tests Pass'
runner: 'depot'
+
+ report-test-timings:
+ name: Report per-test traces
+ needs: [django_tests]
+ runs-on: ubuntu-latest
+ continue-on-error: true
+ timeout-minutes: 10
+ # Master pushes always; PRs only when labelled `capture-test-timings`. Skip forks (no secrets access).
+ if: >-
+ always() &&
+ github.repository == 'PostHog/posthog' &&
+ (
+ (github.event_name != 'pull_request' && github.ref == 'refs/heads/master') ||
+ (github.event_name == 'pull_request' &&
+ github.event.pull_request.head.repo.full_name == 'PostHog/posthog' &&
+ contains(github.event.pull_request.labels.*.name, 'capture-test-timings'))
+ )
+ steps:
+ # Labelled PRs run the trusted base parser, so the same PR can't swap the script that receives secrets.
+ - uses: actions/checkout@v6
+ if: github.run_attempt == '1'
+ with:
+ ref: ${{ github.event_name == 'pull_request' && github.event.pull_request.base.sha || github.sha }}
+ fetch-depth: 1
+ - name: Check for trusted timing reporter
+ id: timing-reporter
+ if: github.run_attempt == '1'
+ shell: bash
+ run: |
+ if [[ -f .github/scripts/report_test_timings.py ]]; then
+ echo "available=true" >> "$GITHUB_OUTPUT"
+ else
+ echo "available=false" >> "$GITHUB_OUTPUT"
+ echo "Trusted timing reporter is not available on the checked-out ref; skipping emit."
+ fi
+ - uses: astral-sh/setup-uv@eac588ad8def6316056a12d4907a9d4d84ff7a3b # v7.3.0
+ if: github.run_attempt == '1' && steps.timing-reporter.outputs.available == 'true'
+ with:
+ version: '0.10.2' # pinned: unpinned setup-uv calls GH API on every job, exhausts rate limit
+ - name: Download junit artifacts
+ if: github.run_attempt == '1' && steps.timing-reporter.outputs.available == 'true'
+ uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v6.0.0
+ with:
+ path: ./junit-artifacts
+ pattern: junit-results-*
+ - name: Emit per-test traces
+ if: github.run_attempt == '1' && steps.timing-reporter.outputs.available == 'true'
+ continue-on-error: true
+ env:
+ POSTHOG_DEVEX_PROJECT_API_TOKEN: ${{ secrets.POSTHOG_DEVEX_PROJECT_API_TOKEN }}
+ POSTHOG_OTLP_TRACES_ENDPOINT: ${{ vars.POSTHOG_OTLP_TRACES_ENDPOINT || 'https://us.i.posthog.com/i/v1/traces' }}
+ run: |
+ uv run --script .github/scripts/report_test_timings.py \
+ --min-duration-seconds=0.5 \
+ ./junit-artifacts