Skip to content

Commit 05765e8

Browse files
authored
fix: preserve explicit parent for child events (#2045)
1 parent 0d63ee4 commit 05765e8

2 files changed

Lines changed: 75 additions & 3 deletions

File tree

‎langfuse/_client/span.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1125,9 +1125,10 @@ def start_observation(
11251125
"""
11261126
if as_type == "event":
11271127
timestamp = time_ns()
1128-
event_span = self._langfuse_client._otel_tracer.start_span(
1129-
name=name, start_time=timestamp
1130-
)
1128+
with otel_trace_api.use_span(self._otel_span):
1129+
event_span = self._langfuse_client._otel_tracer.start_span(
1130+
name=name, start_time=timestamp
1131+
)
11311132
return cast(
11321133
LangfuseEvent,
11331134
LangfuseEvent(

‎tests/unit/test_otel.py‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import json
22
import logging
3+
from contextlib import ExitStack
34
from datetime import datetime
45
from hashlib import sha256
56
from typing import List, Sequence
@@ -72,6 +73,76 @@ def generate_span_id(self) -> int:
7273
return self._span_ids.pop(0)
7374

7475

76+
@pytest.mark.parametrize("active_context", ["none", "parent", "unrelated"])
77+
@pytest.mark.parametrize("creation_method", ["event", "create_event", "span"])
78+
def test_child_observation_uses_explicit_parent(
79+
langfuse_memory_client, memory_exporter, active_context, creation_method
80+
):
81+
client = langfuse_memory_client
82+
with ExitStack() as stack:
83+
active = (
84+
stack.enter_context(client.start_as_current_observation(name="active"))
85+
if active_context != "none"
86+
else None
87+
)
88+
parent = (
89+
active
90+
if active_context == "parent"
91+
else client.start_observation(name="manual-parent")
92+
)
93+
current_id = client.get_current_observation_id()
94+
attributes = {
95+
"name": "child",
96+
"input": {"step": "started"},
97+
"output": {"step": "finished"},
98+
"metadata": {"source": "manual"},
99+
"version": "v1",
100+
"level": "WARNING",
101+
"status_message": "test-status",
102+
}
103+
if creation_method == "create_event":
104+
child = parent.create_event(**attributes)
105+
else:
106+
child = parent.start_observation(as_type=creation_method, **attributes)
107+
if creation_method == "span":
108+
child.end()
109+
110+
assert client.get_current_observation_id() == current_id
111+
if parent is not active:
112+
parent.end()
113+
114+
client.flush()
115+
spans = memory_exporter.get_finished_spans()
116+
child_spans = [span for span in spans if span.name == "child"]
117+
assert len(child_spans) == 1
118+
exported = child_spans[0]
119+
assert exported.parent is not None
120+
assert format(exported.parent.span_id, "016x") == parent.id
121+
assert format(exported.context.trace_id, "032x") == parent.trace_id
122+
assert format(exported.context.span_id, "016x") == child.id
123+
expected_type = "span" if creation_method == "span" else "event"
124+
assert (
125+
exported.attributes[LangfuseOtelSpanAttributes.OBSERVATION_TYPE]
126+
== expected_type
127+
)
128+
assert json.loads(
129+
exported.attributes[LangfuseOtelSpanAttributes.OBSERVATION_INPUT]
130+
) == {"step": "started"}
131+
assert json.loads(
132+
exported.attributes[LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT]
133+
) == {"step": "finished"}
134+
assert exported.attributes["langfuse.observation.metadata.source"] == '"manual"'
135+
assert exported.attributes[LangfuseOtelSpanAttributes.VERSION] == "v1"
136+
assert (
137+
exported.attributes[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL] == "WARNING"
138+
)
139+
assert exported.attributes[
140+
LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE
141+
] == ("test-status")
142+
if creation_method != "span":
143+
assert exported.start_time == exported.end_time
144+
145+
75146
class TestOTelBase:
76147
"""Base class for OTEL tests with common fixtures and helper methods."""
77148

0 commit comments

Comments
 (0)