|
1 | 1 | import json |
| 2 | +import os |
2 | 3 | import uuid |
3 | 4 | import pytest |
4 | 5 |
|
5 | | -from lmnr import Laminar, observe |
| 6 | +from lmnr import Laminar, observe, LaminarSpanContext |
6 | 7 | from opentelemetry.sdk.trace.export.in_memory_span_exporter import InMemorySpanExporter |
7 | 8 | from opentelemetry import trace |
8 | 9 |
|
@@ -1563,3 +1564,48 @@ def func2(): |
1563 | 1564 | str(uuid.UUID(int=func1_span.get_span_context().span_id)), |
1564 | 1565 | str(uuid.UUID(int=func2_span.get_span_context().span_id)), |
1565 | 1566 | ) |
| 1567 | + |
| 1568 | + |
| 1569 | +def test_span_context_from_env_variables_observe(span_exporter: InMemorySpanExporter): |
| 1570 | + test_trace_id = "01234567-89ab-cdef-0123-456789abcdef" |
| 1571 | + test_span_id = "00000000-0000-0000-0123-456789abcdef" |
| 1572 | + test_span_id2 = "00000000-0000-0000-fedc-ba9876543210" |
| 1573 | + old_val = os.getenv("LMNR_SPAN_CONTEXT") |
| 1574 | + test_context = LaminarSpanContext( |
| 1575 | + trace_id=test_trace_id, |
| 1576 | + span_id=test_span_id2, |
| 1577 | + span_path=["grandparent", "parent"], |
| 1578 | + span_ids_path=[test_span_id, test_span_id2], |
| 1579 | + ) |
| 1580 | + |
| 1581 | + os.environ["LMNR_SPAN_CONTEXT"] = str(test_context) |
| 1582 | + |
| 1583 | + Laminar._initialize_context_from_env() |
| 1584 | + |
| 1585 | + @observe() |
| 1586 | + def test(): |
| 1587 | + pass |
| 1588 | + |
| 1589 | + test() |
| 1590 | + |
| 1591 | + spans = span_exporter.get_finished_spans() |
| 1592 | + assert len(spans) == 1 |
| 1593 | + span_id = spans[0].get_span_context().span_id |
| 1594 | + assert spans[0].name == "test" |
| 1595 | + assert spans[0].attributes["lmnr.span.instrumentation_source"] == "python" |
| 1596 | + assert spans[0].attributes["lmnr.span.path"] == ( |
| 1597 | + "grandparent", |
| 1598 | + "parent", |
| 1599 | + "test", |
| 1600 | + ) |
| 1601 | + assert spans[0].attributes["lmnr.span.ids_path"] == ( |
| 1602 | + str(uuid.UUID(test_span_id)), |
| 1603 | + str(uuid.UUID(test_span_id2)), |
| 1604 | + str(uuid.UUID(int=span_id)), |
| 1605 | + ) |
| 1606 | + assert spans[0].get_span_context().trace_id == uuid.UUID(test_trace_id).int |
| 1607 | + assert spans[0].parent.span_id == uuid.UUID(test_span_id2).int |
| 1608 | + if old_val: |
| 1609 | + os.environ["LMNR_SPAN_CONTEXT"] = old_val |
| 1610 | + else: |
| 1611 | + os.environ.pop("LMNR_SPAN_CONTEXT", None) |
0 commit comments