diff --git a/src/sentry/api/endpoints/organization_metrics.py b/src/sentry/api/endpoints/organization_metrics.py index 676c7f2403ba15..d5be7ed826dafe 100644 --- a/src/sentry/api/endpoints/organization_metrics.py +++ b/src/sentry/api/endpoints/organization_metrics.py @@ -7,7 +7,8 @@ from sentry.api.exceptions import ResourceDoesNotExist from sentry.api.paginator import GenericOffsetPaginator from sentry.api.utils import InvalidParams -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID +from sentry.sentry_metrics.utils import string_to_use_case_id from sentry.snuba.metrics import ( QueryDefinition, get_metrics, @@ -21,17 +22,18 @@ from sentry.utils.cursors import Cursor, CursorResult -def get_use_case_id(request: Request) -> UseCaseKey: +def get_use_case_id(request: Request) -> UseCaseID: """ - Get useCase from query params and validate it against UseCaseKey enum type + Get useCase from query params and validate it against UseCaseID enum type Raise a ParseError if the use_case parameter is invalid. """ try: - return UseCaseKey(request.GET.get("useCase", "release-health")) + use_case_param = request.GET.get("useCase", "sessions") + return string_to_use_case_id(use_case_param) except ValueError: raise ParseError( - detail=f"Invalid useCase parameter. Please use one of: {[uc.value for uc in UseCaseKey]}" + detail=f"Invalid useCase parameter. Please use one of: {[uc.value for uc in UseCaseID]}" ) diff --git a/src/sentry/ingest/billing_metrics_consumer.py b/src/sentry/ingest/billing_metrics_consumer.py index fc8d27b4541402..f1575ca0e25a91 100644 --- a/src/sentry/ingest/billing_metrics_consumer.py +++ b/src/sentry/ingest/billing_metrics_consumer.py @@ -12,8 +12,8 @@ from django.conf import settings from sentry.constants import DataCategory -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.indexer.strings import SHARED_TAG_STRINGS, TRANSACTION_METRICS_NAMES +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import reverse_resolve_tag_value from sentry.utils import json from sentry.utils.kafka_config import get_kafka_consumer_cluster_options, get_topic_definition @@ -143,7 +143,7 @@ def _has_profile(self, bucket: MetricsBucket) -> bool: return bool( (tag_value := bucket["tags"].get(self.profile_tag_key)) and "true" - == reverse_resolve_tag_value(UseCaseKey.PERFORMANCE, bucket["org_id"], tag_value) + == reverse_resolve_tag_value(UseCaseID.TRANSACTIONS, bucket["org_id"], tag_value) ) def _produce_billing_outcomes(self, payload: MetricsBucket) -> None: diff --git a/src/sentry/release_health/metrics.py b/src/sentry/release_health/metrics.py index 2b486e58f56b7d..13f972c668c700 100644 --- a/src/sentry/release_health/metrics.py +++ b/src/sentry/release_health/metrics.py @@ -45,7 +45,7 @@ ) from sentry.release_health.metrics_sessions_v2 import run_sessions_query from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.metrics import ( MetricField, MetricGroupByField, @@ -75,7 +75,7 @@ HOUR = MINUTE * 60 DAY = HOUR * 24 LEGACY_SESSIONS_DEFAULT_ROLLUP = HOUR -USE_CASE_ID = UseCaseKey.RELEASE_HEALTH +USE_CASE_ID = UseCaseID.SESSIONS logger = logging.getLogger(__name__) diff --git a/src/sentry/release_health/metrics_sessions_v2.py b/src/sentry/release_health/metrics_sessions_v2.py index ded129612bb6e5..e59110ba6bfad3 100644 --- a/src/sentry/release_health/metrics_sessions_v2.py +++ b/src/sentry/release_health/metrics_sessions_v2.py @@ -52,7 +52,7 @@ SessionsQueryResult, SessionsQueryValue, ) -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.metrics import get_public_name_from_mri from sentry.snuba.metrics.datasource import get_series from sentry.snuba.metrics.naming_layer import SessionMRI @@ -570,7 +570,7 @@ def run_sessions_query( metrics_results = get_series( projects, metrics_query, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, tenant_ids={"organization_id": org_id}, ) except OrderByNotSupportedOverCompositeEntityException: diff --git a/src/sentry/release_health/release_monitor/metrics.py b/src/sentry/release_health/release_monitor/metrics.py index 6dd488e6f60b5f..6207d7dd53b4b8 100644 --- a/src/sentry/release_health/release_monitor/metrics.py +++ b/src/sentry/release_health/release_monitor/metrics.py @@ -19,7 +19,6 @@ from sentry.release_health.release_monitor.base import BaseReleaseMonitorBackend, Totals from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.indexer.strings import SESSION_METRIC_NAMES from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import resolve_tag_key @@ -103,9 +102,9 @@ def fetch_project_release_health_totals( totals: Totals = defaultdict(dict) with metrics.timer("release_monitor.fetch_project_release_health_totals.loop"): while (time.time() - start_time) < self.MAX_SECONDS: - release_key = resolve_tag_key(UseCaseKey.RELEASE_HEALTH, org_id, "release") + release_key = resolve_tag_key(UseCaseID.SESSIONS, org_id, "release") release_col = Column(release_key) - env_key = resolve_tag_key(UseCaseKey.RELEASE_HEALTH, org_id, "environment") + env_key = resolve_tag_key(UseCaseID.SESSIONS, org_id, "environment") env_col = Column(env_key) query = ( Query( @@ -134,7 +133,7 @@ def fetch_project_release_health_totals( Column("metric_id"), Op.EQ, indexer.resolve( - UseCaseKey.RELEASE_HEALTH, org_id, SessionMRI.SESSION.value + UseCaseID.SESSIONS, org_id, SessionMRI.SESSION.value ), ), ], diff --git a/src/sentry/search/events/builder/metrics.py b/src/sentry/search/events/builder/metrics.py index 2022772fd82f30..df56108008579d 100644 --- a/src/sentry/search/events/builder/metrics.py +++ b/src/sentry/search/events/builder/metrics.py @@ -36,7 +36,6 @@ WhereType, ) from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.dataset import Dataset from sentry.snuba.metrics.extraction import QUERY_HASH_KEY, OndemandMetricSpec @@ -703,9 +702,9 @@ def run_query(self, referrer: str, use_cache: bool = False) -> Any: metrics_data = get_series( projects=self.params.projects, metrics_query=metric_query, - use_case_id=UseCaseKey.PERFORMANCE + use_case_id=UseCaseID.TRANSACTIONS if self.is_performance - else UseCaseKey.RELEASE_HEALTH, + else UseCaseID.SESSIONS, include_meta=True, tenant_ids=self.tenant_ids, ) @@ -898,9 +897,7 @@ def get_snql_query(self) -> Request: snuba_queries, _ = SnubaQueryBuilder( projects=self.params.projects, metrics_query=metrics_query, - use_case_id=UseCaseKey.PERFORMANCE - if self.is_performance - else UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.TRANSACTIONS if self.is_performance else UseCaseID.SESSIONS, ).get_snuba_queries() if len(snuba_queries) != 1: @@ -1112,9 +1109,9 @@ def run_query(self, referrer: str, use_cache: bool = False) -> Any: metrics_data = get_series( projects=self.params.projects, metrics_query=metric_query, - use_case_id=UseCaseKey.PERFORMANCE + use_case_id=UseCaseID.TRANSACTIONS if self.is_performance - else UseCaseKey.RELEASE_HEALTH, + else UseCaseID.SESSIONS, include_meta=True, tenant_ids=self.tenant_ids, ) diff --git a/src/sentry/sentry_metrics/use_case_id_registry.py b/src/sentry/sentry_metrics/use_case_id_registry.py index c8b3a488a0fc30..5b080b9c6af449 100644 --- a/src/sentry/sentry_metrics/use_case_id_registry.py +++ b/src/sentry/sentry_metrics/use_case_id_registry.py @@ -1,7 +1,7 @@ from __future__ import annotations from enum import Enum -from typing import Mapping +from typing import Mapping, Optional from sentry.sentry_metrics.configuration import UseCaseKey @@ -40,5 +40,5 @@ class UseCaseID(Enum): } -def get_metric_path_from_usecase(use_case: UseCaseID) -> UseCaseKey: - return METRIC_PATH_MAPPING[use_case] +def get_use_case_key(use_case_id: UseCaseID) -> Optional[UseCaseKey]: + return METRIC_PATH_MAPPING.get(use_case_id) diff --git a/src/sentry/sentry_metrics/utils.py b/src/sentry/sentry_metrics/utils.py index 2871acc03c0621..5825e5e15e37d8 100644 --- a/src/sentry/sentry_metrics/utils.py +++ b/src/sentry/sentry_metrics/utils.py @@ -3,7 +3,8 @@ from sentry.api.utils import InvalidParams from sentry.sentry_metrics import indexer from sentry.sentry_metrics.configuration import UseCaseKey -from sentry.sentry_metrics.use_case_id_registry import UseCaseID +from sentry.sentry_metrics.indexer.base import to_use_case_id +from sentry.sentry_metrics.use_case_id_registry import METRIC_PATH_MAPPING, UseCaseID #: Special integer used to represent a string missing from the indexer STRING_NOT_FOUND = -1 @@ -16,9 +17,22 @@ class MetricIndexNotFound(InvalidParams): pass +def string_to_use_case_id(value: str) -> UseCaseID: + try: + return UseCaseID(value) + except ValueError: + # param doesn't appear to be a UseCaseID try with the obsolete UseCaseKey + # will raise ValueError if it fails + return to_use_case_id(UseCaseKey(value)) + + def reverse_resolve_tag_value( - use_case_id: UseCaseKey, org_id: int, index: Union[int, str, None], weak: bool = False + use_case_id: Union[UseCaseID, UseCaseKey], + org_id: int, + index: Union[int, str, None], + weak: bool = False, ) -> Optional[str]: + use_case_id = to_use_case_id(use_case_id) if isinstance(index, str) or index is None: return index else: @@ -70,8 +84,9 @@ def bulk_reverse_resolve_tag_value( return {**ret_val, **resolved_indexes} -def reverse_resolve(use_case_id: UseCaseKey, org_id: int, index: int) -> str: +def reverse_resolve(use_case_id: Union[UseCaseID, UseCaseKey], org_id: int, index: int) -> str: assert index > 0 + use_case_id = to_use_case_id(use_case_id) resolved = indexer.reverse_resolve(use_case_id, org_id, index) # The indexer should never return None for integers > 0: if resolved is None: @@ -93,7 +108,9 @@ def bulk_reverse_resolve( return indexer.bulk_reverse_resolve(use_case_id, org_id, indexes_to_resolve) -def reverse_resolve_weak(use_case_id: UseCaseKey, org_id: int, index: int) -> Optional[str]: +def reverse_resolve_weak( + use_case_id: Union[UseCaseID, UseCaseKey], org_id: int, index: int +) -> Optional[str]: """ Resolve an index value back to a string, special-casing 0 to return None. @@ -101,6 +118,7 @@ def reverse_resolve_weak(use_case_id: UseCaseKey, org_id: int, index: int) -> Op tuple for metric buckets that are missing that tag, i.e. `tags[123] == 0`. """ + use_case_id = to_use_case_id(use_case_id) if index == TAG_NOT_SET: return None @@ -108,10 +126,11 @@ def reverse_resolve_weak(use_case_id: UseCaseKey, org_id: int, index: int) -> Op def resolve( - use_case_id: UseCaseKey, + use_case_id: Union[UseCaseID, UseCaseKey], org_id: int, string: str, ) -> int: + use_case_id = to_use_case_id(use_case_id) resolved = indexer.resolve(use_case_id, org_id, string) if resolved is None: raise MetricIndexNotFound(f"Unknown string: {string!r}") @@ -119,26 +138,31 @@ def resolve( return resolved -def resolve_tag_key(use_case_id: UseCaseKey, org_id: int, string: str) -> str: +def resolve_tag_key(use_case_id: Union[UseCaseID, UseCaseKey], org_id: int, string: str) -> str: + use_case_id = to_use_case_id(use_case_id) resolved = resolve(use_case_id, org_id, string) - assert use_case_id in (UseCaseKey.PERFORMANCE, UseCaseKey.RELEASE_HEALTH) - if use_case_id == UseCaseKey.PERFORMANCE: + assert isinstance(use_case_id, UseCaseID) + if METRIC_PATH_MAPPING[use_case_id] is UseCaseKey.PERFORMANCE: return f"tags_raw[{resolved}]" else: return f"tags[{resolved}]" -def resolve_tag_value(use_case_id: UseCaseKey, org_id: int, string: str) -> Union[str, int]: +def resolve_tag_value( + use_case_id: Union[UseCaseID, UseCaseKey], org_id: int, string: str +) -> Union[str, int]: + use_case_id = to_use_case_id(use_case_id) assert isinstance(string, str) - assert use_case_id in (UseCaseKey.PERFORMANCE, UseCaseKey.RELEASE_HEALTH) - if use_case_id == UseCaseKey.PERFORMANCE: + assert isinstance(use_case_id, UseCaseID) + if METRIC_PATH_MAPPING[use_case_id] is UseCaseKey.PERFORMANCE: return string return resolve_weak(use_case_id, org_id, string) def resolve_tag_values( - use_case_id: UseCaseKey, org_id: int, strings: Sequence[str] + use_case_id: Union[UseCaseID, UseCaseKey], org_id: int, strings: Sequence[str] ) -> Sequence[Union[str, int]]: + use_case_id = to_use_case_id(use_case_id) rv = [] for string in strings: resolved = resolve_tag_value(use_case_id, org_id, string) @@ -148,7 +172,7 @@ def resolve_tag_values( return rv -def resolve_weak(use_case_id: UseCaseKey, org_id: int, string: str) -> int: +def resolve_weak(use_case_id: Union[UseCaseID, UseCaseKey], org_id: int, string: str) -> int: """ A version of `resolve` that returns -1 for missing values. @@ -156,6 +180,7 @@ def resolve_weak(use_case_id: UseCaseKey, org_id: int, string: str) -> int: useful to make the WHERE-clause "impossible" with `WHERE x = -1` instead of explicitly handling that exception. """ + use_case_id = to_use_case_id(use_case_id) resolved = indexer.resolve(use_case_id, org_id, string) if resolved is None: return STRING_NOT_FOUND @@ -164,12 +189,13 @@ def resolve_weak(use_case_id: UseCaseKey, org_id: int, string: str) -> int: def resolve_many_weak( - use_case_id: UseCaseKey, org_id: int, strings: Sequence[str] + use_case_id: Union[UseCaseID, UseCaseKey], org_id: int, strings: Sequence[str] ) -> Sequence[int]: """ Resolve multiple values at once, omitting missing ones. This is useful in the same way as `resolve_weak` is, e.g. `WHERE x in values`. """ + use_case_id = to_use_case_id(use_case_id) rv = [] for string in strings: resolved = resolve_weak(use_case_id, org_id, string) diff --git a/src/sentry/snuba/entity_subscription.py b/src/sentry/snuba/entity_subscription.py index 710315ea997f46..093df7111371ce 100644 --- a/src/sentry/snuba/entity_subscription.py +++ b/src/sentry/snuba/entity_subscription.py @@ -24,7 +24,7 @@ from sentry.constants import CRASH_RATE_ALERT_AGGREGATE_ALIAS, CRASH_RATE_ALERT_SESSION_COUNT_ALIAS from sentry.exceptions import InvalidQuerySubscription, UnsupportedQuerySubscription from sentry.models import Environment, Organization -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import ( MetricIndexNotFound, resolve, @@ -324,29 +324,29 @@ def get_entity_extra_params(self) -> Mapping[str, Any]: "granularity": self.get_granularity(), } - def _get_use_case_key(self) -> UseCaseKey: + def _get_use_case_id(self) -> UseCaseID: if self.dataset == Dataset.PerformanceMetrics: - return UseCaseKey.PERFORMANCE + return UseCaseID.TRANSACTIONS else: - return UseCaseKey.RELEASE_HEALTH + return UseCaseID.SESSIONS def resolve_tag_key_if_needed(self, string: str) -> str: if self.use_metrics_layer: return string - return resolve_tag_key(self._get_use_case_key(), self.org_id, string) + return resolve_tag_key(self._get_use_case_id(), self.org_id, string) def resolve_tag_value_if_needed(self, string: str) -> Union[str, int]: if self.use_metrics_layer: return string - return resolve_tag_value(self._get_use_case_key(), self.org_id, string) + return resolve_tag_value(self._get_use_case_id(), self.org_id, string) def resolve_tag_values_if_needed(self, strings: Sequence[str]) -> Sequence[Union[str, int]]: if self.use_metrics_layer: return strings - return resolve_tag_values(self._get_use_case_key(), self.org_id, strings) + return resolve_tag_values(self._get_use_case_id(), self.org_id, strings) def _get_environment_condition(self, environment_name: str) -> Condition: return Condition( @@ -445,10 +445,10 @@ def translate_sessions_tag_keys_and_values( value_col_name = alias if alias else "value" try: translated_data: Dict[str, Any] = {} - session_status = resolve_tag_key(UseCaseKey.RELEASE_HEALTH, org_id, "session.status") + session_status = resolve_tag_key(UseCaseID.SESSIONS, org_id, "session.status") for row in data: tag_value = reverse_resolve_tag_value( - UseCaseKey.RELEASE_HEALTH, org_id, row[session_status] + UseCaseID.SESSIONS, org_id, row[session_status] ) if tag_value is None: raise MetricIndexNotFound() @@ -540,7 +540,7 @@ def get_snql_extra_conditions(self) -> List[Condition]: Condition( Column("metric_id"), Op.EQ, - resolve(UseCaseKey.RELEASE_HEALTH, self.org_id, self.metric_key.value), + resolve(UseCaseID.SESSIONS, self.org_id, self.metric_key.value), ) ] diff --git a/src/sentry/snuba/metrics/datasource.py b/src/sentry/snuba/metrics/datasource.py index de4e2b74d75673..3590136c46a7f3 100644 --- a/src/sentry/snuba/metrics/datasource.py +++ b/src/sentry/snuba/metrics/datasource.py @@ -1,8 +1,5 @@ from __future__ import annotations -from sentry.sentry_metrics.indexer.base import to_use_case_id -from sentry.sentry_metrics.use_case_id_registry import UseCaseID - """ Module that gets both metadata and time series from Snuba. For metadata, it fetch metrics metadata (metric names, tag names, tag values, ...) from snuba. @@ -27,7 +24,7 @@ from sentry.api.utils import InvalidParams from sentry.models import Project from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import ( MetricIndexNotFound, bulk_reverse_resolve, @@ -94,7 +91,7 @@ def _get_metrics_for_entity( def get_available_derived_metrics( projects: Sequence[Project], supported_metric_ids_in_entities: Dict[MetricType, Sequence[int]], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, ) -> Set[str]: """ Function that takes as input a dictionary of the available ids in each entity, and in turn @@ -142,7 +139,7 @@ def get_available_derived_metrics( return found_derived_metrics.intersection(public_derived_metrics) -def get_metrics(projects: Sequence[Project], use_case_id: UseCaseKey) -> Sequence[MetricMeta]: +def get_metrics(projects: Sequence[Project], use_case_id: UseCaseID) -> Sequence[MetricMeta]: ENTITY_TO_DATASET = { "sessions": { "c": "metrics_counters", @@ -224,7 +221,7 @@ def get_custom_measurements( def _get_metrics_filter_ids( - projects: Sequence[Project], metric_mris: Sequence[str], use_case_id: UseCaseKey + projects: Sequence[Project], metric_mris: Sequence[str], use_case_id: UseCaseID ) -> Set[int]: """ Returns a set of metric_ids that map to input metric names and raises an exception if @@ -263,7 +260,7 @@ def _validate_requested_derived_metrics_in_input_metrics( projects: Sequence[Project], metric_mris: Sequence[str], supported_metric_ids_in_entities: Dict[MetricType, Sequence[int]], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, ) -> None: """ Function that takes metric_mris list and a mapping of entity to its metric ids, and ensures @@ -292,7 +289,7 @@ def _fetch_tags_or_values_for_metrics( metric_names: Optional[Sequence[str]], referrer: str, column: str, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, ) -> Tuple[Union[Sequence[Tag], Sequence[TagValue]], Optional[str]]: assert len({p.organization_id for p in projects}) == 1 @@ -306,7 +303,7 @@ def _fetch_tags_or_values_for_mri( metric_mris: Optional[Sequence[str]], referrer: str, column: str, - use_case_key: UseCaseKey, + use_case_id: UseCaseID, ) -> Tuple[Union[Sequence[Tag], Sequence[TagValue]], Optional[str]]: """ Function that takes as input projects, metric_mris, and a column, and based on the column @@ -316,7 +313,6 @@ def _fetch_tags_or_values_for_mri( metric_names, then the type (i.e. mapping to the entity) is also returned """ org_id = projects[0].organization_id - use_case_id = to_use_case_id(use_case_key) if metric_mris is not None: private_derived_metrics = set(get_derived_metrics(exclude_private=False).keys()) - set( @@ -327,7 +323,7 @@ def _fetch_tags_or_values_for_mri( try: metric_ids = _get_metrics_filter_ids( - projects=projects, metric_mris=metric_mris, use_case_id=use_case_key + projects=projects, metric_mris=metric_mris, use_case_id=use_case_id ) except MetricDoesNotExistInIndexer: raise InvalidParams( @@ -345,7 +341,7 @@ def _fetch_tags_or_values_for_mri( release_health_metric_types = ("counter", "set", "distribution") performance_metric_types = ("generic_counter", "generic_set", "generic_distribution") - if use_case_key == UseCaseKey.RELEASE_HEALTH: + if use_case_id == UseCaseID.SESSIONS: metric_types = release_health_metric_types else: metric_types = performance_metric_types @@ -402,7 +398,7 @@ def _fetch_tags_or_values_for_mri( projects, metric_mris=metric_mris, supported_metric_ids_in_entities=supported_metric_ids_in_entities, - use_case_id=use_case_key, + use_case_id=use_case_id, ) # Only return tags/tag values that occur in all metrics @@ -441,7 +437,7 @@ def _fetch_tags_or_values_for_mri( def get_single_metric_info( - projects: Sequence[Project], metric_name: str, use_case_id: UseCaseKey + projects: Sequence[Project], metric_name: str, use_case_id: UseCaseID ) -> MetricMetaWithTagKeys: assert projects @@ -477,7 +473,7 @@ def get_single_metric_info( def get_tags( - projects: Sequence[Project], metrics: Optional[Sequence[str]], use_case_id: UseCaseKey + projects: Sequence[Project], metrics: Optional[Sequence[str]], use_case_id: UseCaseID ) -> Sequence[Tag]: """Get all metric tags for the given projects and metric_names""" assert projects @@ -489,7 +485,7 @@ def get_tags( metric_mris=metrics, column="tags.key", referrer="snuba.metrics.meta.get_tags", - use_case_key=use_case_id, + use_case_id=use_case_id, ) else: tags, _ = _fetch_tags_or_values_for_metrics( @@ -508,7 +504,7 @@ def get_tag_values( projects: Sequence[Project], tag_name: str, metric_names: Optional[Sequence[str]], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, ) -> Sequence[TagValue]: """Get all known values for a specific tag""" assert projects @@ -567,7 +563,7 @@ class GroupLimitFilters: def _get_group_limit_filters( - metrics_query: MetricsQuery, results: List[Mapping[str, int]], use_case_id: UseCaseKey + metrics_query: MetricsQuery, results: List[Mapping[str, int]], use_case_id: UseCaseID ) -> Optional[GroupLimitFilters]: if not metrics_query.groupby or not results: return None @@ -697,7 +693,7 @@ def _prune_extra_groups(results: dict, filters: GroupLimitFilters) -> None: def get_series( projects: Sequence[Project], metrics_query: MetricsQuery, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, include_meta: bool = False, tenant_ids: dict[str, Any] | None = None, ) -> dict: diff --git a/src/sentry/snuba/metrics/fields/base.py b/src/sentry/snuba/metrics/fields/base.py index 44a1e7d6b15f76..9ef05ac7416255 100644 --- a/src/sentry/snuba/metrics/fields/base.py +++ b/src/sentry/snuba/metrics/fields/base.py @@ -31,7 +31,7 @@ from sentry.models import Project from sentry.search.events.constants import MISERY_ALPHA, MISERY_BETA from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import resolve_weak from sentry.snuba.dataset import Dataset, EntityKey from sentry.snuba.metrics.fields.histogram import ClickhouseHistogram, rebucket_histogram @@ -156,7 +156,7 @@ def run_metrics_query( def _get_known_entity_of_metric_mri(metric_mri: str) -> Optional[EntityKey]: - # ToDo(ahmed): Add an abstraction that returns relevant data based on usecasekey without repeating code + # ToDo(ahmed): Add an abstraction that returns relevant data based on UseCaseID without repeating code try: SessionMRI(metric_mri) entity_prefix = metric_mri.split(":")[0] @@ -182,7 +182,7 @@ def _get_known_entity_of_metric_mri(metric_mri: str) -> Optional[EntityKey]: def _get_entity_of_metric_mri( - projects: Sequence[Project], metric_mri: str, use_case_id: UseCaseKey + projects: Sequence[Project], metric_mri: str, use_case_id: UseCaseID ) -> EntityKey: known_entity = _get_known_entity_of_metric_mri(metric_mri) if known_entity is not None: @@ -196,11 +196,11 @@ def _get_entity_of_metric_mri( raise InvalidParams entity_keys_set: frozenset[EntityKey] - if use_case_id == UseCaseKey.PERFORMANCE: + if use_case_id is UseCaseID.TRANSACTIONS: entity_keys_set = frozenset( {EntityKey.GenericMetricsSets, EntityKey.GenericMetricsDistributions} ) - elif use_case_id == UseCaseKey.RELEASE_HEALTH: + elif use_case_id is UseCaseID.SESSIONS: entity_keys_set = frozenset( {EntityKey.MetricsCounters, EntityKey.MetricsSets, EntityKey.MetricsDistributions} ) @@ -246,11 +246,11 @@ class MetricObject(MetricObjectDefinition, ABC): """ @abstractmethod - def generate_filter_snql_conditions(self, org_id: int, use_case_id: UseCaseKey) -> Function: + def generate_filter_snql_conditions(self, org_id: int, use_case_id: UseCaseID) -> Function: raise NotImplementedError @abstractmethod - def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> Set[int]: + def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseID) -> Set[int]: raise NotImplementedError @@ -260,10 +260,10 @@ class RawMetric(MetricObject): metric """ - def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> Set[int]: + def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseID) -> Set[int]: return {resolve_weak(use_case_id, org_id_from_projects(projects), self.metric_mri)} - def generate_filter_snql_conditions(self, org_id: int, use_case_id: UseCaseKey) -> Function: + def generate_filter_snql_conditions(self, org_id: int, use_case_id: UseCaseID) -> Function: return Function( "equals", [Column("metric_id"), resolve_weak(use_case_id, org_id, self.metric_mri)], @@ -276,10 +276,10 @@ class AliasedDerivedMetric(AliasedDerivedMetricDefinition, MetricObject): for a raw metric name """ - def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> Set[int]: + def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseID) -> Set[int]: return {resolve_weak(use_case_id, org_id_from_projects(projects), self.raw_metric_mri)} - def generate_filter_snql_conditions(self, org_id: int, use_case_id: UseCaseKey) -> Function: + def generate_filter_snql_conditions(self, org_id: int, use_case_id: UseCaseID) -> Function: conditions = [ Function( "equals", @@ -328,7 +328,7 @@ def run_post_query_function( def generate_snql_function( self, entity: MetricEntity, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, aggregate_filter: Function, org_id: int, @@ -405,13 +405,13 @@ def _wrap_quantiles(self, function: Function, alias: str) -> Function: def generate_snql_function( self, entity: MetricEntity, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, aggregate_filter: Function, org_id: int, params: Optional[MetricOperationParams] = None, ) -> Function: - if use_case_id is UseCaseKey.PERFORMANCE: + if use_case_id is UseCaseID.TRANSACTIONS: snuba_function = GENERIC_OP_TO_SNUBA_FUNCTION[entity][self.op] else: snuba_function = OP_TO_SNUBA_FUNCTION[entity][self.op] @@ -483,14 +483,14 @@ def run_post_query_function( def generate_snql_function( self, entity: MetricEntity, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, aggregate_filter: Function, org_id: int, params: Optional[MetricOperationParams] = None, ) -> Function: metrics_query_args = inspect.signature(self.snql_func).parameters.keys() - kwargs: MutableMapping[str, Union[float, int, str, UseCaseKey, Function]] = {} + kwargs: MutableMapping[str, Union[float, int, str, UseCaseID, Function]] = {} if "alias" in metrics_query_args: kwargs["alias"] = alias @@ -527,7 +527,7 @@ def validate_can_orderby(self) -> None: @abstractmethod def get_entity( - self, projects: Sequence[Project], use_case_id: UseCaseKey + self, projects: Sequence[Project], use_case_id: UseCaseID ) -> Union[MetricEntity, Dict[MetricEntity, Sequence[str]]]: """ Method that generates the entity of an instance of MetricsFieldBase. @@ -537,7 +537,7 @@ def get_entity( raise NotImplementedError @abstractmethod - def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> Set[int]: + def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseID) -> Set[int]: """ Method that generates all the metric ids required to query an instance of MetricsFieldBase @@ -548,7 +548,7 @@ def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseK def generate_select_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -563,7 +563,7 @@ def generate_orderby_clause( self, direction: Direction, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[OrderBy]: @@ -649,7 +649,7 @@ def generate_bottom_up_derived_metrics_dependencies( def generate_groupby_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -663,7 +663,7 @@ def generate_groupby_statements( def generate_where_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -700,13 +700,13 @@ def __str__(self) -> str: def validate_can_orderby(self) -> None: self.metric_operation.validate_can_orderby() - def get_entity(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> MetricEntity: + def get_entity(self, projects: Sequence[Project], use_case_id: UseCaseID) -> MetricEntity: return _get_entity_of_metric_mri(projects, self.metric_object.metric_mri, use_case_id).value def generate_select_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -725,7 +725,7 @@ def generate_orderby_clause( self, direction: Direction, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[OrderBy]: @@ -745,7 +745,7 @@ def generate_available_operations(self) -> Collection[MetricOperationType]: def generate_default_null_values(self) -> Optional[Union[int, List[Tuple[float]]]]: return self.metric_operation.get_default_null_values() - def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> Set[int]: + def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseID) -> Set[int]: return self.metric_object.generate_metric_ids(projects, use_case_id) def run_post_query_function( @@ -773,7 +773,7 @@ def build_conditional_aggregate_for_metric( self, org_id: int, entity: MetricEntity, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> Function: @@ -795,7 +795,7 @@ def build_conditional_aggregate_for_metric( def generate_groupby_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -814,7 +814,7 @@ def generate_groupby_statements( def generate_where_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -886,7 +886,7 @@ def validate_can_orderby(self) -> None: @classmethod def __recursively_get_all_entities_in_derived_metric_dependency_tree( - cls, derived_metric_mri: str, projects: Sequence[Project], use_case_id: UseCaseKey + cls, derived_metric_mri: str, projects: Sequence[Project], use_case_id: UseCaseID ) -> Set[MetricEntity]: """ Method that gets the entity of a derived metric by traversing down its dependency tree @@ -905,7 +905,7 @@ def __recursively_get_all_entities_in_derived_metric_dependency_tree( ) return entities - def get_entity(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> MetricEntity: + def get_entity(self, projects: Sequence[Project], use_case_id: UseCaseID) -> MetricEntity: if not projects: self._raise_entity_validation_exception("get_entity") try: @@ -924,7 +924,7 @@ def get_entity(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> Me @classmethod def __recursively_generate_metric_ids( - cls, org_id: int, derived_metric_mri: str, use_case_id: UseCaseKey + cls, org_id: int, derived_metric_mri: str, use_case_id: UseCaseID ) -> Set[int]: """ Method that traverses a derived metric dependency tree to return a set of the metric ids @@ -941,7 +941,7 @@ def __recursively_generate_metric_ids( ids |= cls.__recursively_generate_metric_ids(org_id, metric_mri, use_case_id) return ids - def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> Set[int]: + def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseID) -> Set[int]: org_id = org_id_from_projects(projects) return self.__recursively_generate_metric_ids( org_id, derived_metric_mri=self.metric_mri, use_case_id=use_case_id @@ -953,7 +953,7 @@ def __recursively_generate_select_snql( project_ids: Sequence[int], org_id: int, derived_metric_mri: str, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: Optional[str] = None, ) -> List[Function]: """ @@ -989,7 +989,7 @@ def __recursively_generate_select_snql( def generate_select_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -1017,7 +1017,7 @@ def generate_orderby_clause( self, direction: Direction, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[OrderBy]: @@ -1071,7 +1071,7 @@ def generate_bottom_up_derived_metrics_dependencies( def generate_groupby_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -1080,7 +1080,7 @@ def generate_groupby_statements( def generate_where_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -1095,13 +1095,13 @@ def __init__(self, *args: Any, **kwargs: Any) -> None: def validate_can_orderby(self) -> None: raise NotSupportedOverCompositeEntityException() - def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseKey) -> Set[Any]: + def generate_metric_ids(self, projects: Sequence[Project], use_case_id: UseCaseID) -> Set[Any]: raise NotSupportedOverCompositeEntityException() def generate_select_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -1111,7 +1111,7 @@ def generate_orderby_clause( self, direction: Direction, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[OrderBy]: @@ -1130,7 +1130,7 @@ def generate_default_null_values(self) -> Optional[Union[int, List[Tuple[float]] return default_null_value def get_entity( - self, projects: Sequence[Project], use_case_id: UseCaseKey + self, projects: Sequence[Project], use_case_id: UseCaseID ) -> Dict[MetricEntity, List[str]]: if not projects: self._raise_entity_validation_exception("get_entity") @@ -1146,7 +1146,7 @@ def __recursively_generate_singular_entity_constituents( cls, projects: Optional[Sequence[Project]], derived_metric_obj: DerivedMetricExpression, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, is_naive: bool = False, ) -> Dict[MetricEntity, List[str]]: entities_and_metric_mris: Dict[MetricEntity, List[str]] = {} @@ -1221,7 +1221,7 @@ def generate_bottom_up_derived_metrics_dependencies( metric_nodes.append(DERIVED_METRICS[metric]) return reversed(results) - def naively_generate_singular_entity_constituents(self, use_case_id: UseCaseKey) -> Set[str]: + def naively_generate_singular_entity_constituents(self, use_case_id: UseCaseID) -> Set[str]: single_entity_constituents = set( list( self.__recursively_generate_singular_entity_constituents( @@ -1260,7 +1260,7 @@ def run_post_query_function( def generate_groupby_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: @@ -1269,7 +1269,7 @@ def generate_groupby_statements( def generate_where_statements( self, projects: Sequence[Project], - use_case_id: UseCaseKey, + use_case_id: UseCaseID, alias: str, params: Optional[MetricOperationParams] = None, ) -> List[Function]: diff --git a/src/sentry/snuba/metrics/fields/snql.py b/src/sentry/snuba/metrics/fields/snql.py index 6a7ac7dcf62789..188a6528c5707e 100644 --- a/src/sentry/snuba/metrics/fields/snql.py +++ b/src/sentry/snuba/metrics/fields/snql.py @@ -4,7 +4,7 @@ from sentry.api.utils import InvalidParams from sentry.search.events.datasets.function_aliases import resolve_project_threshold_config -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import ( resolve_tag_key, resolve_tag_value, @@ -34,14 +34,12 @@ def _snql_on_session_status_factory(org_id, session_status, metric_ids, alias=No [ Column( resolve_tag_key( - UseCaseKey.RELEASE_HEALTH, + UseCaseID.SESSIONS, org_id, "session.status", ) ), - resolve_tag_value( - UseCaseKey.RELEASE_HEALTH, org_id, session_status - ), + resolve_tag_value(UseCaseID.SESSIONS, org_id, session_status), ], ), Function("in", [Column("metric_id"), list(metric_ids)]), @@ -63,13 +61,13 @@ def _aggregation_on_abnormal_mechanism_func_factory( [ Column( resolve_tag_key( - UseCaseKey.RELEASE_HEALTH, + UseCaseID.SESSIONS, org_id, "abnormal_mechanism", ) ), [ - resolve_tag_value(UseCaseKey.RELEASE_HEALTH, org_id, mechanism) + resolve_tag_value(UseCaseID.SESSIONS, org_id, mechanism) for mechanism in abnormal_mechanism ], ], @@ -80,12 +78,12 @@ def _aggregation_on_abnormal_mechanism_func_factory( [ Column( resolve_tag_key( - UseCaseKey.RELEASE_HEALTH, + UseCaseID.SESSIONS, org_id, "abnormal_mechanism", ) ), - resolve_tag_value(UseCaseKey.RELEASE_HEALTH, org_id, abnormal_mechanism), + resolve_tag_value(UseCaseID.SESSIONS, org_id, abnormal_mechanism), ], ) @@ -130,12 +128,12 @@ def _get_snql_conditions(org_id, metric_ids, exclude_tx_statuses): tx_col = Column( resolve_tag_key( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, org_id, TransactionTagsKey.TRANSACTION_STATUS.value, ) ) - excluded_statuses = resolve_tag_values(UseCaseKey.PERFORMANCE, org_id, exclude_tx_statuses) + excluded_statuses = resolve_tag_values(UseCaseID.TRANSACTIONS, org_id, exclude_tx_statuses) exclude_tx_statuses = Function( "notIn", [ @@ -187,13 +185,13 @@ def _snql_on_tx_satisfaction_factory(org_id, satisfaction_value: str, metric_ids [ Column( resolve_tag_key( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, org_id, TransactionTagsKey.TRANSACTION_SATISFACTION.value, ) ), resolve_tag_value( - UseCaseKey.PERFORMANCE, org_id, satisfaction_value + UseCaseID.TRANSACTIONS, org_id, satisfaction_value ), ], ), @@ -325,7 +323,7 @@ def _project_threshold_multi_if_function( project_ids: Sequence[int], org_id: int, metric_ids: Set[int] ) -> Function: metric_ids_dictionary = { - reverse_resolve_weak(UseCaseKey.PERFORMANCE, org_id, metric_id): metric_id + reverse_resolve_weak(UseCaseID.TRANSACTIONS, org_id, metric_id): metric_id for metric_id in metric_ids } @@ -354,12 +352,12 @@ def _satisfaction_equivalence(org_id: int, satisfaction_tag_value: str) -> Funct [ Column( name=resolve_tag_key( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, org_id, TransactionTagsKey.TRANSACTION_SATISFACTION.value, ) ), - resolve_tag_value(UseCaseKey.PERFORMANCE, org_id, satisfaction_tag_value), + resolve_tag_value(UseCaseID.TRANSACTIONS, org_id, satisfaction_tag_value), ], ) @@ -493,8 +491,8 @@ def session_duration_filters(org_id): Function( "equals", ( - Column(resolve_tag_key(UseCaseKey.RELEASE_HEALTH, org_id, "session.status")), - resolve_tag_value(UseCaseKey.RELEASE_HEALTH, org_id, "exited"), + Column(resolve_tag_key(UseCaseID.SESSIONS, org_id, "session.status")), + resolve_tag_value(UseCaseID.SESSIONS, org_id, "exited"), ), ) ] @@ -549,10 +547,10 @@ def count_web_vitals_snql_factory(aggregate_filter, org_id, measurement_rating, ( Column( resolve_tag_key( - UseCaseKey.PERFORMANCE, org_id, "measurement_rating" + UseCaseID.TRANSACTIONS, org_id, "measurement_rating" ) ), - resolve_tag_value(UseCaseKey.PERFORMANCE, org_id, measurement_rating), + resolve_tag_value(UseCaseID.TRANSACTIONS, org_id, measurement_rating), ), ), ], @@ -570,7 +568,7 @@ def count_transaction_name_snql_factory(aggregate_filter, org_id, transaction_na def generate_transaction_name_filter(operation, transaction_name_identifier): if transaction_name_identifier == is_unparameterized: inner_tag_value = resolve_tag_value( - UseCaseKey.PERFORMANCE, org_id, "<< unparameterized >>" + UseCaseID.TRANSACTIONS, org_id, "<< unparameterized >>" ) elif transaction_name_identifier == is_null: inner_tag_value = "" @@ -582,7 +580,7 @@ def generate_transaction_name_filter(operation, transaction_name_identifier): [ Column( resolve_tag_key( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, org_id, "transaction", ) @@ -630,7 +628,7 @@ def team_key_transaction_snql(org_id, team_key_condition_rhs, alias=None): team_key_conditions.add( ( project_id, - resolve_tag_value(UseCaseKey.PERFORMANCE, org_id, transaction_name), + resolve_tag_value(UseCaseID.TRANSACTIONS, org_id, transaction_name), ) ) @@ -639,7 +637,7 @@ def team_key_transaction_snql(org_id, team_key_condition_rhs, alias=None): [ ( Column("project_id"), - Column(resolve_tag_key(UseCaseKey.PERFORMANCE, org_id, "transaction")), + Column(resolve_tag_key(UseCaseID.TRANSACTIONS, org_id, "transaction")), ), list(team_key_conditions), ], @@ -657,7 +655,7 @@ def _resolve_project_threshold_config(project_ids, org_id): ), project_ids=project_ids, org_id=org_id, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) diff --git a/src/sentry/snuba/metrics/mqb_query_transformer.py b/src/sentry/snuba/metrics/mqb_query_transformer.py index ef0e8d83b6eec0..9b08a273cff5e4 100644 --- a/src/sentry/snuba/metrics/mqb_query_transformer.py +++ b/src/sentry/snuba/metrics/mqb_query_transformer.py @@ -5,7 +5,7 @@ from snuba_sdk.query import Query from sentry.api.utils import InvalidParams -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.metrics import ( FIELD_ALIAS_MAPPINGS, FILTERABLE_TAGS, @@ -286,7 +286,7 @@ def _derive_mri_to_apply(project_ids, select, orderby): for select_field in select: if select_field.op != TEAM_KEY_TRANSACTION_OP: expr = metric_object_factory(select_field.op, select_field.metric_mri) - entity = expr.get_entity(project_ids, use_case_id=UseCaseKey.PERFORMANCE) + entity = expr.get_entity(project_ids, use_case_id=UseCaseID.TRANSACTIONS) if isinstance(entity, str): entities.add(entity) else: @@ -302,7 +302,7 @@ def _derive_mri_to_apply(project_ids, select, orderby): expr = metric_object_factory( orderby_field.field.op, orderby_field.field.metric_mri ) - entity = expr.get_entity(project_ids, use_case_id=UseCaseKey.PERFORMANCE) + entity = expr.get_entity(project_ids, use_case_id=UseCaseID.TRANSACTIONS) if isinstance(entity, str): entities.add(entity) diff --git a/src/sentry/snuba/metrics/query.py b/src/sentry/snuba/metrics/query.py index 17bdcfd364d812..7d7a9332206c35 100644 --- a/src/sentry/snuba/metrics/query.py +++ b/src/sentry/snuba/metrics/query.py @@ -11,7 +11,7 @@ from sentry.api.utils import InvalidParams from sentry.models import Project -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.metrics.fields import metric_object_factory from sentry.snuba.metrics.fields.base import get_derived_metrics from sentry.snuba.metrics.naming_layer.mri import parse_mri @@ -166,20 +166,18 @@ def projects(self) -> QuerySet: return Project.objects.filter(id__in=self.project_ids) @cached_property - def use_case_key(self) -> UseCaseKey: + def use_case_id(self) -> UseCaseID: return self._use_case_id(self.select[0].metric_mri) @staticmethod - def _use_case_id(metric_mri: str) -> UseCaseKey: + def _use_case_id(metric_mri: str) -> UseCaseID: """Find correct use_case_id based on metric_name""" parsed_mri = parse_mri(metric_mri) assert parsed_mri is not None - - if parsed_mri.namespace == "transactions": - return UseCaseKey.PERFORMANCE - elif parsed_mri.namespace == "sessions": - return UseCaseKey.RELEASE_HEALTH - raise ValueError("Can't find correct use_case_id based on metric MRI") + try: + return UseCaseID(parsed_mri.namespace) + except ValueError: + raise ValueError("Can't find correct use_case_id based on metric MRI") @staticmethod def _validate_field(field: MetricField) -> None: @@ -341,7 +339,7 @@ def validate_end(self) -> None: def validate_granularity(self) -> None: # Logic specific to how we handle time series in discover in terms of granularity and interval if ( - self.use_case_key == UseCaseKey.PERFORMANCE + self.use_case_id == UseCaseID.TRANSACTIONS and self.include_series and self.interval is not None ): @@ -378,8 +376,8 @@ def validate_granularity(self) -> None: def validate_interval(self) -> None: if self.interval is not None: - if self.use_case_key == UseCaseKey.RELEASE_HEALTH or ( - self.use_case_key == UseCaseKey.PERFORMANCE and not self.include_series + if self.use_case_id is UseCaseID.SESSIONS or ( + self.use_case_id is UseCaseID.TRANSACTIONS and not self.include_series ): raise InvalidParams("Interval is only supported for timeseries performance queries") @@ -400,7 +398,7 @@ def __post_init__(self) -> None: object.__setattr__(self, "limit", Limit(self.get_default_limit())) if ( - self.use_case_key == UseCaseKey.PERFORMANCE + self.use_case_id is UseCaseID.TRANSACTIONS and self.include_series and self.interval is None ): diff --git a/src/sentry/snuba/metrics/query_builder.py b/src/sentry/snuba/metrics/query_builder.py index 753bd942dc1410..4fcefd2c4f8f6f 100644 --- a/src/sentry/snuba/metrics/query_builder.py +++ b/src/sentry/snuba/metrics/query_builder.py @@ -37,7 +37,7 @@ from sentry.models import Project from sentry.search.events.builder import UnresolvedQuery from sentry.search.events.types import WhereType -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import ( STRING_NOT_FOUND, resolve_tag_key, @@ -153,7 +153,7 @@ def transform_null_transaction_to_unparameterized(use_case_id, org_id, alias=Non def resolve_tags( - use_case_id: UseCaseKey, + use_case_id: UseCaseID, org_id: int, input_: Any, projects: Sequence[Project], @@ -563,7 +563,7 @@ def get_date_range(params: Mapping) -> Tuple[datetime, datetime, int]: return start, end, interval -def parse_tag(use_case_id: UseCaseKey, org_id: int, tag_string: str) -> str: +def parse_tag(use_case_id: UseCaseID, org_id: int, tag_string: str) -> str: tag_key = int(tag_string.replace("tags_raw[", "").replace("tags[", "").replace("]", "")) return reverse_resolve(use_case_id, org_id, tag_key) @@ -726,7 +726,7 @@ def __init__( self, projects: Sequence[Project], metrics_query: MetricsQuery, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, ): self._projects = projects self._metrics_query = metrics_query @@ -738,7 +738,7 @@ def __init__( @staticmethod def generate_snql_for_action_by_fields( metric_action_by_field: MetricActionByField, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, org_id: int, projects: Sequence[Project], is_column: bool = False, @@ -951,7 +951,7 @@ def __build_totals_and_series_queries( if self._metrics_query.include_series: series_limit = limit.limit * intervals_len - if self._use_case_id == UseCaseKey.PERFORMANCE: + if self._use_case_id is UseCaseID.TRANSACTIONS: time_groupby_column = self.__generate_time_groupby_column_for_discover_queries( self._metrics_query.interval ) @@ -1125,7 +1125,7 @@ def __init__( fields_in_entities: dict, intervals: List[datetime], results, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, ): self._organization_id = organization_id self._intervals = intervals diff --git a/src/sentry/testutils/cases.py b/src/sentry/testutils/cases.py index b0ac23bd375aa2..80bf967cd68cc1 100644 --- a/src/sentry/testutils/cases.py +++ b/src/sentry/testutils/cases.py @@ -3,7 +3,7 @@ import responses import sentry_kafka_schemas -from sentry.sentry_metrics.use_case_id_registry import REVERSE_METRIC_PATH_MAPPING, UseCaseID +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.utils.dates import to_timestamp __all__ = ( @@ -137,7 +137,6 @@ SPAN_METRICS_MAP, ) from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.snuba.metrics.datasource import get_series from sentry.tagstore.snuba import SnubaTagStorage from sentry.testutils.factories import get_fixture_path @@ -1267,7 +1266,7 @@ def push(type, mri: str, tags, value): else to_timestamp(session["started"]) ), value, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) # seq=0 is equivalent to relay's session.init, init=True is transformed @@ -1313,14 +1312,14 @@ def store_metric( tags: Dict[str, str], timestamp: int, value, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, ): mapping_meta = {} def metric_id(key: str): assert isinstance(key, str) res = indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[use_case_id], + use_case_id=use_case_id, org_id=org_id, string=key, ) @@ -1331,7 +1330,7 @@ def metric_id(key: str): def tag_key(name): assert isinstance(name, str) res = indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[use_case_id], + use_case_id=use_case_id, org_id=org_id, string=name, ) @@ -1342,11 +1341,11 @@ def tag_key(name): def tag_value(name): assert isinstance(name, str) - if use_case_id == UseCaseKey.PERFORMANCE: + if use_case_id == UseCaseID.TRANSACTIONS: return name res = indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[use_case_id], + use_case_id=use_case_id, org_id=org_id, string=name, ) @@ -1375,13 +1374,13 @@ def tag_value(name): # making up a sentry_received_timestamp, but it should be sometime # after the timestamp of the event "sentry_received_timestamp": timestamp + 10, - "version": 2 if use_case_id == UseCaseKey.PERFORMANCE else 1, + "version": 2 if use_case_id == UseCaseID.TRANSACTIONS else 1, } msg["mapping_meta"] = {} msg["mapping_meta"][msg["type"]] = mapping_meta - if use_case_id == UseCaseKey.PERFORMANCE: + if use_case_id == UseCaseID.TRANSACTIONS: entity = f"generic_metrics_{type}s" else: entity = f"metrics_{type}s" @@ -1453,7 +1452,7 @@ def _store_metric( name: str, tags: Dict[str, str], value: int, - use_case_id: UseCaseKey, + use_case_id: UseCaseID, type: Optional[str] = None, org_id: Optional[int] = None, project_id: Optional[int] = None, @@ -1543,7 +1542,7 @@ def store_performance_metric( value=value, org_id=org_id, project_id=project_id, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, days_before_now=days_before_now, hours_before_now=hours_before_now, minutes_before_now=minutes_before_now, @@ -1570,7 +1569,7 @@ def store_release_health_metric( value=value, org_id=org_id, project_id=project_id, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, days_before_now=days_before_now, hours_before_now=hours_before_now, minutes_before_now=minutes_before_now, @@ -1668,7 +1667,7 @@ def store_transaction_metric( tags: Optional[Dict[str, str]] = None, timestamp: Optional[datetime] = None, project: Optional[int] = None, - use_case_id: UseCaseKey = UseCaseKey.PERFORMANCE, + use_case_id: UseCaseID = UseCaseID.TRANSACTIONS, ): internal_metric = METRICS_MAP[metric] if internal_metric is None else internal_metric entity = self.ENTITY_MAP[metric] if entity is None else entity @@ -1696,7 +1695,7 @@ def store_transaction_metric( tags, int(metric_timestamp), subvalue, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) def store_span_metric( @@ -1708,7 +1707,7 @@ def store_span_metric( tags: Optional[Dict[str, str]] = None, timestamp: Optional[datetime] = None, project: Optional[int] = None, - use_case_id: UseCaseKey = UseCaseKey.PERFORMANCE, # TODO(wmak): this needs to be the span id + use_case_id: UseCaseID = UseCaseID.TRANSACTIONS, # TODO(wmak): this needs to be the span id ): internal_metric = SPAN_METRICS_MAP[metric] if internal_metric is None else internal_metric entity = self.ENTITY_MAP[metric] if entity is None else entity @@ -1736,7 +1735,7 @@ def store_span_metric( tags, int(metric_timestamp), subvalue, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) def wait_for_metric_count( @@ -1763,7 +1762,7 @@ def wait_for_metric_count( data = get_series( [project], metrics_query=metrics_query, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) count = data["groups"][0]["totals"][f"count({metric})"] if count >= total: @@ -2475,7 +2474,7 @@ def setUp(self): }, type="counter", value=1, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) self.store_metric( org_id=org_id, @@ -2485,7 +2484,7 @@ def setUp(self): tags={"tag3": "value3"}, type="counter", value=1, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) self.store_metric( org_id=org_id, @@ -2499,7 +2498,7 @@ def setUp(self): }, type="set", value=123, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) self.store_metric( org_id=org_id, @@ -2509,7 +2508,7 @@ def setUp(self): tags={}, type="set", value=123, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) diff --git a/src/sentry/utils/pytest/metrics.py b/src/sentry/utils/pytest/metrics.py index f3c77ddf304f83..3a12f92ae460c6 100644 --- a/src/sentry/utils/pytest/metrics.py +++ b/src/sentry/utils/pytest/metrics.py @@ -3,6 +3,8 @@ import pytest +from sentry.sentry_metrics.use_case_id_registry import UseCaseID + STRINGS_THAT_LOOK_LIKE_TAG_VALUES = ( "", "staging", @@ -20,7 +22,6 @@ @pytest.fixture(autouse=True) def control_metrics_access(monkeypatch, request, set_sentry_option): from sentry.sentry_metrics import indexer - from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.indexer.mock import MockIndexer from sentry.snuba import tasks from sentry.utils import snuba @@ -42,7 +43,7 @@ def control_metrics_access(monkeypatch, request, set_sentry_option): def new_resolve(use_case_id, org_id, string): if ( - use_case_id == UseCaseKey.PERFORMANCE + use_case_id == UseCaseID.TRANSACTIONS and string in STRINGS_THAT_LOOK_LIKE_TAG_VALUES ): pytest.fail( diff --git a/tests/sentry/api/endpoints/test_organization_metric_data.py b/tests/sentry/api/endpoints/test_organization_metric_data.py index 81f63a0c08ddb1..6a12113d259887 100644 --- a/tests/sentry/api/endpoints/test_organization_metric_data.py +++ b/tests/sentry/api/endpoints/test_organization_metric_data.py @@ -70,7 +70,7 @@ def test_incorrect_use_case_id_value(self): assert response.status_code == 400 assert ( response.json()["detail"] - == "Invalid useCase parameter. Please use one of: ['release-health', 'performance']" + == f"Invalid useCase parameter. Please use one of: {[uc.value for uc in UseCaseID]}" ) def test_invalid_field(self): @@ -438,7 +438,7 @@ def test_pagination_limit_without_orderby(self): field=f"count({TransactionMetricKey.MEASUREMENTS_LCP.value})", groupBy="transaction", per_page=2, - useCase="performance", + useCase="transactions", ) assert response.status_code == 200 @@ -476,7 +476,7 @@ def test_pagination_offset_without_orderby(self): groupBy="transaction", cursor=Cursor(0, 1), statsPeriod="1h", - useCase="performance", + useCase="transactions", ) assert response.status_code == 200, response.data @@ -536,7 +536,7 @@ def test_max_and_min_on_distributions(self): statsPeriod="1h", interval="1h", per_page=3, - useCase="performance", + useCase="transactions", includeSeries="0", ) groups = response.data["groups"] @@ -572,7 +572,7 @@ def test_orderby(self): groupBy="transaction", orderBy=f"-count({TransactionMetricKey.MEASUREMENTS_LCP.value})", per_page=2, - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 2 @@ -617,7 +617,7 @@ def test_multi_field_orderby(self): f"-count({TransactionMetricKey.MEASUREMENTS_FCP.value})", ], per_page=2, - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 2 @@ -657,7 +657,7 @@ def test_orderby_percentile(self): interval="1h", groupBy="tag1", orderBy=f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 2 @@ -696,7 +696,7 @@ def test_orderby_percentile_with_pagination(self): groupBy="tag1", orderBy=f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", per_page=1, - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 1 @@ -712,7 +712,7 @@ def test_orderby_percentile_with_pagination(self): orderBy=f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", per_page=1, cursor=Cursor(0, 1), - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 1 @@ -743,7 +743,7 @@ def test_limit_with_orderby_is_overridden_by_paginator_limit(self): groupBy="tag1", orderBy=f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", per_page=1, - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 1 @@ -768,7 +768,7 @@ def test_orderby_percentile_with_many_fields_one_entity_no_data(self): interval="1h", groupBy=["project_id", "transaction"], orderBy=f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 0 @@ -810,7 +810,7 @@ def test_orderby_percentile_with_many_fields_one_entity(self): interval="1h", groupBy=["project_id", "transaction"], orderBy=f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 2 @@ -868,7 +868,7 @@ def test_multi_field_orderby_percentile_with_many_fields_one_entity(self): statsPeriod="1h", interval="1h", groupBy=["project_id", "transaction"], - useCase="performance", + useCase="transactions", ) # Test order by DESC @@ -968,7 +968,7 @@ def test_orderby_percentile_with_many_fields_multiple_entities(self): interval="1h", groupBy=["project_id", "transaction"], orderBy=f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 2 @@ -1032,7 +1032,7 @@ def test_orderby_percentile_with_many_fields_multiple_entities_with_paginator(se "groupBy": ["project_id", "transaction"], "orderBy": f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", "per_page": 1, - "useCase": "performance", + "useCase": "transactions", } response = self.get_success_response(self.organization.slug, **request_args) @@ -1174,7 +1174,7 @@ def test_orderby_percentile_with_many_fields_multiple_entities_with_missing_data interval="1h", groupBy=["project_id", "transaction"], orderBy=f"p50({TransactionMetricKey.MEASUREMENTS_LCP.value})", - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] assert len(groups) == 2 @@ -1226,7 +1226,7 @@ def test_limit_without_orderby(self): interval="1h", groupBy="tag3", per_page=2, - useCase="performance", + useCase="transactions", ) groups = response.data["groups"] @@ -1549,7 +1549,7 @@ def test_derived_metric_incorrectly_defined_as_singular_entity( field=["crash_free_fake"], statsPeriod="6m", interval="1m", - useCase="release-health", + useCase="sessions", ) assert response.status_code == 400 assert response.json()["detail"] == ( @@ -2087,7 +2087,7 @@ def test_failure_rate_transaction(self): field=["transaction.failure_rate"], statsPeriod="1m", interval="1m", - useCase="performance", + useCase="transactions", ) assert len(response.data["groups"]) == 1 @@ -2121,7 +2121,7 @@ def test_failure_rate_without_transactions(self): field=["transaction.failure_rate"], statsPeriod="1m", interval="1m", - useCase="performance", + useCase="transactions", ) assert response.data["groups"] == [ @@ -2174,7 +2174,7 @@ def test_apdex_transactions(self): field=["transaction.apdex"], statsPeriod="1m", interval="1m", - useCase="performance", + useCase="transactions", ) assert len(response.data["groups"]) == 1 @@ -2204,7 +2204,7 @@ def test_miserable_users(self): field=["transaction.miserable_user"], statsPeriod="1m", interval="1m", - useCase="performance", + useCase="transactions", ) assert len(response.data["groups"]) == 1 @@ -2234,7 +2234,7 @@ def test_user_misery(self): field=["transaction.user_misery"], statsPeriod="1m", interval="1m", - useCase="performance", + useCase="transactions", ) assert len(response.data["groups"]) == 1 assert response.data["groups"][0]["totals"] == { diff --git a/tests/sentry/api/endpoints/test_organization_metric_details.py b/tests/sentry/api/endpoints/test_organization_metric_details.py index 97e2464d1e23f6..8f1728a53d59aa 100644 --- a/tests/sentry/api/endpoints/test_organization_metric_details.py +++ b/tests/sentry/api/endpoints/test_organization_metric_details.py @@ -5,7 +5,7 @@ import pytest from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.use_case_id_registry import UseCaseID, UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.metrics import SingularEntityDerivedMetric from sentry.snuba.metrics.fields.snql import complement, division_float from sentry.snuba.metrics.naming_layer.mapping import get_mri, get_public_name_from_mri @@ -261,7 +261,7 @@ def test_same_entity_multiple_metric_ids(self, mocked_derived_metrics): "release": "foow", }, value=5, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) response = self.get_success_response( self.organization.slug, diff --git a/tests/sentry/api/endpoints/test_organization_metric_tags.py b/tests/sentry/api/endpoints/test_organization_metric_tags.py index f83014523bb5ed..0a3e9b8f654405 100644 --- a/tests/sentry/api/endpoints/test_organization_metric_tags.py +++ b/tests/sentry/api/endpoints/test_organization_metric_tags.py @@ -79,7 +79,7 @@ def test_mri_metric_tags(self): response = self.get_success_response( self.organization.slug, metric=["d:transactions/duration@millisecond", "d:sessions/duration.exited@second"], - useCase="performance", + useCase="transactions", ) assert response.data == [] diff --git a/tests/sentry/api/endpoints/test_organization_metrics.py b/tests/sentry/api/endpoints/test_organization_metrics.py index 9efd67da43d788..3af889d0ea6053 100644 --- a/tests/sentry/api/endpoints/test_organization_metrics.py +++ b/tests/sentry/api/endpoints/test_organization_metrics.py @@ -172,7 +172,7 @@ def test_metrics_index(self): because the setUp bypasses it. """ response = self.get_success_response( - self.organization.slug, project=[self.project.id], useCase=["release-health"] + self.organization.slug, project=[self.project.id], useCase=["sessions"] ) assert type(response.data) == list diff --git a/tests/sentry/search/events/builder/test_metrics.py b/tests/sentry/search/events/builder/test_metrics.py index d5398d67e977db..689d9930e236ac 100644 --- a/tests/sentry/search/events/builder/test_metrics.py +++ b/tests/sentry/search/events/builder/test_metrics.py @@ -17,7 +17,6 @@ ) from sentry.search.events.types import HistogramParams from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import resolve_tag_value from sentry.snuba.dataset import Dataset @@ -144,7 +143,7 @@ def build_transaction_transform(self, alias): "transform", [ Column( - f"tags_raw[{indexer.resolve(UseCaseKey.PERFORMANCE, self.organization.id, 'transaction')}]" + f"tags_raw[{indexer.resolve(UseCaseID.TRANSACTIONS, self.organization.id, 'transaction')}]" ), [""], ["<< unparameterized >>"], @@ -263,7 +262,7 @@ def test_percentile_function(self): [ Column("metric_id"), indexer.resolve( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, constants.METRICS_MAP["transaction.duration"], ), @@ -334,7 +333,7 @@ def test_p100(self): [ Column("metric_id"), indexer.resolve( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, constants.METRICS_MAP["transaction.duration"], ), @@ -384,7 +383,7 @@ def test_transaction_filter(self): selected_columns=["transaction", "project", "p95(transaction.duration)"], ) transaction_name = resolve_tag_value( - UseCaseKey.PERFORMANCE, self.organization.id, "foo_transaction" + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction" ) transaction = self.build_transaction_transform("transaction") self.assertCountEqual( @@ -404,10 +403,10 @@ def test_transaction_in_filter(self): selected_columns=["transaction", "project", "p95(transaction.duration)"], ) transaction_name1 = resolve_tag_value( - UseCaseKey.PERFORMANCE, self.organization.id, "foo_transaction" + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction" ) transaction_name2 = resolve_tag_value( - UseCaseKey.PERFORMANCE, self.organization.id, "bar_transaction" + UseCaseID.TRANSACTIONS, self.organization.id, "bar_transaction" ) transaction = self.build_transaction_transform("transaction") self.assertCountEqual( @@ -653,7 +652,7 @@ def test_run_query(self): assert len(result["data"]) == 1 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -695,7 +694,7 @@ def test_run_query_multiple_tables(self): assert len(result["data"]) == 1 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -729,7 +728,7 @@ def test_run_query_with_multiple_groupby_orderby_distribution(self): assert len(result["data"]) == 2 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -739,7 +738,7 @@ def test_run_query_with_multiple_groupby_orderby_distribution(self): } assert result["data"][1] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "bar_transaction", ), @@ -775,7 +774,7 @@ def test_run_query_with_multiple_groupby_orderby_set(self): assert len(result["data"]) == 2 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "bar_transaction", ), @@ -785,7 +784,7 @@ def test_run_query_with_multiple_groupby_orderby_set(self): } assert result["data"][1] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -829,7 +828,7 @@ def test_run_query_with_project_orderby(self): assert len(result["data"]) == 2 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -838,7 +837,7 @@ def test_run_query_with_project_orderby(self): } assert result["data"][1] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -860,7 +859,7 @@ def test_run_query_with_project_orderby(self): assert len(result["data"]) == 2 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -869,7 +868,7 @@ def test_run_query_with_project_orderby(self): } assert result["data"][1] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -899,7 +898,7 @@ def test_run_query_with_transactions_orderby(self): assert len(result["data"]) == 3 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "zzz", ), @@ -909,7 +908,7 @@ def test_run_query_with_transactions_orderby(self): assert result["data"][1] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "bbb", ), @@ -1098,7 +1097,7 @@ def test_run_query_with_multiple_groupby_orderby_null_values_in_second_entity(se assert len(result["data"]) == 3 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "bar_transaction", ), @@ -1108,7 +1107,7 @@ def test_run_query_with_multiple_groupby_orderby_null_values_in_second_entity(se } assert result["data"][1] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -1118,7 +1117,7 @@ def test_run_query_with_multiple_groupby_orderby_null_values_in_second_entity(se } assert result["data"][2] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "baz_transaction", ), @@ -1160,7 +1159,7 @@ def test_run_query_with_multiple_groupby_orderby_null_values_in_first_entity(sel assert len(result["data"]) == 3 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "baz_transaction", ), @@ -1169,7 +1168,7 @@ def test_run_query_with_multiple_groupby_orderby_null_values_in_first_entity(sel } assert result["data"][1] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction", ), @@ -1179,7 +1178,7 @@ def test_run_query_with_multiple_groupby_orderby_null_values_in_first_entity(sel } assert result["data"][2] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "bar_transaction", ), @@ -1280,7 +1279,7 @@ def test_aggregate_query_with_multiple_entities_without_orderby(self): assert len(result["data"]) == 1 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "baz_transaction", ), @@ -1334,7 +1333,7 @@ def test_aggregate_query_with_multiple_entities_with_orderby(self): assert len(result["data"]) == 1 assert result["data"][0] == { "transaction": resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, "baz_transaction", ), @@ -1673,10 +1672,10 @@ def test_transaction_in_filter(self): selected_columns=["p95(transaction.duration)"], ) transaction_name1 = resolve_tag_value( - UseCaseKey.PERFORMANCE, self.organization.id, "foo_transaction" + UseCaseID.TRANSACTIONS, self.organization.id, "foo_transaction" ) transaction_name2 = resolve_tag_value( - UseCaseKey.PERFORMANCE, self.organization.id, "bar_transaction" + UseCaseID.TRANSACTIONS, self.organization.id, "bar_transaction" ) transaction = self.build_transaction_transform("transaction") diff --git a/tests/sentry/sentry_metrics/test_indexer.py b/tests/sentry/sentry_metrics/test_indexer.py index 4ecf6b204c1330..c1a0431ffc0b53 100644 --- a/tests/sentry/sentry_metrics/test_indexer.py +++ b/tests/sentry/sentry_metrics/test_indexer.py @@ -1,25 +1,25 @@ -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.indexer.mock import MockIndexer from sentry.sentry_metrics.indexer.strings import SHARED_STRINGS +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.metrics.naming_layer import SessionMRI INDEXER = MockIndexer() def test_resolve(): - assert INDEXER.resolve(UseCaseKey.RELEASE_HEALTH, 1, "what") is None + assert INDEXER.resolve(UseCaseID.SESSIONS, 1, "what") is None assert ( - INDEXER.resolve(UseCaseKey.RELEASE_HEALTH, 1, SessionMRI.USER.value) + INDEXER.resolve(UseCaseID.SESSIONS, 1, SessionMRI.USER.value) == SHARED_STRINGS[SessionMRI.USER.value] ) # hardcoded values don't depend on org_id assert ( - INDEXER.resolve(UseCaseKey.RELEASE_HEALTH, 0, SessionMRI.USER.value) + INDEXER.resolve(UseCaseID.SESSIONS, 0, SessionMRI.USER.value) == SHARED_STRINGS[SessionMRI.USER.value] ) def test_reverse_resolve(): - assert INDEXER.reverse_resolve(UseCaseKey.RELEASE_HEALTH, 1, 666) is None + assert INDEXER.reverse_resolve(UseCaseID.SESSIONS, 1, 666) is None id = SHARED_STRINGS[SessionMRI.USER.value] - assert INDEXER.reverse_resolve(UseCaseKey.RELEASE_HEALTH, 1, id) == SessionMRI.USER.value + assert INDEXER.reverse_resolve(UseCaseID.SESSIONS, 1, id) == SessionMRI.USER.value diff --git a/tests/sentry/snuba/metrics/fields/test_base.py b/tests/sentry/snuba/metrics/fields/test_base.py index 89aae3a957d4f8..81d531abcd5b5b 100644 --- a/tests/sentry/snuba/metrics/fields/test_base.py +++ b/tests/sentry/snuba/metrics/fields/test_base.py @@ -7,8 +7,7 @@ from snuba_sdk import Column, Direction, Function, OrderBy from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey -from sentry.sentry_metrics.use_case_id_registry import REVERSE_METRIC_PATH_MAPPING +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import resolve_tag_value, resolve_weak from sentry.snuba.dataset import EntityKey from sentry.snuba.metrics import ( @@ -45,14 +44,12 @@ pytestmark = pytest.mark.sentry_metrics -def indexer_record(use_case_id: UseCaseKey, org_id: int, string: str) -> int: - return indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[use_case_id], org_id=org_id, string=string - ) +def indexer_record(use_case_id: UseCaseID, org_id: int, string: str) -> int: + return indexer.record(use_case_id=use_case_id, org_id=org_id, string=string) -perf_indexer_record = partial(indexer_record, UseCaseKey.PERFORMANCE) -rh_indexer_record = partial(indexer_record, UseCaseKey.RELEASE_HEALTH) +perf_indexer_record = partial(indexer_record, UseCaseID.TRANSACTIONS) +rh_indexer_record = partial(indexer_record, UseCaseID.SESSIONS) def get_entity_of_metric_mocked(_, metric_mri, use_case_id): @@ -111,7 +108,7 @@ def test_get_entity_and_validate_dependency_tree_of_a_single_entity_derived_metr RawMetric that belong to the same entity - Return the entity of that derived metric """ - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS expected_derived_metrics_entities = { SessionMRI.ALL.value: "metrics_counters", SessionMRI.ALL_USER.value: "metrics_sets", @@ -151,7 +148,7 @@ def test_generate_select_snql_of_derived_metric(self): required to query for the instance of DerivedMetric """ org_id = self.project.organization_id - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS for status in ("init", "abnormal", "crashed", "errored"): rh_indexer_record(org_id, status) session_ids = [rh_indexer_record(org_id, SessionMRI.SESSION.value)] @@ -290,7 +287,7 @@ def test_generate_metric_ids(self): session_metric_id = rh_indexer_record(org_id, SessionMRI.SESSION.value) session_error_metric_id = rh_indexer_record(org_id, SessionMRI.ERROR.value) session_user_id = rh_indexer_record(org_id, SessionMRI.USER.value) - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS for derived_metric_mri in [ SessionMRI.ALL.value, @@ -326,7 +323,7 @@ def test_generate_metric_ids(self): mocked_mri_resolver(["crash_free_fake"], get_public_name_from_mri), ) def test_generate_order_by_clause(self): - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS for derived_metric_mri in MOCKED_DERIVED_METRICS.keys(): if derived_metric_mri == self.crash_free_fake.metric_mri: @@ -417,7 +414,7 @@ def test_get_entity(self): of SingleEntityDerivedMetric, we are still validating that they exist """ assert self.sessions_errored.get_entity( - projects=[PseudoProject(1, 1)], use_case_id=UseCaseKey.RELEASE_HEALTH + projects=[PseudoProject(1, 1)], use_case_id=UseCaseID.SESSIONS ) == { "metrics_counters": [ SessionMRI.ERRORED_PREAGGREGATED.value, @@ -430,7 +427,7 @@ def test_get_entity(self): "sentry.snuba.metrics.fields.base._get_entity_of_metric_mri", get_entity_of_metric_mocked ) def test_get_entity_and_validate_dependency_tree_of_single_entity_constituents(self): - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS assert self.sessions_errored.get_entity(projects=[1], use_case_id=use_case_id) == { "metrics_counters": [ @@ -451,15 +448,13 @@ def test_get_entity_and_validate_dependency_tree_of_single_entity_constituents(s def test_generate_metric_ids(self): with pytest.raises(NotSupportedOverCompositeEntityException): - self.sessions_errored.generate_metric_ids( - projects=[1], use_case_id=UseCaseKey.RELEASE_HEALTH - ) + self.sessions_errored.generate_metric_ids(projects=[1], use_case_id=UseCaseID.SESSIONS) def test_generate_select_snql_of_derived_metric(self): with pytest.raises(NotSupportedOverCompositeEntityException): self.sessions_errored.generate_select_statements( projects=[1], - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, alias="test", ) @@ -468,7 +463,7 @@ def test_generate_orderby_clause(self): self.sessions_errored.generate_orderby_clause( direction=Direction.ASC, projects=[1], - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, alias="test", ) @@ -603,7 +598,7 @@ def test_run_post_query_function(self): class DerivedMetricAliasTestCase(TestCase): def test_session_duration_derived_alias(self): org_id = self.project.organization_id - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS session_duration_derived_alias = DERIVED_ALIASES[SessionMRI.DURATION.value] assert session_duration_derived_alias.generate_filter_snql_conditions( org_id, use_case_id diff --git a/tests/sentry/snuba/metrics/test_metrics_layer/test_metrics_enhanced_performance.py b/tests/sentry/snuba/metrics/test_metrics_layer/test_metrics_enhanced_performance.py index 31b877a6e804a6..44777edde50e16 100644 --- a/tests/sentry/snuba/metrics/test_metrics_layer/test_metrics_enhanced_performance.py +++ b/tests/sentry/snuba/metrics/test_metrics_layer/test_metrics_enhanced_performance.py @@ -20,7 +20,6 @@ TransactionMetric, ) from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.metrics import ( MAX_POINTS, @@ -65,7 +64,7 @@ def test_valid_filter_include_meta_derived_metrics(self): [self.project], query.to_metrics_query(), include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert data["meta"] == sorted( [ @@ -123,7 +122,7 @@ def test_apdex_transaction_threshold(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = sorted(data["groups"], key=lambda group: group["by"]["transaction_group"]) assert len(groups) == 2 @@ -181,7 +180,7 @@ def test_apdex_project_threshold(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = sorted(data["groups"], key=lambda group: group["by"]["transaction_group"]) assert len(groups) == 2 @@ -254,7 +253,7 @@ def test_alias_on_different_metrics_expression(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] assert len(groups) == 2 @@ -333,7 +332,7 @@ def test_alias_on_same_metrics_expression_but_different_aliases(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] assert len(groups) == 2 @@ -388,7 +387,7 @@ def test_custom_measurement_query_with_valid_mri(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] assert len(groups) == 1 @@ -445,7 +444,7 @@ def test_custom_measurement_query_with_invalid_mri(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) def test_query_with_order_by_valid_str_field(self): @@ -497,7 +496,7 @@ def test_query_with_order_by_valid_str_field(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] @@ -554,7 +553,7 @@ def test_query_with_order_by_invalid_str_field(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) def test_query_with_order_by_str_field_not_in_group_by(self): @@ -590,7 +589,7 @@ def test_query_with_order_by_str_field_not_in_group_by(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) def test_query_with_sum_if_column(self): @@ -622,7 +621,7 @@ def test_query_with_sum_if_column(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] @@ -669,7 +668,7 @@ def test_query_with_uniq_if_column(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] @@ -731,7 +730,7 @@ def test_query_with_tuple_condition(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] @@ -798,7 +797,7 @@ def test_query_with_has_condition(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] @@ -867,7 +866,7 @@ def test_count_transaction_with_valid_condition(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] @@ -934,7 +933,7 @@ def test_count_transaction_with_invalid_condition(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) def test_alias_on_single_entity_derived_metrics(self): @@ -970,7 +969,7 @@ def test_alias_on_single_entity_derived_metrics(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert len(data["groups"]) == 1 group = data["groups"][0] @@ -1038,7 +1037,7 @@ def test_groupby_aliasing_with_multiple_groups_and_orderby(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) groups = data["groups"] @@ -1122,7 +1121,7 @@ def test_histogram_transaction_duration(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert data["groups"] == [ @@ -1168,7 +1167,7 @@ def test_rate_epm_hour_rollup(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) # The order they will be in is the reverse of the order they were inserted so -> [3, 0, 3, 6, 0, 6] and hence @@ -1216,7 +1215,7 @@ def test_rate_epm_day_rollup(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) # The order they will be in is the reverse of the order they were inserted so -> [3, 0, 3, 6, 0, 6] and hence @@ -1281,7 +1280,7 @@ def test_throughput_epm_hour_rollup_offset_of_hour(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert data == { "start": FakeDatetime( @@ -1354,7 +1353,7 @@ def test_throughput_eps_minute_rollup(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) # The order they will be in is the reverse of the order they were inserted so -> [3, 0, 3, 6, 0, 6] and hence # the expected rates would be each of those event counts divided by 86400 / 60 @@ -1413,7 +1412,7 @@ def test_rate_with_missing_numerator_value(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) def test_measurement_rating(self): @@ -1504,7 +1503,7 @@ def test_measurement_rating(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) group_totals = data["groups"][0]["totals"] @@ -1606,7 +1605,7 @@ def test_team_key_transactions_my_teams(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert data["groups"] == [ { @@ -1670,7 +1669,7 @@ def test_unparameterized_transactions_in_where(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert data["groups"] == [ @@ -1721,7 +1720,7 @@ def test_unparameterized_transactions_in_groupby(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert sorted(data["groups"], key=lambda group: group["by"]["transaction_name"]) == [ @@ -1821,7 +1820,7 @@ def test_wildcard_match_with_filterable_tags(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert data["groups"] == [ @@ -1870,7 +1869,7 @@ def test_wildcard_match_with_non_filterable_tags(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) def test_team_key_transaction_as_condition(self): @@ -1972,7 +1971,7 @@ def test_team_key_transaction_as_condition(self): [self.project], metrics_query=metrics_query, include_meta=False, - use_case_id=UseCaseKey.PERFORMANCE, + use_case_id=UseCaseID.TRANSACTIONS, ) assert data["groups"] == [ { @@ -2087,7 +2086,7 @@ def test_simple(self): ], "unit": "millisecond", "metric_id": indexer.resolve( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, something_custom_metric, ), @@ -2141,7 +2140,7 @@ def test_metric_outside_query_daterange(self): ], "unit": "millisecond", "metric_id": indexer.resolve( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.organization.id, something_custom_metric, ), diff --git a/tests/sentry/snuba/metrics/test_metrics_layer/test_release_health.py b/tests/sentry/snuba/metrics/test_metrics_layer/test_release_health.py index 1cf1b93613c98d..d2af562e573137 100644 --- a/tests/sentry/snuba/metrics/test_metrics_layer/test_release_health.py +++ b/tests/sentry/snuba/metrics/test_metrics_layer/test_release_health.py @@ -8,7 +8,7 @@ from freezegun import freeze_time from snuba_sdk import Column, Condition, Limit, Offset, Op -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.metrics import MetricField, MetricGroupByField from sentry.snuba.metrics.datasource import get_series from sentry.snuba.metrics.naming_layer import SessionMRI @@ -48,7 +48,7 @@ def test_valid_filter_include_meta(self): [self.project], query.to_metrics_query(), include_meta=True, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) assert data["meta"] == sorted( [ @@ -76,7 +76,7 @@ def test_validate_include_meta_computes_meta_for_composite_derived_metrics(self) [self.project], query.to_metrics_query(), include_meta=True, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, )["meta"] == sorted( [ {"name": "session.errored", "type": "Float64"}, @@ -123,7 +123,7 @@ def test_alias_on_composite_entity_derived_metric(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) group = data["groups"][0] assert group["totals"]["errored_sessions_alias"] == 7 @@ -170,7 +170,7 @@ def test_aliasing_behavior_on_derived_op_and_derived_alias(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) hist = [(2.0, 5.5, 4), (5.5, 9.0, 4)] @@ -203,7 +203,7 @@ def test_aliasing_behavior_on_derived_op_and_derived_alias(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) hist = [(2.0, 4.0, 2), (4.0, 6.0, 3)] @@ -254,7 +254,7 @@ def test_anr_rate_operations(self): [self.project], metrics_query=metrics_query, include_meta=True, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) group = data["groups"][0] assert group["totals"]["anr_alias"] == 0.5 @@ -303,7 +303,7 @@ def test_having(self): data = get_series( [self.project], metrics_query=metrics_query, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) groups = data["groups"] diff --git a/tests/sentry/snuba/metrics/test_query_builder.py b/tests/sentry/snuba/metrics/test_query_builder.py index 44c8380657b785..33d39c31740128 100644 --- a/tests/sentry/snuba/metrics/test_query_builder.py +++ b/tests/sentry/snuba/metrics/test_query_builder.py @@ -27,8 +27,7 @@ from sentry.api.utils import InvalidParams from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey -from sentry.sentry_metrics.use_case_id_registry import REVERSE_METRIC_PATH_MAPPING, UseCaseID +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import ( resolve, resolve_tag_key, @@ -93,7 +92,7 @@ class PseudoProject: BEG_1D_BEFORE_NOW = datetime(2021, 8, 25, 00, tzinfo=timezone.utc) ORG_ID = 1 -USE_CASE_ID = UseCaseKey.RELEASE_HEALTH +USE_CASE_ID = UseCaseID.SESSIONS def get_entity_of_metric_mocked(_, metric_name, use_case_id): @@ -252,7 +251,7 @@ def get_entity_of_metric_mocked(_, metric_name, use_case_id): ) def test_parse_query(query_string, expected): org_id = ORG_ID - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS for s in ("myapp@2.0.0", "/bar/:orgId/"): # will be values 10000, 10001 respectively indexer.record(use_case_id=UseCaseID.SESSIONS, org_id=org_id, string=s) @@ -371,11 +370,11 @@ def test_build_snuba_query(mock_now, mock_now2): having=having, ) snuba_queries, _ = SnubaQueryBuilder( - [PseudoProject(1, 1)], query_definition, use_case_id=UseCaseKey.RELEASE_HEALTH + [PseudoProject(1, 1)], query_definition, use_case_id=UseCaseID.SESSIONS ).get_snuba_queries() org_id = 1 - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS def expected_query(match, select, extra_groupby, metric_name): function, column, alias = select @@ -486,7 +485,7 @@ def expected_query(match, select, extra_groupby, metric_name): ) def test_build_snuba_query_mri(mock_now, mock_now2): org_id = 1 - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS # Your typical release health query querying everything query_params = MultiValueDict( { @@ -568,7 +567,7 @@ def test_build_snuba_query_mri(mock_now, mock_now2): ) def test_build_snuba_query_derived_metrics(mock_now, mock_now2): org_id = 1 - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS # Your typical release health query querying everything query_params = MultiValueDict( { @@ -745,11 +744,11 @@ def test_build_snuba_query_orderby(mock_now, mock_now2): [PseudoProject(1, 1)], query_params, paginator_kwargs={"limit": 3} ) snuba_queries, _ = SnubaQueryBuilder( - [PseudoProject(1, 1)], query_definition.to_metrics_query(), UseCaseKey.RELEASE_HEALTH + [PseudoProject(1, 1)], query_definition.to_metrics_query(), UseCaseID.SESSIONS ).get_snuba_queries() org_id = 1 - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS counter_queries = snuba_queries.pop("metrics_counters") assert not snuba_queries @@ -847,11 +846,11 @@ def test_build_snuba_query_with_derived_alias(mock_now, mock_now2): snuba_queries, _ = SnubaQueryBuilder( [PseudoProject(1, 1)], query_definition.to_metrics_query(), - UseCaseKey.RELEASE_HEALTH, + UseCaseID.SESSIONS, ).get_snuba_queries() org_id = 1 - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS distribution_queries = snuba_queries.pop("metrics_distributions") assert not snuba_queries @@ -1050,7 +1049,7 @@ def test_translate_results_derived_metrics(_1, _2): fields_in_entities, intervals, results, - UseCaseKey.RELEASE_HEALTH, + UseCaseID.SESSIONS, ).translate_result_groups() == [ { "by": {}, @@ -1072,7 +1071,7 @@ def test_translate_results_derived_metrics(_1, _2): @mock.patch("sentry.api.utils.timezone.now", return_value=MOCK_NOW) def test_translate_results_missing_slots(_1, _2): org_id = 1 - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS query_params = MultiValueDict( { "field": [ @@ -1310,7 +1309,7 @@ def test_translate_meta_result_type_composite_entity_derived_metric(_): [ MetricGroupByField(MetricField("sum", SessionMRI.SESSION.value)), ], - UseCaseKey.RELEASE_HEALTH, + UseCaseID.SESSIONS, re.escape("Cannot group by metrics expression sum(sentry.sessions.session)"), id="invalid grouping by metric expression - release_health", ), @@ -1321,7 +1320,7 @@ def test_translate_meta_result_type_composite_entity_derived_metric(_): [ MetricGroupByField(MetricField("count", TransactionMRI.DURATION.value)), ], - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, re.escape("Cannot group by metrics expression count(transaction.duration)"), id="invalid grouping by metric expression - performance", ), @@ -1332,7 +1331,7 @@ def test_translate_meta_result_type_composite_entity_derived_metric(_): [ MetricGroupByField(MetricField(None, TransactionMRI.FAILURE_RATE.value)), ], - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, "Cannot group by metric transaction.failure_rate", id="invalid grouping by derived metric - release_health", ), @@ -1343,7 +1342,7 @@ def test_translate_meta_result_type_composite_entity_derived_metric(_): [ MetricGroupByField(MetricField(None, SessionMRI.ERRORED.value)), ], - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, "Cannot group by metric session.errored", id="invalid grouping by composite entity derived metric - release_health", ), @@ -1364,7 +1363,7 @@ def test_translate_meta_result_type_composite_entity_derived_metric(_): ) ), ], - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, "", id="valid grouping by metrics expression", ), @@ -1405,7 +1404,7 @@ def test_only_can_groupby_operations_can_be_added_to_groupby( [ MetricConditionField(MetricField("sum", SessionMRI.SESSION.value), Op.GTE, 10), ], - UseCaseKey.RELEASE_HEALTH, + UseCaseID.SESSIONS, re.escape("Cannot filter by metrics expression sum(sentry.sessions.session)"), id="invalid filtering by metric expression - release_health", ), @@ -1416,7 +1415,7 @@ def test_only_can_groupby_operations_can_be_added_to_groupby( [ MetricConditionField(MetricField("count", TransactionMRI.DURATION.value), Op.LT, 2), ], - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, re.escape("Cannot filter by metrics expression count(transaction.duration)"), id="invalid filtering by metric expression - performance", ), @@ -1429,7 +1428,7 @@ def test_only_can_groupby_operations_can_be_added_to_groupby( MetricField(None, TransactionMRI.FAILURE_RATE.value), Op.EQ, 0.5 ), ], - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, "Cannot filter by metric transaction.failure_rate", id="invalid filtering by derived metric - release_health", ), @@ -1440,7 +1439,7 @@ def test_only_can_groupby_operations_can_be_added_to_groupby( [ MetricConditionField(MetricField(None, SessionMRI.ERRORED.value), Op.EQ, 7), ], - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, "Cannot filter by metric session.errored", id="invalid filtering by composite entity derived metric - release_health", ), @@ -1463,7 +1462,7 @@ def test_only_can_groupby_operations_can_be_added_to_groupby( 1, ), ], - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, "", id="valid filtering by metrics expression", ), @@ -1518,14 +1517,14 @@ def test_valid_latest_release_alias_filter(self): class ResolveTagsTestCase(TestCase): def setUp(self): self.org_id = ORG_ID - self.use_case_id = UseCaseKey.PERFORMANCE + self.use_case_id = UseCaseID.TRANSACTIONS def test_resolve_tags_with_unary_tuple(self): transactions = ["/foo", "/bar"] for transaction in ["transaction"] + transactions: indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[self.use_case_id], + use_case_id=self.use_case_id, org_id=self.org_id, string=transaction, ) @@ -1586,12 +1585,12 @@ def test_resolve_tags_with_binary_tuple(self): for transaction, platform in [("transaction", "platform")] + tags: indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[self.use_case_id], + use_case_id=self.use_case_id, org_id=self.org_id, string=transaction, ) indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[self.use_case_id], + use_case_id=self.use_case_id, org_id=self.org_id, string=platform, ) @@ -1660,7 +1659,7 @@ def test_resolve_tags_with_has(self): tag_key = "transaction" indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[self.use_case_id], + use_case_id=self.use_case_id, org_id=self.org_id, string=tag_key, ) @@ -1700,7 +1699,7 @@ def test_resolve_tags_with_has(self): def test_resolve_tags_with_match_and_filterable_tag(self): indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[self.use_case_id], + use_case_id=self.use_case_id, org_id=self.org_id, string="environment", ) @@ -1740,7 +1739,7 @@ def test_resolve_tags_with_match_and_filterable_tag(self): def test_resolve_tags_with_match_and_deep_filterable_tag(self): indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[self.use_case_id], + use_case_id=self.use_case_id, org_id=self.org_id, string="environment", ) @@ -1785,7 +1784,7 @@ def test_resolve_tags_with_match_and_deep_filterable_tag(self): def test_resolve_tags_with_match_and_non_filterable_tag(self): indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[self.use_case_id], + use_case_id=self.use_case_id, org_id=self.org_id, string="http_status_code", ) @@ -1816,7 +1815,7 @@ def test_resolve_tags_with_match_and_non_filterable_tag(self): def test_resolve_tags_with_match_and_deep_non_filterable_tag(self): indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[self.use_case_id], + use_case_id=self.use_case_id, org_id=self.org_id, string="http_status_code", ) @@ -1926,7 +1925,7 @@ def test_timestamp_operators(op: MetricOperationType, clickhouse_op: str): ) builder = SnubaQueryBuilder( - [PseudoProject(1, 1)], query_definition, use_case_id=UseCaseKey.RELEASE_HEALTH + [PseudoProject(1, 1)], query_definition, use_case_id=UseCaseID.SESSIONS ) snuba_queries, fields = builder.get_snuba_queries() @@ -1942,7 +1941,7 @@ def test_timestamp_operators(op: MetricOperationType, clickhouse_op: str): "equals", [ Column("metric_id"), - resolve_weak(UseCaseKey.RELEASE_HEALTH, org_id, SessionMRI.SESSION.value), + resolve_weak(UseCaseID.SESSIONS, org_id, SessionMRI.SESSION.value), ], ), ], @@ -1981,7 +1980,7 @@ def test_having_clause(include_totals, include_series): include_series=include_series, ) snuba_queries, _ = SnubaQueryBuilder( - [PseudoProject(1, 1)], query_definition, use_case_id=UseCaseKey.RELEASE_HEALTH + [PseudoProject(1, 1)], query_definition, use_case_id=UseCaseID.SESSIONS ).get_snuba_queries() queries = snuba_queries["metrics_counters"] diff --git a/tests/sentry/snuba/metrics/test_snql.py b/tests/sentry/snuba/metrics/test_snql.py index 84a18c4cd34e21..6eaa315561288e 100644 --- a/tests/sentry/snuba/metrics/test_snql.py +++ b/tests/sentry/snuba/metrics/test_snql.py @@ -10,7 +10,6 @@ get_project_threshold_cache_key, ) from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import resolve_tag_key, resolve_tag_value, resolve_weak from sentry.snuba.metrics import TransactionMRI @@ -109,12 +108,10 @@ def test_counter_sum_aggregation_on_session_status(self): [ Column( resolve_tag_key( - UseCaseKey.RELEASE_HEALTH, self.org_id, "session.status" + UseCaseID.SESSIONS, self.org_id, "session.status" ), ), - resolve_tag_value( - UseCaseKey.RELEASE_HEALTH, self.org_id, status - ), + resolve_tag_value(UseCaseID.SESSIONS, self.org_id, status), ], ), Function("in", [Column("metric_id"), list(self.metric_ids)]), @@ -142,12 +139,10 @@ def test_set_uniq_aggregation_on_session_status(self): [ Column( resolve_tag_key( - UseCaseKey.RELEASE_HEALTH, self.org_id, "session.status" + UseCaseID.SESSIONS, self.org_id, "session.status" ) ), - resolve_tag_value( - UseCaseKey.RELEASE_HEALTH, self.org_id, status - ), + resolve_tag_value(UseCaseID.SESSIONS, self.org_id, status), ], ), Function("in", [Column("metric_id"), list(self.metric_ids)]), @@ -207,12 +202,12 @@ def test_dist_count_aggregation_on_tx_status(self): ], ), resolve_weak( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionMRI.MEASUREMENTS_LCP.value, ), resolve_weak( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionMRI.DURATION.value, ), @@ -244,24 +239,24 @@ def test_dist_count_aggregation_on_tx_status(self): [ Column( resolve_tag_key( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionTagsKey.TRANSACTION_STATUS.value, ) ), [ resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionStatusTagValue.OK.value, ), resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionStatusTagValue.CANCELLED.value, ), resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionStatusTagValue.UNKNOWN.value, ), @@ -293,13 +288,13 @@ def test_set_count_aggregation_on_tx_satisfaction(self): [ Column( resolve_tag_key( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionTagsKey.TRANSACTION_SATISFACTION.value, ) ), resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionSatisfactionTagValue.FRUSTRATED.value, ), @@ -346,12 +341,12 @@ def test_dist_count_aggregation_on_tx_satisfaction(self): ], ), resolve_weak( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionMRI.MEASUREMENTS_LCP.value, ), resolve_weak( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionMRI.DURATION.value, ), @@ -364,13 +359,13 @@ def test_dist_count_aggregation_on_tx_satisfaction(self): [ Column( name=resolve_tag_key( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionTagsKey.TRANSACTION_SATISFACTION.value, ) ), resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionSatisfactionTagValue.SATISFIED.value, ), @@ -409,12 +404,12 @@ def test_dist_count_aggregation_on_tx_satisfaction(self): ], ), resolve_weak( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionMRI.MEASUREMENTS_LCP.value, ), resolve_weak( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionMRI.DURATION.value, ), @@ -427,13 +422,13 @@ def test_dist_count_aggregation_on_tx_satisfaction(self): [ Column( name=resolve_tag_key( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionTagsKey.TRANSACTION_SATISFACTION.value, ) ), resolve_tag_value( - UseCaseKey.PERFORMANCE, + UseCaseID.TRANSACTIONS, self.org_id, TransactionSatisfactionTagValue.TOLERATED.value, ), @@ -578,9 +573,9 @@ def test_session_duration_filters(self): "equals", ( Column( - resolve_tag_key(UseCaseKey.RELEASE_HEALTH, self.org_id, "session.status"), + resolve_tag_key(UseCaseID.SESSIONS, self.org_id, "session.status"), ), - resolve_tag_value(UseCaseKey.RELEASE_HEALTH, self.org_id, "exited"), + resolve_tag_value(UseCaseID.SESSIONS, self.org_id, "exited"), ), ) ] @@ -648,10 +643,10 @@ def test_count_web_vitals_snql(self): ( Column( resolve_tag_key( - UseCaseKey.PERFORMANCE, self.org_id, "measurement_rating" + UseCaseID.TRANSACTIONS, self.org_id, "measurement_rating" ) ), - resolve_tag_value(UseCaseKey.PERFORMANCE, self.org_id, "good"), + resolve_tag_value(UseCaseID.TRANSACTIONS, self.org_id, "good"), ), ), ], diff --git a/tests/sentry/snuba/test_entity_subscriptions.py b/tests/sentry/snuba/test_entity_subscriptions.py index 0483a0e9b5ac21..903b1580776005 100644 --- a/tests/sentry/snuba/test_entity_subscriptions.py +++ b/tests/sentry/snuba/test_entity_subscriptions.py @@ -11,7 +11,6 @@ ) from sentry.search.events.constants import METRICS_MAP from sentry.sentry_metrics import indexer -from sentry.sentry_metrics.configuration import UseCaseKey from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import resolve, resolve_tag_key, resolve_tag_value from sentry.snuba.dataset import Dataset, EntityKey @@ -153,7 +152,7 @@ def test_get_entity_subscription_for_metrics_dataset_missing_organization(self) # this should be removed. def test_get_entity_subscription_for_metrics_dataset_for_users(self) -> None: org_id = self.organization.id - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS aggregate = "percentage(users_crashed, users) AS _crash_rate_alert_aggregate" entity_subscription = get_entity_subscription( @@ -203,7 +202,7 @@ def test_get_entity_subscription_for_metrics_dataset_for_users(self) -> None: Column("metric_id"), Op.EQ, resolve( - UseCaseKey.RELEASE_HEALTH, + UseCaseID.SESSIONS, self.organization.id, entity_subscription.metric_key.value, ), @@ -213,7 +212,7 @@ def test_get_entity_subscription_for_metrics_dataset_for_users(self) -> None: def test_get_entity_subscription_for_metrics_dataset_for_users_with_metrics_layer(self) -> None: with Feature("organizations:use-metrics-layer"): org_id = self.organization.id - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS aggregate = "percentage(users_crashed, users) AS _crash_rate_alert_aggregate" entity_subscription = get_entity_subscription( @@ -276,7 +275,7 @@ def test_get_entity_subscription_for_metrics_dataset_for_users_with_metrics_laye # this should be removed. def test_get_entity_subscription_for_metrics_dataset_for_sessions(self) -> None: org_id = self.organization.id - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS aggregate = "percentage(sessions_crashed, sessions) AS _crash_rate_alert_aggregate" entity_subscription = get_entity_subscription( query_type=SnubaQuery.Type.CRASH_RATE, @@ -344,7 +343,7 @@ def test_get_entity_subscription_for_metrics_dataset_for_sessions_with_metrics_l ) -> None: with Feature("organizations:use-metrics-layer"): org_id = self.organization.id - use_case_id = UseCaseKey.RELEASE_HEALTH + use_case_id = UseCaseID.SESSIONS aggregate = "percentage(sessions_crashed, sessions) AS _crash_rate_alert_aggregate" entity_subscription = get_entity_subscription( query_type=SnubaQuery.Type.CRASH_RATE, @@ -468,7 +467,7 @@ def test_get_entity_subscription_for_performance_metrics_dataset(self) -> None: ).get_snql_query() metric_id = resolve( - UseCaseKey.PERFORMANCE, self.organization.id, METRICS_MAP["transaction.duration"] + UseCaseID.TRANSACTIONS, self.organization.id, METRICS_MAP["transaction.duration"] ) assert snql_query.query.select == [ @@ -525,7 +524,7 @@ def test_get_entity_subscription_for_performance_metrics_dataset_with_metrics_la ).get_snql_query() metric_id = resolve( - UseCaseKey.PERFORMANCE, self.organization.id, METRICS_MAP["transaction.duration"] + UseCaseID.TRANSACTIONS, self.organization.id, METRICS_MAP["transaction.duration"] ) assert snql_query.query.select == [ diff --git a/tests/sentry/snuba/test_tasks.py b/tests/sentry/snuba/test_tasks.py index f46679ab585e44..3d44959d5f873f 100644 --- a/tests/sentry/snuba/test_tasks.py +++ b/tests/sentry/snuba/test_tasks.py @@ -13,7 +13,7 @@ from sentry.search.events.constants import METRICS_MAP from sentry.sentry_metrics import indexer from sentry.sentry_metrics.configuration import UseCaseKey -from sentry.sentry_metrics.use_case_id_registry import REVERSE_METRIC_PATH_MAPPING +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.sentry_metrics.utils import resolve, resolve_tag_key, resolve_tag_value from sentry.snuba.dataset import Dataset from sentry.snuba.entity_subscription import ( @@ -39,16 +39,16 @@ pytestmark = pytest.mark.sentry_metrics -def indexer_record(use_case_id: UseCaseKey, org_id: int, string: str) -> int: +def indexer_record(use_case_id: UseCaseID, org_id: int, string: str) -> int: return indexer.record( - use_case_id=REVERSE_METRIC_PATH_MAPPING[use_case_id], + use_case_id=use_case_id, org_id=org_id, string=string, ) -perf_indexer_record = partial(indexer_record, UseCaseKey.PERFORMANCE) -rh_indexer_record = partial(indexer_record, UseCaseKey.RELEASE_HEALTH) +perf_indexer_record = partial(indexer_record, UseCaseID.TRANSACTIONS) +rh_indexer_record = partial(indexer_record, UseCaseID.SESSIONS) class BaseSnubaTaskTest(metaclass=abc.ABCMeta): @@ -602,7 +602,7 @@ def test_simple_performance_transactions(self): def test_simple_performance_metrics(self): with Feature("organizations:use-metrics-layer"): - metric_id = resolve(UseCaseKey.PERFORMANCE, self.organization.id, METRICS_MAP["user"]) + metric_id = resolve(UseCaseID.TRANSACTIONS, self.organization.id, METRICS_MAP["user"]) self.run_test( SnubaQuery.Type.PERFORMANCE, Dataset.Metrics, @@ -664,7 +664,7 @@ def test_aliased_query_performance_metrics(self): version = "something" self.create_release(self.project, version=version) metric_id = resolve( - UseCaseKey.PERFORMANCE, self.organization.id, METRICS_MAP["transaction.duration"] + UseCaseID.TRANSACTIONS, self.organization.id, METRICS_MAP["transaction.duration"] ) perf_indexer_record(self.organization.id, "release") perf_indexer_record(self.organization.id, version) @@ -673,10 +673,10 @@ def test_aliased_query_performance_metrics(self): Condition(Column("project_id"), Op.IN, [self.project.id]), Condition( Column( - resolve_tag_key(UseCaseKey.PERFORMANCE, self.organization.id, "release") + resolve_tag_key(UseCaseID.TRANSACTIONS, self.organization.id, "release") ), Op.EQ, - resolve_tag_value(UseCaseKey.PERFORMANCE, self.organization.id, version), + resolve_tag_value(UseCaseID.TRANSACTIONS, self.organization.id, version), ), Condition(Column("metric_id"), Op.IN, [metric_id]), ] @@ -736,7 +736,7 @@ def test_tag_query_performance_metrics(self): # Note: We don't support user queries on the performance metrics dataset, so using a # different tag here. metric_id = resolve( - UseCaseKey.PERFORMANCE, self.organization.id, METRICS_MAP["transaction.duration"] + UseCaseID.TRANSACTIONS, self.organization.id, METRICS_MAP["transaction.duration"] ) tag_key = "some_tag" tag_value = "some_value" @@ -747,9 +747,9 @@ def test_tag_query_performance_metrics(self): Condition(Column("org_id"), Op.EQ, self.organization.id), Condition(Column("project_id"), Op.IN, [self.project.id]), Condition( - Column(resolve_tag_key(UseCaseKey.PERFORMANCE, self.organization.id, tag_key)), + Column(resolve_tag_key(UseCaseID.TRANSACTIONS, self.organization.id, tag_key)), Op.EQ, - resolve_tag_value(UseCaseKey.PERFORMANCE, self.organization.id, tag_value), + resolve_tag_value(UseCaseID.TRANSACTIONS, self.organization.id, tag_value), ), Condition(Column("metric_id"), Op.IN, [metric_id]), ] diff --git a/tests/snuba/sessions/test_sessions.py b/tests/snuba/sessions/test_sessions.py index d6deb5d690a780..8199c5e438f5eb 100644 --- a/tests/snuba/sessions/test_sessions.py +++ b/tests/snuba/sessions/test_sessions.py @@ -9,7 +9,7 @@ from sentry.release_health.base import OverviewStat from sentry.release_health.metrics import MetricsReleaseHealthBackend from sentry.release_health.sessions import SessionsReleaseHealthBackend -from sentry.sentry_metrics.configuration import UseCaseKey +from sentry.sentry_metrics.use_case_id_registry import UseCaseID from sentry.snuba.sessions import _make_stats from sentry.testutils.cases import BaseMetricsTestCase, SnubaTestCase, TestCase from sentry.testutils.silo import region_silo_test @@ -1159,7 +1159,7 @@ def test_with_other_metrics(self): tags={}, type="set", value=value, - use_case_id=UseCaseKey.RELEASE_HEALTH, + use_case_id=UseCaseID.SESSIONS, ) assert (