|
| 1 | +import logging |
1 | 2 | from typing import Sequence |
| 3 | +from unittest.mock import patch |
2 | 4 |
|
3 | | -from opentelemetry.sdk.trace import ReadableSpan |
| 5 | +import pytest |
| 6 | +from opentelemetry.sdk.trace import ReadableSpan, TracerProvider |
4 | 7 | from opentelemetry.sdk.trace.export import SpanExporter, SpanExportResult |
5 | 8 |
|
| 9 | +import langfuse._client.span_processor as span_processor_module |
6 | 10 | from langfuse._client.environment_variables import ( |
7 | 11 | LANGFUSE_FLUSH_AT, |
8 | 12 | LANGFUSE_FLUSH_INTERVAL, |
@@ -54,3 +58,41 @@ def test_span_processor_uses_env_flush_settings_when_constructor_omits_them( |
54 | 58 | assert processor._batch_processor._schedule_delay_millis == 3250 |
55 | 59 | finally: |
56 | 60 | processor.shutdown() |
| 61 | + |
| 62 | + |
| 63 | +@pytest.fixture |
| 64 | +def tracer_with_processor(): |
| 65 | + processor = LangfuseSpanProcessor( |
| 66 | + public_key="pk-test", |
| 67 | + secret_key="sk-test", |
| 68 | + base_url="http://localhost:3000", |
| 69 | + span_exporter=NoOpSpanExporter(), |
| 70 | + ) |
| 71 | + provider = TracerProvider() |
| 72 | + provider.add_span_processor(processor) |
| 73 | + yield provider.get_tracer("test-instrumentor") |
| 74 | + processor.shutdown() |
| 75 | + |
| 76 | + |
| 77 | +@pytest.mark.parametrize( |
| 78 | + ("level", "expected_formatter_calls"), |
| 79 | + [(logging.WARNING, 0), (logging.DEBUG, 1)], |
| 80 | +) |
| 81 | +def test_on_end_formats_span_only_when_debug_enabled( |
| 82 | + caplog, tracer_with_processor, level, expected_formatter_calls |
| 83 | +): |
| 84 | + caplog.set_level(level, logger="langfuse") |
| 85 | + |
| 86 | + with patch.object( |
| 87 | + span_processor_module, "span_formatter", return_value="{}" |
| 88 | + ) as span_formatter: |
| 89 | + # gen_ai.* attribute makes the span pass the default export filter |
| 90 | + with tracer_with_processor.start_as_current_span( |
| 91 | + "llm-call", attributes={"gen_ai.system": "test"} |
| 92 | + ): |
| 93 | + pass |
| 94 | + |
| 95 | + assert span_formatter.call_count == expected_formatter_calls |
| 96 | + assert ("Processing span name='llm-call'" in caplog.text) == bool( |
| 97 | + expected_formatter_calls |
| 98 | + ) |
0 commit comments