Skip to content

Commit 235b13f

Browse files
authored
Add start_span() on quotient.tracer (#123)
* Add start_span() method for tracer * Make sure tracer has the method not just resource * Make sure we initialize the resource
1 parent e539b41 commit 235b13f

File tree

2 files changed

+59
-25
lines changed

2 files changed

+59
-25
lines changed

quotientai/client.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ def init(
500500

501501
# Configure the underlying tracing resource (if available)
502502
if self.tracing_resource:
503-
self.tracing_resource.configure(
503+
self.tracing_resource.init(
504504
app_name=app_name,
505505
environment=environment,
506506
instruments=instruments,
@@ -544,6 +544,27 @@ async def my_async_function():
544544
# Call the tracing resource without parameters since it's now configured
545545
return self.tracing_resource.trace(name)
546546

547+
def start_span(self, name: str):
548+
"""
549+
Start a span.
550+
"""
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)
567+
547568
def _create_lazy_decorator(self, name: Optional[str] = None):
548569
"""
549570
Create a lazy decorator that defers the tracing decision until function execution.

quotientai/tracing/core.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,12 @@ def init(
100100
"""
101101
self.configure(app_name, environment, instruments, detections)
102102
detections_str = ",".join(detections) if detections else None
103-
self._setup_auto_collector(app_name, environment, instruments, detections_str)
103+
self._setup_auto_collector(
104+
app_name=app_name,
105+
environment=environment,
106+
instruments=tuple(instruments),
107+
detections=detections_str,
108+
)
104109

105110
def get_vector_db_instrumentors(self):
106111
"""
@@ -228,6 +233,7 @@ def _setup_auto_collector(
228233
if instruments:
229234
for instrument in instruments:
230235
instrument.instrument()
236+
231237

232238
# Initialize tracer if not already done
233239
if self.tracer is None:
@@ -272,20 +278,19 @@ def my_other_function():
272278
def decorator(func):
273279
span_name = name if name is not None else func.__qualname__
274280

281+
self._setup_auto_collector(
282+
app_name=self._app_name,
283+
environment=self._environment,
284+
instruments=(
285+
tuple(self._instruments)
286+
if self._instruments is not None
287+
else None
288+
),
289+
detections=self._detections,
290+
)
291+
275292
@functools.wraps(func)
276293
def sync_func_wrapper(*args, **kwargs):
277-
278-
self._setup_auto_collector(
279-
app_name=self._app_name,
280-
environment=self._environment,
281-
instruments=(
282-
tuple(self._instruments)
283-
if self._instruments is not None
284-
else None
285-
),
286-
detections=self._detections,
287-
)
288-
289294
# if there is no tracer, just run the function normally
290295
if self.tracer is None:
291296
return func(*args, **kwargs)
@@ -307,17 +312,6 @@ def sync_func_wrapper(*args, **kwargs):
307312

308313
@functools.wraps(func)
309314
async def async_func_wrapper(*args, **kwargs):
310-
self._setup_auto_collector(
311-
app_name=self._app_name,
312-
environment=self._environment,
313-
instruments=(
314-
tuple(self._instruments)
315-
if self._instruments is not None
316-
else None
317-
),
318-
detections=self._detections,
319-
)
320-
321315
if self.tracer is None:
322316
return await func(*args, **kwargs)
323317

@@ -343,6 +337,25 @@ async def async_func_wrapper(*args, **kwargs):
343337

344338
return decorator
345339

340+
def start_span(self, name: str):
341+
"""
342+
Start a span.
343+
"""
344+
# Initialize tracer if not already done
345+
if self.tracer is None:
346+
self.tracer = get_tracer(
347+
TRACER_NAME, tracer_provider=get_tracer_provider()
348+
)
349+
350+
# Use only configured values - no parameters accepted
351+
if not self._app_name or not self._environment:
352+
logger.error(
353+
"tracer must be initialized with valid inputs before using trace(). Double check your inputs and try again."
354+
)
355+
return lambda func: func
356+
357+
return self.tracer.start_span(name)
358+
346359
def _cleanup(self):
347360
"""
348361
Internal cleanup method registered with atexit.

0 commit comments

Comments
 (0)