def handle_subscription_metrics_logger(subscription_update, subscription): """ Logs results from a `QuerySubscription`. :param subscription_update: dict formatted according to schemas in sentry.snuba.json_schemas.SUBSCRIPTION_PAYLOAD_VERSIONS :param subscription: The `QuerySubscription` that this update is for """ from sentry.incidents.subscription_processor import SubscriptionProcessor try: processor = SubscriptionProcessor(subscription) processor.alert_rule = AlertRule() aggregation_value = int( processor.get_aggregation_value(subscription_update)) processor.alert_rule.comparison_delta = int( timedelta(days=7).total_seconds()) comparison_value = processor.get_aggregation_value(subscription_update) tags = { "project_id": subscription.project_id, "project_slug": subscription.project.slug, "subscription_id": subscription.id, "time_window": subscription.snuba_query.time_window, } metrics.incr("subscriptions.result.value", aggregation_value, tags=tags, sample_rate=1.0) if comparison_value is not None: metrics.incr("subscriptions.result.comparison", int(comparison_value), tags=tags, sample_rate=1.0) except Exception: logger.exception("Failed to log subscription results")
def get(self, request): organization = Organization(slug="myorg") project = Project(id=30, slug="myproj") incident = Incident(identifier=123, organization=organization, title="Something broke") alert_rule = AlertRule(id=1, organization=organization, aggregation=1, query="is:unresolved") alert_rule_trigger = AlertRuleTrigger(id=5, alert_rule=alert_rule, alert_threshold=100, resolve_threshold=50) action = AlertRuleTriggerAction(id=10, alert_rule_trigger=alert_rule_trigger) handler = EmailActionHandler(action, incident, project) email = handler.build_message( handler.generate_email_context(TriggerStatus.ACTIVE), TriggerStatus.ACTIVE, 1) return MailPreview(html_template=email.html_template, text_template=email.template, context=email.context).render(request)
def get(self, request: Request) -> Response: organization = Organization(slug="myorg") project = Project(slug="myproject", organization=organization) query = SnubaQuery( time_window=60, query="transaction:/some/transaction", aggregate="count()" ) alert_rule = AlertRule(id=1, organization=organization, name="My Alert", snuba_query=query) incident = Incident( id=2, identifier=123, organization=organization, title="Something broke", alert_rule=alert_rule, status=IncidentStatus.CRITICAL, ) trigger = AlertRuleTrigger(alert_rule=alert_rule) context = generate_incident_trigger_email_context( project, incident, trigger, TriggerStatus.ACTIVE ) return MailPreview( text_template="sentry/emails/incidents/trigger.txt", html_template="sentry/emails/incidents/trigger.html", context=context, ).render(request)
def test(self): stat_keys = build_alert_rule_stat_keys(AlertRule(id=1), QuerySubscription(project_id=2)) assert stat_keys == [ "{alert_rule:1:project:2}:last_update", "{alert_rule:1:project:2}:resolve_triggered", ]
def test(self): stat_keys = build_trigger_stat_keys( AlertRule(id=1), QuerySubscription(project_id=2), [AlertRuleTrigger(id=3), AlertRuleTrigger(id=4)], ) assert stat_keys == [ "{alert_rule:1:project:2}:trigger:3:alert_triggered", "{alert_rule:1:project:2}:trigger:4:alert_triggered", ]
def test_remove_unchanged_fields(self): alert_rule = AlertRule(**self.valid_params) self._run_changed_fields_test(alert_rule, self.valid_params, {}) self._run_changed_fields_test(alert_rule, {"name": "a name"}, {"name": "a name"}) self._run_changed_fields_test(alert_rule, {"aggregation": 0}, {}) self._run_changed_fields_test( alert_rule, {"aggregation": 1}, {"aggregation": QueryAggregations.UNIQUE_USERS}) self._run_changed_fields_test(alert_rule, {"threshold_type": 0}, {}) self._run_changed_fields_test( alert_rule, {"threshold_type": 1}, {"threshold_type": AlertRuleThresholdType.BELOW})
def test(self): alert_rule = AlertRule(id=1) sub = QuerySubscription(project_id=2) date = datetime.utcnow().replace(tzinfo=pytz.utc) update_alert_rule_stats(alert_rule, sub, date, {3: 20, 4: 3}, 15) client = get_redis_client() results = map( int, client.mget([ "{alert_rule:1:project:2}:last_update", "{alert_rule:1:project:2}:trigger:3:alert_triggered", "{alert_rule:1:project:2}:trigger:4:alert_triggered", "{alert_rule:1:project:2}:resolve_triggered", ]), ) assert results == [int(to_timestamp(date)), 20, 3, 15]
def test(self): alert_rule = AlertRule(id=1) sub = QuerySubscription(project_id=2) update_alert_rule_stats(alert_rule, sub, 1234, {3: 20, 4: 3}, {3: 10, 4: 15}) client = get_redis_client() results = map( int, client.mget( [ "{alert_rule:1:project:2}:last_update", "{alert_rule:1:project:2}:trigger:3:alert_triggered", "{alert_rule:1:project:2}:trigger:3:resolve_triggered", "{alert_rule:1:project:2}:trigger:4:alert_triggered", "{alert_rule:1:project:2}:trigger:4:resolve_triggered", ] ), ) assert results == [1234, 20, 10, 3, 15]
def test(self): alert_rule = AlertRule(id=1) sub = QuerySubscription(project_id=2) triggers = [AlertRuleTrigger(id=3), AlertRuleTrigger(id=4)] client = get_redis_client() pipeline = client.pipeline() timestamp = datetime.now().replace(tzinfo=pytz.utc, microsecond=0) pipeline.set("{alert_rule:1:project:2}:last_update", int(to_timestamp(timestamp))) pipeline.set("{alert_rule:1:project:2}:resolve_triggered", 20) for key, value in [ ("{alert_rule:1:project:2}:trigger:3:alert_triggered", 1), ("{alert_rule:1:project:2}:trigger:4:alert_triggered", 3), ]: pipeline.set(key, value) pipeline.execute() last_update, alert_counts, resolve_counts = get_alert_rule_stats(alert_rule, sub, triggers) assert last_update == timestamp assert alert_counts == {3: 1, 4: 3} assert resolve_counts == 20
def test(self): alert_rule = AlertRule(id=1) sub = QuerySubscription(project_id=2) triggers = [AlertRuleTrigger(id=3), AlertRuleTrigger(id=4)] client = get_redis_client() pipeline = client.pipeline() pipeline.set("{alert_rule:1:project:2}:last_update", 1234) for key, value in [ ("{alert_rule:1:project:2}:trigger:3:alert_triggered", 1), ("{alert_rule:1:project:2}:trigger:3:resolve_triggered", 2), ("{alert_rule:1:project:2}:trigger:4:alert_triggered", 3), ("{alert_rule:1:project:2}:trigger:4:resolve_triggered", 4), ]: pipeline.set(key, value) pipeline.execute() last_update, alert_counts, resolve_counts = get_alert_rule_stats(alert_rule, sub, triggers) assert last_update == 1234 assert alert_counts == {3: 1, 4: 3} assert resolve_counts == {3: 2, 4: 4}