Skip to content

Commit fed05aa

Browse files
author
Riccardo Busetti
authored
feat(use-case): Add visibility modifiers to use cases and use internal MRI parser (#67774)
1 parent 240c808 commit fed05aa

File tree

7 files changed

+49
-20
lines changed

7 files changed

+49
-20
lines changed

src/sentry/search/events/builder/metrics.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
WhereType,
5353
)
5454
from sentry.sentry_metrics import indexer
55-
from sentry.sentry_metrics.use_case_id_registry import UseCaseID, extract_use_case_id
55+
from sentry.sentry_metrics.use_case_id_registry import UseCaseID
5656
from sentry.snuba.dataset import Dataset
5757
from sentry.snuba.discover import create_result_key
5858
from sentry.snuba.metrics.extraction import (
@@ -64,6 +64,7 @@
6464
should_use_on_demand_metrics_for_querying,
6565
)
6666
from sentry.snuba.metrics.fields import histogram as metrics_histogram
67+
from sentry.snuba.metrics.naming_layer.mri import extract_use_case_id
6768
from sentry.snuba.metrics.query import (
6869
MetricField,
6970
MetricGroupByField,

src/sentry/sentry_metrics/aggregation_option_registry.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from enum import Enum
22

33
from sentry import options
4-
from sentry.sentry_metrics.use_case_id_registry import UseCaseID, extract_use_case_id
4+
from sentry.sentry_metrics.use_case_id_registry import UseCaseID
5+
from sentry.snuba.metrics.naming_layer.mri import extract_use_case_id
56

67

78
class AggregationOption(Enum):

src/sentry/sentry_metrics/consumers/indexer/batch.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@
2727
from sentry.sentry_metrics.consumers.indexer.parsed_message import ParsedMessage
2828
from sentry.sentry_metrics.consumers.indexer.routing_producer import RoutingPayload
2929
from sentry.sentry_metrics.indexer.base import Metadata
30-
from sentry.sentry_metrics.use_case_id_registry import UseCaseID, extract_use_case_id
30+
from sentry.sentry_metrics.use_case_id_registry import UseCaseID
31+
from sentry.snuba.metrics.naming_layer.mri import extract_use_case_id
3132
from sentry.utils import json, metrics
3233

3334
logger = logging.getLogger(__name__)

src/sentry/sentry_metrics/use_case_id_registry.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,18 @@
11
from __future__ import annotations
22

3-
import re
43
from collections.abc import Mapping
54
from enum import Enum
65

7-
from sentry_kafka_schemas.codecs import ValidationError
8-
96
from sentry.sentry_metrics.configuration import UseCaseKey
107

11-
MRI_RE_PATTERN = re.compile("^([c|s|d|g|e]):([a-zA-Z0-9_]+)/.*$")
8+
9+
class UseCaseIDAPIAccess(Enum):
10+
"""
11+
Represents the access levels of a UseCaseID for sentry's APIs.
12+
"""
13+
14+
PUBLIC = 0
15+
PRIVATE = 1
1216

1317

1418
class UseCaseID(Enum):
@@ -22,6 +26,17 @@ class UseCaseID(Enum):
2226
METRIC_STATS = "metric_stats"
2327

2428

29+
USE_CASE_ID_API_ACCESSES: Mapping[UseCaseID, UseCaseIDAPIAccess] = {
30+
UseCaseID.SPANS: UseCaseIDAPIAccess.PUBLIC,
31+
UseCaseID.TRANSACTIONS: UseCaseIDAPIAccess.PUBLIC,
32+
UseCaseID.SESSIONS: UseCaseIDAPIAccess.PUBLIC,
33+
UseCaseID.ESCALATING_ISSUES: UseCaseIDAPIAccess.PRIVATE,
34+
UseCaseID.CUSTOM: UseCaseIDAPIAccess.PUBLIC,
35+
UseCaseID.PROFILES: UseCaseIDAPIAccess.PRIVATE,
36+
UseCaseID.BUNDLE_ANALYSIS: UseCaseIDAPIAccess.PRIVATE,
37+
UseCaseID.METRIC_STATS: UseCaseIDAPIAccess.PRIVATE,
38+
}
39+
2540
# UseCaseKey will be renamed to MetricPathKey
2641
METRIC_PATH_MAPPING: Mapping[UseCaseID, UseCaseKey] = {
2742
UseCaseID.SPANS: UseCaseKey.PERFORMANCE,
@@ -58,16 +73,11 @@ class UseCaseID(Enum):
5873
}
5974

6075

61-
def get_use_case_key(use_case_id: UseCaseID) -> UseCaseKey | None:
62-
return METRIC_PATH_MAPPING.get(use_case_id)
63-
64-
65-
def extract_use_case_id(mri: str) -> UseCaseID:
76+
def get_use_case_id_api_access(use_case_id: UseCaseID) -> UseCaseIDAPIAccess:
6677
"""
67-
Returns the use case ID given the MRI, returns None if MRI is invalid.
78+
Returns the api access visibility of a use case and defaults to private in case no api access is provided.
79+
80+
The rationale for defaulting to private visibility is that we do not want to leak by mistake any internal metrics
81+
that users should not have access to.
6882
"""
69-
if matched := MRI_RE_PATTERN.match(mri):
70-
use_case_str = matched.group(2)
71-
if use_case_str in {id.value for id in UseCaseID}:
72-
return UseCaseID(use_case_str)
73-
raise ValidationError(f"Invalid mri: {mri}")
83+
return USE_CASE_ID_API_ACCESSES.get(use_case_id, UseCaseIDAPIAccess.PRIVATE)

src/sentry/snuba/metrics/naming_layer/mri.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
from enum import Enum
4040
from typing import cast
4141

42+
from sentry_kafka_schemas.codecs import ValidationError
43+
4244
from sentry.exceptions import InvalidParams
4345
from sentry.sentry_metrics.use_case_id_registry import UseCaseID
4446
from sentry.snuba.dataset import EntityKey
@@ -358,3 +360,17 @@ def get_available_operations(parsed_mri: ParsedMRI) -> Sequence[str]:
358360
else:
359361
entity_key = get_entity_key_from_entity_type(parsed_mri.entity, True).value
360362
return AVAILABLE_GENERIC_OPERATIONS[entity_key]
363+
364+
365+
def extract_use_case_id(mri: str) -> UseCaseID:
366+
"""
367+
Returns the use case ID given the MRI, throws an error if MRI is invalid or the use case doesn't exist.
368+
"""
369+
parsed_mri = parse_mri(mri)
370+
if parsed_mri is not None:
371+
if parsed_mri.namespace in {id.value for id in UseCaseID}:
372+
return UseCaseID(parsed_mri.namespace)
373+
374+
raise ValidationError(f"The use case of the MRI {parsed_mri.namespace} does not exist")
375+
376+
raise ValidationError(f"The MRI {mri} is not valid")

tests/sentry/sentry_metrics/test_gen_metrics_multiprocess_steps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ def test_process_messages_default_card_rollout(set_sentry_option: Callable[...,
419419
),
420420
(
421421
{
422-
"name": "c:transactions/alert@none" * 21,
422+
"name": "c:transactions/alert@" + ("none" * 50),
423423
"tags": {
424424
"environment": "production",
425425
"session.status": "errored",

tests/sentry/sentry_metrics/test_rh_metrics_multiprocess_steps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ def test_process_messages() -> None:
363363
),
364364
(
365365
{
366-
"name": SessionMRI.RAW_ERROR.value * 21,
366+
"name": "s:sessions/error@" + ("none" * 50),
367367
"tags": {
368368
"environment": "production",
369369
"session.status": "errored",

0 commit comments

Comments
 (0)