@@ -469,7 +469,10 @@ class QuotientTracer:
469469 """
470470
471471 def __init__ (
472- self , tracing_resource : Optional [TracingResource ], lazy_init : bool = False
472+ self ,
473+ tracing_resource : Optional [TracingResource ],
474+ lazy_init : bool = False ,
475+ ensure_init_callback = None ,
473476 ):
474477 self .tracing_resource = tracing_resource
475478 self .app_name : Optional [str ] = None
@@ -478,6 +481,7 @@ def __init__(
478481 self .instruments : Optional [List [Any ]] = None
479482 self .detections : Optional [List [str ]] = None
480483 self .lazy_init = lazy_init
484+ self .ensure_init_callback = ensure_init_callback
481485
482486 self ._configured = False
483487
@@ -548,22 +552,23 @@ def start_span(self, name: str):
548552 """
549553 Start a span.
550554 """
551- if self .lazy_init :
552- return self ._create_lazy_decorator (name )
553- else :
554- # For non-lazy_init, use the original behavior
555- if not self .tracing_resource :
556- logger .error (
557- "tracer is not configured. Please call init() before tracing."
558- )
559- return lambda func : func
560- # Warn if not configured but still allow tracing since resource is available
561- if not self ._configured :
562- logger .warning (
563- "tracer is not explicitly configured. Consider calling tracer.init() for full configuration."
564- )
565-
566- return self .tracing_resource .start_span (name )
555+ if not self ._configured :
556+ logger .error (
557+ "tracer is not configured. Please call init() before using start_span()."
558+ )
559+ return None
560+
561+ # For lazy_init, ensure initialization if tracing_resource is not available
562+ if self .lazy_init and not self .tracing_resource and self .ensure_init_callback :
563+ self .ensure_init_callback ()
564+
565+ if not self .tracing_resource :
566+ logger .error (
567+ "tracer is configured but tracing resource is not available. This may indicate an initialization issue."
568+ )
569+ return None
570+
571+ return self .tracing_resource .start_span (name )
567572
568573 def _create_lazy_decorator (self , name : Optional [str ] = None ):
569574 """
@@ -645,7 +650,7 @@ def __init__(self, api_key: Optional[str] = None, lazy_init: bool = False):
645650 # Always create a tracer instance for lazy_init mode to avoid decorator errors
646651 if lazy_init :
647652 # Create a minimal tracer instance that can handle decorators
648- self .tracer = QuotientTracer (None , lazy_init = True )
653+ self .tracer = QuotientTracer (None , lazy_init = True , ensure_init_callback = self . _ensure_initialized )
649654 else :
650655 self .tracer = None
651656
@@ -680,6 +685,10 @@ def _ensure_initialized(self):
680685 try :
681686 _client = _BaseQuotientClient (self .api_key )
682687 self .auth = resources .AuthResource (_client )
688+
689+ # Authenticate FIRST to set the user ID
690+ self .auth .authenticate ()
691+
683692 self .logs = resources .LogsResource (_client )
684693 self .tracing = resources .TracingResource (_client )
685694 self .traces = resources .TracesResource (_client )
@@ -690,10 +699,16 @@ def _ensure_initialized(self):
690699 # Update tracer with the actual tracing resource if it was created in lazy mode
691700 if self .lazy_init and self .tracer :
692701 self .tracer .tracing_resource = self .tracing
702+ # Configure the tracing resource with the settings from the tracer
703+ if self .tracer ._configured :
704+ self .tracing .init (
705+ app_name = self .tracer .app_name ,
706+ environment = self .tracer .environment ,
707+ instruments = self .tracer .instruments ,
708+ )
693709 else :
694- self .tracer = QuotientTracer (self .tracing , lazy_init = self .lazy_init )
710+ self .tracer = QuotientTracer (self .tracing , lazy_init = self .lazy_init , ensure_init_callback = self . _ensure_initialized )
695711
696- self .auth .authenticate ()
697712 self ._initialized = True
698713 except Exception as e :
699714 error_msg = (
@@ -718,6 +733,7 @@ def configure(self, api_key: str):
718733 self ._ensure_initialized ()
719734 return self
720735
736+
721737 def log (
722738 self ,
723739 * ,
0 commit comments