Skip to content

Commit b5d447d

Browse files
authored
ref(trace-explorer): Lift load all projects (#72318)
Preparing for the stats endpoint. Lifting this logic so it can be shared.
1 parent 87e90b5 commit b5d447d

File tree

1 file changed

+34
-13
lines changed

1 file changed

+34
-13
lines changed

src/sentry/api/endpoints/organization_traces.py

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import dataclasses
21
import math
32
from collections import defaultdict
43
from collections.abc import Generator, Mapping, MutableMapping
@@ -23,10 +22,12 @@
2322
from sentry.api.paginator import GenericOffsetPaginator
2423
from sentry.api.utils import handle_query_errors
2524
from sentry.models.organization import Organization
25+
from sentry.models.project import Project
2626
from sentry.search.events.builder import QueryBuilder, SpansIndexedQueryBuilder
2727
from sentry.search.events.constants import TIMEOUT_SPAN_ERROR_MESSAGE
2828
from sentry.search.events.types import ParamsType, QueryBuilderConfig, SnubaParams, WhereType
2929
from sentry.sentry_metrics.querying.samples_list import SpanKey, get_sample_list_executor_cls
30+
from sentry.services.hybrid_cloud.organization import RpcOrganization
3031
from sentry.snuba.dataset import Dataset
3132
from sentry.snuba.referrer import Referrer
3233
from sentry.utils.iterators import chunked
@@ -98,8 +99,39 @@ def handle_span_query_errors() -> Generator[None, None, None]:
9899
raise ParseError(detail=TIMEOUT_SPAN_ERROR_MESSAGE)
99100

100101

102+
class OrganizationTracesEndpointBase(OrganizationEventsV2EndpointBase):
103+
def get_snuba_dataclass(
104+
self, request: Request, organization: Organization, check_global_views: bool = True
105+
) -> tuple[SnubaParams, dict[str, Any]]:
106+
"""The trace endpoint always wants to get all projects regardless of what's passed into the API.
107+
This is because a trace can span any number of projects in an organization. So disable the
108+
check_global_views condition."""
109+
return super().get_snuba_dataclass(request, organization, check_global_views=False)
110+
111+
def get_projects( # type: ignore[override]
112+
self,
113+
request: Request,
114+
organization: Organization | RpcOrganization,
115+
project_ids: set[int] | None = None,
116+
project_slugs: set[str] | None = None,
117+
include_all_accessible: bool = True,
118+
) -> list[Project]:
119+
"""The trace endpoint always wants to get all projects regardless of what's passed into the API.
120+
121+
This is because a trace can span any number of projects in an organization. But we still want to
122+
use the get_projects function to check for any permissions. So we'll just pass project_ids=-1 everytime
123+
which is what would be sent if we wanted all projects"""
124+
return super().get_projects(
125+
request,
126+
organization,
127+
project_ids={-1},
128+
project_slugs=None,
129+
include_all_accessible=True,
130+
)
131+
132+
101133
@region_silo_endpoint
102-
class OrganizationTracesEndpoint(OrganizationEventsV2EndpointBase):
134+
class OrganizationTracesEndpoint(OrganizationTracesEndpointBase):
103135
publish_status = {
104136
"GET": ApiPublishStatus.EXPERIMENTAL,
105137
}
@@ -113,17 +145,6 @@ def get(self, request: Request, organization: Organization) -> Response:
113145

114146
try:
115147
snuba_params, params = self.get_snuba_dataclass(request, organization)
116-
all_projects = self.get_projects(
117-
request,
118-
organization,
119-
project_ids={-1},
120-
project_slugs=None,
121-
include_all_accessible=True,
122-
)
123-
snuba_params = dataclasses.replace(snuba_params, projects=all_projects)
124-
params["projects"] = snuba_params.projects
125-
params["projects_objects"] = snuba_params.projects
126-
params["projects_id"] = snuba_params.project_ids
127148
except NoProjects:
128149
return Response(status=404)
129150

0 commit comments

Comments
 (0)