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.

fix(eng-analytics): report product test spans (#73290)

Raúl Negrón-Otero791c1898f054
diff --git a/.depot/workflows/ci-backend.yml b/.depot/workflows/ci-backend.yml
index d85d6c2e615f..df9781c6be02 100644
--- a/.depot/workflows/ci-backend.yml
+++ b/.depot/workflows/ci-backend.yml
@@ -34,9 +34,9 @@
# - report-test-timings: canonical emits per-test OTLP spans from the uploaded JUnit. The shadow
# strips artifact uploads (above) and posts no telemetry, so the whole job is depot-exempt —
# changes confined to it (script deps, the reporter's availability guard, emission secret
-# plumbing such as dual-project tokens, re-run attempt handling — including the
-# attempt-suffixed junit artifact names canonical uploads so the reporter can pair re-run
-# recoveries with attempt-1 failures) need no depot bump.
+# plumbing such as dual-project tokens, the JUnit-artifact download pattern, re-run attempt
+# handling — including the attempt-suffixed junit artifact names canonical uploads so the
+# reporter can pair re-run recoveries with attempt-1 failures) need no depot bump.
# - OpenAPI types: the check-openapi-types job is folded into check-migrations as a
# check-only step (drift still fails the run); only its auto-commit side effect is dropped
# - events_json variants: the shadow never doubles the matrix (no RUN_NEW_EVENTS_SCHEMA
diff --git a/.github/scripts/report_test_timings.py b/.github/scripts/report_test_timings.py
index 984fc7e30977..9cac35a29506 100644
--- a/.github/scripts/report_test_timings.py
+++ b/.github/scripts/report_test_timings.py
@@ -14,8 +14,8 @@
# ///
"""Emit OTLP traces from Backend CI JUnit XML artifacts.
-Reads `junit-results-*` artifacts (downloaded by the workflow) and emits one
-trace per job (shard) shaped:
+Reads the JUnit artifacts downloaded by the workflow (`junit-results-backend-*`
+and `product-junit-results-*`) and emits one trace per job (shard) shaped:
<workflow> / <job> (root, one trace per job)
├── <pytest nodeid> (test)
@@ -48,7 +48,7 @@ import logging
import secrets
import argparse
from collections.abc import Callable, Mapping
-from dataclasses import dataclass
+from dataclasses import dataclass, replace
from datetime import UTC, datetime, timedelta
from functools import cache
from pathlib import Path
@@ -250,6 +250,27 @@ def to_selector(file: str, classname: str, name: str) -> str:
return ""
+def product_name(junit_filename: str) -> str:
+ """`junit-product-<name>.xml` → `<name>`; '' for any other junit filename."""
+ if not junit_filename.startswith("junit-product-") or not junit_filename.endswith(".xml"):
+ return ""
+ return junit_filename[len("junit-product-") : -len(".xml")]
+
+
+def product_shard_info(info: ArtifactInfo, junit_filename: str) -> ArtifactInfo:
+ """Give each product its own suite/segment so its spans form a distinct, readable trace.
+
+ Product shards arrive in bin-packed `product-junit-results-<job-index>` artifacts, so the
+ artifact-derived suite/segment is a meaningless `product-junit-results`; the product name
+ lives in the JUnit filename instead. Keep `<job-index>` as the group so a product split across
+ buckets stays one trace per bucket rather than colliding with its siblings into a single trace.
+ """
+ product = product_name(junit_filename)
+ if not product:
+ return info
+ return replace(info, suite="product", segment=product, total=None)
+
+
def parse_iso_utc(value: str) -> datetime | None:
"""Parse an ISO 8601 timestamp (pytest emits naive); treat as UTC."""
if not value:
@@ -313,6 +334,8 @@ def parse_shard(xml_path: Path, info: ArtifactInfo) -> Shard | None:
for tc in suite_elem.iter("testcase"):
classname = tc.get("classname", "")
name = tc.get("name", "")
+ # Products run pytest with `--rootdir ../..`, so `file`/`classname` are already
+ # repo-relative (`products/<name>/...`) — no prefixing needed here.
file = tc.get("file", "")
outcome, attempts = classify_testcase(tc)
try:
@@ -346,7 +369,7 @@ def parse_shard(xml_path: Path, info: ArtifactInfo) -> Shard | None:
end = start + timedelta(seconds=wall_seconds)
testcase_seconds = sum(t.duration_seconds for t in tests)
return Shard(
- info=info,
+ info=product_shard_info(info, xml_path.name),
junit_filename=xml_path.name,
start=start,
end=end,
@@ -655,7 +678,11 @@ def emission_tokens(env: Mapping[str, str]) -> list[str]:
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(
+ "artifacts_root",
+ type=Path,
+ help="directory of downloaded JUnit artifacts (junit-results-backend-* and product-junit-results-*)",
+ )
parser.add_argument(
"--min-duration-seconds",
type=float,
diff --git a/.github/scripts/test_report_test_timings.py b/.github/scripts/test_report_test_timings.py
index 3e1221acd89d..7dd7a82fd6c5 100644
--- a/.github/scripts/test_report_test_timings.py
+++ b/.github/scripts/test_report_test_timings.py
@@ -531,6 +531,47 @@ def test_emit_shard_span_stamps_owner_team_only_for_owned_files(monkeypatch: pyt
assert "test.owner_team" not in tracer.spans[2].attributes
+def test_product_shard_derives_product_suite_and_keeps_repo_relative_paths(
+ tmp_path: Path, monkeypatch: pytest.MonkeyPatch
+) -> None:
+ # Products run pytest with `--rootdir ../..`, so real product JUnit `file`/`classname`
+ # are already repo-relative; the span must carry them through, not double-prefix them.
+ _write_shard_xml(
+ tmp_path / "product-junit-results-1",
+ filename="junit-product-warehouse_sources.xml",
+ timestamp="2026-05-04T10:00:00",
+ time="1.0",
+ body=(
+ '<testcase classname="products.warehouse_sources.backend.migrations.test.test_migration_0075.TestBackfillApiVersion" '
+ 'file="products/warehouse_sources/backend/migrations/test/test_migration_0075.py" name="test_backfill" time="1.0"/>'
+ ),
+ )
+ shard = report_test_timings.collect_shards(tmp_path)[0]
+ assert shard.info.suite == "product"
+ assert shard.info.segment == "warehouse_sources"
+ assert report_test_timings.job_trace_name("Backend CI", shard.info) == "Backend CI / warehouse_sources (1)"
+
+ tracer = _FakeTracer()
+ owner_paths: list[str] = []
+ monkeypatch.setattr(report_test_timings.trace, "use_span", _noop_use_span)
+
+ def owner_of(file: str) -> str:
+ owner_paths.append(file)
+ return "team-data-warehouse"
+
+ report_test_timings._emit_shard_span(tracer, shard, "Backend CI / warehouse_sources (1)", owner_of)
+
+ test_span = tracer.spans[1]
+ expected_file = "products/warehouse_sources/backend/migrations/test/test_migration_0075.py"
+ assert (
+ test_span.name
+ == "products/warehouse_sources/backend/migrations/test/test_migration_0075/TestBackfillApiVersion::test_backfill"
+ )
+ assert test_span.attributes["test.selector"] == f"{expected_file}::TestBackfillApiVersion::test_backfill"
+ assert test_span.attributes["test.owner_team"] == "team-data-warehouse"
+ assert owner_paths == [expected_file]
+
+
# ---------- workflow context ----------
diff --git a/.github/workflows/ci-backend.yml b/.github/workflows/ci-backend.yml
index 978a9d72df78..a83ab449ae32 100644
--- a/.github/workflows/ci-backend.yml
+++ b/.github/workflows/ci-backend.yml
@@ -953,8 +953,9 @@ jobs:
[ -f "$f" ] || continue
cp "$f" "junit-staging/junit-product-$(basename "$(dirname "$f")").xml"
done
- # Separate prefix from junit-results-backend-* on purpose: those are consumed by
- # test-selection-verdict (snob shadow) and report-test-timings, not product tests.
+ # Separate artifact prefix from junit-results-backend-* so consumers can select
+ # product JUnit by download pattern; the product name rides in each inner filename
+ # (junit-product-<name>.xml). The failure rollup and timing reporter consume both.
- name: Upload product test results
uses: actions/upload-artifact@b7c566a772e6b6bfb58ed0dc250532a479d7789f # v6.0.0
if: always()
@@ -3607,7 +3608,7 @@ jobs:
uses: actions/download-artifact@634f93cb2916e3fdff6788551b99b062d0335ce0 # v6.0.0
with:
path: ./junit-artifacts
- pattern: junit-results-*
+ pattern: '{junit-results-backend,product-junit-results}-*'
- name: Emit per-test traces
if: steps.timing-reporter.outputs.available == 'true'
continue-on-error: true