diff --git a/src/sentry/utils/sdk.py b/src/sentry/utils/sdk.py index b278c4d2c7fad5..a84219d86ccf64 100644 --- a/src/sentry/utils/sdk.py +++ b/src/sentry/utils/sdk.py @@ -3,6 +3,7 @@ import asyncio import copy import logging +import re import sys import typing from collections.abc import Generator, Mapping, Sequence, Sized @@ -92,6 +93,12 @@ "/api/0/auth/validate/": 0.0, } +# List of (compiled_pattern, sample_rate) tried in order when no exact route matches. +SAMPLED_ROUTE_PATTERNS: list[tuple[re.Pattern[str], float]] = [ + # Temporary: monitoring AI Conversations rollout + (re.compile(r"^/api/0/organizations/[^/]+/ai-conversations/"), 1.0), +] + if settings.ADDITIONAL_SAMPLED_TASKS: SAMPLED_TASKS.update(settings.ADDITIONAL_SAMPLED_TASKS) @@ -182,8 +189,12 @@ def get_project_key(): def traces_sampler(sampling_context): wsgi_path = sampling_context.get("wsgi_environ", {}).get("PATH_INFO") - if wsgi_path and wsgi_path in SAMPLED_ROUTES: - return SAMPLED_ROUTES[wsgi_path] + if wsgi_path: + if wsgi_path in SAMPLED_ROUTES: + return SAMPLED_ROUTES[wsgi_path] + for pattern, rate in SAMPLED_ROUTE_PATTERNS: + if pattern.search(wsgi_path): + return rate # Apply sample_rate from custom_sampling_context custom_sample_rate = sampling_context.get("sample_rate")