Skip to content

Commit 0183b07

Browse files
feat(CapMan): More tsdb and legacy queries with tenant_ids (#45405)
### Overview - Added `organization_id` to more Snuba Requests, specifically targeting `tsdb-modelid:300` and `tsdb-modelid:100` - Legacy queries now pass `tenant_ids` to Snuba as well - Removed hardcoded referrer in tenant_ids, moved to populate referrer in query functions - More context: #44788
1 parent 04f3442 commit 0183b07

File tree

20 files changed

+74
-45
lines changed

20 files changed

+74
-45
lines changed

src/sentry/api/endpoints/group_hashes_split.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -159,14 +159,14 @@ def _get_full_hierarchical_hashes(group: Group, hash: str) -> Optional[Sequence[
159159
]
160160
)
161161
)
162-
referrer = "group_split.get_full_hierarchical_hashes"
162+
163163
request = SnubaRequest(
164164
dataset="events",
165165
app_id="grouping",
166166
query=query,
167-
tenant_ids={"referrer": referrer, "organization_id": group.project.organization_id},
167+
tenant_ids={"organization_id": group.project.organization_id},
168168
)
169-
data = snuba.raw_snql_query(request, referrer)["data"]
169+
data = snuba.raw_snql_query(request, "group_split.get_full_hierarchical_hashes")["data"]
170170
if not data:
171171
return None
172172

@@ -390,17 +390,13 @@ def _render_trees(group: Group, user):
390390
)
391391

392392
rv = []
393-
referrer = "api.group_split.render_grouping_tree"
394393
request = SnubaRequest(
395394
dataset="events",
396395
app_id="grouping",
397396
query=query,
398-
tenant_ids={
399-
"referrer": referrer,
400-
"organization_id": group.project.organization_id,
401-
},
397+
tenant_ids={"organization_id": group.project.organization_id},
402398
)
403-
for row in snuba.raw_snql_query(request, referrer)["data"]:
399+
for row in snuba.raw_snql_query(request, "api.group_split.render_grouping_tree")["data"]:
404400
if len(row["hash_slice"]) == 0:
405401
hash = row["primary_hash"]
406402
parent_hash = child_hash = None

src/sentry/api/endpoints/grouping_level_new_issues.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,14 +195,13 @@ def _query_snuba(group: Group, id: int, offset=None, limit=None):
195195
if limit is not None:
196196
query = query.set_limit(limit)
197197

198-
referrer = "api.group_hashes_levels.get_level_new_issues"
199198
request = SnubaRequest(
200199
dataset="events",
201200
app_id="grouping",
202201
query=query,
203-
tenant_ids={"referrer": referrer, "organization_id": group.project.organization_id},
202+
tenant_ids={"organization_id": group.project.organization_id},
204203
)
205-
return snuba.raw_snql_query(request, referrer)["data"]
204+
return snuba.raw_snql_query(request, "api.group_hashes_levels.get_level_new_issues")["data"]
206205

207206

208207
def _process_snuba_results(query_res, group: Group, id: int, user):

src/sentry/api/endpoints/grouping_levels.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,14 +127,13 @@ def get_levels_overview(group: Group):
127127
.set_where(_get_group_filters(group))
128128
.set_groupby([Column("primary_hash")])
129129
)
130-
referrer = "api.group_hashes_levels.get_levels_overview"
131130
request = SnubaRequest(
132131
dataset="events",
133132
app_id="grouping",
134133
query=query,
135-
tenant_ids={"referrer": referrer, "organization_id": group.project.organization_id},
134+
tenant_ids={"organization_id": group.project.organization_id},
136135
)
137-
res = snuba.raw_snql_query(request, referrer)
136+
res = snuba.raw_snql_query(request, "api.group_hashes_levels.get_levels_overview")
138137

139138
if not res["data"]:
140139
raise NoEvents()

src/sentry/api/endpoints/organization_events_span_ops.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,9 @@ def data_fn(offset: int, limit: int) -> Any:
3838
offset=offset,
3939
orderby="-count",
4040
)
41-
referrer = "api.organization-events-span-ops"
4241
snql_query = builder.get_snql_query()
43-
snql_query.tenant_ids = {"referrer": referrer, "organization_id": organization.id}
44-
results = raw_snql_query(snql_query, referrer)
42+
snql_query.tenant_ids = {"organization_id": organization.id}
43+
results = raw_snql_query(snql_query, "api.organization-events-span-ops")
4544
return [SpanOp(op=row["spans_op"], count=row["count"]) for row in results["data"]]
4645

4746
with self.handle_query_errors():

src/sentry/api/endpoints/project_dynamic_sampling.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -110,16 +110,15 @@ def __project_stats_query(self, query, projects_in_org, org_id, query_time_range
110110
alias="root_count",
111111
)
112112
]
113-
referrer = Referrer.DYNAMIC_SAMPLING_DISTRIBUTION_FETCH_PROJECT_STATS.value
114113
snuba_query = snuba_query.set_select(snuba_query.select + extra_select)
115114
data = raw_snql_query(
116115
SnubaRequest(
117116
dataset=Dataset.Discover.value,
118117
app_id="default",
119118
query=snuba_query,
120-
tenant_ids={"referrer": referrer, "organization_id": org_id},
119+
tenant_ids={"organization_id": org_id},
121120
),
122-
referrer,
121+
Referrer.DYNAMIC_SAMPLING_DISTRIBUTION_FETCH_PROJECT_STATS.value,
123122
)
124123
return builder.process_results(data)["data"]
125124

@@ -266,8 +265,6 @@ def __fetch_randomly_sampled_transactions(self, project, query, sample_size, que
266265
]
267266
)
268267

269-
referrer = Referrer.DYNAMIC_SAMPLING_DISTRIBUTION_FETCH_TRANSACTIONS.value
270-
271268
snuba_query = snuba_query.set_groupby(
272269
snuba_query.groupby + [Column("modulo_num"), Column("contexts.key")]
273270
)
@@ -277,9 +274,9 @@ def __fetch_randomly_sampled_transactions(self, project, query, sample_size, que
277274
dataset=Dataset.Discover.value,
278275
app_id="default",
279276
query=snuba_query,
280-
tenant_ids={"referrer": referrer, "organization_id": project.organization_id},
277+
tenant_ids={"organization_id": project.organization_id},
281278
),
282-
referrer,
279+
Referrer.DYNAMIC_SAMPLING_DISTRIBUTION_FETCH_TRANSACTIONS.value,
283280
)["data"]
284281
return data
285282

src/sentry/api/serializers/models/group.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,10 @@ def _execute_error_seen_stats_query(
10071007
filters = {"project_id": project_ids, "group_id": group_ids}
10081008
if environment_ids:
10091009
filters["environment"] = environment_ids
1010+
1011+
org_id = item_list[0].project.organization_id if item_list else None
1012+
tenant_ids = {"organization_id": org_id} if org_id else dict()
1013+
10101014
return aliased_query(
10111015
dataset=Dataset.Events,
10121016
start=start,
@@ -1016,6 +1020,7 @@ def _execute_error_seen_stats_query(
10161020
filter_keys=filters,
10171021
aggregations=aggregations,
10181022
referrer="serializers.GroupSerializerSnuba._execute_error_seen_stats_query",
1023+
tenant_ids=tenant_ids,
10191024
)
10201025

10211026
@staticmethod

src/sentry/digests/notifications.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ def fetch_state(project: Project, records: Sequence[Record]) -> Mapping[str, Any
7676
end = records[0].datetime
7777

7878
groups = Group.objects.in_bulk(record.value.event.group_id for record in records)
79+
tenant_ids = {"organization_id": project.organization_id}
7980
return {
8081
"project": project,
8182
"groups": groups,
@@ -87,10 +88,14 @@ def fetch_state(project: Project, records: Sequence[Record]) -> Mapping[str, Any
8788
list(groups.keys()),
8889
start,
8990
end,
90-
tenant_ids={"organization_id": project.organization_id},
91+
tenant_ids=tenant_ids,
9192
),
9293
"user_counts": tsdb.get_distinct_counts_totals(
93-
tsdb.models.users_affected_by_group, list(groups.keys()), start, end
94+
tsdb.models.users_affected_by_group,
95+
list(groups.keys()),
96+
start,
97+
end,
98+
tenant_ids=tenant_ids,
9499
),
95100
}
96101

src/sentry/ingest/transaction_clusterer/datasource/snuba.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ def fetch_unique_transaction_names(
1212
project: Project, time_range: Tuple[datetime, datetime], limit: int
1313
) -> Iterable[str]:
1414
then, now = time_range
15-
referrer = "src.sentry.ingest.transaction_clusterer"
1615
snuba_request = Request(
1716
"transactions",
1817
app_id="transactions",
@@ -28,8 +27,8 @@ def fetch_unique_transaction_names(
2827
groupby=[Column("transaction")],
2928
limit=Limit(limit),
3029
),
31-
tenant_ids={"referrer": referrer, "organization_id": project.organization_id},
30+
tenant_ids={"organization_id": project.organization_id},
3231
)
33-
snuba_response = raw_snql_query(snuba_request, referrer)
32+
snuba_response = raw_snql_query(snuba_request, "src.sentry.ingest.transaction_clusterer")
3433

3534
return (row["transaction"] for row in snuba_response["data"])

src/sentry/models/groupsnooze.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ def test_user_rates(self):
119119
keys=[self.group_id],
120120
start=start,
121121
end=end,
122+
tenant_ids={"organization_id": self.group.project.organization_id},
122123
)[self.group_id]
123124

124125
if rate >= self.user_count:

src/sentry/release_health/release_monitor/metrics.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,16 @@ def fetch_project_release_health_totals(
147147
.set_limit(self.CHUNK_SIZE + 1)
148148
.set_offset(offset)
149149
)
150-
referrer = "release_monitor.fetch_project_release_health_totals"
151150
request = Request(
152151
dataset=Dataset.Metrics.value,
153152
app_id="release_health",
154153
query=query,
155-
tenant_ids={"referrer": referrer, "organization_id": org_id},
154+
tenant_ids={"organization_id": org_id},
156155
)
157156
with metrics.timer("release_monitor.fetch_project_release_health_totals.query"):
158-
data = raw_snql_query(request, referrer)["data"]
157+
data = raw_snql_query(
158+
request, "release_monitor.fetch_project_release_health_totals"
159+
)["data"]
159160
count = len(data)
160161
more_results = count > self.CHUNK_SIZE
161162
offset += self.CHUNK_SIZE

0 commit comments

Comments
 (0)