|
| 1 | +import os |
| 2 | +import pytest |
| 3 | +from opentelemetry.trace import SpanKind |
| 4 | +from langtrace_python_sdk.langtrace import LangtraceConfig |
| 5 | +from langtrace_python_sdk.extensions.langtrace_exporter import LangTraceExporter |
| 6 | +from langtrace_python_sdk.utils.with_root_span import with_langtrace_root_span |
| 7 | +from langtrace_python_sdk.constants.exporter.langtrace_exporter import LANGTRACE_SESSION_ID_HEADER |
| 8 | + |
| 9 | +def test_session_id_from_env(exporter): |
| 10 | + # Test session ID from environment variable |
| 11 | + test_session_id = "test-session-123" |
| 12 | + os.environ["LANGTRACE_SESSION_ID"] = test_session_id |
| 13 | + |
| 14 | + @with_langtrace_root_span() |
| 15 | + def test_function(): |
| 16 | + pass |
| 17 | + |
| 18 | + test_function() |
| 19 | + |
| 20 | + spans = exporter.get_finished_spans() |
| 21 | + assert len(spans) == 1 |
| 22 | + span = spans[0] |
| 23 | + assert span.attributes.get("session.id") == test_session_id |
| 24 | + |
| 25 | + # Cleanup |
| 26 | + del os.environ["LANGTRACE_SESSION_ID"] |
| 27 | + |
| 28 | +def test_session_id_in_config(): |
| 29 | + # Test session ID through LangtraceConfig |
| 30 | + test_session_id = "config-session-456" |
| 31 | + config = LangtraceConfig(session_id=test_session_id) |
| 32 | + exporter = LangTraceExporter( |
| 33 | + api_host="http://test", |
| 34 | + api_key="test-key", |
| 35 | + session_id=config.session_id |
| 36 | + ) |
| 37 | + |
| 38 | + assert exporter.session_id == test_session_id |
| 39 | + |
| 40 | +def test_session_id_in_headers(): |
| 41 | + # Test session ID in HTTP headers |
| 42 | + test_session_id = "header-session-789" |
| 43 | + exporter = LangTraceExporter( |
| 44 | + api_host="http://test", |
| 45 | + api_key="test-key", |
| 46 | + session_id=test_session_id |
| 47 | + ) |
| 48 | + |
| 49 | + # Export method adds headers, so we'll check the headers directly |
| 50 | + headers = { |
| 51 | + "Content-Type": "application/json", |
| 52 | + "x-api-key": "test-key", |
| 53 | + "User-Agent": "LangtraceExporter", |
| 54 | + } |
| 55 | + |
| 56 | + if test_session_id: |
| 57 | + headers[LANGTRACE_SESSION_ID_HEADER] = test_session_id |
| 58 | + |
| 59 | + assert headers[LANGTRACE_SESSION_ID_HEADER] == test_session_id |
0 commit comments