Skip to content

Commit

Permalink
code
Browse files Browse the repository at this point in the history
  • Loading branch information
Rachel Chen authored and Rachel Chen committed Aug 8, 2024
1 parent a1afa29 commit b587759
Showing 1 changed file with 10 additions and 15 deletions.
25 changes: 10 additions & 15 deletions src/sentry/utils/snuba.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import functools
import logging
import os
import random
import re
import time
from collections import namedtuple
Expand Down Expand Up @@ -1043,6 +1042,15 @@ def _apply_cache_and_build_results(
return [result[1] for result in results]


def _is_rejected_query(body: Any) -> bool:
return (
"quota_allowance" in body
and "summary" in body["quota_allowance"]
and "rejected_by" in body["quota_allowance"]["summary"]
and body["quota_allowance"]["summary"]["rejected_by"] is not None
)


def _bulk_snuba_query(snuba_requests: Sequence[SnubaRequest]) -> ResultSet:
snuba_requests_list = list(snuba_requests)

Expand Down Expand Up @@ -1099,7 +1107,7 @@ def _bulk_snuba_query(snuba_requests: Sequence[SnubaRequest]) -> ResultSet:
raise UnexpectedResponseError(f"Could not decode JSON response: {response.data!r}")

allocation_policy_prefix = "allocation_policy."
if "quota_allowance" in body and "summary" in body["quota_allowance"]:
if _is_rejected_query(body):
quota_allowance_summary = body["quota_allowance"]["summary"]
span.set_tag(
f"{allocation_policy_prefix}threads_used",
Expand All @@ -1118,19 +1126,6 @@ def _bulk_snuba_query(snuba_requests: Sequence[SnubaRequest]) -> ResultSet:
span.set_tag(k, v)
sentry_sdk.set_tag(k, v)

if (
"throttled_by" in quota_allowance_summary
and quota_allowance_summary["throttled_by"]
):
metrics.incr("snuba.client.query.throttle", tags={"referrer": referrer})
if random.random() < 0.01:
logger.warning(
"Warning: Query is throttled", extra={"response.data": response.data}
)
sentry_sdk.capture_message(
f"Warning: Query from referrer {referrer} is throttled", level="warning"
)

if response.status != 200:
_log_request_query(snuba_requests_list[index].request)
metrics.incr(
Expand Down

0 comments on commit b587759

Please sign in to comment.