Skip to content

Commit 896d4f4

Browse files
authored
Ensure lazy start span (#124)
* Ensure initialization during lazy init * remove debug
1 parent 6c10776 commit 896d4f4

File tree

3 files changed

+46
-25
lines changed

3 files changed

+46
-25
lines changed

quotientai/client.py

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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
*,

quotientai/resources/auth.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ def authenticate(self):
1414
# Set the user_id if successful
1515
if response and isinstance(response, dict) and "user_id" in response:
1616
self._client._user = response["user_id"]
17+
print(f"✅ Authentication successful! User ID set to: {response['user_id']}")
18+
else:
19+
# Print what we got for debugging
20+
print(f"❌ Authentication response missing user_id. Response: {response}")
1721

1822
return response
1923

quotientai/tracing/core.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,9 @@ def _create_otlp_exporter(self, endpoint: str, headers: dict):
147147
def _get_user(self):
148148
"""
149149
Get user_id from client.
150-
Returns the user_id or None if not found.
150+
Returns the user_id or "None" if not found.
151151
"""
152-
if hasattr(self._client, "_user"):
152+
if hasattr(self._client, "_user") and self._client._user is not None:
153153
return self._client._user
154154
return "None"
155155

@@ -344,15 +344,16 @@ def start_span(self, name: str):
344344
# Initialize tracer if not already done
345345
if self.tracer is None:
346346
self.tracer = get_tracer(
347-
TRACER_NAME, tracer_provider=get_tracer_provider()
347+
TRACER_NAME,
348+
tracer_provider=get_tracer_provider(),
348349
)
349350

350351
# Use only configured values - no parameters accepted
351352
if not self._app_name or not self._environment:
352353
logger.error(
353-
"tracer must be initialized with valid inputs before using trace(). Double check your inputs and try again."
354+
"tracer must be initialized with valid inputs before using start_span(). Double check your inputs and try again."
354355
)
355-
return lambda func: func
356+
return None
356357

357358
return self.tracer.start_span(name)
358359

0 commit comments

Comments
 (0)