Exemplo n.º 1
0
    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
Exemplo n.º 2
0
    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())
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
    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())
Exemplo n.º 5
0
    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())
Exemplo n.º 6
0
    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
Exemplo n.º 7
0
def update_alert_rule(
    alert_rule,
    name=None,
    threshold_type=None,
    query=None,
    aggregation=None,
    time_window=None,
    alert_threshold=None,
    resolve_threshold=None,
    threshold_period=None,
):
    """
    Updates an alert rule.

    :param alert_rule: The alert rule to update
    :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: An AlertRuleAggregation that we want 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 updated `AlertRule`
    """
    if (name and alert_rule.name != name and AlertRule.objects.filter(
            project=alert_rule.project, name=name).exists()):
        raise AlertRuleNameAlreadyUsedError()

    updated_fields = {}
    if name:
        updated_fields["name"] = name
    if threshold_type:
        updated_fields["threshold_type"] = threshold_type.value
    if query is not None:
        validate_alert_rule_query(query)
        updated_fields["query"] = query
    if aggregation is not None:
        updated_fields["aggregation"] = aggregation.value
    if time_window:
        updated_fields["time_window"] = time_window
    if alert_threshold is not None:
        updated_fields["alert_threshold"] = alert_threshold
    if resolve_threshold is not None:
        updated_fields["resolve_threshold"] = resolve_threshold
    if threshold_period:
        updated_fields["threshold_period"] = threshold_period

    with transaction.atomic():
        if query is not None or aggregation is not None or time_window is not None:
            # TODO: We're assuming only one subscription for the moment
            subscription = (AlertRuleQuerySubscription.objects.select_related(
                "query_subscription").get(
                    alert_rule=alert_rule).query_subscription)
            # If updating any details of the query, update the Snuba subscription
            update_snuba_subscription(
                subscription,
                query if query is not None else alert_rule.query,
                aggregation if aggregation is not None else QueryAggregations(
                    alert_rule.aggregation),
                time_window if time_window else alert_rule.time_window,
                DEFAULT_ALERT_RULE_RESOLUTION,
            )
        alert_rule.update(**updated_fields)

    return alert_rule