|
| 1 | +import pytest |
| 2 | + |
| 3 | +from posthog.tracing._limits import ( |
| 4 | + bound_attributes, |
| 5 | + truncate_attribute_value, |
| 6 | + truncate_attributes, |
| 7 | +) |
| 8 | +from posthog.tracing._otlp import ( |
| 9 | + CIRCULAR_VALUE, |
| 10 | + MAX_VALUE_ITEMS, |
| 11 | + MAX_VALUE_NODES, |
| 12 | + TRUNCATED_VALUE, |
| 13 | + to_any_value, |
| 14 | +) |
| 15 | +from posthog.tracing._sanitize import UNSERIALIZABLE_VALUE |
| 16 | + |
| 17 | + |
| 18 | +class TestTruncateAttributeValue: |
| 19 | + def test_truncates_a_long_string(self): |
| 20 | + assert truncate_attribute_value("x" * 40000, 8192) == "x" * 8192 |
| 21 | + |
| 22 | + def test_returns_a_short_string_unchanged(self): |
| 23 | + assert truncate_attribute_value("short", 8192) == "short" |
| 24 | + |
| 25 | + @pytest.mark.parametrize("value", [42, 1.5, True, None, 2**70]) |
| 26 | + def test_leaves_numbers_booleans_and_none_alone(self, value): |
| 27 | + assert truncate_attribute_value(value, 3) == value |
| 28 | + |
| 29 | + def test_reaches_strings_nested_in_mappings_and_lists(self): |
| 30 | + value = {"body": "x" * 40000, "items": ["y" * 20, {"deep": "z" * 20}]} |
| 31 | + assert truncate_attribute_value(value, 8) == { |
| 32 | + "body": "x" * 8, |
| 33 | + "items": ["y" * 8, {"deep": "z" * 8}], |
| 34 | + } |
| 35 | + |
| 36 | + def test_does_not_mutate_the_callers_value(self): |
| 37 | + value = {"body": "x" * 20} |
| 38 | + truncate_attribute_value(value, 4) |
| 39 | + assert value == {"body": "x" * 20} |
| 40 | + |
| 41 | + def test_a_self_referencing_value_terminates_with_the_encoders_marker(self): |
| 42 | + value: dict = {"name": "n" * 20} |
| 43 | + value["self"] = value |
| 44 | + assert truncate_attribute_value(value, 4) == { |
| 45 | + "name": "nnnn", |
| 46 | + "self": CIRCULAR_VALUE, |
| 47 | + } |
| 48 | + |
| 49 | + def test_siblings_sharing_one_object_are_not_a_cycle(self): |
| 50 | + shared = {"k": "v" * 10} |
| 51 | + assert truncate_attribute_value([shared, shared], 2) == [ |
| 52 | + {"k": "vv"}, |
| 53 | + {"k": "vv"}, |
| 54 | + ] |
| 55 | + |
| 56 | + def test_marks_items_past_the_encoders_item_cap(self): |
| 57 | + bounded = truncate_attribute_value(["a"] * (MAX_VALUE_ITEMS + 5), 8) |
| 58 | + assert len(bounded) == MAX_VALUE_ITEMS + 1 |
| 59 | + assert bounded[-1] == TRUNCATED_VALUE |
| 60 | + # The encoder emits the same shape it would have for the original. |
| 61 | + assert to_any_value(bounded) == to_any_value(["a"] * (MAX_VALUE_ITEMS + 5)) |
| 62 | + |
| 63 | + def test_stringifies_and_bounds_a_type_the_encoder_would_stringify(self): |
| 64 | + class Big: |
| 65 | + def __str__(self): |
| 66 | + return "b" * 100 |
| 67 | + |
| 68 | + assert truncate_attribute_value(Big(), 10) == "b" * 10 |
| 69 | + assert truncate_attribute_value(b"\x00" * 100, 10) == "b'\\x00\\x00" |
| 70 | + |
| 71 | + def test_a_key_the_encoder_skips_does_not_spend_the_walks_budget(self): |
| 72 | + # The encoder drops "" without charging for its value, so the walk must |
| 73 | + # too, or "x" would ship unbounded once the walk's budget ran out. |
| 74 | + value = {"": list(range(999)), "a": [list(range(999))] * 9, "x": "A" * 1000} |
| 75 | + bounded = truncate_attribute_value(value, 100) |
| 76 | + assert "" not in bounded |
| 77 | + assert bounded["x"] == "A" * 100 |
| 78 | + encoded = to_any_value(bounded)["kvlistValue"]["values"] |
| 79 | + x = next(kv for kv in encoded if kv["key"] == "x") |
| 80 | + assert len(x["value"]["stringValue"]) == 100 |
| 81 | + |
| 82 | + def test_a_raising_str_costs_only_that_value(self): |
| 83 | + class Hostile: |
| 84 | + def __str__(self): |
| 85 | + raise RuntimeError("no") |
| 86 | + |
| 87 | + assert truncate_attribute_value({"a": Hostile(), "b": "ok"}, 8) == { |
| 88 | + "a": UNSERIALIZABLE_VALUE, |
| 89 | + "b": "ok", |
| 90 | + } |
| 91 | + |
| 92 | + def test_a_raising_accessor_costs_only_that_key(self): |
| 93 | + class Explosive(dict): |
| 94 | + def __getitem__(self, key): |
| 95 | + if key == "bad": |
| 96 | + raise RuntimeError("no") |
| 97 | + return super().__getitem__(key) |
| 98 | + |
| 99 | + assert truncate_attribute_value(Explosive(good="g" * 9, bad=1), 3) == { |
| 100 | + "good": "ggg", |
| 101 | + "bad": UNSERIALIZABLE_VALUE, |
| 102 | + } |
| 103 | + |
| 104 | + |
| 105 | +class TestBoundAttributes: |
| 106 | + def test_keeps_the_earliest_entries_and_counts_the_rest(self): |
| 107 | + source = {f"k{i}": i for i in range(130)} |
| 108 | + attributes, dropped = bound_attributes(source, 128, 8192) |
| 109 | + assert list(attributes) == [f"k{i}" for i in range(128)] |
| 110 | + assert dropped == 2 |
| 111 | + |
| 112 | + def test_a_none_value_spends_no_slot(self): |
| 113 | + attributes, dropped = bound_attributes({"a": None, "b": 1, "c": 2}, 2, 8) |
| 114 | + assert attributes == {"b": 1, "c": 2} |
| 115 | + assert dropped == 0 |
| 116 | + |
| 117 | + def test_bounds_each_value(self): |
| 118 | + attributes, _ = bound_attributes({"a": "x" * 20}, 2, 5) |
| 119 | + assert attributes == {"a": "xxxxx"} |
| 120 | + |
| 121 | + def test_a_non_mapping_yields_nothing(self): |
| 122 | + assert bound_attributes(["a"], 2, 5) == ({}, 0) |
| 123 | + |
| 124 | + |
| 125 | +class TestTruncateAttributes: |
| 126 | + def test_bounds_every_value_as_a_copy(self): |
| 127 | + source = {"service.name": "api", "blob": "x" * 20} |
| 128 | + assert truncate_attributes(source, 4) == {"service.name": "api", "blob": "xxxx"} |
| 129 | + assert source["blob"] == "x" * 20 |
| 130 | + |
| 131 | + |
| 132 | +class TestWalkBounds: |
| 133 | + def test_walks_no_more_strings_than_the_encoder_would_emit(self): |
| 134 | + # A thousand paths to one shared list of a thousand strings. Leaves are |
| 135 | + # charged against the node budget, as in the encoder, so the walk does |
| 136 | + # not copy every string on every path. |
| 137 | + inner = ["x" * 50] * 1000 |
| 138 | + bounded = truncate_attribute_value([inner] * 1000, 8) |
| 139 | + walked = sum( |
| 140 | + 1 |
| 141 | + for items in bounded |
| 142 | + if items is not inner |
| 143 | + for item in items |
| 144 | + if item == "x" * 8 |
| 145 | + ) |
| 146 | + assert 0 < walked <= MAX_VALUE_NODES |
| 147 | + |
| 148 | + def test_stops_walking_a_mapping_at_the_encoders_item_cap(self): |
| 149 | + value = {f"k{i}": "v" * 50 for i in range(MAX_VALUE_ITEMS + 50)} |
| 150 | + bounded = truncate_attribute_value(value, 4) |
| 151 | + assert len(bounded) == MAX_VALUE_ITEMS |
0 commit comments