Skip to content
Merged
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
68 changes: 65 additions & 3 deletions src/sentry/api/endpoints/organization_events_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@
from sentry.api.serializers.models.event import get_tags_with_meta
from sentry.eventstore.models import Event
from sentry.models import Organization
from sentry.search.events.builder import QueryBuilder
from sentry.snuba import discover
from sentry.utils.numbers import format_grouped_length
from sentry.utils.snuba import Dataset, SnubaQueryParams, bulk_raw_query
from sentry.utils.snuba import Dataset, SnubaQueryParams, bulk_raw_query, bulk_snql_query
from sentry.utils.validators import INVALID_ID_DETAILS, is_event_id

logger: logging.Logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -204,8 +205,61 @@ def child_sort_key(item: TraceEvent) -> List[int]:


def query_trace_data(
trace_id: str, params: Mapping[str, str]
trace_id: str, params: Mapping[str, str], use_snql: bool = False
) -> Tuple[Sequence[SnubaTransaction], Sequence[SnubaError]]:
if use_snql:
transaction_query = QueryBuilder(
Dataset.Transactions,
params,
query=f"trace:{trace_id}",
selected_columns=[
"id",
"transaction.status",
"transaction.op",
"transaction.duration",
"transaction",
"timestamp",
"project",
"project.id",
"trace.span",
"trace.parent_span",
'to_other(trace.parent_span, "", 0, 1) AS root',
],
# We want to guarantee at least getting the root, and hopefully events near it with timestamp
# id is just for consistent results
orderby=["-root", "timestamp", "id"],
)
error_query = QueryBuilder(
Dataset.Events,
params,
query=f"trace:{trace_id}",
selected_columns=[
"id",
"project",
"project.id",
"timestamp",
"trace.span",
"transaction",
"issue",
"title",
"tags[level]",
],
# Don't add timestamp to this orderby as snuba will have to split the time range up and make multiple queries
orderby=["id"],
auto_fields=False,
)
results = bulk_snql_query(
[transaction_query.get_snql_query(), error_query.get_snql_query()],
referrer="api.trace-view.get-events",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feel free to ignore this but using the same referrer for each of the bulk queries has been a pet peeve of mine since the two queries are different but we have no way of distinguishing between the two in the transaction.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed, but the current bulk APIs don't support a way to provide >1 referrer yet. I have this mentally backlogged but let me create something in Jira

)
transformed_results = [
discover.transform_results(result, query.function_alias_map, {}, None)["data"]
for result, query in zip(results, [transaction_query, error_query])
]
return cast(Sequence[SnubaTransaction], transformed_results[0]), cast(
Sequence[SnubaError], transformed_results[1]
)

transaction_query = discover.prepare_discover_query(
selected_columns=[
"id",
Expand Down Expand Up @@ -280,6 +334,11 @@ def has_feature(self, organization: Organization, request: HttpRequest) -> bool:
features.has("organizations:performance-view", organization, actor=request.user)
)

def has_snql_feature(self, organization: Organization, request: HttpRequest) -> bool:
return bool(
features.has("organizations:performance-use-snql", organization, actor=request.user)
)

@staticmethod
def serialize_error(event: SnubaError) -> TraceError:
return {
Expand Down Expand Up @@ -356,7 +415,9 @@ def get(self, request: HttpRequest, organization: Organization, trace_id: str) -
return Response({"detail": INVALID_ID_DETAILS.format("Event ID")}, status=400)

with self.handle_query_errors():
transactions, errors = query_trace_data(trace_id, params)
transactions, errors = query_trace_data(
trace_id, params, self.has_snql_feature(organization, request)
)
if len(transactions) == 0:
return Response(status=404)
self.record_analytics(transactions, trace_id, self.request.user.id, organization.id)
Expand Down Expand Up @@ -707,6 +768,7 @@ def get(self, request: HttpRequest, organization: Organization, trace_id: str) -
query=f"trace:{trace_id}",
limit=1,
referrer="api.trace-view.get-meta",
use_snql=self.has_snql_feature(organization, request),
)
if len(result["data"]) == 0:
return Response(status=404)
Expand Down
7 changes: 4 additions & 3 deletions src/sentry/features/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,15 @@
default_manager.add("organizations:native-stack-trace-v2", OrganizationFeature, True)
default_manager.add("organizations:onboarding", OrganizationFeature)
default_manager.add("organizations:org-subdomains", OrganizationFeature)
default_manager.add("organizations:performance-chart-interpolation", OrganizationFeature, True)
default_manager.add("organizations:performance-events-page", OrganizationFeature, True)
default_manager.add("organizations:performance-landing-widgets", OrganizationFeature, True)
default_manager.add("organizations:performance-mobile-vitals", OrganizationFeature, True)
default_manager.add("organizations:performance-ops-breakdown", OrganizationFeature)
default_manager.add("organizations:performance-suspect-spans-view", OrganizationFeature, True)
default_manager.add("organizations:performance-tag-explorer", OrganizationFeature, True)
default_manager.add("organizations:performance-tag-page", OrganizationFeature, True)
default_manager.add("organizations:performance-events-page", OrganizationFeature, True)
default_manager.add("organizations:performance-chart-interpolation", OrganizationFeature, True)
default_manager.add("organizations:performance-suspect-spans-view", OrganizationFeature, True)
default_manager.add("organizations:performance-use-snql", OrganizationFeature, True)
Comment on lines +117 to +125
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted these which is why there's additional diffs

default_manager.add("organizations:performance-view", OrganizationFeature)
default_manager.add("organizations:prompt-dashboards", OrganizationFeature)
default_manager.add("organizations:related-events", OrganizationFeature)
Expand Down
12 changes: 12 additions & 0 deletions tests/snuba/api/endpoints/test_organization_events_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -1233,3 +1233,15 @@ def test_with_default(self):
assert data["projects"] == 4
assert data["transactions"] == 8
assert data["errors"] == 1


class OrganizationEventsTraceLightEndpointTestWithSnql(OrganizationEventsTraceLightEndpointTest):
FEATURES = ["organizations:performance-view", "organizations:performance-use-snql"]


class OrganizationEventsTraceEndpointTestWithSnql(OrganizationEventsTraceEndpointTest):
FEATURES = ["organizations:performance-view", "organizations:performance-use-snql"]


class OrganizationEventsTraceMetaEndpointTestWithSnql(OrganizationEventsTraceMetaEndpointTest):
FEATURES = ["organizations:performance-view", "organizations:performance-use-snql"]