|
1 | 1 | import json |
2 | 2 | import logging |
| 3 | +from contextlib import ExitStack |
3 | 4 | from datetime import datetime |
4 | 5 | from hashlib import sha256 |
5 | 6 | from typing import List, Sequence |
@@ -72,6 +73,76 @@ def generate_span_id(self) -> int: |
72 | 73 | return self._span_ids.pop(0) |
73 | 74 |
|
74 | 75 |
|
| 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 | + |
75 | 146 | class TestOTelBase: |
76 | 147 | """Base class for OTEL tests with common fixtures and helper methods.""" |
77 | 148 |
|
|
0 commit comments