def test(self): subscription = create_snuba_subscription( self.project, "something", QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), [], ) other_subscription = create_snuba_subscription( self.create_project(organization=self.organization), "something", QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), [], ) subscription_ids = [subscription.id, other_subscription.id] bulk_delete_snuba_subscriptions([subscription, other_subscription]) assert not QuerySubscription.objects.filter( id__in=subscription_ids).exists()
def test(self): with self.tasks(): snuba_query = create_snuba_query( QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), None, ) subscription = create_snuba_subscription(self.project, "something", snuba_query) snuba_query = create_snuba_query( QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), None, ) other_subscription = create_snuba_subscription( self.create_project(organization=self.organization), "something", snuba_query) subscription_ids = [subscription.id, other_subscription.id] bulk_delete_snuba_subscriptions([subscription, other_subscription]) assert (QuerySubscription.objects.filter( id__in=subscription_ids, status=QuerySubscription.Status.DELETING.value, subscription_id__isnull=False, ).count() == 2)
def create_alert_rule( project, name, threshold_type, query, aggregation, time_window, alert_threshold, resolve_threshold, threshold_period, ): """ Creates an alert rule for a project. :param project: :param name: Name for the alert rule. This will be used as part of the incident name, and must be unique per project. :param threshold_type: An AlertRuleThresholdType :param query: An event search query to subscribe to and monitor for alerts :param aggregation: A QueryAggregation to fetch for this alert rule :param time_window: Time period to aggregate over, in minutes. :param alert_threshold: Value that the subscription needs to reach to trigger the alert :param resolve_threshold: Value that the subscription needs to reach to resolve the alert :param threshold_period: How many update periods the value of the subscription needs to exceed the threshold before triggering :return: The created `AlertRule` """ dataset = QueryDatasets.EVENTS resolution = DEFAULT_ALERT_RULE_RESOLUTION validate_alert_rule_query(query) if AlertRule.objects.filter(project=project, name=name).exists(): raise AlertRuleNameAlreadyUsedError() with transaction.atomic(): query_subscription = create_snuba_subscription( project, tasks.INCIDENTS_SNUBA_SUBSCRIPTION_TYPE, dataset, query, aggregation, time_window, resolution, ) alert_rule = AlertRule.objects.create( project=project, name=name, threshold_type=threshold_type.value, dataset=dataset.value, query=query, aggregation=aggregation.value, time_window=time_window, resolution=resolution, alert_threshold=alert_threshold, resolve_threshold=resolve_threshold, threshold_period=threshold_period, ) AlertRuleQuerySubscription.objects.create( query_subscription=query_subscription, alert_rule=alert_rule) return alert_rule
def test(self): with self.tasks(): snuba_query = create_snuba_query( QueryDatasets.EVENTS, "level:error", "count()", timedelta(minutes=10), timedelta(minutes=1), None, ) subscription = create_snuba_subscription(self.project, "something", snuba_query) query = "level:warning" aggregate = "count_unique(tags[sentry:user])" time_window = timedelta(minutes=20) resolution = timedelta(minutes=2) subscription = QuerySubscription.objects.get(id=subscription.id) subscription_id = subscription.subscription_id snuba_query.update( query=query, time_window=int(time_window.total_seconds()), resolution=int(resolution.total_seconds()), environment=self.environment, aggregate=aggregate, ) assert subscription_id is not None update_snuba_subscription(subscription, snuba_query) assert subscription.status == QuerySubscription.Status.UPDATING.value assert subscription.subscription_id == subscription_id assert subscription.snuba_query.query == query assert subscription.snuba_query.aggregate == aggregate assert subscription.snuba_query.time_window == int(time_window.total_seconds()) assert subscription.snuba_query.resolution == int(resolution.total_seconds())
def test_with_task(self): with self.tasks(): old_dataset = QueryDatasets.EVENTS snuba_query = create_snuba_query( old_dataset, "level:error", "count()", timedelta(minutes=10), timedelta(minutes=1), None, ) subscription = create_snuba_subscription(self.project, "something", snuba_query) dataset = QueryDatasets.TRANSACTIONS query = "level:warning" aggregate = "count_unique(tags[sentry:user])" time_window = timedelta(minutes=20) resolution = timedelta(minutes=2) subscription = QuerySubscription.objects.get(id=subscription.id) subscription_id = subscription.subscription_id assert subscription_id is not None snuba_query.update( dataset=dataset.value, query=query, time_window=int(time_window.total_seconds()), resolution=int(resolution.total_seconds()), environment=self.environment, aggregate=aggregate, ) update_snuba_subscription(subscription, old_dataset) subscription = QuerySubscription.objects.get(id=subscription.id) assert subscription.status == QuerySubscription.Status.ACTIVE.value assert subscription.subscription_id is not None assert subscription.subscription_id != subscription_id
def test(self): with self.tasks(): subscription = create_snuba_subscription( self.project, "something", QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), [], ) query = "level:warning" aggregation = QueryAggregations.UNIQUE_USERS time_window = timedelta(minutes=20) resolution = timedelta(minutes=2) subscription = QuerySubscription.objects.get(id=subscription.id) subscription_id = subscription.subscription_id assert subscription_id is not None update_snuba_subscription(subscription, query, aggregation, time_window, resolution, []) assert subscription.status == QuerySubscription.Status.UPDATING.value assert subscription.subscription_id == subscription_id assert subscription.query == query assert subscription.aggregation == aggregation.value assert subscription.time_window == int(time_window.total_seconds()) assert subscription.resolution == int(resolution.total_seconds())
def test_with_task(self): with self.tasks(): snuba_query = create_snuba_query( QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), None, ) subscription = create_snuba_subscription(self.project, "something", snuba_query) query = "level:warning" aggregation = QueryAggregations.UNIQUE_USERS time_window = timedelta(minutes=20) resolution = timedelta(minutes=2) subscription = QuerySubscription.objects.get(id=subscription.id) subscription_id = subscription.subscription_id assert subscription_id is not None snuba_query.update( query=query, time_window=int(time_window.total_seconds()), resolution=int(resolution.total_seconds()), environment=self.environment, aggregate=translate_aggregation(aggregation), ) update_snuba_subscription(subscription, snuba_query) subscription = QuerySubscription.objects.get(id=subscription.id) assert subscription.status == QuerySubscription.Status.ACTIVE.value assert subscription.subscription_id is not None assert subscription.subscription_id != subscription_id
def test_subscription_registered(self): registration_key = "registered_test" mock_callback = Mock() register_subscriber(registration_key)(mock_callback) with self.tasks(): snuba_query = create_snuba_query( QueryDatasets.EVENTS, "hello", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), None, ) sub = create_snuba_subscription(self.project, registration_key, snuba_query) sub.refresh_from_db() data = self.valid_wrapper data["payload"]["subscription_id"] = sub.subscription_id self.consumer.handle_message(self.build_mock_message(data)) data = deepcopy(data) data["payload"]["values"] = data["payload"]["result"] data["payload"]["timestamp"] = parse_date( data["payload"]["timestamp"]).replace(tzinfo=pytz.utc) mock_callback.assert_called_once_with(data["payload"], sub)
def test(self): subscription = create_snuba_subscription( self.project, "something", QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), ) subscription_id = subscription.id delete_snuba_subscription(subscription) assert not QuerySubscription.objects.filter(id=subscription_id).exists()
def test_with_task(self): with self.tasks(): snuba_query = create_snuba_query( QueryDatasets.EVENTS, "level:error", "count()", timedelta(minutes=10), timedelta(minutes=1), None, ) subscription = create_snuba_subscription(self.project, "something", snuba_query) subscription_id = subscription.id delete_snuba_subscription(subscription) assert not QuerySubscription.objects.filter(id=subscription_id).exists()
def test(self): type = "something" dataset = QueryDatasets.EVENTS query = "level:error" time_window = timedelta(minutes=10) resolution = timedelta(minutes=1) snuba_query = create_snuba_query( dataset, query, "count()", time_window, resolution, self.environment ) subscription = create_snuba_subscription(self.project, type, snuba_query) assert subscription.status == QuerySubscription.Status.CREATING.value assert subscription.project == self.project assert subscription.type == type assert subscription.subscription_id is None
def create_subscription(self): with self.tasks(): snuba_query = create_snuba_query( QueryDatasets.EVENTS, "hello", "count()", timedelta(minutes=1), timedelta(minutes=1), None, ) sub = create_snuba_subscription(self.project, self.registration_key, snuba_query) sub.subscription_id = self.subscription_id sub.status = 0 sub.save() return sub
def test_translated_query(self): type = "something" dataset = QueryDatasets.EVENTS query = "event.type:error" time_window = timedelta(minutes=10) resolution = timedelta(minutes=1) with self.tasks(): snuba_query = create_snuba_query( dataset, query, "count()", time_window, resolution, self.environment ) subscription = create_snuba_subscription(self.project, type, snuba_query) subscription = QuerySubscription.objects.get(id=subscription.id) assert subscription.status == QuerySubscription.Status.ACTIVE.value assert subscription.project == self.project assert subscription.type == type assert subscription.subscription_id is not None
def test_translated_query(self): type = "something" dataset = QueryDatasets.EVENTS query = "event.type:error" aggregation = QueryAggregations.TOTAL time_window = timedelta(minutes=10) resolution = timedelta(minutes=1) subscription = create_snuba_subscription( self.project, type, dataset, query, aggregation, time_window, resolution ) assert subscription.project == self.project assert subscription.type == type assert subscription.subscription_id != "" assert subscription.dataset == dataset.value assert subscription.query == query assert subscription.aggregation == aggregation.value assert subscription.time_window == int(time_window.total_seconds()) assert subscription.resolution == int(resolution.total_seconds())
def test(self): type = "something" dataset = QueryDatasets.EVENTS query = "level:error" aggregation = QueryAggregations.TOTAL time_window = 10 resolution = 1 subscription = create_snuba_subscription(self.project, type, dataset, query, aggregation, time_window, resolution) assert subscription.project == self.project assert subscription.type == type assert subscription.subscription_id != "" assert subscription.dataset == dataset.value assert subscription.query == query assert subscription.aggregation == aggregation.value assert subscription.time_window == time_window assert subscription.resolution == resolution
def test(self): with self.tasks(): snuba_query = create_snuba_query( QueryDatasets.EVENTS, "level:error", "count()", timedelta(minutes=10), timedelta(minutes=1), None, ) subscription = create_snuba_subscription(self.project, "something", snuba_query) # Refetch since snuba creation happens in a task subscription = QuerySubscription.objects.get(id=subscription.id) subscription_id = subscription.subscription_id assert subscription_id is not None delete_snuba_subscription(subscription) assert subscription.status == QuerySubscription.Status.DELETING.value assert subscription.subscription_id == subscription_id
def test_with_task(self): with self.tasks(): type = "something" dataset = QueryDatasets.EVENTS query = "level:error" aggregation = QueryAggregations.TOTAL time_window = timedelta(minutes=10) resolution = timedelta(minutes=1) snuba_query = create_snuba_query(dataset, query, aggregation, time_window, resolution, self.environment) subscription = create_snuba_subscription(self.project, type, snuba_query) subscription = QuerySubscription.objects.get(id=subscription.id) assert subscription.status == QuerySubscription.Status.ACTIVE.value assert subscription.project == self.project assert subscription.type == type assert subscription.subscription_id is not None
def test(self): type = "something" dataset = QueryDatasets.EVENTS query = "level:error" aggregation = QueryAggregations.TOTAL time_window = timedelta(minutes=10) resolution = timedelta(minutes=1) subscription = create_snuba_subscription( self.project, type, dataset, query, aggregation, time_window, resolution, [] ) assert subscription.status == QuerySubscription.Status.CREATING.value assert subscription.project == self.project assert subscription.type == type assert subscription.subscription_id is None assert subscription.dataset == dataset.value assert subscription.query == query assert subscription.aggregation == aggregation.value assert subscription.time_window == int(time_window.total_seconds()) assert subscription.resolution == int(resolution.total_seconds())
def test_subscriptions(self): dataset = QueryDatasets.EVENTS snuba_query = create_snuba_query( dataset, "hello", "count_unique(tags[sentry:user])", timedelta(minutes=100), timedelta(minutes=2), self.environment, ) sub = create_snuba_subscription(self.project, "hi", snuba_query) new_env = self.create_environment() query = "level:error" aggregate = "count()" time_window = timedelta(minutes=10) resolution = timedelta(minutes=1) update_snuba_query(snuba_query, query, aggregate, time_window, resolution, new_env) sub.refresh_from_db() assert sub.snuba_query == snuba_query assert sub.status == QuerySubscription.Status.UPDATING.value
def test_with_task(self): with self.tasks(): type = "something" dataset = QueryDatasets.EVENTS query = "level:error" aggregation = QueryAggregations.TOTAL time_window = timedelta(minutes=10) resolution = timedelta(minutes=1) subscription = create_snuba_subscription(self.project, type, dataset, query, aggregation, time_window, resolution, None) subscription = QuerySubscription.objects.get(id=subscription.id) assert subscription.status == QuerySubscription.Status.ACTIVE.value assert subscription.project == self.project assert subscription.type == type assert subscription.subscription_id is not None assert subscription.dataset == dataset.value assert subscription.query == query assert subscription.aggregation == aggregation.value assert subscription.time_window == int(time_window.total_seconds()) assert subscription.resolution == int(resolution.total_seconds())
def test(self): subscription = create_snuba_subscription( self.project, "something", QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, timedelta(minutes=10), timedelta(minutes=1), ) query = "level:warning" aggregation = QueryAggregations.UNIQUE_USERS time_window = timedelta(minutes=20) resolution = timedelta(minutes=2) old_subscription_id = subscription.subscription_id update_snuba_subscription(subscription, query, aggregation, time_window, resolution) assert subscription.subscription_id != old_subscription_id assert subscription.query == query assert subscription.aggregation == aggregation.value assert subscription.time_window == int(time_window.total_seconds()) assert subscription.resolution == int(resolution.total_seconds())
def test(self): subscription = create_snuba_subscription( self.project, "something", QueryDatasets.EVENTS, "level:error", QueryAggregations.TOTAL, 10, 1, ) query = "level:warning" aggregation = QueryAggregations.UNIQUE_USERS time_window = 20 resolution = 2 old_subscription_id = subscription.subscription_id update_snuba_subscription(subscription, query, aggregation, time_window, resolution) assert subscription.subscription_id != old_subscription_id assert subscription.query == query assert subscription.aggregation == aggregation.value assert subscription.time_window == time_window assert subscription.resolution == resolution