Skip to content

Commit 07462a2

Browse files
committed
ref(o11y): Add set_attribute calls in API bases and query utils
1 parent 562b553 commit 07462a2

7 files changed

Lines changed: 44 additions & 1 deletion

File tree

src/sentry/api/bases/organization.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,7 @@ def get_filter_params(
548548
if start and end:
549549
total_seconds = (end - start).total_seconds()
550550
sentry_sdk.set_tag("query.period", total_seconds)
551+
sentry_sdk.set_attribute("query.period", total_seconds)
551552
one_day = 86400
552553
grouped_period = ">30d"
553554
if total_seconds <= one_day:
@@ -559,6 +560,7 @@ def get_filter_params(
559560
elif total_seconds <= one_day * 30:
560561
grouped_period = "<=30d"
561562
sentry_sdk.set_tag("query.period.grouped", grouped_period)
563+
sentry_sdk.set_attribute("query.period.grouped", grouped_period)
562564
except InvalidParams as e:
563565
raise ParseError(detail=f"Invalid date range: {e}")
564566

@@ -578,7 +580,9 @@ def get_filter_params(
578580

579581
len_projects = len(projects)
580582
sentry_sdk.set_tag("query.num_projects", len_projects)
583+
sentry_sdk.set_attribute("query.num_projects", len_projects)
581584
sentry_sdk.set_tag("query.num_projects.grouped", format_grouped_length(len_projects))
585+
sentry_sdk.set_attribute("query.num_projects.grouped", format_grouped_length(len_projects))
582586
set_span_attribute("query.num_projects", len_projects)
583587

584588
params: FilterParams = {

src/sentry/api/bases/organization_events.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,7 @@ def get_dataset(self, request: Request, organization: Organization) -> Any:
163163
if result is None:
164164
raise ParseError(detail=f"dataset must be one of: {', '.join(PUBLIC_DATASET_LABELS)}")
165165
sentry_sdk.set_tag("query.dataset", dataset_label)
166+
sentry_sdk.set_attribute("query.dataset", dataset_label)
166167
return result
167168

168169
def get_snuba_params(
@@ -190,6 +191,7 @@ def get_snuba_params(
190191
raise ParseError(f"sampling mode: {sampling_mode} is not supported")
191192
sampling_mode = cast(SAMPLING_MODES, sampling_mode.upper())
192193
sentry_sdk.set_tag("sampling_mode", sampling_mode)
194+
sentry_sdk.set_attribute("sampling_mode", sampling_mode)
193195

194196
if quantize_date_params:
195197
filter_params = self.quantize_date_params(request, filter_params)
@@ -289,16 +291,20 @@ def save_split_decision(
289291
if has_errors and not has_transactions_data:
290292
decision = DashboardWidgetTypes.ERROR_EVENTS
291293
sentry_sdk.set_tag("discover.split_reason", "query_result")
294+
sentry_sdk.set_attribute("discover.split_reason", "query_result")
292295
elif not has_errors and has_transactions_data:
293296
decision = DashboardWidgetTypes.TRANSACTION_LIKE
294297
sentry_sdk.set_tag("discover.split_reason", "query_result")
298+
sentry_sdk.set_attribute("discover.split_reason", "query_result")
295299
else:
296300
# In the case that neither side has data, or both sides have data, default to errors.
297301
decision = DashboardWidgetTypes.ERROR_EVENTS
298302
source = DashboardDatasetSourcesTypes.FORCED.value
299303
sentry_sdk.set_tag("discover.split_reason", "default")
304+
sentry_sdk.set_attribute("discover.split_reason", "default")
300305

301306
sentry_sdk.set_tag("discover.split_decision", decision)
307+
sentry_sdk.set_attribute("discover.split_decision", decision)
302308
if decision is not None and widget.discover_widget_split != decision:
303309
widget.discover_widget_split = decision
304310
widget.dataset_source = source
@@ -322,20 +328,25 @@ def save_discover_saved_query_split_decision(
322328
if dataset_inferred_from_query is not None:
323329
decision = dataset_inferred_from_query
324330
sentry_sdk.set_tag("discover.split_reason", "inferred_from_query")
331+
sentry_sdk.set_attribute("discover.split_reason", "inferred_from_query")
325332
elif has_errors and not has_transactions_data:
326333
decision = DiscoverSavedQueryTypes.ERROR_EVENTS
327334
sentry_sdk.set_tag("discover.split_reason", "query_result")
335+
sentry_sdk.set_attribute("discover.split_reason", "query_result")
328336
elif not has_errors and has_transactions_data:
329337
decision = DiscoverSavedQueryTypes.TRANSACTION_LIKE
330338
sentry_sdk.set_tag("discover.split_reason", "query_result")
339+
sentry_sdk.set_attribute("discover.split_reason", "query_result")
331340
else:
332341
# In the case that neither or both datasets return data,
333342
# default to Errors.
334343
decision = DiscoverSavedQueryTypes.ERROR_EVENTS
335344
dataset_source = DatasetSourcesTypes.FORCED.value
336345
sentry_sdk.set_tag("discover.split_reason", "default")
346+
sentry_sdk.set_attribute("discover.split_reason", "default")
337347

338348
sentry_sdk.set_tag("discover.split_decision", decision)
349+
sentry_sdk.set_attribute("discover.split_decision", decision)
339350
if query.dataset != decision:
340351
query.dataset = decision
341352
query.dataset_source = dataset_source
@@ -556,6 +567,7 @@ def get_rollup(
556567
if use_rpc:
557568
raise
558569
sentry_sdk.set_tag("user.invalid_interval", request.GET.get("interval"))
570+
sentry_sdk.set_attribute("user.invalid_interval", request.GET.get("interval") or "")
559571
date_range = snuba_params.date_range
560572
stats_period = parse_stats_period(get_interval_from_range(date_range, False))
561573
rollup = int(stats_period.total_seconds()) if stats_period is not None else 3600

src/sentry/api/bases/project.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,8 @@ def convert_args(
204204

205205
self.check_object_permissions(request, project)
206206

207-
sentry_sdk.get_isolation_scope().set_tag("project", project.id)
207+
sentry_sdk.set_tag("project", project.id)
208+
sentry_sdk.set_attribute("project", project.id)
208209

209210
bind_organization_context(project.organization)
210211

src/sentry/api/endpoints/organization_ai_conversations.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ def _get_conversations(
245245
conversation_ids = _extract_conversation_ids(conversation_ids_results)
246246

247247
sentry_sdk.set_tag("ai_conversations.count", len(conversation_ids))
248+
sentry_sdk.set_attribute("ai_conversations.count", len(conversation_ids))
248249

249250
if not conversation_ids:
250251
return []

src/sentry/api/helpers/group_index/index.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,13 +126,19 @@ def build_query_params_from_request(
126126
query = saved_search.query
127127

128128
sentry_sdk.set_tag("search.query", query)
129+
sentry_sdk.set_attribute("search.query", query)
129130
sentry_sdk.set_tag("search.sort", query_kwargs["sort_by"])
131+
sentry_sdk.set_attribute("search.sort", query_kwargs["sort_by"])
130132
if projects:
131133
sentry_sdk.set_tag("search.projects", len(projects) if len(projects) <= 5 else ">5")
134+
sentry_sdk.set_attribute("search.projects", len(projects) if len(projects) <= 5 else ">5")
132135
if environments:
133136
sentry_sdk.set_tag(
134137
"search.environments", len(environments) if len(environments) <= 5 else ">5"
135138
)
139+
sentry_sdk.set_attribute(
140+
"search.environments", len(environments) if len(environments) <= 5 else ">5"
141+
)
136142
if query:
137143
query_kwargs["search_filters"] = parse_and_convert_issue_search_query(
138144
query, organization, projects, environments, request.user

src/sentry/api/utils.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,41 +372,50 @@ def handle_query_errors() -> Generator[None]:
372372
except InvalidColumnError as error:
373373
message = str(error)
374374
sentry_sdk.set_tag("query.error_reason", message)
375+
sentry_sdk.set_attribute("query.error_reason", message)
375376
raise ParseError(detail=message)
376377
except InvalidSearchQuery as error:
377378
message = original_error = str(error)
378379
# Special case the project message since it has so many variants so tagging is messy otherwise
379380
if message.endswith("do not exist or are not actively selected."):
380381
message = "Project in query does not exist or not selected"
381382
sentry_sdk.set_tag("query.error_reason", message)
383+
sentry_sdk.set_attribute("query.error_reason", message)
382384
logger.info("A query error was handled", extra={"query.error_reason": message})
383385
raise ParseError(detail=original_error)
384386
except ArithmeticError as error:
385387
message = str(error)
386388
sentry_sdk.set_tag("query.error_reason", message)
389+
sentry_sdk.set_attribute("query.error_reason", message)
387390
raise ParseError(detail=message)
388391
except QueryOutsideRetentionError as error:
389392
sentry_sdk.set_tag("query.error_reason", "QueryOutsideRetentionError")
393+
sentry_sdk.set_attribute("query.error_reason", "QueryOutsideRetentionError")
390394
raise ParseError(detail=str(error))
391395
except QueryIllegalTypeOfArgument:
392396
message = "Invalid query. Argument to function is wrong type."
393397
sentry_sdk.set_tag("query.error_reason", message)
398+
sentry_sdk.set_attribute("query.error_reason", message)
394399
raise ParseError(detail=message)
395400
except IncompatibleMetricsQuery as error:
396401
message = str(error)
397402
sentry_sdk.set_tag("query.error_reason", f"Metric Error: {message}")
403+
sentry_sdk.set_attribute("query.error_reason", f"Metric Error: {message}")
398404
raise ParseError(detail=message)
399405
except SnubaRPCRateLimitExceeded:
400406
sentry_sdk.set_tag("query.error_reason", "RateLimitExceeded")
407+
sentry_sdk.set_attribute("query.error_reason", "RateLimitExceeded")
401408
raise Throttled(detail=RATE_LIMIT_ERROR_MESSAGE)
402409
except SnubaRPCTooManySimultaneous:
403410
sentry_sdk.set_tag("query.error_reason", "TooManySimultaneousQueries")
411+
sentry_sdk.set_attribute("query.error_reason", "TooManySimultaneousQueries")
404412
raise Throttled(detail=RATE_LIMIT_ERROR_MESSAGE)
405413
except SnubaRPCError as error:
406414
message = "Internal error. Please try again."
407415
arg = error.args[0] if len(error.args) > 0 else None
408416
if isinstance(arg, TimeoutError):
409417
sentry_sdk.set_tag("query.error_reason", "Timeout")
418+
sentry_sdk.set_attribute("query.error_reason", "Timeout")
410419
raise TimeoutException(detail=TIMEOUT_RPC_ERROR_MESSAGE)
411420
sentry_sdk.capture_exception(error)
412421
if hasattr(error, "debug"):
@@ -422,9 +431,11 @@ def handle_query_errors() -> Generator[None]:
422431
arg = error.args[0] if len(error.args) > 0 else None
423432
if isinstance(error, RateLimitExceeded):
424433
sentry_sdk.set_tag("query.error_reason", "RateLimitExceeded")
434+
sentry_sdk.set_attribute("query.error_reason", "RateLimitExceeded")
425435
raise
426436
if isinstance(error, QueryTooManySimultaneous):
427437
sentry_sdk.set_tag("query.error_reason", "TooManySimultaneousQueries")
438+
sentry_sdk.set_attribute("query.error_reason", "TooManySimultaneousQueries")
428439
raise Throttled(detail=RATE_LIMIT_ERROR_MESSAGE)
429440
if isinstance(
430441
error,
@@ -437,9 +448,11 @@ def handle_query_errors() -> Generator[None]:
437448
ReadTimeoutError,
438449
):
439450
sentry_sdk.set_tag("query.error_reason", "Timeout")
451+
sentry_sdk.set_attribute("query.error_reason", "Timeout")
440452
raise TimeoutException(detail=TIMEOUT_ERROR_MESSAGE)
441453
elif isinstance(error, (UnqualifiedQueryError)):
442454
sentry_sdk.set_tag("query.error_reason", str(error))
455+
sentry_sdk.set_attribute("query.error_reason", str(error))
443456
raise ParseError(detail=str(error))
444457
elif isinstance(
445458
error,
@@ -468,6 +481,7 @@ def handle_query_errors() -> Generator[None]:
468481
is_timeout = "canceling statement due to statement timeout" in error_message
469482
if is_timeout:
470483
sentry_sdk.set_tag("query.error_reason", "Postgres statement timeout")
484+
sentry_sdk.set_attribute("query.error_reason", "Postgres statement timeout")
471485
sentry_sdk.capture_exception(error, level="warning")
472486
raise Throttled(
473487
detail="Query timeout. Please try with a smaller date range or fewer conditions."
@@ -486,6 +500,7 @@ def update_snuba_params_with_timestamp(
486500
faster than the default 7d or 14d queries"""
487501
# during the transition this is optional but it will become required for the trace view
488502
sentry_sdk.set_tag("trace_view.used_timestamp", timestamp_key in request.GET)
503+
sentry_sdk.set_attribute("trace_view.used_timestamp", timestamp_key in request.GET)
489504
has_dates = params.start is not None and params.end is not None
490505
if timestamp_key in request.GET and has_dates:
491506
example_timestamp = parse_datetime_string(request.GET[timestamp_key])

src/sentry/utils/pagination_factory.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,8 @@ def get_paginator(
6969
def annotate_span_with_pagination_args(span: Span, per_page: int) -> None:
7070
span.set_data("Limit", per_page)
7171
sentry_sdk.set_tag("query.per_page", per_page)
72+
sentry_sdk.set_attribute("query.per_page", per_page)
7273
sentry_sdk.set_tag("query.per_page.grouped", format_grouped_length(per_page, [1, 10, 50, 100]))
74+
sentry_sdk.set_attribute(
75+
"query.per_page.grouped", format_grouped_length(per_page, [1, 10, 50, 100])
76+
)

0 commit comments

Comments
 (0)