Skip to content
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

feat(discover): Prevent calling Snuba with an empty list of projects #69577

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 10 additions & 6 deletions src/sentry/search/events/builder/discover.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,17 @@ def resolve_params(self) -> list[WhereType]:
if self.end:
conditions.append(Condition(self.column("timestamp"), Op.LT, self.end))

conditions.append(
Condition(
self.column("project_id"),
Op.IN,
self.params.project_ids,
# The clause will prevent calling Snuba with an empty list of projects, thus, returning
# no data. It will not instead complain with:
# sentry.utils.snuba.UnqualifiedQueryError: validation failed for entity events: missing required conditions for project_id
armenzg marked this conversation as resolved.
Show resolved Hide resolved
if self.params.project_ids:
conditions.append(
Condition(
self.column("project_id"),
Op.IN,
self.params.project_ids,
)
)
)

if len(self.params.environments) > 0:
term = event_search.SearchFilter(
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 @@ -1022,6 +1022,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_query_without_project_ids(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_query_with_empty_project_ids(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