Skip to content

Raise an error rather than relying on Snuba telling us #69575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/sentry/search/events/builder/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
from sentry.utils.dates import outside_retention_with_modified_start
from sentry.utils.snuba import (
QueryOutsideRetentionError,
UnqualifiedQueryError,
is_duration_measurement,
is_measurement,
is_numeric_measurement,
Expand Down Expand Up @@ -583,6 +584,10 @@ def resolve_params(self) -> list[WhereType]:
if self.end:
conditions.append(Condition(self.column("timestamp"), Op.LT, self.end))

# This will prevent calling Snuba with an empty list of projects, thus, returning no data.
if not self.params.project_ids:
raise UnqualifiedQueryError("You need to specify at least one project.")

conditions.append(
Condition(
self.column("project_id"),
Expand Down
38 changes: 23 additions & 15 deletions src/sentry/snuba/metrics/query_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,9 +716,11 @@ def translate_meta_results(
results.append(
{
"name": parent_alias,
"type": record["type"]
if defined_parent_meta_type is None
else defined_parent_meta_type,
"type": (
record["type"]
if defined_parent_meta_type is None
else defined_parent_meta_type
),
}
)
continue
Expand Down Expand Up @@ -911,9 +913,11 @@ def _build_where(self) -> list[BooleanCondition | Condition]:
alias=condition.lhs.alias,
)[0],
op=condition.op,
rhs=resolve_tag_value(self._use_case_id, self._org_id, condition.rhs)
if require_rhs_condition_resolution(condition.lhs.op)
else condition.rhs,
rhs=(
resolve_tag_value(self._use_case_id, self._org_id, condition.rhs)
if require_rhs_condition_resolution(condition.lhs.op)
else condition.rhs
),
)
)
except IndexError:
Expand Down Expand Up @@ -1339,9 +1343,11 @@ def translate_result_groups(self):
# to determine whether we need to reverse the tag value or not.
groupby_alias_to_groupby_column = (
{
metric_groupby_obj.alias: metric_groupby_obj.field
if isinstance(metric_groupby_obj.field, str)
else metric_groupby_obj.field.op
metric_groupby_obj.alias: (
metric_groupby_obj.field
if isinstance(metric_groupby_obj.field, str)
else metric_groupby_obj.field.op
)
for metric_groupby_obj in self._metrics_query.groupby
}
if self._metrics_query.groupby
Expand All @@ -1352,13 +1358,15 @@ def translate_result_groups(self):
dict(
by=dict(
(
key,
reverse_resolve_tag_value(
self._use_case_id, self._organization_id, value, weak=True
),
(
key,
reverse_resolve_tag_value(
self._use_case_id, self._organization_id, value, weak=True
),
)
if groupby_alias_to_groupby_column.get(key) not in NON_RESOLVABLE_TAG_VALUES
else (key, value)
)
if groupby_alias_to_groupby_column.get(key) not in NON_RESOLVABLE_TAG_VALUES
else (key, value)
for key, value in tags
),
**data,
Expand Down
2 changes: 2 additions & 0 deletions src/sentry/utils/snuba.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,6 +1033,8 @@ def _bulk_snuba_query(
raise RateLimitExceeded(error["message"])
elif error["type"] == "schema":
raise SchemaValidationError(error["message"])
elif error["type"] == "invalid_query":
raise UnqualifiedQueryError(error["message"])
elif error["type"] == "clickhouse":
raise clickhouse_error_codes_map.get(error["code"], QueryExecutionError)(
error["message"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,8 @@ def test_save_with_invalid_orderby_not_from_columns_or_aggregates(self):
assert "queries" in response.data, response.data

def test_save_with_total_count(self):
# We cannot query the Discover entity without a project being defined for the org
self.create_project()
data = {
"title": "Test Query",
"displayType": "table",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ def test_get_num_samples(self):
"""
Tests that the num_samples function returns the correct number of samples
"""
# We cannot query the discover_transactions entity without a project being defined for the org
self.create_project()
num_samples = get_num_samples(self.rule)
assert num_samples == 0
self.create_transaction()
Expand Down
24 changes: 23 additions & 1 deletion tests/sentry/search/events/builder/test_discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
from sentry.search.events.builder import QueryBuilder
from sentry.search.events.types import ParamsType, QueryBuilderConfig
from sentry.snuba.dataset import Dataset
from sentry.snuba.referrer import Referrer
from sentry.testutils.cases import TestCase
from sentry.utils.snuba import QueryOutsideRetentionError
from sentry.utils.snuba import QueryOutsideRetentionError, UnqualifiedQueryError, bulk_snuba_queries
from sentry.utils.validators import INVALID_ID_DETAILS

pytestmark = pytest.mark.sentry_metrics
Expand Down Expand Up @@ -67,6 +68,27 @@ def test_simple_query(self):
)
query.get_snql_query().validate()

def test_project_ids_missing(self):
params: ParamsType = {
"start": self.params["start"],
"end": self.params["end"],
"organization_id": self.organization.id,
}
with pytest.raises(UnqualifiedQueryError):
query = QueryBuilder(Dataset.Discover, params, query="foo", selected_columns=["id"])
bulk_snuba_queries([query.get_snql_query()], referrer=Referrer.TESTING_TEST.value)

def test_project_ids_with_empty_list(self):
params: ParamsType = {
"start": self.params["start"],
"end": self.params["end"],
"project_id": [], # We add an empty project_id list
"organization_id": self.organization.id,
}
with pytest.raises(UnqualifiedQueryError):
query = QueryBuilder(Dataset.Discover, params, query="foo", selected_columns=["id"])
bulk_snuba_queries([query.get_snql_query()], referrer=Referrer.TESTING_TEST.value)

def test_simple_orderby(self):
query = QueryBuilder(
Dataset.Discover,
Expand Down
Loading
Loading