|
2 | 2 | import random |
3 | 3 | import string |
4 | 4 | import time |
5 | | -from unittest.mock import patch |
6 | 5 |
|
| 6 | +from unittest.mock import patch |
7 | 7 | import pytest |
| 8 | +from openai import OpenAI |
| 9 | +from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam |
| 10 | +from opentelemetry.sdk.trace import Tracer |
| 11 | +from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter |
| 12 | +from opentelemetry.sdk.trace import ReadableSpan |
| 13 | + |
8 | 14 | from humanloop.utilities.flow import flow |
9 | 15 | from humanloop.utilities.prompt import prompt |
10 | 16 | from humanloop.utilities.tool import tool |
11 | 17 | from humanloop.otel.constants import HUMANLOOP_FILE_KEY |
12 | 18 | from humanloop.otel.exporter import HumanloopSpanExporter |
13 | 19 | from humanloop.otel.helpers import read_from_opentelemetry_span |
14 | | -from openai import OpenAI |
15 | | -from openai.types.chat.chat_completion_message_param import ChatCompletionMessageParam |
16 | | -from opentelemetry.sdk.trace import Tracer |
17 | | -from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter |
18 | 20 |
|
19 | 21 |
|
20 | 22 | def _test_scenario( |
@@ -255,14 +257,17 @@ def test_flow_decorator_hl_exporter_flow_inside_flow( |
255 | 257 | time.sleep(3) |
256 | 258 |
|
257 | 259 | # THEN 5 spans are arrive at the exporter in the following order: |
258 | | - # 0. Intercepted OpenAI call, which is ignored by the exporter |
259 | | - # 1. Tool Span (called after the OpenAI call but before the Prompt Span finishes) |
260 | | - # 2. Prompt Span |
261 | | - # 3. Nested Flow Span |
262 | | - # 4. Flow Span |
263 | 260 | assert len(mock_export_method.call_args_list) == 5 |
264 | | - # THEN the last uploaded span is the larger Flow |
265 | | - # THEN the second to last uploaded span is the nested Flow |
266 | | - flow_span = mock_export_method.call_args_list[4][0][0][0] |
267 | | - nested_flow_span = mock_export_method.call_args_list[3][0][0][0] |
268 | | - assert nested_flow_span.parent.span_id == flow_span.context.span_id |
| 261 | + |
| 262 | + # THEN one of the flows is nested inside the other |
| 263 | + spans: list[ReadableSpan] = [mock_export_method.call_args_list[i][0][0][0] for i in range(1, 5)] |
| 264 | + counter = 0 |
| 265 | + for span in spans: |
| 266 | + if span.name == "humanloop.flow": |
| 267 | + counter += 1 |
| 268 | + if span.parent: |
| 269 | + nested_flow_span = span |
| 270 | + else: |
| 271 | + flow_span = span |
| 272 | + # We are certain span_id exists for these 2 spans |
| 273 | + assert nested_flow_span.parent.span_id == flow_span.context.span_id # type: ignore |
0 commit comments