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(logs): show recent sends on a logs alert destination (#120702)

Jimmy Zhu3f86d8014f1e
diff --git a/frontend/snapshots.yml b/frontend/snapshots.yml
index 748c8ecb9254..4b3fa57e7f62 100644
--- a/frontend/snapshots.yml
+++ b/frontend/snapshots.yml
@@ -12810,6 +12810,22 @@ snapshots:
hash: v1.k794b7964.4e5e8fcbc5e9ff76ccda6b84a6bfb7a99d28334c6b2afc4ee6bb08e221fde8e6.Euz0mSfdHk2e8kSuHfbkcOEuljQWO8FJAMneFQE5EHk
scenes-app-llm-observability-taggers-deprecation-banner--default--light:
hash: v1.k794b7964.4afb0ca40061311438558a25b30e8afe725a6645742a036410f3803dcb7ce3b1.I-UIHserKVYTZtQOJ1HTlRwwhbn-uFPSzYzjgdbUGK0
+ scenes-app-logs-alerts-destination-recent-sends--both-paths--dark:
+ hash: v1.k794b7964.3bb4a9da0eade0ef5f796131e57c13648db672ab33d95b04b0a5b1625a120938.8lvCSL27-TQR53BixqjvwDy7sSZHQ3iXIG-leLgKcNE
+ scenes-app-logs-alerts-destination-recent-sends--both-paths--light:
+ hash: v1.k794b7964.1633f5b4a9dd7d4711c175090c1b8975039f27312cb56f6b85c82cfc60d73c3c.Pn-ZmyiPCySmgNwX7xKyZcuCKz1L2DdR_DaM9eOLcM4
+ scenes-app-logs-alerts-destination-recent-sends--empty--dark:
+ hash: v1.k794b7964.5d009774caa348fd16cd53215e01bd32be96e185a55b0955f5cac2d66fd47e3d.8CnU1iOavYzBp6LPZhxxp_T_EPmx-saWR3zK3y6t7yI
+ scenes-app-logs-alerts-destination-recent-sends--empty--light:
+ hash: v1.k794b7964.56c1383cb1903e606cf3612674bfa195bbbd318c5267cf42a350104e9674313e.59q00kFTcIwe7QH2yi02ibh-euk4pyTlV0ijy-Lh6x4
+ scenes-app-logs-alerts-destination-recent-sends--failed--dark:
+ hash: v1.k794b7964.dc049b45db200405d20d2d9204ccc40436d462283cc6efa71a4b13636e2f9447.KNt9fs4R02JB66MnR_YyGPK3Xr33gAMYfY9cg5lAxj4
+ scenes-app-logs-alerts-destination-recent-sends--failed--light:
+ hash: v1.k794b7964.034b911742bd7113869c61bd9914ccbb53bcaae78e04026d3c1d1435c95433f7.hpGA6Q_iv09QpryVoq1r-EW-tkW2MsgCawFWkOqDHyo
+ scenes-app-logs-alerts-destination-recent-sends--loading--dark:
+ hash: v1.k794b7964.22e7e9fcfdfe4e2f479e8b03566a50551f0ad4800c7ab851bc5d9b73f7476d87.EjbvTKQL8vNyGPXLUvvvAuMaL7IwbnIDtdb96nu5jDM
+ scenes-app-logs-alerts-destination-recent-sends--loading--light:
+ hash: v1.k794b7964.00b73522018206d387b1e609fc568a4e090a79d40092b92df6dc0dad8b37e7e5.rzagY8k4iTWqG_whXUkYWnr2U9mcx-PvpfMNI7S7MrA
scenes-app-logs-logsimpactcounts--no-coverage--dark:
hash: v1.k794b7964.fcfa5be29c5721d0db9fa21ecab20f16c04a4074c9af7fb3a58311900e168a48.2eslY8aigbAuKJ2Bsn8AjSOah5Q77FoWONVdG2fb6C4
scenes-app-logs-logsimpactcounts--no-coverage--light:
diff --git a/products/alerts/backend/facade/destinations.py b/products/alerts/backend/facade/destinations.py
index 2c914e60fc2f..3ffec29c9b64 100644
--- a/products/alerts/backend/facade/destinations.py
+++ b/products/alerts/backend/facade/destinations.py
@@ -162,6 +162,14 @@ def list_alert_destination_groups(
)
+def owned_alert_destination_ids(
+ *, team_id: int, alert_id: str, allowed_event_ids: Collection[str], hog_function_ids: Collection[UUID]
+) -> frozenset[UUID]:
+ return destinations.owned_alert_destination_ids(
+ team_id=team_id, alert_id=alert_id, allowed_event_ids=allowed_event_ids, hog_function_ids=hog_function_ids
+ )
+
+
def list_owned_alert_destinations(
*,
team_id: int,
diff --git a/products/alerts/backend/logic/destinations.py b/products/alerts/backend/logic/destinations.py
index e7bd34bc8626..192fe440b853 100644
--- a/products/alerts/backend/logic/destinations.py
+++ b/products/alerts/backend/logic/destinations.py
@@ -158,6 +158,17 @@ def owned_alert_destinations_qs(
return functions.filter(template_id__in=SPEC_BY_TEMPLATE_ID) if known_templates_only else functions
+def owned_alert_destination_ids(
+ *, team_id: int, alert_id: str, allowed_event_ids: Collection[str], hog_function_ids: Collection[UUID]
+) -> frozenset[UUID]:
+ """The named HogFunctions that are destinations of this alert, without reading their inputs."""
+ return frozenset(
+ owned_alert_destinations_qs(team_id=team_id, alert_ids=[alert_id], allowed_event_ids=allowed_event_ids)
+ .filter(pk__in=list(hog_function_ids))
+ .values_list("id", flat=True)
+ )
+
+
def list_owned_alert_destinations(
*,
team_id: int,
diff --git a/products/alerts_platform/backend/delivery/dispatch.py b/products/alerts_platform/backend/delivery/dispatch.py
index 00e28d55060a..f74340434bfc 100644
--- a/products/alerts_platform/backend/delivery/dispatch.py
+++ b/products/alerts_platform/backend/delivery/dispatch.py
@@ -13,7 +13,12 @@ from products.alerts_platform.backend.delivery.message import AlertMessage, buil
from products.alerts_platform.backend.delivery.root_state import state_line
from products.alerts_platform.backend.delivery.telemetry import record_delivery
from products.alerts_platform.backend.delivery.thread_store import ThreadBusy, ThreadKey, ThreadStore
-from products.alerts_platform.backend.delivery.transport import DeliveryTransport, MessageHandle, RootEditor
+from products.alerts_platform.backend.delivery.transport import (
+ DeliveryError,
+ DeliveryTransport,
+ MessageHandle,
+ RootEditor,
+)
from products.alerts_platform.backend.facade.contracts import (
AlertDestinationData,
AnnouncedTransition,
@@ -34,6 +39,7 @@ def deliver(
configuration_id: str,
evaluation_key: str,
target: AlertDestinationData,
+ destination_id: str | None,
announcement: EvaluationAnnouncement,
incident_action: IncidentAction | None = None,
source_alert_url: str | None = None,
@@ -57,6 +63,7 @@ def deliver(
team_id=team_id,
configuration_id=configuration_id,
target=target,
+ destination_id=destination_id,
message=build_message(
announcement,
transition,
@@ -86,6 +93,7 @@ def deliver(
team_id=team_id,
configuration_id=configuration_id,
target=target,
+ destination_id=destination_id,
message=build_message(
announcement,
transition,
@@ -116,19 +124,32 @@ def _send(
team_id: int,
configuration_id: str,
target: AlertDestinationData,
+ destination_id: str | None,
message: AlertMessage,
in_reply_to: MessageHandle | None,
) -> MessageHandle | None:
try:
sent = transport.deliver(team_id=team_id, target=target, message=message, in_reply_to=in_reply_to)
- except Exception:
+ except Exception as error:
# Every failure class. Counting only the refusals a provider names leaves an outage
- # reading as no delivery attempted at all.
+ # reading as no delivery attempted at all. A transport words its own refusals safely, and
+ # any other exception's text can carry a credential URL, so it is named by class.
record_delivery(
- team_id=team_id, configuration_id=configuration_id, provider=transport.provider, succeeded=False
+ team_id=team_id,
+ configuration_id=configuration_id,
+ provider=transport.provider,
+ destination_id=destination_id,
+ succeeded=False,
+ error=str(error) if isinstance(error, DeliveryError) else type(error).__name__,
)
raise
- record_delivery(team_id=team_id, configuration_id=configuration_id, provider=transport.provider, succeeded=True)
+ record_delivery(
+ team_id=team_id,
+ configuration_id=configuration_id,
+ provider=transport.provider,
+ destination_id=destination_id,
+ succeeded=True,
+ )
return sent
diff --git a/products/alerts_platform/backend/delivery/evaluation.py b/products/alerts_platform/backend/delivery/evaluation.py
index 5f941f1b90b6..745782b170b2 100644
--- a/products/alerts_platform/backend/delivery/evaluation.py
+++ b/products/alerts_platform/backend/delivery/evaluation.py
@@ -235,7 +235,8 @@ def deliver_evaluation(request: AlertDeliveryRequest) -> DeliveryOutcome:
# Destinations already told about the overflow, so each hears it once, on its first message.
noticed: set[str] = set()
for event_id, transitions in _by_subscription(request, announced).items():
- for target in _destinations(request, event_id):
+ for subscribed_destination in _destinations(request, event_id):
+ target = subscribed_destination.data
overflowed = 0
destination = json.dumps(target, sort_keys=True)
owns_notice = request.parts <= 1 or any(
@@ -257,7 +258,9 @@ def deliver_evaluation(request: AlertDeliveryRequest) -> DeliveryOutcome:
team_id=request.team_id,
configuration_id=request.configuration_id,
provider=WebhookTransport.provider,
+ destination_id=subscribed_destination.id,
succeeded=False,
+ error=str(error),
)
failures.append(str(error))
first_refusal = first_refusal or error
@@ -270,6 +273,7 @@ def deliver_evaluation(request: AlertDeliveryRequest) -> DeliveryOutcome:
configuration_id=request.configuration_id,
evaluation_key=request.evaluation_key,
target=target,
+ destination_id=subscribed_destination.id,
announcement=replace(announced, transitions=transitions, overflowed=overflowed),
incident_action=action_by_event_id.get(event_id),
source_alert_url=request.source_alert_url,
@@ -379,8 +383,8 @@ def _overflow_notice_keys(request: AlertDeliveryRequest, kinds: dict[str, AlertE
first_by_event_id[event_id] = min(first_by_event_id.get(event_id, grouping_key), grouping_key)
keys: dict[str, str] = {}
for event_id, first in first_by_event_id.items():
- for target in _destinations(request, event_id):
- destination = json.dumps(target, sort_keys=True)
+ for subscribed_destination in _destinations(request, event_id):
+ destination = json.dumps(subscribed_destination.data, sort_keys=True)
keys[destination] = min(keys.get(destination, first), first)
return keys
@@ -422,7 +426,14 @@ def _subscribed_events(request: AlertDeliveryRequest, transition: AnnouncedTrans
)
-def _destinations(request: AlertDeliveryRequest, event_id: str) -> list[AlertDestinationData]:
+@frozen
+class _SubscribedDestination:
+ # The HogFunction that subscribed this destination to the event, which its sends are recorded under.
+ id: str | None
+ data: AlertDestinationData
+
+
+def _destinations(request: AlertDeliveryRequest, event_id: str) -> list[_SubscribedDestination]:
"""Who subscribed to this event, resolved now rather than pinned when the check ran.
A destination removed between the check and the send is not sent to, which a pinned set
@@ -433,4 +444,8 @@ def _destinations(request: AlertDeliveryRequest, event_id: str) -> list[AlertDes
alert_id=request.destination_alert_id,
allowed_event_ids=[event_id],
)
- return [group.data for group in groups if group.fully_enabled]
+ return [
+ _SubscribedDestination(id=str(min(group.hog_function_ids)) if group.hog_function_ids else None, data=group.data)
+ for group in groups
+ if group.fully_enabled
+ ]
diff --git a/products/alerts_platform/backend/delivery/telemetry.py b/products/alerts_platform/backend/delivery/telemetry.py
index 2850fca45189..b69dad18648c 100644
--- a/products/alerts_platform/backend/delivery/telemetry.py
+++ b/products/alerts_platform/backend/delivery/telemetry.py
@@ -1,31 +1,57 @@
-"""Delivery outcomes, on the counters an alert's history reads.
+"""Delivery outcomes, on the counters an alert's history reads, and one log line per send.
`app_source` is `alert`. A HogFunction delivery writes `hog_function`, so a surface showing an
alert's delivery history has to read both. An alert that moves onto native delivery otherwise
looks like its history starts over on the day it moved.
+
+A send names its destination by the HogFunction that subscribed the destination to the event
+sent. That is the id the HogFunction path records the same send under, so one destination's
+sends on both paths are found by the same ids. Not the destination group's ids: a group gains and
+loses ids as event kinds are switched on and off, and its history would go with them.
"""
import datetime as dt
from typing import Final
+from uuid import uuid4
import structlog
from posthog.kafka_client.routing import get_producer
-from posthog.kafka_client.topics import KAFKA_APP_METRICS2
+from posthog.kafka_client.topics import KAFKA_APP_METRICS2, KAFKA_LOG_ENTRIES
from posthog.models.event.util import format_clickhouse_timestamp
ALERT_APP_SOURCE: Final = "alert"
+# `log_source_id` is the destination HogFunction id. `instance_id` is unique per send, because `log_entries`
+# is a ReplacingMergeTree and two sends with the same key and microsecond would collapse into one row.
+NATIVE_SEND_LOG_SOURCE: Final = "alert_destination"
+NATIVE_SEND_SUCCEEDED_MESSAGE: Final = "Sent"
+_MAX_ERROR_LENGTH: Final = 500
logger = structlog.get_logger(__name__)
-def record_delivery(*, team_id: int, configuration_id: str, provider: str, succeeded: bool) -> None:
+def record_delivery(
+ *,
+ team_id: int,
+ configuration_id: str,
+ provider: str,
+ destination_id: str | None,
+ succeeded: bool,
+ error: str | None = None,
+) -> None:
+ """Counts one send, and logs it when it has a destination id.
+
+ `error` reaches the person who owns the alert, so it must already be safe to show: a
+ transport's own refusal text or an exception class name, never a raw exception message.
+ """
+ timestamp = dt.datetime.now(dt.UTC)
payload = {
"team_id": team_id,
"app_source": ALERT_APP_SOURCE,
"app_source_id": configuration_id,
- "instance_id": provider,
- "timestamp": format_clickhouse_timestamp(dt.datetime.now(dt.UTC)),
+ # A destination with no HogFunction behind it, such as an insight's subscriber email, has no id.
+ "instance_id": destination_id or provider,
+ "timestamp": format_clickhouse_timestamp(timestamp),
"metric_kind": "success" if succeeded else "failure",
"metric_name": "succeeded" if succeeded else "failed",
"count": 1,
@@ -33,6 +59,20 @@ def record_delivery(*, team_id: int, configuration_id: str, provider: str, succe
try:
producer = get_producer(topic=KAFKA_APP_METRICS2)
producer.produce(topic=KAFKA_APP_METRICS2, data=payload)
+ if destination_id is not None:
+ producer = get_producer(topic=KAFKA_LOG_ENTRIES)
+ producer.produce(
+ topic=KAFKA_LOG_ENTRIES,
+ data={
+ "team_id": team_id,
+ "log_source": NATIVE_SEND_LOG_SOURCE,
+ "log_source_id": destination_id,
+ "instance_id": str(uuid4()),
+ "timestamp": format_clickhouse_timestamp(timestamp),
+ "level": "INFO" if succeeded else "ERROR",
+ "message": NATIVE_SEND_SUCCEEDED_MESSAGE if succeeded else (error or "Failed")[:_MAX_ERROR_LENGTH],
+ },
+ )
except Exception:
# A counter is not worth failing a send that already reached the provider.
logger.exception("Failed to emit alert delivery metrics", team_id=team_id, alert_id=configuration_id)
diff --git a/products/alerts_platform/backend/facade/api.py b/products/alerts_platform/backend/facade/api.py
index b76c2fa20505..4f1ce4088626 100644
--- a/products/alerts_platform/backend/facade/api.py
+++ b/products/alerts_platform/backend/facade/api.py
@@ -12,6 +12,7 @@ from datetime import datetime
from uuid import UUID
from products.alerts_platform.backend.facade.contracts import (
+ DestinationSend,
HandbackRefusal,
PlatformAlertCheckInput,
PlatformAlertConfigurationEdit,
@@ -27,7 +28,12 @@ from products.alerts_platform.backend.facade.contracts import (
PlatformOwnershipHandover,
SourceKind,
)
-from products.alerts_platform.backend.logic import platform_lifecycle, platform_reads, platform_writes
+from products.alerts_platform.backend.logic import (
+ destination_sends,
+ platform_lifecycle,
+ platform_reads,
+ platform_writes,
+)
def due_checks(
@@ -218,3 +224,10 @@ def close_owned_firings_now(*, team_id: int, legacy_configuration_id: UUID) -> N
"""Ends the firings of a copy the platform owns and sends their incident resolves before returning,
for a delete that is about to remove the destinations."""
platform_writes.close_owned_firings_now(team_id=team_id, legacy_configuration_id=legacy_configuration_id)
+
+
+def recent_destination_sends(
+ *, team_id: int, destination_ids: Collection[str], limit: int
+) -> tuple[DestinationSend, ...]:
+ """The newest sends to the named destination HogFunctions, on both delivery paths, newest first."""
+ return destination_sends.recent_destination_sends(team_id=team_id, destination_ids=destination_ids, limit=limit)
diff --git a/products/alerts_platform/backend/facade/contracts.py b/products/alerts_platform/backend/facade/contracts.py
index 9e868f61c7e1..c83206163a92 100644
--- a/products/alerts_platform/backend/facade/contracts.py
+++ b/products/alerts_platform/backend/facade/contracts.py
@@ -1417,3 +1417,19 @@ class SourceCorrespondence(Protocol):
intentional_divergences: tuple[IntentionalDivergence, ...]
def verdicts_for(self, checks: Sequence[PlatformCheck]) -> Mapping[CheckRef, SourceVerdict]: ...
+
+
+@frozen
+class DestinationSend:
+ """One message sent to one destination, on either delivery path, with how it went.
+
+ `destination_id` is the HogFunction the send was for, which names its event kind.
+ `error` is set only for a failed native send. A HogFunction failure has none, because its log
+ lines can quote credential-bearing inputs.
+ """
+
+ destination_id: str
+ sent_at: datetime
+ path: DeliveryPath
+ succeeded: bool
+ error: str | None
diff --git a/products/alerts_platform/backend/logic/destination_sends.py b/products/alerts_platform/backend/logic/destination_sends.py
new file mode 100644
index 000000000000..9920fc75e178
--- /dev/null
+++ b/products/alerts_platform/backend/logic/destination_sends.py
@@ -0,0 +1,91 @@
+"""What was sent to a destination lately, read from the log lines both delivery paths write.
+
+A native send writes one line per message (`delivery/telemetry.py`). A HogFunction send writes
+several per invocation, so an invocation is one send. It is sent once the function completed, and
+failed on an error that no retry follows. A failed fetch that retries logs an error ending in
+"Retrying." under the same invocation, so such a line is not an outcome. An invocation with
+neither is still running and is left out.
+
+Only a native failure carries its reason. A HogFunction's log lines can quote its inputs, a
+credential-bearing webhook URL among them, and reading them needs `hog_function:read`, so its
+failures carry no text here.
+"""
+
+from collections.abc import Collection
+from datetime import UTC, datetime, timedelta
+from typing import Final
+
+from posthog.api.log_entries import LOG_ENTRIES_DEFAULT_LOOKBACK_DAYS
+from posthog.clickhouse.client import sync_execute
+from posthog.clickhouse.query_tagging import Feature, Product, tag_queries
+from posthog.models.event.util import format_clickhouse_timestamp
+
+from products.alerts_platform.backend.delivery.telemetry import NATIVE_SEND_LOG_SOURCE
+from products.alerts_platform.backend.facade.contracts import DestinationSend
+from products.alerts_platform.backend.facade.enums import DeliveryPath
+
+_HOG_FUNCTION_LOG_SOURCE: Final = "hog_function"
+_HOG_FUNCTION_COMPLETED_PREFIX: Final = "Function completed"
+_HOG_FUNCTION_RETRY_SUFFIX: Final = "Retrying."
+
+_SENDS_SQL: Final = """
+SELECT destination_id, sent_at, path, failed, error
+FROM (
+ SELECT log_source_id AS destination_id, timestamp AS sent_at, %(native_path)s AS path,
+ upper(level) = 'ERROR' AS failed, message AS error
+ FROM log_entries
+ WHERE team_id = %(team_id)s
+ AND log_source = %(native_source)s
+ AND log_source_id IN %(destination_ids)s
+ AND timestamp > %(after)s
+
+ UNION ALL
+
+ SELECT log_source_id AS destination_id, min(timestamp) AS sent_at, %(hog_function_path)s AS path,
+ countIf(startsWith(message, %(completed_prefix)s)) = 0 AS failed,
+ '' AS error
+ FROM log_entries
+ WHERE team_id = %(team_id)s
+ AND log_source = %(hog_function_source)s
+ AND log_source_id IN %(destination_ids)s
+ AND timestamp > %(after)s
+ GROUP BY log_source_id, instance_id
+ HAVING NOT failed OR countIf(upper(level) = 'ERROR' AND NOT endsWith(message, %(retry_suffix)s)) > 0
+)
+ORDER BY sent_at DESC
+LIMIT %(limit)s
+"""
+
+
+def recent_destination_sends(
+ *, team_id: int, destination_ids: Collection[str], limit: int
+) -> tuple[DestinationSend, ...]:
+ if not destination_ids:
+ return ()
+ tag_queries(product=Product.PLATFORM_AND_SUPPORT, feature=Feature.ALERTING)
+ rows = sync_execute(
+ _SENDS_SQL,
+ {
+ "team_id": team_id,
+ "destination_ids": list(destination_ids),
+ # Bounds the read to the hot tier of `log_entries`, the same window its logs endpoint uses.
+ "after": format_clickhouse_timestamp(datetime.now(UTC) - timedelta(days=LOG_ENTRIES_DEFAULT_LOOKBACK_DAYS)),
+ "limit": limit,
+ "native_source": NATIVE_SEND_LOG_SOURCE,
+ "native_path": DeliveryPath.NATIVE.value,
+ "hog_function_source": _HOG_FUNCTION_LOG_SOURCE,
+ "hog_function_path": DeliveryPath.HOG_FUNCTION.value,
+ "completed_prefix": _HOG_FUNCTION_COMPLETED_PREFIX,
+ "retry_suffix": _HOG_FUNCTION_RETRY_SUFFIX,
+ },
+ )
+ return tuple(
+ DestinationSend(
+ destination_id=destination_id,
+ sent_at=sent_at if sent_at.tzinfo is not None else sent_at.replace(tzinfo=UTC),
+ path=DeliveryPath(path),
+ succeeded=not failed,
+ error=error if failed and error else None,
+ )
+ for destination_id, sent_at, path, failed, error in rows
+ )
diff --git a/products/alerts_platform/backend/tests/test_delivery_dispatch.py b/products/alerts_platform/backend/tests/test_delivery_dispatch.py
index d6a9e594805f..7f7b1950ed90 100644
--- a/products/alerts_platform/backend/tests/test_delivery_dispatch.py
+++ b/products/alerts_platform/backend/tests/test_delivery_dispatch.py
@@ -7,6 +7,8 @@ from unittest.mock import patch
from django.test import SimpleTestCase
from django.utils import timezone
+from parameterized import parameterized
+
from products.alerts_platform.backend.delivery.dispatch import deliver
from products.alerts_platform.backend.delivery.message import AlertMessage
from products.alerts_platform.backend.delivery.telemetry import record_delivery
@@ -145,6 +147,7 @@ class TestDeliveryDispatch(SimpleTestCase):
configuration_id="cfg-1",
evaluation_key=evaluation_key,
target=TARGET,
+ destination_id="destination-1",
announcement=announcement or _announcement(),
)
return recorded
@@ -169,10 +172,12 @@ class TestDeliveryDispatch(SimpleTestCase):
configuration_id="cfg-1",
evaluation_key="eval-1",
target=TARGET,
+ destination_id="destination-1",
announcement=_announcement(),
)
assert recorded.call_args.kwargs["succeeded"] is False
+ assert recorded.call_args.kwargs["error"] == "nope"
def test_a_resolve_replies_under_its_own_firing_rather_than_an_earlier_one(self) -> None:
store = RecordingThreadStore()
@@ -270,10 +275,13 @@ class TestDeliveryDispatch(SimpleTestCase):
configuration_id="cfg-1",
evaluation_key="eval-1",
target=TARGET,
+ destination_id="destination-1",
announcement=_announcement(),
)
assert recorded.call_args.kwargs["succeeded"] is False
+ # Its text can carry a credential URL, so only the class reaches the person's log.
+ assert recorded.call_args.kwargs["error"] == "ConnectionError"
def test_a_store_that_remembers_nothing_still_delivers(self) -> None:
transport = FakeTransport()
@@ -286,15 +294,40 @@ class TestDeliveryDispatch(SimpleTestCase):
class TestDeliveryTelemetry(SimpleTestCase):
def test_an_outcome_is_keyed_to_alerts_rather_than_to_hog_functions(self) -> None:
with patch("products.alerts_platform.backend.delivery.telemetry.get_producer") as producer:
- record_delivery(team_id=2, configuration_id="cfg-1", provider="slack", succeeded=True)
+ record_delivery(team_id=2, configuration_id="cfg-1", provider="slack", destination_id=None, succeeded=True)
+
+ [metrics] = [c.kwargs["data"] for c in producer.return_value.produce.call_args_list]
+ assert metrics["app_source"] == "alert"
+ assert metrics["app_source_id"] == "cfg-1"
+ assert metrics["instance_id"] == "slack"
+ assert (metrics["metric_kind"], metrics["metric_name"]) == ("success", "succeeded")
+
+ @parameterized.expand(
+ [("sent", True, None, "INFO", "Sent"), ("refused", False, "channel_not_found", "ERROR", "channel_not_found")]
+ )
+ def test_a_send_with_a_destination_is_counted_and_logged_under_it(
+ self, _name: str, succeeded: bool, error: str | None, level: str, message: str
+ ) -> None:
+ with patch("products.alerts_platform.backend.delivery.telemetry.get_producer") as producer:
+ for _ in range(2):
+ record_delivery(
+ team_id=2,
+ configuration_id="cfg-1",
+ provider="slack",
+ destination_id="fn-1",
+ succeeded=succeeded,
+ error=error,
+ )
- payload = producer.return_value.produce.call_args.kwargs["data"]
- assert payload["app_source"] == "alert"
- assert payload["app_source_id"] == "cfg-1"
- assert (payload["metric_kind"], payload["metric_name"]) == ("success", "succeeded")
+ metrics, log, _, second_log = [c.kwargs["data"] for c in producer.return_value.produce.call_args_list]
+ assert metrics["instance_id"] == "fn-1"
+ assert (log["log_source"], log["log_source_id"]) == ("alert_destination", "fn-1")
+ assert (log["level"], log["message"]) == (level, message)
+ # Two sends in the same microsecond would otherwise share a sorting key and merge into one row.
+ assert log["instance_id"] != second_log["instance_id"]
def test_a_broken_producer_does_not_fail_a_send_that_already_happened(self) -> None:
with patch(
"products.alerts_platform.backend.delivery.telemetry.get_producer", side_effect=RuntimeError("down")
):
- record_delivery(team_id=2, configuration_id="cfg-1", provider="slack", succeeded=True)
+ record_delivery(team_id=2, configuration_id="cfg-1", provider="slack", destination_id=None, succeeded=True)
diff --git a/products/alerts_platform/backend/tests/test_delivery_evaluation.py b/products/alerts_platform/backend/tests/test_delivery_evaluation.py
index 0986192da5fb..2b0deb25f4e2 100644
--- a/products/alerts_platform/backend/tests/test_delivery_evaluation.py
+++ b/products/alerts_platform/backend/tests/test_delivery_evaluation.py
@@ -1,6 +1,7 @@
from dataclasses import replace
from datetime import UTC, datetime
from typing import Any, cast
+from uuid import UUID
import pytest
from posthog.test.base import APIBaseTest
@@ -146,8 +147,10 @@ def _announcement(*transitions: AnnouncedTransition) -> EvaluationAnnouncement:
)
-def _group(data: AlertDestinationData, fully_enabled: bool = True) -> AlertDestinationGroup:
- return AlertDestinationGroup(hog_function_ids=(), data=data, fully_enabled=fully_enabled)
+def _group(
+ data: AlertDestinationData, fully_enabled: bool = True, hog_function_ids: tuple[UUID, ...] = ()
+) -> AlertDestinationGroup:
+ return AlertDestinationGroup(hog_function_ids=hog_function_ids, data=data, fully_enabled=fully_enabled)
class TestDeliverEvaluation(APIBaseTest):
@@ -221,6 +224,27 @@ class TestDeliverEvaluation(APIBaseTest):
("C-resolves", "Log alert 'API errors' is resolved"),
]
+ def test_each_send_is_recorded_under_the_hog_function_of_the_event_it_sent(self) -> None:
+ firing_function = UUID("00000000-0000-0000-0000-00000000000f")
+ resolved_function = UUID("00000000-0000-0000-0000-00000000000a")
+ announced = _announcement(
+ _transition(AlertEventKind.FIRING, "checkout"),
+ _transition(AlertEventKind.RESOLVED, "search"),
+ )
+
+ with patch("products.alerts_platform.backend.delivery.dispatch.record_delivery") as recorded:
+ self._run(
+ announced,
+ {
+ FIRING_EVENT: [_group(SLACK, hog_function_ids=(firing_function,))],
+ RESOLVED_EVENT: [_group(SLACK, hog_function_ids=(resolved_function,))],
+ },
+ )
+
+ assert sorted(c.kwargs["destination_id"] for c in recorded.call_args_list) == sorted(
+ [str(firing_function), str(resolved_function)]
+ )
+
def test_a_destination_subscribed_to_both_hears_about_both(self) -> None:
announced = _announcement(
_transition(AlertEventKind.FIRING, "checkout"),
diff --git a/products/alerts_platform/backend/tests/test_delivery_thread_store.py b/products/alerts_platform/backend/tests/test_delivery_thread_store.py
index 98a302756ade..0ac5ad91f120 100644
--- a/products/alerts_platform/backend/tests/test_delivery_thread_store.py
+++ b/products/alerts_platform/backend/tests/test_delivery_thread_store.py
@@ -110,6 +110,7 @@ class TestDatabaseThreadStore(APIBaseTest):
configuration_id=str(self.configuration.id),
evaluation_key="eval-1",
target=TARGET,
+ destination_id="destination-1",
announcement=_announcement(episode_started_at=FIRING),
)
diff --git a/products/alerts_platform/backend/tests/test_delivery_webhook.py b/products/alerts_platform/backend/tests/test_delivery_webhook.py
index 77f6398ec3f4..709c1aae7331 100644
--- a/products/alerts_platform/backend/tests/test_delivery_webhook.py
+++ b/products/alerts_platform/backend/tests/test_delivery_webhook.py
@@ -229,6 +229,7 @@ class TestWebhookThreads(APIBaseTest):
configuration_id=str(self.configuration.id),
evaluation_key=evaluation_key,
target=TARGET,
+ destination_id="destination-1",
announcement=announcement,
)
return adapter
diff --git a/products/logs/backend/presentation/views/alerts_api.py b/products/logs/backend/presentation/views/alerts_api.py
index fecb0cf27ab0..fa01b019182d 100644
--- a/products/logs/backend/presentation/views/alerts_api.py
+++ b/products/logs/backend/presentation/views/alerts_api.py
@@ -22,6 +22,7 @@ from rest_framework.response import Response
from posthog.schema import LogsAlertFilters
+from posthog.api.fields import RepeatedOrCommaSeparatedListField
from posthog.api.pagination import stable_queryset_ordering
from posthog.api.routing import TeamAndOrgViewSetMixin
from posthog.api.shared import UserBasicSerializer
@@ -43,12 +44,18 @@ from products.alerts.backend.facade.destinations import (
destination_handles_event_kind,
destination_template_id,
list_alert_destination_groups,
+ owned_alert_destination_ids,
redact_destination_data,
soft_delete_alert_destinations,
soft_delete_all_alert_destinations,
validate_destination_data,
)
-from products.alerts_platform.backend.facade.api import history_by_legacy_id, reset_broken, runtime_by_legacy_id
+from products.alerts_platform.backend.facade.api import (
+ history_by_legacy_id,
+ recent_destination_sends,
+ reset_broken,
+ runtime_by_legacy_id,
+)
from products.alerts_platform.backend.facade.contracts import (
AlertDestinationData,
AlertDestinationValidationError,
@@ -60,7 +67,7 @@ from products.alerts_platform.backend.facade.contracts import (
PlatformAlertHistory,
PlatformAlertRuntime,
)
-from products.alerts_platform.backend.facade.enums import PlatformAlertState
+from products.alerts_platform.backend.facade.enums import DeliveryPath, PlatformAlertState
from products.alerts_platform.backend.facade.scheduling import validate_and_normalize_schedule_restriction
from products.alerts_platform.backend.presentation.views.platform_alert import GroupingSerializer
from products.alerts_platform.backend.presentation.views.schedule_restriction import ScheduleRestrictionField
@@ -1136,6 +1143,41 @@ class LogsAlertDeleteDestinationSerializer(serializers.Serializer):
)
+MAX_DESTINATION_SENDS: Final = 20
+
+
+class LogsAlertDestinationSendsQuerySerializer(serializers.Serializer):
+ hog_function_ids = RepeatedOrCommaSeparatedListField(
+ child=serializers.UUIDField(),
+ min_length=1,
+ max_length=MAX_DESTINATION_IDS_PER_DELETE_REQUEST,
+ help_text="HogFunction IDs of one destination group. IDs that are not this alert's are ignored.",
+ )
+
+
+class LogsAlertDestinationSendSerializer(serializers.Serializer):
+ hog_function_id = serializers.CharField(
+ source="destination_id",
+ help_text="The HogFunction the message was sent for. It names the lifecycle event that was sent.",
+ )
+ sent_at = serializers.DateTimeField(help_text="When the send started.")
+ path = serializers.ChoiceField(
+ choices=DeliveryPath.choices,
+ help_text=(
+ "Which delivery path sent it. `native` is the stock message sent by the alerts platform. "
+ "`hog_function` is the destination's HogFunction."
+ ),
+ )
+ succeeded = serializers.BooleanField(help_text="Whether the destination accepted the message.")
+ error = serializers.CharField(
+ allow_null=True,
+ help_text=(
+ "Why a `native` send failed. Null when it succeeded, and for a `hog_function` send, "
+ "whose reason is in the HogFunction's own logs."
+ ),
+ )
+
+
def _build_reason(
prev_state: AlertState,
outcome: AlertCheckOutcome,
@@ -1485,6 +1527,36 @@ class LogsAlertViewSet(TeamAndOrgViewSetMixin, viewsets.ModelViewSet):
)
return Response(status=204)
+ @extend_schema(
+ parameters=[LogsAlertDestinationSendsQuerySerializer],
+ responses={200: LogsAlertDestinationSendSerializer(many=True)},
+ description=(
+ "The last messages sent to one destination of this alert in the past 7 days, newest first, "
+ "on both delivery paths, with whether each one succeeded."
+ ),
+ )
+ @action(
+ detail=True,
+ methods=["GET"],
+ url_path="destinations/sends",
+ required_scopes=["logs:read"],
+ pagination_class=None,
+ )
+ def destination_sends(self, request: Request, *args: object, **kwargs: object) -> Response:
+ alert = self.get_object()
+ query = LogsAlertDestinationSendsQuerySerializer(data=request.query_params)
+ query.is_valid(raise_exception=True)
+ owned = owned_alert_destination_ids(
+ team_id=self.team_id,
+ alert_id=str(alert.id),
+ allowed_event_ids=LOGS_ALERT_EVENT_IDS,
+ hog_function_ids=query.validated_data["hog_function_ids"],
+ )
+ sends = recent_destination_sends(
+ team_id=self.team_id, destination_ids=[str(i) for i in owned], limit=MAX_DESTINATION_SENDS
+ )
+ return Response(LogsAlertDestinationSendSerializer(sends, many=True).data)
+
@extend_schema(
request=None,
responses={200: LogsAlertEventSerializer(many=True)},
diff --git a/products/logs/backend/test/test_alerts_api.py b/products/logs/backend/test/test_alerts_api.py
index 51413e00460e..6018f43ce32d 100644
--- a/products/logs/backend/test/test_alerts_api.py
+++ b/products/logs/backend/test/test_alerts_api.py
@@ -19,6 +19,7 @@ from rest_framework import status
from rest_framework.test import APIClient
from posthog.clickhouse.client import sync_execute
+from posthog.clickhouse.log_entries import INSERT_LOG_ENTRY_SQL
from posthog.models.scoping import team_scope
from posthog.models.team.team import Team
from posthog.models.user import User
@@ -3494,3 +3495,95 @@ class TestPlatformOwnedAlertHistory(ClickhouseTestMixin, APIBaseTest):
assert response.status_code == status.HTTP_400_BAD_REQUEST, response.json()
assert response.json()["attr"] == "offset"
+
+
+class TestLogsAlertDestinationSends(ClickhouseTestMixin, APIBaseTest):
+ def setUp(self):
+ super().setUp()
+ self.base_url = f"/api/projects/{self.team.pk}/logs/alerts/"
+ patcher = patch("posthoganalytics.feature_enabled", return_value=True)
+ patcher.start()
+ self.addCleanup(patcher.stop)
+ HogFunctionTemplate.objects.get_or_create(
+ template_id="template-webhook",
+ defaults={
+ "sha": "1.0.0",
+ "name": "Webhook",
+ "code": "return event",
+ "code_language": "hog",
+ "inputs_schema": [{"key": "url", "type": "string"}, {"key": "body", "type": "json"}],
+ "type": "destination",
+ "status": "stable",
+ "free": True,
+ },
+ )
+
+ def _log(self, log_source: str, log_source_id: str, instance_id: str, at: datetime, level: str, message: str):
+ sync_execute(
+ INSERT_LOG_ENTRY_SQL,
+ {
+ "team_id": self.team.pk,
+ "log_source": log_source,
+ "log_source_id": log_source_id,
+ "instance_id": instance_id,
+ "timestamp": at.strftime("%Y-%m-%d %H:%M:%S.%f"),
+ "level": level,
+ "message": message,
+ },
+ )
+
+ def test_a_destination_lists_its_sends_on_both_paths_and_nothing_else(self) -> None:
+ response = self.client.post(
+ self.base_url,
+ {"name": "Errors", "threshold_count": 10, "filters": {"severityLevels": ["error"]}},
+ format="json",
+ )
+ assert response.status_code == status.HTTP_201_CREATED, response.json()
+ alert_id = response.json()["id"]
+ response = self.client.post(
+ f"{self.base_url}{alert_id}/destinations/",
+ {"type": "webhook", "webhook_url": "https://example.com/hook"},
+ format="json",
+ )
+ assert response.status_code == status.HTTP_201_CREATED, response.json()
+ functions = {
+ str(function.id): (function.filters or {})["events"][0]["id"]
+ for function in HogFunction.objects.filter(id__in=response.json()["hog_function_ids"])
+ }
+ firing = next(i for i, event in functions.items() if event == "$logs_alert_firing")
+ resolved = next(i for i, event in functions.items() if event == "$logs_alert_resolved")
+ other_alerts_function = str(uuid4())
+ now = datetime.now(UTC)
+
+ self._log("alert_destination", firing, "cfg", now - timedelta(minutes=10), "ERROR", "Webhook returned 500")
+ self._log("hog_function", firing, "inv-1", now - timedelta(minutes=30), "DEBUG", "Executing function")
+ retrying = "HTTP fetch failed on attempt 1 with status code 503. Retrying."
+ self._log("hog_function", firing, "inv-1", now - timedelta(minutes=30), "ERROR", retrying)
+ self._log("hog_function", firing, "inv-1", now - timedelta(minutes=29), "INFO", "Function completed in 12ms.")
+ self._log(
+ "hog_function",
+ resolved,
+ "inv-2",
+ now - timedelta(minutes=20),
+ "ERROR",
+ "Error executing function: POST https://hooks.example.com/token-abc failed",
+ )
+ # Still retrying, so neither sent nor failed yet.
+ self._log("hog_function", firing, "inv-3", now - timedelta(minutes=5), "DEBUG", "Executing function")
+ self._log("hog_function", firing, "inv-3", now - timedelta(minutes=5), "ERROR", retrying)
+ self._log("alert_destination", other_alerts_function, "cfg", now - timedelta(minutes=1), "INFO", "Sent")
+
+ response = self.client.get(
+ f"{self.base_url}{alert_id}/destinations/sends/",
+ {"hog_function_ids": ",".join([*functions, other_alerts_function])},
+ )
+
+ assert response.status_code == status.HTTP_200_OK, response.json()
+ assert [
+ (send["hog_function_id"], send["path"], send["succeeded"], send["error"]) for send in response.json()
+ ] == [
+ (firing, "native", False, "Webhook returned 500"),
+ # Its log lines can quote a webhook URL, and reading them needs `hog_function:read`.
+ (resolved, "hog_function", False, None),
+ (firing, "hog_function", True, None),
+ ]
diff --git a/products/logs/frontend/generated/api.schemas.ts b/products/logs/frontend/generated/api.schemas.ts
index 15ee75984750..56db74a0d251 100644
--- a/products/logs/frontend/generated/api.schemas.ts
+++ b/products/logs/frontend/generated/api.schemas.ts
@@ -1047,6 +1047,36 @@ export interface LogsAlertDeleteDestinationApi {
hog_function_ids: string[]
}
+/**
+ * * `native` - Native
+ * * `hog_function` - HogFunction
+ */
+export type DeliveryPathEnumApi = (typeof DeliveryPathEnumApi)[keyof typeof DeliveryPathEnumApi]
+
+export const DeliveryPathEnumApi = {
+ Native: 'native',
+ HogFunction: 'hog_function',
+} as const
+
+export interface LogsAlertDestinationSendApi {
+ /** The HogFunction the message was sent for. It names the lifecycle event that was sent. */
+ hog_function_id: string
+ /** When the send started. */
+ sent_at: string
+ /** Which delivery path sent it. `native` is the stock message sent by the alerts platform. `hog_function` is the destination's HogFunction.
+ *
+ * * `native` - Native
+ * * `hog_function` - HogFunction */
+ path: DeliveryPathEnumApi
+ /** Whether the destination accepted the message. */
+ succeeded: boolean
+ /**
+ * Why a `native` send failed. Null when it succeeded, and for a `hog_function` send, whose reason is in the HogFunction's own logs.
+ * @nullable
+ */
+ error: string | null
+}
+
/**
* * `check` - Check
* * `reset` - Reset
@@ -3359,6 +3389,15 @@ export type LogsAlertsListParams = {
offset?: number
}
+export type LogsAlertsDestinationsSendsListParams = {
+ /**
+ * HogFunction IDs of one destination group. IDs that are not this alert's are ignored.
+ * @minItems 1
+ * @maxItems 100
+ */
+ hog_function_ids: string[]
+}
+
export type LogsAlertsEventsListParams = {
/**
* Number of results to return per page.
diff --git a/products/logs/frontend/generated/api.ts b/products/logs/frontend/generated/api.ts
index ca97ca3c7884..1201fe3fa9a6 100644
--- a/products/logs/frontend/generated/api.ts
+++ b/products/logs/frontend/generated/api.ts
@@ -15,8 +15,10 @@ import type {
LogsAlertCreateDestinationApi,
LogsAlertDeleteDestinationApi,
LogsAlertDestinationResponseApi,
+ LogsAlertDestinationSendApi,
LogsAlertSimulateRequestApi,
LogsAlertSimulateResponseApi,
+ LogsAlertsDestinationsSendsListParams,
LogsAlertsEventsListParams,
LogsAlertsListParams,
LogsAnomalyScanRequestApi,
@@ -291,6 +293,41 @@ export const logsAlertsDestinationsDeleteCreate = async (
})
}
+export const getLogsAlertsDestinationsSendsListUrl = (
+ projectId: string,
+ id: string,
+ params: LogsAlertsDestinationsSendsListParams
+) => {
+ const normalizedParams = new URLSearchParams()
+
+ Object.entries(params || {}).forEach(([key, value]) => {
+ if (value !== undefined) {
+ normalizedParams.append(key, value === null ? 'null' : String(value))
+ }
+ })
+
+ const stringifiedParams = normalizedParams.toString()
+
+ return stringifiedParams.length > 0
+ ? `/api/projects/${projectId}/logs/alerts/${id}/destinations/sends/?${stringifiedParams}`
+ : `/api/projects/${projectId}/logs/alerts/${id}/destinations/sends/`
+}
+
+/**
+ * The last messages sent to one destination of this alert in the past 7 days, newest first, on both delivery paths, with whether each one succeeded.
+ */
+export const logsAlertsDestinationsSendsList = async (
+ projectId: string,
+ id: string,
+ params: LogsAlertsDestinationsSendsListParams,
+ options?: RequestInit
+): Promise<LogsAlertDestinationSendApi[]> => {
+ return apiMutator<LogsAlertDestinationSendApi[]>(getLogsAlertsDestinationsSendsListUrl(projectId, id, params), {
+ ...options,
+ method: 'GET',
+ })
+}
+
export const getLogsAlertsEventsListUrl = (projectId: string, id: string, params?: LogsAlertsEventsListParams) => {
const normalizedParams = new URLSearchParams()
diff --git a/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/DestinationRecentSends.stories.tsx b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/DestinationRecentSends.stories.tsx
new file mode 100644
index 000000000000..84f1017bdd68
--- /dev/null
+++ b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/DestinationRecentSends.stories.tsx
@@ -0,0 +1,62 @@
+import { Meta, StoryObj } from '@storybook/react'
+
+import { LogsAlertDestinationSendApi } from 'products/logs/frontend/generated/api.schemas'
+
+import { DestinationRecentSends } from './DestinationRecentSends'
+
+const FIRING_FUNCTION = 'fn-firing'
+const RESOLVED_FUNCTION = 'fn-resolved'
+
+const SENDS: LogsAlertDestinationSendApi[] = [
+ {
+ hog_function_id: FIRING_FUNCTION,
+ sent_at: '2023-02-18T09:40:00Z',
+ path: 'native',
+ succeeded: false,
+ error: 'Webhook returned HTTP 500',
+ },
+ {
+ hog_function_id: RESOLVED_FUNCTION,
+ sent_at: '2023-02-18T09:20:00Z',
+ path: 'native',
+ succeeded: true,
+ error: null,
+ },
+ {
+ hog_function_id: FIRING_FUNCTION,
+ sent_at: '2023-02-17T16:05:00Z',
+ path: 'hog_function',
+ succeeded: true,
+ error: null,
+ },
+]
+
+const meta: Meta<typeof DestinationRecentSends> = {
+ title: 'Scenes-App/Logs/Alerts/Destination recent sends',
+ component: DestinationRecentSends,
+ parameters: { mockDate: '2023-02-18T10:00:00Z' },
+ args: {
+ sends: SENDS,
+ loading: false,
+ error: false,
+ onRetry: () => {},
+ eventLabels: { [FIRING_FUNCTION]: 'Firing', [RESOLVED_FUNCTION]: 'Resolved' },
+ },
+ render: (args) => (
+ <div className="max-w-3xl p-4">
+ <DestinationRecentSends {...args} />
+ </div>
+ ),
+}
+export default meta
+
+type Story = StoryObj<typeof DestinationRecentSends>
+
+export const BothPaths: Story = {}
+export const Empty: Story = { args: { sends: [] } }
+export const Loading: Story = {
+ args: { sends: null, loading: true },
+ // The table never finishes loading here, so the snapshot cannot wait for the loader to go.
+ parameters: { testOptions: { waitForLoadersToDisappear: false } },
+}
+export const Failed: Story = { args: { sends: null, error: true } }
diff --git a/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/DestinationRecentSends.tsx b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/DestinationRecentSends.tsx
new file mode 100644
index 000000000000..4890bf3dea15
--- /dev/null
+++ b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/DestinationRecentSends.tsx
@@ -0,0 +1,97 @@
+import { LemonButton, LemonTable, LemonTag, Tooltip } from '@posthog/lemon-ui'
+
+import { TZLabel } from 'lib/components/TZLabel'
+
+import { LogsAlertDestinationSendApi } from 'products/logs/frontend/generated/api.schemas'
+
+export interface DestinationRecentSendsProps {
+ sends: LogsAlertDestinationSendApi[] | null
+ loading: boolean
+ error: boolean
+ onRetry: () => void
+ // Keyed by HogFunction id. A send whose function is gone reads as an unknown event.
+ eventLabels: Record<string, string>
+}
+
+const PATH_META: Record<LogsAlertDestinationSendApi['path'], { label: string; tooltip: string }> = {
+ native: { label: 'PostHog alerts', tooltip: 'Sent by PostHog alerts with the standard message.' },
+ hog_function: { label: 'Hog function', tooltip: "Sent by this destination's hog function." },
+}
+
+export function DestinationRecentSends({
+ sends,
+ loading,
+ error,
+ onRetry,
+ eventLabels,
+}: DestinationRecentSendsProps): JSX.Element {
+ return (
+ <div className="flex flex-col gap-2">
+ <div>
+ <h3 className="text-base font-semibold m-0">Recent sends</h3>
+ <p className="text-sm text-muted m-0">Messages sent to this destination in the last 7 days.</p>
+ </div>
+ {error && !loading ? (
+ <div className="flex items-center justify-between gap-2 border rounded p-3">
+ <span className="text-sm">Couldn't load recent sends.</span>
+ <LemonButton
+ size="small"
+ type="secondary"
+ onClick={onRetry}
+ data-attr="logs-alert-destination-sends-retry"
+ >
+ Retry
+ </LemonButton>
+ </div>
+ ) : (
+ <LemonTable
+ dataSource={sends ?? []}
+ loading={loading || sends === null}
+ rowKey={(send) => `${send.hog_function_id}:${send.path}:${send.sent_at}`}
+ size="small"
+ emptyState="Nothing sent in the last 7 days. Sends show here after the alert fires."
+ columns={[
+ {
+ title: 'Sent',
+ key: 'sent_at',
+ render: (_, send) => <TZLabel time={send.sent_at} />,
+ },
+ {
+ title: 'Event',
+ key: 'event',
+ render: (_, send) => eventLabels[send.hog_function_id] ?? 'Unknown event',
+ },
+ {
+ title: 'Outcome',
+ key: 'outcome',
+ render: (_, send) =>
+ send.succeeded ? (
+ <LemonTag type="success">Sent</LemonTag>
+ ) : (
+ <div className="flex flex-col items-start gap-1 min-w-0">
+ <LemonTag type="danger">Failed</LemonTag>
+ {send.error ? (
+ <span className="text-xs text-muted break-words">{send.error}</span>
+ ) : send.path === 'hog_function' ? (
+ <span className="text-xs text-muted">
+ The reason is in this event's hog function logs.
+ </span>
+ ) : null}
+ </div>
+ ),
+ },
+ {
+ title: 'Sent by',
+ key: 'path',
+ render: (_, send) => (
+ <Tooltip title={PATH_META[send.path].tooltip}>
+ <LemonTag>{PATH_META[send.path].label}</LemonTag>
+ </Tooltip>
+ ),
+ },
+ ]}
+ />
+ )}
+ </div>
+ )
+}
diff --git a/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/LogsAlertNotificationDetailScene.tsx b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/LogsAlertNotificationDetailScene.tsx
index f683299bb9d7..7f9d21dd72ab 100644
--- a/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/LogsAlertNotificationDetailScene.tsx
+++ b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/LogsAlertNotificationDetailScene.tsx
@@ -24,6 +24,7 @@ import {
resolveGroupLabel,
} from 'products/logs/frontend/components/LogsAlerting/logsAlertUtils'
+import { DestinationRecentSends } from './DestinationRecentSends'
import {
LogsAlertNotificationDetailSceneLogicProps,
logsAlertNotificationDetailSceneLogic,
@@ -49,9 +50,13 @@ export function LogsAlertNotificationDetailScene(): JSX.Element {
hogFunctionId,
firstSlackIntegration,
messageCustomized,
+ recentSends,
+ recentSendsLoading,
+ recentSendsError,
+ eventLabelByHogFunctionId,
} = useValues(logsAlertNotificationDetailSceneLogic)
const customizationEnabled = useFeatureFlag('ALERTS_DESTINATION_CUSTOMIZATION')
- const { deleteDestination, loadHogFunctions, setHogFunctionEnabled } = useActions(
+ const { deleteDestination, loadHogFunctions, loadRecentSends, setHogFunctionEnabled } = useActions(
logsAlertNotificationDetailSceneLogic
)
@@ -267,6 +272,16 @@ export function LogsAlertNotificationDetailScene(): JSX.Element {
})}
</div>
)}
+
+ {customizationEnabled && destinationGroup ? (
+ <DestinationRecentSends
+ sends={recentSends}
+ loading={recentSendsLoading}
+ error={recentSendsError}
+ onRetry={loadRecentSends}
+ eventLabels={eventLabelByHogFunctionId}
+ />
+ ) : null}
</div>
</SceneContent>
)
diff --git a/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/__tests__/logsAlertNotificationDetailSceneLogic.test.ts b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/__tests__/logsAlertNotificationDetailSceneLogic.test.ts
new file mode 100644
index 000000000000..2c5e40859f06
--- /dev/null
+++ b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/__tests__/logsAlertNotificationDetailSceneLogic.test.ts
@@ -0,0 +1,91 @@
+import { expectLogic } from 'kea-test-utils'
+
+import api from 'lib/api'
+import { FEATURE_FLAGS } from 'lib/constants'
+import { featureFlagLogic } from 'lib/logic/featureFlagLogic'
+
+import { initKeaTests } from '~/test/init'
+import { HogFunctionType } from '~/types'
+
+import { logsAlertsDestinationsSendsList, logsAlertsRetrieve } from 'products/logs/frontend/generated/api'
+
+import { logsAlertNotificationDetailSceneLogic } from '../logsAlertNotificationDetailSceneLogic'
+
+jest.mock('lib/api', () => {
+ const actual = jest.requireActual('lib/api')
+ return {
+ ...actual,
+ __esModule: true,
+ default: { ...actual.default, hogFunctions: { list: jest.fn() } },
+ }
+})
+
+jest.mock('products/logs/frontend/generated/api', () => ({
+ __esModule: true,
+ logsAlertsDestinationsDeleteCreate: jest.fn(),
+ logsAlertsDestinationsSendsList: jest.fn(),
+ logsAlertsRetrieve: jest.fn(),
+}))
+
+const mockList = api.hogFunctions.list as jest.Mock
+const mockSends = logsAlertsDestinationsSendsList as jest.MockedFunction<typeof logsAlertsDestinationsSendsList>
+
+function webhookFunction(id: string, event: string): HogFunctionType {
+ return {
+ id,
+ name: id,
+ enabled: true,
+ template_id: 'template-webhook',
+ inputs: { url: { value: 'https://example.com/hook' } },
+ filters: { events: [{ id: event, type: 'events' }] },
+ } as unknown as HogFunctionType
+}
+
+describe('logsAlertNotificationDetailSceneLogic', () => {
+ beforeEach(() => {
+ initKeaTests()
+ jest.clearAllMocks()
+ ;(logsAlertsRetrieve as jest.Mock).mockResolvedValue(null)
+ mockList.mockResolvedValue({
+ results: [
+ webhookFunction('fn-firing', '$logs_alert_firing'),
+ webhookFunction('fn-resolved', '$logs_alert_resolved'),
+ {
+ ...webhookFunction('fn-other', '$logs_alert_firing'),
+ inputs: { url: { value: 'https://example.com/other' } },
+ },
+ ],
+ })
+ mockSends.mockResolvedValue([])
+ })
+
+ it.each([
+ [true, [['fn-firing', 'fn-resolved']]],
+ [false, []],
+ ])('asks for the sends of the whole group only behind the flag (flag on: %s)', async (flagOn, expected) => {
+ featureFlagLogic.mount()
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.ALERTS_DESTINATION_CUSTOMIZATION]: flagOn })
+ const logic = logsAlertNotificationDetailSceneLogic({ alertId: 'alert-1', hogFunctionId: 'fn-resolved' })
+ logic.mount()
+
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(mockSends.mock.calls.map(([, , params]) => params.hog_function_ids)).toEqual(expected)
+ })
+
+ it('loads the sends when the flag turns on after the functions loaded', async () => {
+ featureFlagLogic.mount()
+ featureFlagLogic.actions.setFeatureFlags([], {})
+ const logic = logsAlertNotificationDetailSceneLogic({ alertId: 'alert-1', hogFunctionId: 'fn-resolved' })
+ logic.mount()
+ await expectLogic(logic).toFinishAllListeners()
+ expect(mockSends).not.toHaveBeenCalled()
+
+ featureFlagLogic.actions.setFeatureFlags([], { [FEATURE_FLAGS.ALERTS_DESTINATION_CUSTOMIZATION]: true })
+ await expectLogic(logic).toFinishAllListeners()
+
+ expect(mockSends.mock.calls.map(([, , params]) => params.hog_function_ids)).toEqual([
+ ['fn-firing', 'fn-resolved'],
+ ])
+ })
+})
diff --git a/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/logsAlertNotificationDetailSceneLogic.ts b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/logsAlertNotificationDetailSceneLogic.ts
index 6dbe56dec57b..5e2773998cf7 100644
--- a/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/logsAlertNotificationDetailSceneLogic.ts
+++ b/products/logs/frontend/scenes/LogsAlertNotificationDetailScene/logsAlertNotificationDetailSceneLogic.ts
@@ -7,7 +7,9 @@ import posthog from 'posthog-js'
import { lemonToast } from '@posthog/lemon-ui'
import api from 'lib/api'
+import { FEATURE_FLAGS } from 'lib/constants'
import { integrationsLogic } from 'lib/integrations/integrationsLogic'
+import { FeatureFlagsSet, featureFlagLogic } from 'lib/logic/featureFlagLogic'
import { Scene } from 'scenes/sceneTypes'
import { teamLogic } from 'scenes/teamLogic'
import { urls } from 'scenes/urls'
@@ -16,12 +18,18 @@ import { Breadcrumb, HogFunctionType, IntegrationType } from '~/types'
import {
buildLogsAlertFilterConfig,
+ getHogFunctionEventKind,
groupLogsAlertDestinations,
+ LOGS_ALERT_EVENT_KIND_META,
LogsAlertDestinationGroup,
} from 'products/logs/frontend/components/LogsAlerting/logsAlertUtils'
-import { logsAlertsDestinationsDeleteCreate, logsAlertsRetrieve } from 'products/logs/frontend/generated/api'
+import {
+ logsAlertsDestinationsDeleteCreate,
+ logsAlertsDestinationsSendsList,
+ logsAlertsRetrieve,
+} from 'products/logs/frontend/generated/api'
-import type { LogsAlertConfigurationDetailApi } from '../../generated/api.schemas'
+import type { LogsAlertConfigurationDetailApi, LogsAlertDestinationSendApi } from '../../generated/api.schemas'
export interface LogsAlertNotificationDetailSceneLogicProps {
alertId: string
@@ -30,6 +38,7 @@ export interface LogsAlertNotificationDetailSceneLogicProps {
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface logsAlertNotificationDetailSceneLogicValues {
+ featureFlags: FeatureFlagsSet // featureFlagLogic
slackIntegrations: IntegrationType[] | undefined // integrationsLogic
currentTeamId: number | null // teamLogic
alert: LogsAlertConfigurationDetailApi | null
@@ -37,6 +46,7 @@ export interface logsAlertNotificationDetailSceneLogicValues {
alertLoading: boolean
breadcrumbs: Breadcrumb[]
destinationGroup: LogsAlertDestinationGroup | null
+ eventLabelByHogFunctionId: Record<string, string>
firstSlackIntegration: IntegrationType | undefined
hasLoaded: boolean
hogFunctionId: string
@@ -45,6 +55,10 @@ export interface logsAlertNotificationDetailSceneLogicValues {
hogFunctionsLoading: boolean
isDeleting: boolean
messageCustomized: boolean
+ recentSends: LogsAlertDestinationSendApi[] | null
+ recentSendsDue: boolean
+ recentSendsError: boolean
+ recentSendsLoading: boolean
togglingHogFunctionIds: string[]
}
@@ -86,6 +100,21 @@ export interface logsAlertNotificationDetailSceneLogicActions {
hogFunctions: HogFunctionType[]
payload?: any
}
+ loadRecentSends: () => any
+ loadRecentSendsFailure: (
+ error: string,
+ errorObject?: any
+ ) => {
+ error: string
+ errorObject?: any
+ }
+ loadRecentSendsSuccess: (
+ recentSends: LogsAlertDestinationSendApi[] | null,
+ payload?: any
+ ) => {
+ recentSends: LogsAlertDestinationSendApi[] | null
+ payload?: any
+ }
setHogFunctionEnabled: (
hogFunctionId: string,
enabled: boolean
@@ -103,6 +132,14 @@ export interface logsAlertNotificationDetailSceneLogicMeta {
hogFunctionId: (arg: any) => string
firstSlackIntegration: (slackIntegrations: IntegrationType[] | undefined) => IntegrationType | undefined
destinationGroup: (hogFunctions: HogFunctionType[], hogFunctionId: string) => LogsAlertDestinationGroup | null
+ recentSendsDue: (
+ featureFlags: FeatureFlagsSet,
+ destinationGroup: LogsAlertDestinationGroup | null,
+ recentSends: LogsAlertDestinationSendApi[] | null,
+ recentSendsLoading: boolean,
+ recentSendsError: boolean
+ ) => boolean
+ eventLabelByHogFunctionId: (destinationGroup: LogsAlertDestinationGroup | null) => Record<string, string>
messageCustomized: (
alert: LogsAlertConfigurationDetailApi | null,
destinationGroup: LogsAlertDestinationGroup | null
@@ -136,7 +173,14 @@ export const logsAlertNotificationDetailSceneLogic = kea<logsAlertNotificationDe
key((props) => `${props.alertId}:${props.hogFunctionId}`),
connect({
- values: [teamLogic, ['currentTeamId'], integrationsLogic, ['slackIntegrations']],
+ values: [
+ teamLogic,
+ ['currentTeamId'],
+ integrationsLogic,
+ ['slackIntegrations'],
+ featureFlagLogic,
+ ['featureFlags'],
+ ],
}),
actions({
@@ -167,6 +211,13 @@ export const logsAlertNotificationDetailSceneLogic = kea<logsAlertNotificationDe
deleteDestinationDone: () => false,
},
],
+ recentSendsError: [
+ false,
+ {
+ loadRecentSends: () => false,
+ loadRecentSendsFailure: () => true,
+ },
+ ],
togglingHogFunctionIds: [
[] as string[],
{
@@ -206,6 +257,20 @@ export const logsAlertNotificationDetailSceneLogic = kea<logsAlertNotificationDe
},
},
],
+ recentSends: [
+ null as LogsAlertDestinationSendApi[] | null,
+ {
+ loadRecentSends: async () => {
+ const group = values.destinationGroup
+ if (!values.currentTeamId || !group) {
+ return null
+ }
+ return logsAlertsDestinationsSendsList(String(values.currentTeamId), props.alertId, {
+ hog_function_ids: group.hogFunctions.map((hogFunction) => hogFunction.id),
+ })
+ },
+ },
+ ],
})),
selectors({
@@ -222,6 +287,34 @@ export const logsAlertNotificationDetailSceneLogic = kea<logsAlertNotificationDe
return groups.find((g) => g.hogFunctions.some((hf) => hf.id === hogFunctionId)) ?? null
},
],
+ // Due once per page: a pause or enable reloads the functions but changes no past send, and a
+ // failed load waits for Retry. Watched rather than checked after the functions load, so a flag
+ // that turns on later still loads the sends.
+ recentSendsDue: [
+ (s) => [s.featureFlags, s.destinationGroup, s.recentSends, s.recentSendsLoading, s.recentSendsError],
+ (
+ featureFlags: FeatureFlagsSet,
+ group: LogsAlertDestinationGroup | null,
+ recentSends: LogsAlertDestinationSendApi[] | null,
+ loading: boolean,
+ error: boolean
+ ): boolean =>
+ !!featureFlags[FEATURE_FLAGS.ALERTS_DESTINATION_CUSTOMIZATION] &&
+ group !== null &&
+ recentSends === null &&
+ !loading &&
+ !error,
+ ],
+ eventLabelByHogFunctionId: [
+ (s) => [s.destinationGroup],
+ (group: LogsAlertDestinationGroup | null): Record<string, string> =>
+ Object.fromEntries(
+ (group?.hogFunctions ?? []).flatMap((hogFunction) => {
+ const kind = getHogFunctionEventKind(hogFunction)
+ return kind ? [[hogFunction.id, LOGS_ALERT_EVENT_KIND_META[kind].label]] : []
+ })
+ ),
+ ],
messageCustomized: [
(s) => [s.alert, s.destinationGroup],
(alert: LogsAlertConfigurationDetailApi | null, group: LogsAlertDestinationGroup | null): boolean => {
@@ -306,6 +399,11 @@ export const logsAlertNotificationDetailSceneLogic = kea<logsAlertNotificationDe
})),
subscriptions(({ values, actions }) => ({
+ recentSendsDue: (due: boolean) => {
+ if (due) {
+ actions.loadRecentSends()
+ }
+ },
currentTeamId: (currentTeamId: number | null) => {
if (!currentTeamId) {
return
diff --git a/services/mcp/src/api/generated.ts b/services/mcp/src/api/generated.ts
index b14fa2f94302..02ac7f484a37 100644
--- a/services/mcp/src/api/generated.ts
+++ b/services/mcp/src/api/generated.ts
@@ -35832,6 +35832,18 @@ export namespace Schemas {
deployed_merged_pr_count: number;
}
+ /**
+ * * `native` - Native
+ * * `hog_function` - HogFunction
+ */
+ export type DeliveryPathEnum = typeof DeliveryPathEnum[keyof typeof DeliveryPathEnum];
+
+
+ export const DeliveryPathEnum = {
+ Native: 'native',
+ HogFunction: 'hog_function',
+ } as const;
+
/**
* * `open_to_gate` - Open To Gate
* * `gate_to_merge` - Gate To Merge
@@ -64466,6 +64478,25 @@ export namespace Schemas {
hog_function_ids: string[];
}
+ export interface LogsAlertDestinationSend {
+ /** The HogFunction the message was sent for. It names the lifecycle event that was sent. */
+ hog_function_id: string;
+ /** When the send started. */
+ sent_at: string;
+ /** Which delivery path sent it. `native` is the stock message sent by the alerts platform. `hog_function` is the destination's HogFunction.
+ *
+ * * `native` - Native
+ * * `hog_function` - HogFunction */
+ path: DeliveryPathEnum;
+ /** Whether the destination accepted the message. */
+ succeeded: boolean;
+ /**
+ * Why a `native` send failed. Null when it succeeded, and for a `hog_function` send, whose reason is in the HogFunction's own logs.
+ * @nullable
+ */
+ error: string | null;
+ }
+
/**
* * `check` - Check
* * `reset` - Reset
@@ -130131,6 +130162,15 @@ export namespace Schemas {
offset?: number;
};
+ export type LogsAlertsDestinationsSendsListParams = {
+ /**
+ * HogFunction IDs of one destination group. IDs that are not this alert's are ignored.
+ * @minItems 1
+ * @maxItems 100
+ */
+ hog_function_ids: string[];
+ };
+
export type LogsAlertsEventsListParams = {
/**
* Number of results to return per page.