@@ -319,58 +319,34 @@ def wrapper(*args, **kwargs):
319319
320320
321321def observe (name ):
322- """
323- A decorator factory that instruments a function for observability using opentelemetry tracing.
324- Args:
325- name (str): The name of the span to be created for tracing.
326- Returns:
327- Callable: A decorator that wraps the target function, extracting opentelemetry tracing context
328- from the incoming request (if available), and starts a new tracing span using
329- the provided name. If no context is found, a new span is started without context.
330- Behavior:
331- - Adds session ID to span attributes if available in kwargs.
332- - Attempts to extract a tracing context from the 'request' argument or from positional arguments.
333- - Starts a tracing span with the extracted context (if present) or as a new trace.
334- - Logs debug information about the tracing context and span creation.
335- - Supports usage both within and outside of HTTP request contexts.
336- Example:
337- @observe("my_function_span")
338- def my_function(request: Request, ...):
339- ...
340- """
341322 def decorator (func ):
342- decorated_func = agent (name )(func )
343- logger .debug ("Decorating function with observe:" , name )
344-
323+ print ("Decorating:" , func .__name__ )
345324 @functools .wraps (func )
346325 def wrapper (* args , ** kwargs ):
347- add_session_id_to_span_attributes (** kwargs )
348- request : Request = kwargs .get ("request" )
326+ print (f"[observe] wrapper called for { func .__name__ } " )
327+ request = kwargs .get ("request" )
328+ print (f"request: { request } " )
329+ print (f"kwargs: { kwargs } " )
349330 if request is None :
350331 for arg in args :
351332 if isinstance (arg , Request ):
352333 request = arg
353334 break
354335
355- # Extract context from request if available
356- ctx = extract (request .headers ) if request else None
357-
358- if ctx :
359- logger .debug (f"Starting span with context: { ctx } " )
360- # Start span with context
361- with tracer .start_as_current_span (name , context = ctx , kind = SpanKind .SERVER ):
362- return decorated_func (* args , ** kwargs )
336+ if request :
337+ print (f"[observe] Request headers: { request .headers } " )
338+ ctx = extract (request .headers ) if request else None
339+ if ctx :
340+ print (f"[observe] Extracted context: { ctx } " )
341+ with tracer .start_as_current_span (name , context = ctx , kind = SpanKind .SERVER ):
342+ return func (* args , ** kwargs )
343+ else :
344+ print ("[observe] No context found in request headers." )
363345 else :
364- logger .debug ("No context found, starting span without context." )
365-
366- # Start span without context
367- # This is useful for cases where we want to start a span without any specific context
368- # e.g., when the function is called outside of an HTTP request context
369- # or when we want to create a fresh new trace or context is not properly propagated.
370- return decorated_func (* args , ** kwargs )
346+ print ("[observe] No request found." )
371347
348+ return func (* args , ** kwargs )
372349 return wrapper
373-
374350 return decorator
375351
376352class CustomFileSpanExporter (SpanExporter ):
0 commit comments