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
7 changes: 4 additions & 3 deletions langfuse/_client/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -1125,9 +1125,10 @@ def start_observation(
"""
if as_type == "event":
timestamp = time_ns()
event_span = self._langfuse_client._otel_tracer.start_span(
name=name, start_time=timestamp
)
with otel_trace_api.use_span(self._otel_span):
event_span = self._langfuse_client._otel_tracer.start_span(
name=name, start_time=timestamp
)
return cast(
LangfuseEvent,
LangfuseEvent(
Expand Down
71 changes: 71 additions & 0 deletions tests/unit/test_otel.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import logging
from contextlib import ExitStack
from datetime import datetime
from hashlib import sha256
from typing import List, Sequence
Expand Down Expand Up @@ -72,6 +73,76 @@ def generate_span_id(self) -> int:
return self._span_ids.pop(0)


@pytest.mark.parametrize("active_context", ["none", "parent", "unrelated"])
@pytest.mark.parametrize("creation_method", ["event", "create_event", "span"])
def test_child_observation_uses_explicit_parent(
langfuse_memory_client, memory_exporter, active_context, creation_method
):
client = langfuse_memory_client
with ExitStack() as stack:
active = (
stack.enter_context(client.start_as_current_observation(name="active"))
if active_context != "none"
else None
)
parent = (
active
if active_context == "parent"
else client.start_observation(name="manual-parent")
)
current_id = client.get_current_observation_id()
attributes = {
"name": "child",
"input": {"step": "started"},
"output": {"step": "finished"},
"metadata": {"source": "manual"},
"version": "v1",
"level": "WARNING",
"status_message": "test-status",
}
if creation_method == "create_event":
child = parent.create_event(**attributes)
else:
child = parent.start_observation(as_type=creation_method, **attributes)
if creation_method == "span":
child.end()

assert client.get_current_observation_id() == current_id
if parent is not active:
parent.end()

client.flush()
spans = memory_exporter.get_finished_spans()
child_spans = [span for span in spans if span.name == "child"]
assert len(child_spans) == 1
exported = child_spans[0]
assert exported.parent is not None
assert format(exported.parent.span_id, "016x") == parent.id
assert format(exported.context.trace_id, "032x") == parent.trace_id
assert format(exported.context.span_id, "016x") == child.id
expected_type = "span" if creation_method == "span" else "event"
assert (
exported.attributes[LangfuseOtelSpanAttributes.OBSERVATION_TYPE]
== expected_type
)
assert json.loads(
exported.attributes[LangfuseOtelSpanAttributes.OBSERVATION_INPUT]
) == {"step": "started"}
assert json.loads(
exported.attributes[LangfuseOtelSpanAttributes.OBSERVATION_OUTPUT]
) == {"step": "finished"}
assert exported.attributes["langfuse.observation.metadata.source"] == '"manual"'
assert exported.attributes[LangfuseOtelSpanAttributes.VERSION] == "v1"
assert (
exported.attributes[LangfuseOtelSpanAttributes.OBSERVATION_LEVEL] == "WARNING"
)
assert exported.attributes[
LangfuseOtelSpanAttributes.OBSERVATION_STATUS_MESSAGE
] == ("test-status")
if creation_method != "span":
assert exported.start_time == exported.end_time


class TestOTelBase:
"""Base class for OTEL tests with common fixtures and helper methods."""

Expand Down
Loading