77This module provides functionality for correlating code execution with telemetry data.
88"""
99
10- __version__ = "1.0.0"
11-
12-
13- """
14- Utility functions for adding code information to OpenTelemetry spans.
15- """
16-
17- from typing import Any , Callable
1810from functools import wraps
11+ from typing import Any , Callable
12+
1913from opentelemetry import trace
2014
15+ __version__ = "1.0.0"
16+
2117
2218# Code correlation attribute constants
2319CODE_FUNCTION_NAME = "code.function.name"
2824def _add_code_attributes_to_span (span , func : Callable [..., Any ]) -> None :
2925 """
3026 Add code-related attributes to a span based on a Python function.
31-
27+
3228 This utility method extracts function metadata and adds the following
3329 span attributes:
3430 - CODE_FUNCTION_NAME: The name of the function
3531 - CODE_FILE_PATH: The file path where the function is defined
3632 - CODE_LINE_NUMBER: The line number where the function is defined
37-
33+
3834 Args:
3935 span: The OpenTelemetry span to add attributes to
4036 func: The Python function to extract metadata from
4137 """
4238 if not span .is_recording ():
4339 return
44-
40+
4541 try :
4642 # Get function name
4743 function_name = getattr (func , '__name__' , str (func ))
4844 span .set_attribute (CODE_FUNCTION_NAME , function_name )
49-
45+
5046 # Get function source file from code object
5147 try :
5248 if hasattr (func , '__code__' ):
@@ -56,7 +52,7 @@ def _add_code_attributes_to_span(span, func: Callable[..., Any]) -> None:
5652 # Handle cases where code object is not available
5753 # (e.g., built-in functions, C extensions)
5854 pass
59-
55+
6056 # Get function line number from code object
6157 try :
6258 if hasattr (func , '__code__' ):
@@ -65,7 +61,7 @@ def _add_code_attributes_to_span(span, func: Callable[..., Any]) -> None:
6561 except (AttributeError , TypeError ):
6662 # Handle cases where code object is not available
6763 pass
68-
64+
6965 except Exception :
7066 # Silently handle any unexpected errors to avoid breaking
7167 # the instrumentation flow
@@ -75,37 +71,37 @@ def _add_code_attributes_to_span(span, func: Callable[..., Any]) -> None:
7571def add_code_attributes_to_span (func : Callable [..., Any ]) -> Callable [..., Any ]:
7672 """
7773 Decorator to automatically add code attributes to the current OpenTelemetry span.
78-
74+
7975 This decorator extracts metadata from the decorated function and adds it as
8076 attributes to the current active span. The attributes added are:
8177 - code.function.name: The name of the function
8278 - code.file.path: The file path where the function is defined
8379 - code.line.number: The line number where the function is defined
84-
80+
8581 This decorator supports both synchronous and asynchronous functions.
86-
82+
8783 Usage:
8884 @add_code_attributes_to_span
8985 def my_sync_function():
9086 # Sync function implementation
9187 pass
92-
88+
9389 @add_code_attributes_to_span
9490 async def my_async_function():
9591 # Async function implementation
9692 pass
97-
93+
9894 Args:
9995 func: The function to be decorated
100-
96+
10197 Returns:
10298 The wrapped function with current span code attributes tracing
10399 """
104100 # Detect async functions: check function code object flags or special attributes
105101 # CO_ITERABLE_COROUTINE = 0x80, async functions will have this flag set
106- is_async = (hasattr (func , '__code__' ) and
102+ is_async = (hasattr (func , '__code__' ) and
107103 func .__code__ .co_flags & 0x80 ) or hasattr (func , '_is_coroutine' )
108-
104+
109105 if is_async :
110106 # Async function wrapper
111107 @wraps (func )
@@ -118,10 +114,10 @@ async def async_wrapper(*args, **kwargs):
118114 except Exception :
119115 # Silently handle any unexpected errors
120116 pass
121-
117+
122118 # Call and await the original async function
123119 return await func (* args , ** kwargs )
124-
120+
125121 return async_wrapper
126122 else :
127123 # Sync function wrapper
@@ -135,8 +131,8 @@ def sync_wrapper(*args, **kwargs):
135131 except Exception :
136132 # Silently handle any unexpected errors
137133 pass
138-
134+
139135 # Call the original sync function
140136 return func (* args , ** kwargs )
141-
137+
142138 return sync_wrapper
0 commit comments