Skip to content

Commit 2c55ca9

Browse files
committed
ref(protos): Share get_trace_item_type_name for indexed-column item types
Move get_trace_item_type_name from deletion_settings to snuba.protos.common so the indexed-column lookup can reuse it instead of keeping its own item type name mapping. Its tests move along with it.
1 parent a22c703 commit 2c55ca9

6 files changed

Lines changed: 72 additions & 93 deletions

File tree

‎snuba/datasets/deletion_settings.py‎

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
from collections.abc import Sequence
44
from dataclasses import dataclass, field
5-
from typing import cast
6-
7-
from sentry_protos.snuba.v1.request_common_pb2 import TraceItemType
85

96
MAX_ROWS_TO_DELETE_DEFAULT = 100000
107

@@ -18,37 +15,3 @@ class DeletionSettings:
1815
max_rows_to_delete: int = MAX_ROWS_TO_DELETE_DEFAULT
1916
allowed_attributes_by_item_type: dict[str, list[str]] = field(default_factory=dict)
2017
partition_column: str | None = None
21-
22-
23-
def get_trace_item_type_name(item_type: int) -> str:
24-
"""
25-
Get the string name for a TraceItemType enum value.
26-
27-
Uses the protobuf enum's Name() method and strips the "TRACE_ITEM_TYPE_" prefix,
28-
then converts to lowercase to match storage configuration naming conventions.
29-
30-
Args:
31-
item_type: The integer value of the TraceItemType enum
32-
33-
Returns:
34-
The string name used in storage configurations (e.g., "occurrence", "span")
35-
36-
Raises:
37-
ValueError: If the item_type is not a valid TraceItemType enum value
38-
"""
39-
try:
40-
# Get the full protobuf enum name (e.g., "TRACE_ITEM_TYPE_SPAN")
41-
# Cast to TraceItemType.ValueType to satisfy type checker
42-
full_name = TraceItemType.Name(cast("TraceItemType.ValueType", item_type))
43-
44-
# Strip the "TRACE_ITEM_TYPE_" prefix and convert to lowercase
45-
prefix = "TRACE_ITEM_TYPE_"
46-
if not full_name.startswith(prefix):
47-
raise ValueError(f"Unexpected TraceItemType name format: {full_name}")
48-
49-
return full_name[len(prefix) :].lower()
50-
except ValueError as e:
51-
# This happens when item_type is not a valid enum value
52-
raise ValueError(
53-
f"Unknown TraceItemType value: {item_type}. Must be a valid TraceItemType enum value."
54-
) from e

‎snuba/protos/common.py‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
from collections import defaultdict
22
from collections.abc import Mapping, Sequence
3-
from typing import Final, NamedTuple
3+
from typing import Final, NamedTuple, cast
44

55
from sentry_conventions.attributes import ATTRIBUTE_METADATA
6+
from sentry_protos.snuba.v1.request_common_pb2 import TraceItemType
67
from sentry_protos.snuba.v1.trace_item_attribute_pb2 import AttributeKey
78

89
from snuba.query.dsl import Functions as f
@@ -393,3 +394,37 @@ def attribute_key_to_expression(attr_key: AttributeKey) -> Expression:
393394
raise MalformedAttributeException(
394395
f"Attribute {attr_key.name} has an unknown type: {AttributeKey.Type.Name(attr_key.type)}"
395396
)
397+
398+
399+
def get_trace_item_type_name(item_type: int) -> str:
400+
"""
401+
Get the string name for a TraceItemType enum value.
402+
403+
Uses the protobuf enum's Name() method and strips the "TRACE_ITEM_TYPE_" prefix,
404+
then converts to lowercase to match storage configuration naming conventions.
405+
406+
Args:
407+
item_type: The integer value of the TraceItemType enum
408+
409+
Returns:
410+
The string name used in storage configurations (e.g., "occurrence", "span")
411+
412+
Raises:
413+
ValueError: If the item_type is not a valid TraceItemType enum value
414+
"""
415+
try:
416+
# Get the full protobuf enum name (e.g., "TRACE_ITEM_TYPE_SPAN")
417+
# Cast to TraceItemType.ValueType to satisfy type checker
418+
full_name = TraceItemType.Name(cast("TraceItemType.ValueType", item_type))
419+
420+
# Strip the "TRACE_ITEM_TYPE_" prefix and convert to lowercase
421+
prefix = "TRACE_ITEM_TYPE_"
422+
if not full_name.startswith(prefix):
423+
raise ValueError(f"Unexpected TraceItemType name format: {full_name}")
424+
425+
return full_name[len(prefix) :].lower()
426+
except ValueError as e:
427+
# This happens when item_type is not a valid enum value
428+
raise ValueError(
429+
f"Unknown TraceItemType value: {item_type}. Must be a valid TraceItemType enum value."
430+
) from e

‎snuba/web/bulk_delete_query.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@
1414
from snuba.clickhouse.query import Query
1515
from snuba.clusters.cluster import UndefinedClickhouseCluster, get_cluster
1616
from snuba.clusters.storage_sets import StorageSetKey
17-
from snuba.datasets.deletion_settings import DeletionSettings, get_trace_item_type_name
17+
from snuba.datasets.deletion_settings import DeletionSettings
1818
from snuba.datasets.schemas.tables import TableSchema
1919
from snuba.datasets.storage import WritableTableStorage
2020
from snuba.datasets.storages.factory import get_config_built_storages
2121
from snuba.datasets.storages.storage_key import StorageKey
2222
from snuba.lw_deletions.types import AttributeConditions, ConditionsBag, ConditionsType
23+
from snuba.protos.common import get_trace_item_type_name
2324
from snuba.query.conditions import combine_or_conditions
2425
from snuba.query.data_source.simple import Table
2526
from snuba.query.dsl import literal

‎snuba/web/rpc/common/common.py‎

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
array_element_column,
2929
coalesced_attribute_names,
3030
first_present_value,
31+
get_trace_item_type_name,
3132
key_existence_conditions,
3233
sentry_column,
3334
type_array_to_membership_array_expression_from_typed_columns,
@@ -1025,23 +1026,6 @@ class IndexedColumn(TypedDict):
10251026
INDEXED_COLUMNS_OPTION = "indexed_columns"
10261027

10271028

1028-
_ITEM_TYPE_NAMES: dict[TraceItemType.ValueType, str] = {
1029-
TraceItemType.TRACE_ITEM_TYPE_SPAN: "span",
1030-
TraceItemType.TRACE_ITEM_TYPE_ERROR: "error",
1031-
TraceItemType.TRACE_ITEM_TYPE_LOG: "log",
1032-
TraceItemType.TRACE_ITEM_TYPE_UPTIME_CHECK: "uptime_check",
1033-
TraceItemType.TRACE_ITEM_TYPE_UPTIME_RESULT: "uptime_result",
1034-
TraceItemType.TRACE_ITEM_TYPE_REPLAY: "replay",
1035-
TraceItemType.TRACE_ITEM_TYPE_OCCURRENCE: "occurrence",
1036-
TraceItemType.TRACE_ITEM_TYPE_METRIC: "metric",
1037-
TraceItemType.TRACE_ITEM_TYPE_PROFILE_FUNCTION: "profile_function",
1038-
TraceItemType.TRACE_ITEM_TYPE_ATTACHMENT: "attachment",
1039-
TraceItemType.TRACE_ITEM_TYPE_PREPROD: "preprod",
1040-
TraceItemType.TRACE_ITEM_TYPE_USER_SESSION: "user_session",
1041-
TraceItemType.TRACE_ITEM_TYPE_PROCESSING_ERROR: "processing_error",
1042-
}
1043-
1044-
10451029
def indexed_column_for(
10461030
start_timestamp: ProtobufTimestamp | None,
10471031
item_type: TraceItemType.ValueType,
@@ -1057,7 +1041,7 @@ def indexed_column_for(
10571041
)
10581042
if (indexed_column := indexed_columns.get(unindexed_column)) is None:
10591043
return None
1060-
if indexed_column.get("item_type") != _ITEM_TYPE_NAMES.get(item_type):
1044+
if indexed_column.get("item_type") != get_trace_item_type_name(item_type):
10611045
return None
10621046
if start_timestamp.seconds < indexed_column.get("indexed_start_timestamp", 2**63 - 1):
10631047
return None

‎tests/datasets/test_deletion_settings.py‎

Lines changed: 0 additions & 36 deletions
This file was deleted.

‎tests/protos/test_protos_common.py‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import pytest
22
from sentry_conventions.attributes import ATTRIBUTE_METADATA
3+
from sentry_protos.snuba.v1.request_common_pb2 import TraceItemType
34
from sentry_protos.snuba.v1.trace_item_attribute_pb2 import AttributeKey
45

56
from snuba.protos.common import (
67
ATTRIBUTES_TO_COALESCE,
78
MalformedAttributeException,
89
_resolve_canonical,
910
attribute_key_to_expression,
11+
get_trace_item_type_name,
1012
)
1113
from snuba.query.dsl import Functions as f
1214
from snuba.query.dsl import arrayElement, column, literal, map_key_exists
@@ -157,3 +159,33 @@ def test_invalid_type_for_normalized_column_raises_exception(self) -> None:
157159
AttributeKey(type=AttributeKey.TYPE_BOOLEAN, name="sentry.trace_id")
158160
)
159161
assert "must be one of" in str(exc_info.value)
162+
163+
164+
def test_get_trace_item_type_name_valid() -> None:
165+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_UNSPECIFIED) == "unspecified"
166+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_SPAN) == "span"
167+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_ERROR) == "error"
168+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_LOG) == "log"
169+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_UPTIME_CHECK) == "uptime_check"
170+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_UPTIME_RESULT) == "uptime_result"
171+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_REPLAY) == "replay"
172+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_OCCURRENCE) == "occurrence"
173+
assert get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_METRIC) == "metric"
174+
assert (
175+
get_trace_item_type_name(TraceItemType.TRACE_ITEM_TYPE_PROFILE_FUNCTION)
176+
== "profile_function"
177+
)
178+
179+
180+
def test_get_trace_item_type_name_by_integer() -> None:
181+
assert get_trace_item_type_name(0) == "unspecified"
182+
assert get_trace_item_type_name(1) == "span"
183+
assert get_trace_item_type_name(7) == "occurrence"
184+
185+
186+
def test_get_trace_item_type_name_invalid() -> None:
187+
with pytest.raises(ValueError, match="Unknown TraceItemType value: 999"):
188+
get_trace_item_type_name(999)
189+
190+
with pytest.raises(ValueError, match="Unknown TraceItemType value: -1"):
191+
get_trace_item_type_name(-1)

0 commit comments

Comments
 (0)