Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
48 changes: 48 additions & 0 deletions posthog/test/tracing/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,51 @@ def test_an_unusable_value_falls_back_rather_than_dropping_every_span(self, valu
assert resolved.max_attributes_per_span == DEFAULT_MAX_ATTRIBUTES_PER_SPAN
assert resolved.max_events_per_span == DEFAULT_MAX_EVENTS_PER_SPAN
assert resolved.max_attribute_value_length == DEFAULT_MAX_ATTRIBUTE_VALUE_LENGTH


class TestBeforeSpanSendConfig:
def test_accepts_one_hook_or_a_list(self):
def hook(span):
return span

assert resolve_traces_config({"before_span_send": hook}).before_span_send == (
hook,
)
assert resolve_traces_config(
{"before_span_send": [hook, hook]}
).before_span_send == (hook, hook)

def test_defaults_to_no_hooks(self):
assert resolve_traces_config({}).before_span_send == ()

def test_skips_falsy_entries_silently(self, caplog):
caplog.set_level("WARNING", logger="posthog")

def hook(span):
return span

resolved = resolve_traces_config({"before_span_send": [None, False, hook]})
assert resolved.before_span_send == (hook,)
assert not caplog.records

def test_rejects_an_entry_that_is_not_callable_rather_than_exporting_unhooked(
self,
):
def hook(span):
return span

with pytest.raises(ValueError, match="not callable"):
resolve_traces_config({"before_span_send": ["scrub", hook]})

def test_a_hook_whose_truthiness_raises_is_still_resolved(self):
class Hook:
def __bool__(self):
raise RuntimeError("no")

def __call__(self, span):
return span

hook = Hook()
assert resolve_traces_config({"before_span_send": hook}).before_span_send == (
hook,
)
17 changes: 17 additions & 0 deletions posthog/test/tracing/test_limits.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

from posthog.tracing import _limits as limits_module
from posthog.tracing._limits import (
apply_span_limits,
bound_attributes,
truncate_attribute_value,
truncate_attributes,
)
from posthog.tracing._otlp import (
CIRCULAR_VALUE,
SpanRecord,
MAX_VALUE_ITEMS,
MAX_VALUE_NODES,
TRUNCATED_VALUE,
Expand Down Expand Up @@ -181,3 +183,18 @@ def test_stops_walking_a_mapping_at_the_encoders_item_cap(self):
value = {f"k{i}": "v" * 50 for i in range(MAX_VALUE_ITEMS + 50)}
bounded = truncate_attribute_value(value, 4)
assert len(bounded) == MAX_VALUE_ITEMS


class TestApplySpanLimits:
def test_rebounds_a_container_the_hook_grew_in_place(self):
grown = ["x" * 3]
record = SpanRecord("t", "s", "n", 1, 2, attributes={"k": grown})
grown.append("y" * 20)
apply_span_limits(record, frozenset(), 128, 128, 128, 8)
assert record.attributes["k"] == ["xxx", "y" * 8]

def test_an_empty_key_spends_no_slot(self):
record = SpanRecord("t", "s", "n", 1, 2, attributes={"": 1, "a": 2, "b": 3})
apply_span_limits(record, frozenset(), 2, 128, 128, 8)
assert record.attributes == {"a": 2, "b": 3}
assert record.dropped_attributes_count == 0
Loading
Loading