def _create_in_snuba(project, dataset, query, aggregation, time_window, resolution, environments): conditions = resolve_discover_aliases( {"conditions": get_filter(query).conditions})[0]["conditions"] if environments: conditions.append( ["environment", "IN", [env.name for env in environments]]) response = _snuba_pool.urlopen( "POST", "/%s/subscriptions" % (dataset.value, ), body=json.dumps({ "project_id": project.id, "dataset": dataset.value, # We only care about conditions here. Filter keys only matter for # filtering to project and groups. Projects are handled with an # explicit param, and groups can't be queried here. "conditions": conditions, "aggregations": [query_aggregation_to_snuba[aggregation]], "time_window": int(time_window.total_seconds()), "resolution": int(resolution.total_seconds()), }), ) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status) return json.loads(response.data)["subscription_id"]
def _create_in_snuba(subscription): snuba_query = subscription.snuba_query snuba_filter = get_filter(snuba_query.query) snuba_filter.update_with( resolve_field_list([snuba_query.aggregate], snuba_filter, auto_fields=False)) snuba_filter = resolve_discover_aliases(snuba_filter)[0] if snuba_query.environment: snuba_filter.conditions.append( ["environment", "=", snuba_query.environment.name]) conditions = apply_dataset_conditions(QueryDatasets(snuba_query.dataset), snuba_filter.conditions) response = _snuba_pool.urlopen( "POST", "/%s/subscriptions" % (snuba_query.dataset, ), body=json.dumps({ "project_id": subscription.project_id, "dataset": snuba_query.dataset, "conditions": conditions, "aggregations": snuba_filter.aggregations, "time_window": snuba_query.time_window, "resolution": snuba_query.resolution, }), ) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status) return json.loads(response.data)["subscription_id"]
def _create_in_snuba(subscription): snuba_query = subscription.snuba_query snuba_filter = build_snuba_filter( QueryDatasets(snuba_query.dataset), snuba_query.query, snuba_query.aggregate, snuba_query.environment, snuba_query.event_types, ) response = _snuba_pool.urlopen( "POST", "/%s/subscriptions" % (snuba_query.dataset,), body=json.dumps( { "project_id": subscription.project_id, "dataset": snuba_query.dataset, "conditions": snuba_filter.conditions, "aggregations": snuba_filter.aggregations, "time_window": snuba_query.time_window, "resolution": snuba_query.resolution, } ), ) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status) return json.loads(response.data)["subscription_id"]
def _create_in_snuba(subscription): conditions = resolve_discover_aliases( {"conditions": get_filter(subscription.query).conditions})[0]["conditions"] environments = list(subscription.environments.all()) if environments: conditions.append( ["environment", "IN", [env.name for env in environments]]) response = _snuba_pool.urlopen( "POST", "/%s/subscriptions" % (subscription.dataset, ), body=json.dumps({ "project_id": subscription.project_id, "dataset": subscription.dataset, # We only care about conditions here. Filter keys only matter for # filtering to project and groups. Projects are handled with an # explicit param, and groups can't be queried here. "conditions": conditions, "aggregations": [ query_aggregation_to_snuba[QueryAggregations( subscription.aggregation)] ], "time_window": subscription.time_window, "resolution": subscription.resolution, }), ) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status) return json.loads(response.data)["subscription_id"]
def _create_in_snuba(subscription): conditions = resolve_discover_aliases(get_filter(subscription.query))[0].conditions try: environment = subscription.environments.all()[:1].get() except Environment.DoesNotExist: environment = None if environment: conditions.append(["environment", "=", environment.name]) conditions = apply_dataset_conditions(QueryDatasets(subscription.dataset), conditions) response = _snuba_pool.urlopen( "POST", "/%s/subscriptions" % (subscription.dataset,), body=json.dumps( { "project_id": subscription.project_id, "dataset": subscription.dataset, # We only care about conditions here. Filter keys only matter for # filtering to project and groups. Projects are handled with an # explicit param, and groups can't be queried here. "conditions": conditions, "aggregations": [ query_aggregation_to_snuba[QueryAggregations(subscription.aggregation)] ], "time_window": subscription.time_window, "resolution": subscription.resolution, } ), ) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status) return json.loads(response.data)["subscription_id"]
def _delete_from_snuba(subscription): response = _snuba_pool.urlopen("DELETE", "/subscriptions/%s" % subscription.subscription_id, retries=False) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status)
def delete_snuba_subscription(subscription_id): """ Deletes a subscription to a snuba query. :param subscription_id: The uuid of the subscription to delete :return: """ response = _snuba_pool.urlopen("DELETE", "/subscriptions/%s" % subscription_id, retries=False) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status)
def _create_in_snuba(subscription): snuba_query = subscription.snuba_query snuba_filter = build_snuba_filter( QueryDatasets(snuba_query.dataset), snuba_query.query, snuba_query.aggregate, snuba_query.environment, snuba_query.event_types, ) body = { "project_id": subscription.project_id, "project": subscription.project_id, # for SnQL SDK "dataset": snuba_query.dataset, "conditions": snuba_filter.conditions, "aggregations": snuba_filter.aggregations, "time_window": snuba_query.time_window, "resolution": snuba_query.resolution, } if Dataset(snuba_query.dataset) == Dataset.Sessions: body.update({ "organization": subscription.project.organization_id, }) try: metrics.incr("snuba.snql.subscription.create", tags={"dataset": snuba_query.dataset}) snql_query = json_to_snql(body, snuba_query.dataset) snql_query.validate() body["query"] = str(snql_query) body["type"] = "delegate" # mark this as a combined subscription except Exception as e: logger.warning( "snuba.snql.subscription.parsing.error", extra={ "error": str(e), "params": json.dumps(body), "dataset": snuba_query.dataset }, ) metrics.incr("snuba.snql.subscription.parsing.error", tags={"dataset": snuba_query.dataset}) response = _snuba_pool.urlopen( "POST", f"/{snuba_query.dataset}/subscriptions", body=json.dumps(body), ) if response.status != 202: metrics.incr("snuba.snql.subscription.http.error", tags={"dataset": snuba_query.dataset}) raise SnubaError("HTTP %s response from Snuba!" % response.status) return json.loads(response.data)["subscription_id"]
def _create_in_snuba(subscription: QuerySubscription) -> str: snuba_query = subscription.snuba_query entity_subscription = get_entity_subscription_for_dataset( dataset=QueryDatasets(snuba_query.dataset), aggregate=snuba_query.aggregate, time_window=snuba_query.time_window, extra_fields={ "org_id": subscription.project.organization_id, "event_types": snuba_query.event_types, }, ) snuba_filter = build_snuba_filter( entity_subscription, snuba_query.query, snuba_query.environment, ) body = { "project_id": subscription.project_id, "project": subscription.project_id, # for SnQL SDK "dataset": snuba_query.dataset, "conditions": snuba_filter.conditions, "aggregations": snuba_filter.aggregations, "time_window": snuba_query.time_window, "resolution": snuba_query.resolution, **entity_subscription.get_entity_extra_params(), } try: metrics.incr("snuba.snql.subscription.create", tags={"dataset": snuba_query.dataset}) snql_query = json_to_snql(body, entity_subscription.entity_key.value) snql_query.validate() body["query"] = str(snql_query) body["type"] = "delegate" # mark this as a combined subscription except Exception as e: logger.warning( "snuba.snql.subscription.parsing.error", extra={"error": str(e), "params": json.dumps(body), "dataset": snuba_query.dataset}, ) metrics.incr("snuba.snql.subscription.parsing.error", tags={"dataset": snuba_query.dataset}) response = _snuba_pool.urlopen( "POST", f"/{snuba_query.dataset}/{entity_subscription.entity_key.value}/subscriptions", body=json.dumps(body), ) if response.status != 202: metrics.incr("snuba.snql.subscription.http.error", tags={"dataset": snuba_query.dataset}) raise SnubaError("HTTP %s response from Snuba!" % response.status) return json.loads(response.data)["subscription_id"]
def _create_in_snuba(project, dataset, query, aggregation, time_window, resolution): response = _snuba_pool.urlopen( "POST", "/%s/subscriptions" % (dataset.value, ), body=json.dumps({ "project_id": project.id, "dataset": dataset.value, # We only care about conditions here. Filter keys only matter for # filtering to project and groups. Projects are handled with an # explicit param, and groups can't be queried here. "conditions": get_filter(query).conditions, "aggregates": [query_aggregation_to_snuba[aggregation]], "time_window": time_window, "resolution": resolution, }), retries=False, ) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status) return json.loads(response.data)["subscription_id"]
def create_snuba_subscription(project, dataset, query, aggregations, time_window, resolution): """ Creates a subscription to a snuba query. :param project: The project we're applying the query to :param dataset: The snuba dataset to query and aggregate over :param query: An event search query that we can parse and convert into a set of Snuba conditions :param aggregations: A list of aggregations to calculate over the time window :param time_window: The time window to aggregate over :param resolution: How often to receive updates/bucket size :return: A uuid representing the subscription id. """ # TODO: Might make sense to move this into snuba if we have wider use for # it. response = _snuba_pool.urlopen( "POST", "/subscriptions", body=json.dumps( { "project_id": project.id, "dataset": dataset.value, # We only care about conditions here. Filter keys only matter for # filtering to project and groups. Projects are handled with an # explicit param, and groups can't be queried here. "conditions": get_snuba_query_args(query)["conditions"], "aggregates": [alert_aggregation_to_snuba[agg] for agg in aggregations], "time_window": time_window, "resolution": resolution, } ), retries=False, ) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status) return uuid.UUID(json.loads(response.data)["subscription_id"])
def _delete_from_snuba(dataset, subscription_id): response = _snuba_pool.urlopen( "DELETE", "/%s/subscriptions/%s" % (dataset.value, subscription_id)) if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status)
def _delete_from_snuba(dataset: QueryDatasets, subscription_id: str) -> None: response = _snuba_pool.urlopen( "DELETE", f"/{dataset.value}/subscriptions/{subscription_id}") if response.status != 202: raise SnubaError("HTTP %s response from Snuba!" % response.status)