Skip to content

Commit 7f04286

Browse files
feat(tags): Adds sampling to project tag keys endpoint (#87985)
Updates the project `/tags` endpoint to clamp timerange and sample events when feature flagged with `organizations:tag-key-sample-n`. These changes were copied over from the organization level `/tags` endpoint
1 parent 2735b99 commit 7f04286

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

src/sentry/api/endpoints/project_tags.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
1+
import datetime
2+
13
from rest_framework.request import Request
24
from rest_framework.response import Response
35

4-
from sentry import tagstore
6+
from sentry import features, options, tagstore
57
from sentry.api.api_owners import ApiOwner
68
from sentry.api.api_publish_status import ApiPublishStatus
79
from sentry.api.base import EnvironmentMixin, region_silo_endpoint
810
from sentry.api.bases.project import ProjectEndpoint
11+
from sentry.api.utils import clamp_date_range, default_start_end_dates
912
from sentry.constants import DS_DENYLIST, PROTECTED_TAG_KEYS
1013
from sentry.models.environment import Environment
1114

@@ -23,7 +26,7 @@ def get(self, request: Request, project) -> Response:
2326
except Environment.DoesNotExist:
2427
tag_keys = []
2528
else:
26-
kwargs = {}
29+
kwargs: dict = {}
2730
if request.GET.get("onlySamplingTags") == "1":
2831
kwargs["denylist"] = DS_DENYLIST
2932

@@ -38,6 +41,15 @@ def get(self, request: Request, project) -> Response:
3841

3942
include_values_seen = request.GET.get("includeValuesSeen") != "0"
4043

44+
if features.has("organizations:tag-key-sample-n", project.organization):
45+
# Tag queries longer than 14 days tend to time out for large customers. For getting a list of tags, clamping to 14 days is a reasonable compromise of speed vs. completeness
46+
(start, end) = clamp_date_range(
47+
default_start_end_dates(),
48+
datetime.timedelta(days=options.get("visibility.tag-key-max-date-range.days")),
49+
)
50+
kwargs["start"] = start
51+
kwargs["end"] = end
52+
4153
tag_keys = sorted(
4254
backend.get_tag_keys(
4355
project.id,

src/sentry/tagstore/snuba/backend.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,14 @@ def __get_tag_keys(
236236
tenant_ids=None,
237237
**kwargs,
238238
):
239+
optimize_kwargs: _OptimizeKwargs = {}
240+
if turbo := kwargs.get("turbo"):
241+
if isinstance(turbo, bool):
242+
optimize_kwargs["turbo"] = turbo
243+
if sample := kwargs.get("sample"):
244+
if isinstance(sample, int):
245+
optimize_kwargs["sample"] = sample
246+
239247
return self.__get_tag_keys_for_projects(
240248
get_project_list(project_id),
241249
group,
@@ -248,6 +256,7 @@ def __get_tag_keys(
248256
dataset=dataset,
249257
denylist=denylist,
250258
tenant_ids=tenant_ids,
259+
**optimize_kwargs,
251260
)
252261

253262
def __get_tag_keys_for_projects(
@@ -404,15 +413,30 @@ def get_tag_keys(
404413
include_values_seen=False,
405414
denylist=None,
406415
tenant_ids=None,
416+
**kwargs,
407417
):
408418
assert status is TagKeyStatus.ACTIVE
419+
420+
optimize_kwargs: _OptimizeKwargs = {}
421+
422+
organization_id = get_organization_id_from_project_ids(get_project_list(project_id))
423+
organization = Organization.objects.get_from_cache(id=organization_id)
424+
if features.has("organizations:tag-key-sample-n", organization):
425+
# Add static sample amount to the query. Turbo will sample at 10% by
426+
# default, but organizations with many events still get timeouts. A
427+
# static sample creates more consistent performance.
428+
optimize_kwargs["turbo"] = True
429+
optimize_kwargs["sample"] = options.get("visibility.tag-key-sample-size")
430+
409431
return self.__get_tag_keys(
410432
project_id,
411433
None,
412434
environment_id and [environment_id],
413435
denylist=denylist,
414436
tenant_ids=tenant_ids,
415437
include_values_seen=include_values_seen,
438+
**optimize_kwargs,
439+
**kwargs,
416440
)
417441

418442
def get_tag_keys_for_projects(

0 commit comments

Comments
 (0)