: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import base64
import datetime as dt
from typing import Any
from posthog.test.base import APIBaseTest, ClickhouseTestMixin
from django.utils import timezone
from products.metrics.backend.facade.api import investigate, investigate_incident
from products.metrics.backend.facade.contracts import CompanionMetric, CompanionVerdict, IncidentContext
from products.metrics.backend.tests._seeder import seed_metric, seed_metric_event, truncate_metrics_tables
def _trace_hex(i: int) -> str:
return f"{i:032x}".upper()
def _trace_b64(i: int) -> str:
return base64.b64encode(bytes.fromhex(_trace_hex(i))).decode()
class TestInvestigate(ClickhouseTestMixin, APIBaseTest):
CLASS_DATA_LEVEL_SETUP = True
def setUp(self):
super().setUp()
truncate_metrics_tables()
# 20-minute window: minutes 0-9 baseline, 10-19 anomaly (spike from 12).
self.start = (timezone.now() - dt.timedelta(hours=1)).replace(second=0, microsecond=0)
self.anomaly_from = self.start + dt.timedelta(minutes=10)
self.anomaly_to = self.start + dt.timedelta(minutes=20)
def _spike(self, baseline: float, peak: float) -> list[tuple[dt.datetime, float]]:
baseline_pts = [(self.start + dt.timedelta(minutes=m), baseline) for m in range(12)]
spike_pts = [(self.start + dt.timedelta(minutes=m), peak) for m in range(12, 20)]
return baseline_pts + spike_pts
def _flat(self, value: float) -> list[tuple[dt.datetime, float]]:
return [(self.start + dt.timedelta(minutes=m), value) for m in range(20)]
def _investigate(self, **overrides: Any):
defaults: dict[str, Any] = {
"team": self.team,
"metric_name": "ingestion_lag",
"anomaly_from": self.anomaly_from,
"anomaly_to": self.anomaly_to,
}
defaults.update(overrides)
return investigate(**defaults)
def _verdict(self, result: Any, metric_name: str) -> CompanionVerdict:
return next(c for c in result.companions if c.metric_name == metric_name)
def test_localized_spike_rules_companions_in_and_out(self):
# The metric spikes on one service; a second service stays flat → localized.
seed_metric(
team_id=self.team.id,
metric_name="ingestion_lag",
points=self._spike(10.0, 100.0),
service_name="logs-ingestion",
)
seed_metric(team_id=self.team.id, metric_name="ingestion_lag", points=self._flat(10.0), service_name="other")
# Throughput held flat (not a traffic surge); error rate moved with it.
seed_metric(
team_id=self.team.id, metric_name="throughput", points=self._flat(120.0), service_name="logs-ingestion"
)
seed_metric(
team_id=self.team.id, metric_name="error_rate", points=self._spike(1.0, 40.0), service_name="logs-ingestion"
)
result = self._investigate(
companions=(
CompanionMetric(metric_name="throughput", role="traffic"),
CompanionMetric(metric_name="error_rate", role="saturation"),
),
)
self.assertEqual(result.symptom.direction, "up")
self.assertEqual(result.blast_radius, "localized")
self.assertEqual(result.evidence.service_name, "logs-ingestion")
assert result.evidence.log_filter is not None
self.assertEqual(result.evidence.log_filter["service_name"], "logs-ingestion")
self.assertEqual(result.evidence.log_filter["severity"], "error")
self.assertFalse(self._verdict(result, "throughput").moved_with_symptom)
self.assertTrue(self._verdict(result, "error_rate").moved_with_symptom)
# No metrics rows exist -> the pivot stays empty rather than erroring.
self.assertEqual(result.evidence.trace_exemplars, ())
self.assertEqual(result.confidence, "high")
self.assertIn("logs-ingestion", result.narrative)
# Hero chart + one per companion, each on the symptom's (non-empty) window.
self.assertEqual([c.metric_name for c in result.chart_specs], ["ingestion_lag", "throughput", "error_rate"])
self.assertTrue(all(c.anomaly_from and c.anomaly_to for c in result.chart_specs))
# Non-histogram aggregations carry no quantile on their chart specs.
self.assertTrue(all(c.quantile is None for c in result.chart_specs))
def test_many_services_moving_together_is_a_shared_cause(self):
seed_metric(
team_id=self.team.id, metric_name="ingestion_lag", points=self._spike(10.0, 100.0), service_name="svc-a"
)
seed_metric(
team_id=self.team.id, metric_name="ingestion_lag", points=self._spike(10.0, 90.0), service_name="svc-b"
)
result = self._investigate()
self.assertEqual(result.symptom.direction, "up")
self.assertEqual(result.blast_radius, "shared")
def test_dominant_scale_mover_is_localized_not_shared(self):
# A large-footprint service shifts modestly while a tiny one triples. By
# magnitude the big service dominates (-> localized); a raw change_ratio
# compare would rank the tiny explosive one above it and mislabel the
# blast radius "shared".
seed_metric(
team_id=self.team.id, metric_name="ingestion_lag", points=self._spike(1000.0, 1100.0), service_name="big"
)
seed_metric(
team_id=self.team.id, metric_name="ingestion_lag", points=self._spike(1.0, 3.0), service_name="small"
)
result = self._investigate()
self.assertEqual(result.symptom.direction, "up")
self.assertEqual(result.blast_radius, "localized")
self.assertEqual(result.symptom.top_movers[0].label, "big")
def test_attaches_trace_exemplars_from_the_anomaly_window_only(self):
# If exemplar wiring regresses (window ignored, untraced samples included,
# or the cap dropped), an investigation would pivot into the wrong traces
# or bloat the result; nothing else exercises evidence.trace_exemplars.
seed_metric(
team_id=self.team.id,
metric_name="ingestion_lag",
points=self._spike(10.0, 100.0),
service_name="logs-ingestion",
)
in_window = self.anomaly_from + dt.timedelta(minutes=2)
# Six traced samples inside the window (one more than the cap), each its
# own emission so each carries a distinct trace id.
for i in range(1, 7):
seed_metric_event(
team_id=self.team.id,
metric_name="ingestion_lag",
points=[(in_window + dt.timedelta(seconds=i), 100.0 + i)],
trace_id=_trace_b64(i),
attributes={"pod": f"pod-{i}"},
)
# A traced sample before the window and an untraced one inside it must
# both be excluded.
seed_metric_event(
team_id=self.team.id,
metric_name="ingestion_lag",
points=[(self.start + dt.timedelta(minutes=1), 10.0)],
trace_id=_trace_b64(99),
attributes={"pod": "baseline"},
)
seed_metric_event(
team_id=self.team.id,
metric_name="ingestion_lag",
points=[(in_window, 100.0)],
attributes={"pod": "untraced"},
)
result = self._investigate()
self.assertEqual(len(result.evidence.trace_exemplars), 5)
# Newest first, so the cap keeps traces 6..2 and drops 1.
self.assertEqual(
[e.trace_id for e in result.evidence.trace_exemplars], [_trace_hex(i) for i in (6, 5, 4, 3, 2)]
)
newest = result.evidence.trace_exemplars[0]
self.assertEqual(newest.value, 106.0)
self.assertTrue(
newest.timestamp.startswith((in_window + dt.timedelta(seconds=6)).strftime("%Y-%m-%dT%H:%M:%S"))
)
self.assertNotIn(_trace_hex(99), [e.trace_id for e in result.evidence.trace_exemplars])
def test_flat_metric_yields_low_confidence(self):
seed_metric(
team_id=self.team.id, metric_name="ingestion_lag", points=self._flat(50.0), service_name="logs-ingestion"
)
result = self._investigate()
self.assertEqual(result.symptom.direction, "flat")
self.assertEqual(result.confidence, "low")
self.assertIn("held flat", result.narrative)
def test_incident_derives_window_from_fire_time(self):
# No timestamp math on the caller: the window comes from fired_at alone.
seed_metric(
team_id=self.team.id,
metric_name="ingestion_lag",
points=self._spike(10.0, 100.0),
service_name="logs-ingestion",
)
result = investigate_incident(
team=self.team,
context=IncidentContext(
metric_name="ingestion_lag",
fired_at=self.start + dt.timedelta(minutes=20),
lookback=dt.timedelta(minutes=10),
leadout=dt.timedelta(minutes=5),
),
)
self.assertEqual(result.symptom.anomaly_from, (self.start + dt.timedelta(minutes=10)).isoformat())
self.assertEqual(result.symptom.anomaly_to, (self.start + dt.timedelta(minutes=25)).isoformat())
self.assertEqual(result.symptom.direction, "up")
def test_incident_scopes_to_the_implicated_service(self):
# svc-a is flat, svc-b spikes; scoping to svc-a must hide svc-b's spike.
seed_metric(team_id=self.team.id, metric_name="ingestion_lag", points=self._flat(10.0), service_name="svc-a")
seed_metric(
team_id=self.team.id, metric_name="ingestion_lag", points=self._spike(10.0, 100.0), service_name="svc-b"
)
result = investigate_incident(
team=self.team,
context=IncidentContext(
metric_name="ingestion_lag",
fired_at=self.start + dt.timedelta(minutes=20),
lookback=dt.timedelta(minutes=10),
leadout=dt.timedelta(0),
service_name="svc-a",
),
)
self.assertEqual(result.symptom.direction, "flat")
def test_incident_context_normalizes_fired_at_to_utc(self):
# fired_at must be an explicit instant: naive datetimes are rejected at
# construction rather than silently mis-bucketed as UTC, and aware
# non-UTC instants are normalized so window math always runs in UTC.
with self.assertRaises(ValueError):
IncidentContext(metric_name="ingestion_lag", fired_at=dt.datetime(2026, 1, 1, 12, 0))
ist = dt.timezone(dt.timedelta(hours=5, minutes=30))
context = IncidentContext(metric_name="ingestion_lag", fired_at=dt.datetime(2026, 1, 1, 17, 30, tzinfo=ist))
self.assertEqual(context.fired_at, dt.datetime(2026, 1, 1, 12, 0, tzinfo=dt.UTC))
self.assertEqual(context.fired_at.tzinfo, dt.UTC)