Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 30 additions & 4 deletions tests/test_the_test/test_deserializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,29 @@ def test_deserialize_v1_trace_bytes_attributes_match_convert_bytes_values():
@scenarios.test_the_test
def test_deserialize_v1_trace_key_value_list_attribute():
"""Test that span attributes with key_value_list type (V1AnyValueKeys.key_value_list=7)
are deserialized to a nested dict.
# TODO(v1-kvlist-e2e): add an E2E system test once a tracer emits keyValueList in a V1 payload.
are deserialized to a nested dict. A key_value_list is a flat [key, type, value, ...] array.
"""
kvl = int(V1AnyValueKeys.key_value_list)
# _dd.stack has the shape the PHP tracer sends: arrays of key_value_lists, with string refs.
# Streamed strings: 11 is "id" (first seen in the exploit), reused as a ref in the frame.
stack = [
"exploit",
int(V1AnyValueKeys.array),
[
kvl,
[
"language",
int(V1AnyValueKeys.string),
"php",
"id",
int(V1AnyValueKeys.string),
"stack-id",
"frames",
int(V1AnyValueKeys.array),
[kvl, ["line", int(V1AnyValueKeys.int_value), 21, 11, int(V1AnyValueKeys.int_value), 0]],
],
],
]
content = msgpack.packb(
{
2: "cid",
Expand All @@ -315,8 +335,11 @@ def test_deserialize_v1_trace_key_value_list_attribute():
2: "op",
9: [
"my-map",
int(V1AnyValueKeys.key_value_list),
{"nested-key": [int(V1AnyValueKeys.string), "nested-val"]},
kvl,
["nested-key", int(V1AnyValueKeys.string), "nested-val"],
"_dd.stack",
kvl,
stack,
],
}
],
Expand All @@ -332,6 +355,9 @@ def test_deserialize_v1_trace_key_value_list_attribute():
attrs = result["chunks"][0]["spans"][0]["attributes"]
assert "my-map" in attrs
assert attrs["my-map"] == {"nested-key": "nested-val"}
assert attrs["_dd.stack"] == {
"exploit": [{"language": "php", "id": "stack-id", "frames": [{"line": 21, "id": 0}]}],
}


@scenarios.test_the_test
Expand Down
27 changes: 9 additions & 18 deletions utils/proxy/traces/trace_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,21 +256,16 @@ def _attributes_to_dict(attrs: list, strings: list[str]) -> dict:
return attrs_dict


def _uncompress_key_value_list(kvl: dict[Any, Any], strings: list[str]) -> dict[str, Any]:
"""Uncompress a key_value_list AnyValue, resolving string refs and nested values recursively."""
def _uncompress_key_value_list(kvl: list, strings: list[str]) -> dict[str, Any]:
"""Uncompress a key_value_list AnyValue: a flat [key, type, value, ...] triplet array, like attributes.
Nested values are decoded recursively, without the top-level attribute post-processing.
"""
if len(kvl) % 3 != 0:
raise ValueError(f"Key value list must be a multiple of 3: {kvl}")
result: dict[str, Any] = {}
for raw_key, type_value_pair in kvl.items():
for i in range(0, len(kvl), 3):
raw_key, v_type, v = kvl[i], kvl[i + 1], kvl[i + 2]
key = strings[raw_key] if isinstance(raw_key, int) else raw_key
try:
v_type_int, v = type_value_pair
except (TypeError, ValueError):
result[key] = type_value_pair
continue
try:
v_type = V1AnyValueKeys(v_type_int)
except ValueError:
result[key] = v
continue
if v_type == V1AnyValueKeys.string:
if isinstance(v, int):
v = strings[v]
Expand Down Expand Up @@ -312,11 +307,7 @@ def _uncompress_array(array: list, strings: list[str]) -> list:
elif v_type == V1AnyValueKeys.array:
v = _uncompress_array(v, strings)
elif v_type == V1AnyValueKeys.key_value_list:
warnings.warn(
"key_value_list inside array is left as-is (not yet uncompressed)",
UserWarning,
stacklevel=2,
)
v = _uncompress_key_value_list(v, strings)
result.append(v)
return result

Expand Down
Loading