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(logs): build logs alert links when a filter holds a regex (#127939)

Jimmy Zhu5063109d7f86
diff --git a/products/logs/backend/platform_alert_wording.py b/products/logs/backend/platform_alert_wording.py
index 663ed304f624..08421e836d4c 100644
--- a/products/logs/backend/platform_alert_wording.py
+++ b/products/logs/backend/platform_alert_wording.py
@@ -78,7 +78,13 @@ def _context(filters: dict[str, Any]) -> tuple[str, ...]:
def _logs_url(project_id: int, transition: AnnouncedTransition) -> str:
params = _window_params(transition)
- return absolute_uri(f"/project/{project_id}/logs" + (f"?{params}" if params else ""))
+ return _logs_page_url(project_id) + (f"?{params}" if params else "")
+
+
+def _logs_page_url(project_id: int) -> str:
+ # `absolute_uri` refuses any URL that holds `%5C`, and a regex filter encodes its backslash that
+ # way. The query goes on afterwards, which is safe because a query cannot change the link's host.
+ return absolute_uri(f"/project/{project_id}/logs")
def in_utc(transition: AnnouncedTransition) -> AnnouncedTransition:
@@ -173,7 +179,7 @@ def legacy_logs_webhook_body(
"threshold_operator": condition["threshold_operator"],
"window_minutes": condition["window_minutes"],
# The legacy template always added the "?", even with no parameters.
- "logs_url": absolute_uri(f"/project/{project_id}/logs?{_window_params(transition)}"),
+ "logs_url": f"{_logs_page_url(project_id)}?{_window_params(transition)}",
}
else:
error_key = "last_error_message" if transition.kind == AlertEventKind.BROKEN else "error_message"
diff --git a/products/logs/backend/test/test_platform_alert_wording.py b/products/logs/backend/test/test_platform_alert_wording.py
index 9177122ee893..5bca7d590086 100644
--- a/products/logs/backend/test/test_platform_alert_wording.py
+++ b/products/logs/backend/test/test_platform_alert_wording.py
@@ -220,11 +220,13 @@ class TestLogsPlatformAlertWording(SimpleTestCase):
assert body["data"]["logs_url"] == aware["data"]["logs_url"]
@parameterized.expand([("legacy_webhook_body",), ("message_link",)])
- def test_a_logs_link_opens_the_window_the_check_queried(self, link: str) -> None:
+ def test_a_logs_link_opens_the_window_and_filters_the_check_queried(self, link: str) -> None:
# The ingestion checkpoint clamped this check's window end to well before it was recorded.
window_end = OCCURRED - timedelta(minutes=7)
+ status_regex = {"key": "http.status_code", "type": "log_attribute", "operator": "regex", "value": r"^4\d{2}$"}
+ filter_group = {"type": "AND", "values": [{"type": "AND", "values": [status_regex]}]}
transition = replace(
- _transition(filters={"serviceNames": ["api"]}),
+ _transition(filters={"serviceNames": ["api"], "filterGroup": filter_group}),
evaluation_key=f"slot:{OCCURRED.isoformat()}|window:{window_end.isoformat()}",
)
@@ -240,6 +242,7 @@ class TestLogsPlatformAlertWording(SimpleTestCase):
query = parse_qs(urlsplit(url).query)
date_range = json.loads(query["dateRange"][0])
assert date_range["date_to"] == window_end.isoformat()
+ assert json.loads(query["filterGroup"][0]) == filter_group
def _render(node: Any, globals_: dict[str, Any]) -> Any: