Skip to content

Commit 2c8fdfc

Browse files
authored
fix(trace-items): Now that meta is more in progress update key (#95262)
- This updates the key we pull meta data from now that the relay work is in progress (see: getsentry/relay#4929) - Also moves the prefixes to a constant so its easier to update in the future
1 parent 6274785 commit 2c8fdfc

File tree

5 files changed

+49
-28
lines changed

5 files changed

+49
-28
lines changed

src/sentry/api/endpoints/project_trace_item_details.py

Lines changed: 40 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def convert_rpc_attribute_to_json(
4040
internal_name = attribute["name"]
4141
if internal_name in PRIVATE_ATTRIBUTES.get(trace_item_type, []):
4242
continue
43-
if internal_name.startswith("sentry._meta"):
43+
if internal_name.startswith(constants.META_PREFIX):
4444
continue
4545
source = attribute["value"]
4646
if len(source) == 0:
@@ -102,38 +102,55 @@ def serialize_meta(
102102
) -> dict:
103103
internal_name = ""
104104
attribute = {}
105+
meta_result = {}
106+
meta_attributes = {}
105107
for attribute in attributes:
106108
internal_name = attribute["name"]
107-
if internal_name.startswith("sentry._meta"):
108-
break
109-
110-
if not internal_name.startswith("sentry._meta") or "valStr" not in attribute["value"]:
111-
return {}
112-
113-
try:
114-
result = json.loads(attribute["value"]["valStr"])
115-
attribute_map = {item.get("name", ""): item.get("value", {}) for item in attributes}
116-
mapped_result = {}
117-
for key, value in result.items():
118-
if key in attribute_map:
109+
if internal_name.startswith(constants.META_PREFIX):
110+
meta_attributes[internal_name] = attribute
111+
112+
def extract_key(key: str) -> str | None:
113+
if key.startswith(f"{constants.META_ATTRIBUTE_PREFIX}."):
114+
return key.replace(f"{constants.META_ATTRIBUTE_PREFIX}.", "")
115+
elif key.startswith(f"{constants.META_FIELD_PREFIX}."):
116+
return key.replace(f"{constants.META_FIELD_PREFIX}.", "")
117+
# Unknown meta field, skip for now
118+
else:
119+
return None
120+
121+
attribute_map = {item.get("name", ""): item.get("value", {}) for item in attributes}
122+
for internal_name, attribute in sorted(meta_attributes.items()):
123+
if "valStr" not in attribute["value"]:
124+
continue
125+
field_key = extract_key(internal_name)
126+
if field_key is None:
127+
continue
128+
129+
try:
130+
result = json.loads(attribute["value"]["valStr"])
131+
# Map the internal field key name back to its public name
132+
if field_key in attribute_map:
119133
item_type: Literal["string", "number"]
120134
if (
121-
"valInt" in attribute_map[key]
122-
or "valFloat" in attribute_map[key]
123-
or "valDouble" in attribute_map[key]
135+
"valInt" in attribute_map[field_key]
136+
or "valFloat" in attribute_map[field_key]
137+
or "valDouble" in attribute_map[field_key]
124138
):
125139
item_type = "number"
126140
else:
127141
item_type = "string"
128-
external_name = translate_internal_to_public_alias(key, item_type, trace_item_type)
142+
external_name = translate_internal_to_public_alias(
143+
field_key, item_type, trace_item_type
144+
)
129145
if external_name:
130-
key = external_name
146+
field_key = external_name
131147
elif item_type == "number":
132-
key = f"tags[{key},number]"
133-
mapped_result[key] = value
134-
return mapped_result
135-
except json.JSONDecodeError:
136-
return {}
148+
field_key = f"tags[{field_key},number]"
149+
meta_result[field_key] = result
150+
except json.JSONDecodeError:
151+
continue
152+
153+
return meta_result
137154

138155

139156
def serialize_item_id(item_id: str, trace_item_type: SupportedTraceItemType) -> str:

src/sentry/search/eap/constants.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,3 +173,7 @@
173173
"plus": Column.BinaryFormula.OP_ADD,
174174
"minus": Column.BinaryFormula.OP_SUBTRACT,
175175
}
176+
177+
META_PREFIX = "sentry._meta"
178+
META_FIELD_PREFIX = f"{META_PREFIX}.fields"
179+
META_ATTRIBUTE_PREFIX = f"{META_FIELD_PREFIX}.attributes"

src/sentry/search/eap/ourlogs/attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@
108108
}
109109

110110
# For dynamic internal attributes (eg. meta information for attributes) we match by the beginning of the key.
111-
LOGS_PRIVATE_ATTRIBUTE_PREFIXES: set[str] = {"sentry._meta"}
111+
LOGS_PRIVATE_ATTRIBUTE_PREFIXES: set[str] = {constants.META_PREFIX}
112112

113113
LOGS_REPLACEMENT_ATTRIBUTES: set[str] = {
114114
definition.replacement

src/sentry/search/eap/spans/attributes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -581,7 +581,7 @@ def is_starred_segment_context_constructor(params: SnubaParams) -> VirtualColumn
581581
}
582582

583583
# For dynamic internal attributes (eg. meta information for attributes) we match by the beginning of the key.
584-
SPANS_PRIVATE_ATTRIBUTE_PREFIXES: set[str] = {"sentry._meta"}
584+
SPANS_PRIVATE_ATTRIBUTE_PREFIXES: set[str] = {constants.META_PREFIX}
585585

586586
SPANS_REPLACEMENT_ATTRIBUTES: set[str] = {
587587
definition.replacement

tests/snuba/api/endpoints/test_project_trace_item_details.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -335,8 +335,8 @@ def test_logs_with_a_meta_key(self):
335335
"str_attr": {
336336
"string_value": "1",
337337
},
338-
# This is a guess on how this will look, the key & storage may change at some point
339-
"sentry._meta.fields.attributes": '{"sentry.body": {"length": 300, "reason": "value too long"}, "float_attr": {"unit": "float"}}',
338+
"sentry._meta.fields.attributes.sentry.body": '{"length": 300, "reason": "value too long"}',
339+
"sentry._meta.fields.attributes.float_attr": '{"unit": "float"}',
340340
"int_attr": {"int_value": 2},
341341
"float_attr": {
342342
"double_value": 3.0,
@@ -397,8 +397,8 @@ def test_logs_with_a_meta_key(self):
397397
{"name": "trace", "type": "str", "value": self.trace_uuid},
398398
],
399399
"meta": {
400-
"message": {"length": 300, "reason": "value too long"},
401400
"tags[float_attr,number]": {"unit": "float"},
401+
"message": {"length": 300, "reason": "value too long"},
402402
},
403403
"itemId": item_id,
404404
"timestamp": self.one_min_ago.replace(

0 commit comments

Comments
 (0)