11from contextlib import contextmanager
2- from contextvars import Context , Token
2+ from contextvars import Context
33import warnings
44from lmnr .opentelemetry_lib import TracerManager
55from lmnr .opentelemetry_lib .tracing import TracerWrapper , get_current_context
1414 push_span_context ,
1515 set_association_prop_context ,
1616)
17- from opentelemetry .context import get_value , set_value
18- from lmnr .opentelemetry_lib .tracing .instruments import Instruments
19- from lmnr .opentelemetry_lib .tracing .processor import LaminarSpanProcessor
20- from lmnr .opentelemetry_lib .tracing .span import LaminarSpan
21- from lmnr .opentelemetry_lib .tracing .tracer import get_tracer_with_context
17+ from opentelemetry .context import get_value
2218from lmnr .opentelemetry_lib .tracing .attributes import (
2319 ASSOCIATION_PROPERTIES ,
2420 PARENT_SPAN_IDS_PATH ,
2723 Attributes ,
2824 SPAN_TYPE ,
2925)
26+ from lmnr .opentelemetry_lib .tracing .instruments import Instruments
27+ from lmnr .opentelemetry_lib .tracing .processor import LaminarSpanProcessor
28+ from lmnr .opentelemetry_lib .tracing .span import LaminarSpan
29+ from lmnr .opentelemetry_lib .tracing .tracer import get_tracer_with_context
30+ from lmnr .opentelemetry_lib .tracing .utils import set_association_props_in_context
3031from lmnr .sdk .utils import get_otel_env_var
3132
3233from opentelemetry import trace
@@ -69,54 +70,6 @@ class ParsedParentSpanContext(TypedDict):
6970 metadata : dict [str , Any ] | None
7071
7172
72- def _set_span_association_props_in_context (span : Span ) -> Token [Context ] | None :
73- """Set association properties from span in context.
74-
75- Extracts association properties from span attributes and sets them in the
76- isolated context so child spans can inherit them.
77-
78- Returns the token that needs to be stored on the span for cleanup.
79- """
80- if not isinstance (span , LaminarSpan ):
81- return None
82-
83- props = span .laminar_association_properties
84- user_id_key = f"{ ASSOCIATION_PROPERTIES } .{ USER_ID } "
85- session_id_key = f"{ ASSOCIATION_PROPERTIES } .{ SESSION_ID } "
86- trace_type_key = f"{ ASSOCIATION_PROPERTIES } .{ TRACE_TYPE } "
87-
88- # Extract values from props
89- extracted_user_id = props .get (user_id_key )
90- extracted_session_id = props .get (session_id_key )
91- extracted_trace_type = props .get (trace_type_key )
92-
93- # Extract metadata from props (keys without ASSOCIATION_PROPERTIES prefix)
94- metadata_dict = {}
95- for key , value in props .items ():
96- if not key .startswith (f"{ ASSOCIATION_PROPERTIES } ." ):
97- metadata_dict [key ] = value
98-
99- # Set context with association props
100- current_ctx = get_current_context ()
101- ctx_with_props = current_ctx
102- if extracted_user_id :
103- ctx_with_props = set_value (
104- CONTEXT_USER_ID_KEY , extracted_user_id , ctx_with_props
105- )
106- if extracted_session_id :
107- ctx_with_props = set_value (
108- CONTEXT_SESSION_ID_KEY , extracted_session_id , ctx_with_props
109- )
110- if extracted_trace_type :
111- ctx_with_props = set_value (
112- CONTEXT_TRACE_TYPE_KEY , extracted_trace_type , ctx_with_props
113- )
114- if metadata_dict :
115- ctx_with_props = set_value (CONTEXT_METADATA_KEY , metadata_dict , ctx_with_props )
116-
117- return attach_context (ctx_with_props )
118-
119-
12073def _parse_parent_span_context (
12174 parent_span_context : LaminarSpanContext | dict | str | None ,
12275 logger : logging .Logger ,
@@ -883,7 +836,7 @@ def use_span(
883836 try :
884837 # Set association props in context before push_span_context
885838 # so child spans inherit them
886- assoc_props_token = _set_span_association_props_in_context (span )
839+ assoc_props_token = set_association_props_in_context (span )
887840 if assoc_props_token and isinstance (span , LaminarSpan ):
888841 span ._lmnr_assoc_props_token = assoc_props_token
889842
@@ -1039,7 +992,7 @@ def bar():
1039992
1040993 # Set association props in context before push_span_context
1041994 # so child spans inherit them
1042- assoc_props_token = _set_span_association_props_in_context (span )
995+ assoc_props_token = set_association_props_in_context (span )
1043996 if assoc_props_token and isinstance (span , LaminarSpan ):
1044997 span ._lmnr_assoc_props_token = assoc_props_token
1045998
@@ -1062,7 +1015,7 @@ def set_span_output(cls, output: Any = None):
10621015 output (Any, optional): output of the span. Will be sent as an\
10631016 attribute, so must be json serializable. Defaults to None.
10641017 """
1065- span = cls .current_span ()
1018+ span = cls .get_current_span ()
10661019 if span is None :
10671020 return
10681021 span .set_output (output )
@@ -1094,7 +1047,7 @@ def set_span_attributes(
10941047 Args:
10951048 attributes (dict[Attributes | str, Any]): attributes to set for the span
10961049 """
1097- span = cls .current_span ()
1050+ span = cls .get_current_span ()
10981051 if span == trace .INVALID_SPAN or span is None :
10991052 return
11001053
@@ -1116,7 +1069,7 @@ def get_laminar_span_context(
11161069 if not cls .is_initialized ():
11171070 return None
11181071
1119- span = span or cls .current_span ()
1072+ span = span or cls .get_current_span ()
11201073 if span == trace .INVALID_SPAN or span is None :
11211074 return None
11221075 if not isinstance (span , LaminarSpan ):
@@ -1171,7 +1124,7 @@ def deserialize_span_context(cls, span_context: dict | str) -> LaminarSpanContex
11711124 return LaminarSpanContext .deserialize (span_context )
11721125
11731126 @classmethod
1174- def current_span (cls , context : Context | None = None ) -> LaminarSpan | None :
1127+ def get_current_span (cls , context : Context | None = None ) -> LaminarSpan | None :
11751128 """Get the current active span. If a context is provided, the span will
11761129 be retrieved from that context.
11771130
@@ -1237,15 +1190,15 @@ def set_span_tags(cls, tags: list[str]):
12371190 if not cls .is_initialized ():
12381191 return
12391192
1240- span = cls .current_span ()
1193+ span = cls .get_current_span ()
12411194 if span is None :
12421195 return
12431196 span .set_tags (tags )
12441197
12451198 @classmethod
12461199 def add_span_tags (cls , tags : list [str ]):
12471200 """Add tags to the current span."""
1248- span = cls .current_span ()
1201+ span = cls .get_current_span ()
12491202 if span is None :
12501203 return
12511204 span .add_tags (tags )
@@ -1263,7 +1216,7 @@ def set_trace_session_id(cls, session_id: str | None = None):
12631216
12641217 context = set_association_prop_context (session_id = session_id , attach = True )
12651218
1266- span = cls .current_span (context = context )
1219+ span = cls .get_current_span (context = context )
12671220 if span is None :
12681221 cls .__logger .warning ("No active span to set session id on" )
12691222 return
@@ -1282,7 +1235,7 @@ def set_trace_user_id(cls, user_id: str | None = None):
12821235
12831236 context = set_association_prop_context (user_id = user_id , attach = True )
12841237
1285- span = cls .current_span (context = context )
1238+ span = cls .get_current_span (context = context )
12861239 if span is None :
12871240 cls .__logger .warning ("No active span to set user id on" )
12881241 return
@@ -1300,7 +1253,7 @@ def set_trace_metadata(cls, metadata: dict[str, AttributeValue]):
13001253
13011254 merged_metadata = {** cls .__global_metadata , ** (metadata or {})}
13021255
1303- span = cls .current_span ()
1256+ span = cls .get_current_span ()
13041257 if span is None :
13051258 cls .__logger .warning ("No active span to set metadata on" )
13061259 return
0 commit comments