Skip to content
Open
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
79 changes: 79 additions & 0 deletions packages/traceloop-sdk/tests/test_metrics_common_attributes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import json

from opentelemetry.context import attach, set_value
from opentelemetry.semconv_ai import SpanAttributes

from traceloop.sdk.tracing.tracing import metrics_common_attributes


def test_scalar_association_properties_pass_through():
attach(set_value("association_properties", {"user_id": 1, "user_name": "John"}))

attributes = metrics_common_attributes()

assert attributes[f"{SpanAttributes.TRACELOOP_ASSOCIATION_PROPERTIES}.user_id"] == 1
assert (
attributes[f"{SpanAttributes.TRACELOOP_ASSOCIATION_PROPERTIES}.user_name"]
== "John"
)


def test_list_valued_association_property_is_json_encoded():
"""LangGraph tags spans with list-valued baggage (e.g. langgraph_triggers).
Metric attributes must be hashable/scalar, so a raw list here previously
crashed the OTel metrics SDK's aggregation and silently dropped every
response-side span attribute set after it (gen_ai.output.messages, etc.)."""
attach(
set_value(
"association_properties",
{"langgraph_triggers": ["branch:to:resolve_naics"]},
)
)

attributes = metrics_common_attributes()

key = f"{SpanAttributes.TRACELOOP_ASSOCIATION_PROPERTIES}.langgraph_triggers"
assert attributes[key] == json.dumps(["branch:to:resolve_naics"])
# must be hashable, matching what the OTel metrics SDK requires for its
# aggregation key: frozenset(attributes.items())
hash(attributes[key])


def test_dict_valued_association_property_is_json_encoded():
attach(
set_value(
"association_properties",
{"metadata": {"nested": "value"}},
)
)

attributes = metrics_common_attributes()

key = f"{SpanAttributes.TRACELOOP_ASSOCIATION_PROPERTIES}.metadata"
assert attributes[key] == json.dumps({"nested": "value"}, sort_keys=True)
hash(attributes[key])


def test_dict_valued_association_property_is_order_independent():
"""Equal dicts with different key insertion order must serialize to the same
string, otherwise the metrics SDK treats them as distinct attribute values and
splits what should be one aggregated series into several."""
attach(
set_value(
"association_properties",
{"metadata": {"a": 1, "b": 2}},
)
)
attributes_a = metrics_common_attributes()

attach(
set_value(
"association_properties",
{"metadata": {"b": 2, "a": 1}},
)
)
attributes_b = metrics_common_attributes()

key = f"{SpanAttributes.TRACELOOP_ASSOCIATION_PROPERTIES}.metadata"
assert attributes_a[key] == attributes_b[key]

3 changes: 3 additions & 0 deletions packages/traceloop-sdk/traceloop/sdk/tracing/tracing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import atexit
import json
import logging
import os
from urllib.parse import urlparse
Expand Down Expand Up @@ -1221,6 +1222,8 @@ def metrics_common_attributes():
association_properties = get_value("association_properties")
if association_properties is not None:
for key, value in association_properties.items():
if isinstance(value, (list, dict)):
value = json.dumps(value, sort_keys=True)
common_attributes[
f"{SpanAttributes.TRACELOOP_ASSOCIATION_PROPERTIES}.{key}"
] = value
Expand Down