Skip to content

Commit

Permalink
feedback + merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
sentaur-athena committed Aug 6, 2024
1 parent a3ab8a9 commit c16ddc4
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 36 deletions.
19 changes: 16 additions & 3 deletions src/sentry/api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
from sentry.apidocs.hooks import HTTP_METHOD_NAME
from sentry.auth import access
from sentry.auth.staff import has_staff_option
from sentry.middleware import is_frontend_request
from sentry.models.environment import Environment
from sentry.organizations.absolute_url import generate_organization_url
from sentry.ratelimits.config import DEFAULT_RATE_LIMIT_CONFIG, RateLimitConfig
from sentry.silo.base import SiloLimit, SiloMode
from sentry.snuba.query_sources import QuerySource
from sentry.types.ratelimit import RateLimit, RateLimitCategory
from sentry.utils.audit import create_audit_entry
from sentry.utils.cursors import Cursor
Expand Down Expand Up @@ -221,9 +223,11 @@ class Endpoint(APIView):

owner: ApiOwner = ApiOwner.UNOWNED
publish_status: dict[HTTP_METHOD_NAME, ApiPublishStatus] = {}
rate_limits: RateLimitConfig | dict[str, dict[RateLimitCategory, RateLimit]] | Callable[
..., RateLimitConfig | dict[str, dict[RateLimitCategory, RateLimit]]
] = DEFAULT_RATE_LIMIT_CONFIG
rate_limits: (
RateLimitConfig
| dict[str, dict[RateLimitCategory, RateLimit]]
| Callable[..., RateLimitConfig | dict[str, dict[RateLimitCategory, RateLimit]]]
) = DEFAULT_RATE_LIMIT_CONFIG
enforce_rate_limit: bool = settings.SENTRY_RATELIMITER_ENABLED
snuba_methods: list[HTTP_METHOD_NAME] = []

Expand Down Expand Up @@ -582,6 +586,15 @@ def paginate(
self.add_cursor_headers(request, response, cursor_result)
return response

def get_request_source(request: Request) -> QuerySource:
"""
This is an estimate of query source. Treat it more like a good guess and
don't write logic that depends on it. Used for monitoring only atm.
"""
if is_frontend_request(request):
return QuerySource.FRONTEND
return QuerySource.API


class EnvironmentMixin:
def _get_environment_func(self, request: Request, organization_id):
Expand Down
32 changes: 15 additions & 17 deletions src/sentry/api/endpoints/organization_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
from sentry.apidocs.utils import inline_sentry_response_serializer
from sentry.discover.models import DiscoverSavedQuery, DiscoverSavedQueryTypes
from sentry.exceptions import InvalidParams
from sentry.middleware import is_frontend_request
from sentry.models.dashboard_widget import DashboardWidget, DashboardWidgetTypes
from sentry.models.organization import Organization
from sentry.snuba import (
Expand All @@ -32,7 +31,6 @@
transactions,
)
from sentry.snuba.metrics.extraction import MetricSpecType
from sentry.snuba.query_sources import QuerySource
from sentry.snuba.referrer import Referrer
from sentry.snuba.utils import dataset_split_decision_inferred_from_query, get_dataset
from sentry.types.ratelimit import RateLimit, RateLimitCategory
Expand Down Expand Up @@ -377,7 +375,7 @@ def get(self, request: Request, organization) -> Response:
referrer = Referrer.API_ORGANIZATION_EVENTS.value

def _data_fn(scoped_dataset, offset, limit, query) -> dict[str, Any]:
query_source = QuerySource.FRONTEND if is_frontend_request(request) else QuerySource.API
query_source = self.get_request_source(request)
return scoped_dataset.query(
selected_columns=self.get_field_list(organization, request),
query=query,
Expand Down Expand Up @@ -457,14 +455,14 @@ def _dashboards_data_fn(scoped_dataset, offset, limit, scoped_query, dashboard_w
if decision == DashboardWidgetTypes.DISCOVER:
return _data_fn(discover, offset, limit, scoped_query)
elif decision == DashboardWidgetTypes.TRANSACTION_LIKE:
original_results["meta"]["discoverSplitDecision"] = (
DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.TRANSACTION_LIKE)
)
original_results["meta"][
"discoverSplitDecision"
] = DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.TRANSACTION_LIKE)
return original_results
elif decision == DashboardWidgetTypes.ERROR_EVENTS and error_results:
error_results["meta"]["discoverSplitDecision"] = (
DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.ERROR_EVENTS)
)
error_results["meta"][
"discoverSplitDecision"
] = DashboardWidgetTypes.get_type_name(DashboardWidgetTypes.ERROR_EVENTS)
return error_results
else:
return original_results
Expand Down Expand Up @@ -537,21 +535,21 @@ def _discover_data_fn(scoped_dataset, offset, limit, scoped_query, discover_save

try:
error_results = map["errors"]
error_results["meta"]["discoverSplitDecision"] = (
DiscoverSavedQueryTypes.get_type_name(
DiscoverSavedQueryTypes.ERROR_EVENTS
)
error_results["meta"][
"discoverSplitDecision"
] = DiscoverSavedQueryTypes.get_type_name(
DiscoverSavedQueryTypes.ERROR_EVENTS
)
has_errors = len(error_results["data"]) > 0
except KeyError:
error_results = None

try:
transaction_results = map["transactions"]
transaction_results["meta"]["discoverSplitDecision"] = (
DiscoverSavedQueryTypes.get_type_name(
DiscoverSavedQueryTypes.TRANSACTION_LIKE
)
transaction_results["meta"][
"discoverSplitDecision"
] = DiscoverSavedQueryTypes.get_type_name(
DiscoverSavedQueryTypes.TRANSACTION_LIKE
)
has_transactions = len(transaction_results["data"]) > 0
except KeyError:
Expand Down
3 changes: 1 addition & 2 deletions src/sentry/api/endpoints/organization_events_stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from sentry.api.base import region_silo_endpoint
from sentry.api.bases import OrganizationEventsV2EndpointBase
from sentry.constants import MAX_TOP_EVENTS
from sentry.middleware import is_frontend_request
from sentry.models.dashboard_widget import DashboardWidget, DashboardWidgetTypes
from sentry.models.organization import Organization
from sentry.snuba import (
Expand Down Expand Up @@ -181,7 +180,7 @@ def check_if_results_have_data(self, results: SnubaTSResult | dict[str, SnubaTSR
return has_data

def get(self, request: Request, organization: Organization) -> Response:
query_source = QuerySource.FRONTEND if is_frontend_request(request) else QuerySource.API
query_source = self.get_request_source(request)
with sentry_sdk.start_span(op="discover.endpoint", description="filter_params") as span:
span.set_data("organization", organization)

Expand Down
55 changes: 44 additions & 11 deletions src/sentry/api/endpoints/organization_events_trace.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,11 @@ def child_sort_key(item: TraceEvent) -> list[int | str]:
]


def count_performance_issues(trace_id: str, params: SnubaParams, query_source: QuerySource | None = QuerySource.SENTRY_BACKEND) -> int:
def count_performance_issues(
trace_id: str,
params: SnubaParams,
query_source: QuerySource | None = QuerySource.SENTRY_BACKEND,
) -> int:
transaction_query = DiscoverQueryBuilder(
Dataset.IssuePlatform,
params={},
Expand Down Expand Up @@ -1068,20 +1072,38 @@ def get(self, request: Request, organization: Organization, trace_id: str) -> Ht
if event_id and not is_event_id(event_id):
return Response({"detail": INVALID_ID_DETAILS.format("Event ID")}, status=400)

query_source = QuerySource.FRONTEND if is_frontend_request(request) else QuerySource.API
query_source = self.get_request_source(request)
with handle_query_errors():
transaction_params = create_transaction_params(trace_id, snuba_params, query_source=query_source)
transaction_params = create_transaction_params(
trace_id, snuba_params, query_source=query_source
)

if use_spans:
transactions, errors = query_trace_data(
trace_id, snuba_params, transaction_params, limit, event_id, use_spans, query_source=query_source
trace_id,
snuba_params,
transaction_params,
limit,
event_id,
use_spans,
query_source=query_source,
)
transactions = augment_transactions_with_spans(
transactions, errors, trace_id, snuba_params, query_source=query_source,
transactions,
errors,
trace_id,
snuba_params,
query_source=query_source,
)
else:
transactions, errors = query_trace_data(
trace_id, snuba_params, transaction_params, limit, None, False, query_source=query_source
trace_id,
snuba_params,
transaction_params,
limit,
None,
False,
query_source=query_source,
)
self.record_analytics(transactions, trace_id, self.request.user.id, organization.id)

Expand Down Expand Up @@ -1271,7 +1293,12 @@ def serialize(
break

current_event = TraceEvent(
snuba_event, root_id, current_generation, True, snuba_params=snuba_params, query_source=query_source,
snuba_event,
root_id,
current_generation,
True,
snuba_params=snuba_params,
query_source=query_source,
)
trace_results.append(current_event)

Expand Down Expand Up @@ -1402,7 +1429,9 @@ def serialize(
if roots:
results_map[None] = []
for root in roots:
root_event = TraceEvent(root, None, 0, snuba_params=snuba_params, query_source=query_source)
root_event = TraceEvent(
root, None, 0, snuba_params=snuba_params, query_source=query_source
)
parent_events[root["id"]] = root_event
results_map[None].append(root_event)
to_check.append(root)
Expand All @@ -1423,7 +1452,11 @@ def serialize(
parent_map[parent_span_id] = siblings

previous_event = parent_events[current_event["id"]] = TraceEvent(
current_event, None, 0, snuba_params=snuba_params, query_source=query_source,
current_event,
None,
0,
snuba_params=snuba_params,
query_source=query_source,
)

# Used to avoid removing the orphan from results entirely if we loop
Expand Down Expand Up @@ -1571,7 +1604,7 @@ def serialize_with_spans(
event_id: str | None,
detailed: bool = False,
) -> SerializedTrace:
query_source: QuerySource | None = None,
query_source: QuerySource | None = (None,)
root_traces: list[TraceEvent] = []
orphans: list[TraceEvent] = []
orphan_event_ids: set[str] = set()
Expand Down Expand Up @@ -1681,7 +1714,7 @@ def get(self, request: Request, organization: Organization, trace_id: str) -> Ht
return Response(status=404)

update_snuba_params_with_timestamp(request, snuba_params)
query_source = QuerySource.FRONTEND if is_frontend_request(request) else QuerySource.API
query_source = self.get_request_source(request)
with handle_query_errors():
result = discover.query(
selected_columns=[
Expand Down
4 changes: 1 addition & 3 deletions src/sentry/api/endpoints/organization_events_trends_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@
from sentry.api.bases import NoProjects, OrganizationEventsV2EndpointBase
from sentry.api.paginator import GenericOffsetPaginator
from sentry.api.utils import handle_query_errors
from sentry.middleware import is_frontend_request
from sentry.search.events.constants import METRICS_GRANULARITIES
from sentry.seer.breakpoints import detect_breakpoints
from sentry.snuba import metrics_performance
from sentry.snuba.discover import create_result_key, zerofill
from sentry.snuba.metrics_performance import query as metrics_query
from sentry.snuba.query_sources import QuerySource
from sentry.snuba.referrer import Referrer
from sentry.types.ratelimit import RateLimit, RateLimitCategory
from sentry.utils.iterators import chunked
Expand Down Expand Up @@ -92,7 +90,7 @@ def get(self, request: Request, organization) -> Response:
selected_columns = ["project_id", "transaction"]

query = request.GET.get("query")
query_source = QuerySource.FRONTEND if is_frontend_request(request) else QuerySource.API
query_source = self.get_request_source(request)

def get_top_events(user_query, params, event_limit, referrer):
top_event_columns = selected_columns[:]
Expand Down

0 comments on commit c16ddc4

Please sign in to comment.