Skip to content

Commit 02ff96b

Browse files
authored
feat(trace): Add an additional query that gets timestamps and projects (#68738)
- This adds a new query to the trace view where we'll first query for the min and max timestamps and the list of projects - This should mean that all the subsequent queries will be significantly faster since we're only querying the timestamps and projects we care about - You can read more about the data used to determine this in the private notion doc [here](https://www.notion.so/sentry/Trace-Queries-64a39d3854e24a738dfb1dc503a57f6b) - Should be very low risk, everything new is behind an option: - Turning it on here: getsentry/sentry-options-automator#1097 - Turning it off here: getsentry/sentry-options-automator#1098
1 parent 547c13a commit 02ff96b

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed

src/sentry/api/endpoints/organization_events_trace.py

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,61 @@ def count_performance_issues(trace_id: str, params: Mapping[str, str]) -> int:
426426
return cast(int, count["data"][0].get("total_groups", 0))
427427

428428

429+
@sentry_sdk.tracing.trace
430+
def update_params_with_trace_timestamp_projects(
431+
trace_id: str,
432+
params: Mapping[str, str],
433+
) -> None:
434+
query_metadata = options.get("performance.traces.query_timestamp_projects")
435+
sentry_sdk.set_tag("trace_view.queried_timestamp_projects", query_metadata)
436+
if not query_metadata:
437+
return
438+
439+
metadata_query = QueryBuilder(
440+
Dataset.Discover,
441+
params,
442+
query=f"trace:{trace_id}",
443+
selected_columns=[
444+
"min(timestamp)",
445+
"max(timestamp)",
446+
"project.id",
447+
],
448+
)
449+
results = metadata_query.run_query(Referrer.API_TRACE_VIEW_GET_TIMESTAMP_PROJECTS.value)
450+
results = metadata_query.process_results(results)
451+
project_id_set = set()
452+
min_timestamp = None
453+
max_timestamp = None
454+
for row in results["data"]:
455+
current_min = datetime.fromisoformat(row["min_timestamp"])
456+
current_max = datetime.fromisoformat(row["max_timestamp"])
457+
if min_timestamp is None:
458+
min_timestamp = current_min
459+
if max_timestamp is None:
460+
max_timestamp = current_max
461+
462+
if current_min < min_timestamp:
463+
min_timestamp = current_min
464+
if current_max > max_timestamp:
465+
max_timestamp = current_max
466+
467+
project_id_set.add(row["project.id"])
468+
469+
# Do not modify the params if anything comes back empty
470+
if len(project_id_set) == 0 or min_timestamp is None or max_timestamp is None:
471+
return
472+
473+
project_ids = list(project_id_set)
474+
# Reusing this option for now
475+
time_buffer = options.get("performance.traces.span_query_timebuffer_hours")
476+
if min_timestamp:
477+
params["start"] = min_timestamp - timedelta(hours=time_buffer)
478+
if max_timestamp:
479+
params["end"] = max_timestamp + timedelta(hours=time_buffer)
480+
params["project_objects"] = [p for p in params["project_objects"] if p.id in project_ids]
481+
params["project_id"] = project_ids
482+
483+
429484
def query_trace_data(
430485
trace_id: str,
431486
params: Mapping[str, str],
@@ -852,6 +907,8 @@ def get(self, request: HttpRequest, organization: Organization, trace_id: str) -
852907
actor=request.user,
853908
)
854909
with handle_query_errors():
910+
update_params_with_trace_timestamp_projects(trace_id, params)
911+
855912
if use_spans:
856913
transactions, errors = query_trace_data(
857914
trace_id, params, limit, event_id, use_spans
@@ -1435,6 +1492,7 @@ def get(self, request: HttpRequest, organization: Organization, trace_id: str) -
14351492
update_snuba_params_with_timestamp(request, params)
14361493

14371494
with handle_query_errors():
1495+
update_params_with_trace_timestamp_projects(trace_id, params)
14381496
result = discover.query(
14391497
selected_columns=[
14401498
"count_unique(project_id) as projects",

src/sentry/options/defaults.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1728,6 +1728,12 @@
17281728
default=1.0,
17291729
flags=FLAG_AUTOMATOR_MODIFIABLE,
17301730
) # hours
1731+
register(
1732+
"performance.traces.query_timestamp_projects",
1733+
type=Bool,
1734+
default=False,
1735+
flags=FLAG_AUTOMATOR_MODIFIABLE,
1736+
)
17311737

17321738
# Dynamic Sampling system-wide options
17331739
# Size of the sliding window used for dynamic sampling. It is defaulted to 24 hours.

src/sentry/snuba/referrer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,6 +443,7 @@ class Referrer(Enum):
443443
API_SERIALIZER_CHECKINS_TRACE_IDS = "api.serializer.checkins.trace-ids"
444444
API_STARFISH_PROFILE_FLAMEGRAPH = "api.starfish.profile-flamegraph"
445445
API_TRACE_VIEW_ERRORS_VIEW = "api.trace-view.errors-view"
446+
API_TRACE_VIEW_GET_TIMESTAMP_PROJECTS = "api.trace-view.get-timestamp-projects"
446447
API_TRACE_VIEW_GET_EVENTS = "api.trace-view.get-events"
447448
API_TRACE_VIEW_GET_META = "api.trace-view.get-meta"
448449
API_TRACE_VIEW_HOVER_CARD = "api.trace-view.hover-card"

0 commit comments

Comments
 (0)