Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(alerts): Improve alert evaluation failure message #25790

Merged
merged 5 commits into from
Nov 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions frontend/src/lib/components/Alerts/views/ManageAlertsModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ export function AlertListItem({ alert, onClick }: AlertListItemProps): JSX.Eleme

{alert.enabled ? (
<div className="text-muted pl-3">
{bounds?.lower &&
{bounds?.lower !== undefined &&
`Low ${isPercentage ? bounds.lower * 100 : bounds.lower}${isPercentage ? '%' : ''}`}
{bounds?.lower && bounds?.upper ? ' · ' : ''}
{bounds?.upper &&
{bounds?.lower !== undefined && bounds?.upper ? ' · ' : ''}
{bounds?.upper !== undefined &&
`High ${isPercentage ? bounds.upper * 100 : bounds.upper}${isPercentage ? '%' : ''}`}
</div>
) : (
Expand Down Expand Up @@ -76,9 +76,10 @@ export function ManageAlertsModal(props: ManageAlertsModalProps): JSX.Element {
return (
<LemonModal onClose={props.onClose} isOpen={props.isOpen} width={600} simple title="">
<LemonModal.Header>
<h3>
Manage Alerts <LemonTag type="warning">ALPHA</LemonTag>
</h3>
<div className="flex items-center gap-2">
<h3 className="m-0">Manage Alerts</h3>
<LemonTag type="warning">ALPHA</LemonTag>
</div>
</LemonModal.Header>
<LemonModal.Content>
<div className="mb-4">
Expand Down
15 changes: 13 additions & 2 deletions frontend/src/scenes/saved-insights/SavedInsights.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
IconVideoCamera,
IconWarning,
} from '@posthog/icons'
import { LemonSelectOptions } from '@posthog/lemon-ui'
import { LemonSelectOptions, LemonTag } from '@posthog/lemon-ui'
import { useActions, useValues } from 'kea'
import { ActivityLog } from 'lib/components/ActivityLog/ActivityLog'
import { Alerts } from 'lib/components/Alerts/views/Alerts'
Expand Down Expand Up @@ -622,7 +622,18 @@ export function SavedInsights(): JSX.Element {
{ key: SavedInsightsTabs.Yours, label: 'Your insights' },
{ key: SavedInsightsTabs.Favorites, label: 'Favorites' },
{ key: SavedInsightsTabs.History, label: 'History' },
...(showAlerts ? [{ key: SavedInsightsTabs.Alerts, label: 'Alerts' }] : []),
...(showAlerts
? [
{
key: SavedInsightsTabs.Alerts,
label: (
<div className="flex items-center gap-2">
Alerts <LemonTag type="highlight">ALPHA</LemonTag>
</div>
),
},
]
: []),
]}
/>

Expand Down
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"] != instance.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 %}
Loading