Skip to content

Commit

Permalink
fix(alerts): Improve alert evaluation failure message
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudhpillai committed Oct 24, 2024
1 parent 75e28ba commit 6cf92c6
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
8 changes: 8 additions & 0 deletions posthog/api/alert.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,14 @@ def update(self, instance, validated_data):
# If anything changed we set to NOT_FIRING, so it's firing and notifying with the new settings
instance.state = AlertState.NOT_FIRING

calculation_interval_changed = (
"calculation_interval" in validated_data
and validated_data["calculation_interval"] != self.calculation_interval

Check failure on line 220 in posthog/api/alert.py

View workflow job for this annotation

GitHub Actions / Python code quality checks

"AlertSerializer" has no attribute "calculation_interval"
)
if conditions_or_threshold_changed or calculation_interval_changed:
# calculate alert right now, don't wait until preset time
self.next_check_at = None

return super().update(instance, validated_data)

def validate_snoozed_until(self, value):
Expand Down
10 changes: 5 additions & 5 deletions posthog/tasks/alerts/checks.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from django.conf import settings
from django.db import transaction
import structlog
from sentry_sdk import capture_exception
from sentry_sdk import capture_exception, set_tag

from posthog.errors import CHQueryErrorTooManySimultaneousQueries
from posthog.hogql_queries.legacy_compatibility.flagged_conversion_manager import (
Expand Down Expand Up @@ -289,10 +289,10 @@ def check_alert_and_notify_atomically(alert: AlertConfiguration) -> None:
error_message = f"AlertCheckError: error sending notifications for alert_id = {alert.id}"
logger.exception(error_message)

capture_exception(
Exception(error_message),
{"alert_id": alert.id, "message": str(err)},
)
set_tag("alert_config_id", alert.id)
set_tag("evaluation_error_message", str(err))

capture_exception(Exception(error_message))

# don't want alert state to be updated (so that it's retried as next_check_at won't be updated)
# so we raise again as @transaction.atomic decorator won't commit db updates
Expand Down
12 changes: 6 additions & 6 deletions posthog/tasks/alerts/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,8 @@ def alert_calculation_interval_to_relativedelta(alert_calculation_interval: Aler
def send_notifications_for_breaches(alert: AlertConfiguration, breaches: list[str]) -> None:
subject = f"PostHog alert {alert.name} is firing"
campaign_key = f"alert-firing-notification-{alert.id}-{timezone.now().timestamp()}"
insight_url = f"/project/{alert.team.pk}/insights/{alert.insight.short_id}?alert_id={alert.id}"
alert_url = f"{insight_url}/alerts/{alert.id}"
insight_url = f"/project/{alert.team.pk}/insights/{alert.insight.short_id}"
alert_url = f"{insight_url}?alert_id={alert.id}"
message = EmailMessage(
campaign_key=campaign_key,
subject=subject,
Expand All @@ -86,14 +86,14 @@ def send_notifications_for_breaches(alert: AlertConfiguration, breaches: list[st
def send_notifications_for_errors(alert: AlertConfiguration, error: dict) -> None:
subject = f"PostHog alert {alert.name} check failed to evaluate"
campaign_key = f"alert-firing-notification-{alert.id}-{timezone.now().timestamp()}"
insight_url = f"/project/{alert.team.pk}/insights/{alert.insight.short_id}?alert_id={alert.id}"
alert_url = f"{insight_url}/alerts/{alert.id}"
insight_url = f"/project/{alert.team.pk}/insights/{alert.insight.short_id}"
alert_url = f"{insight_url}?alert_id={alert.id}"
message = EmailMessage(
campaign_key=campaign_key,
subject=subject,
template_name="alert_check_firing",
template_name="alert_check_failed_to_evaluate",
template_context={
"match_descriptions": error,
"alert_error": error,
"insight_url": insight_url,
"insight_name": alert.insight.name,
"alert_url": alert_url,
Expand Down
10 changes: 10 additions & 0 deletions posthog/templates/alert_check_failed_to_evaluate.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{% extends "email/base.html" %} {% load posthog_assets %} {% block section %}
<p>
The <a href="{% absolute_uri alert_url %}">{{ alert_name }}</a> alert failed to evaluate for insight <a
href="{% absolute_uri insight_url %}">{{ insight_name }}</> with the following error:
</p>
<p><i>{{ alert_error }}</i></p>
<div class="mb mt text-center">
<a class="button" href="{% absolute_uri alert_url %}">Manage alert</a>
</div>
{% endblock %}{% load posthog_filters %}

0 comments on commit 6cf92c6

Please sign in to comment.