@@ -63,27 +63,19 @@ def decorator(func: Callable[P, R]) -> Callable[P, R]:
6363 attributes = get_attributes (func , msg_template , tags )
6464 open_span = get_open_span (logfire , attributes , span_name , extract_args , func )
6565
66- # Check if function has logfire_span parameter
67- sig = inspect .signature (func )
68- has_logfire_span_param = 'logfire_span' in sig .parameters
69-
7066 if inspect .isgeneratorfunction (func ):
7167 if not allow_generator :
7268 warnings .warn (GENERATOR_WARNING_MESSAGE , stacklevel = 2 )
7369
7470 def wrapper (* func_args : P .args , ** func_kwargs : P .kwargs ): # type: ignore
75- with open_span (* func_args , ** func_kwargs ) as span :
76- if has_logfire_span_param :
77- func_kwargs ['logfire_span' ] = span
71+ with open_span (* func_args , ** func_kwargs ):
7872 yield from func (* func_args , ** func_kwargs )
7973 elif inspect .isasyncgenfunction (func ):
8074 if not allow_generator :
8175 warnings .warn (GENERATOR_WARNING_MESSAGE , stacklevel = 2 )
8276
8377 async def wrapper (* func_args : P .args , ** func_kwargs : P .kwargs ): # type: ignore
84- with open_span (* func_args , ** func_kwargs ) as span :
85- if has_logfire_span_param :
86- func_kwargs ['logfire_span' ] = span
78+ with open_span (* func_args , ** func_kwargs ):
8779 # `yield from` is invalid syntax in an async function.
8880 # This loop is not quite equivalent, because `yield from` also handles things like
8981 # sending values to the subgenerator.
@@ -98,8 +90,6 @@ async def wrapper(*func_args: P.args, **func_kwargs: P.kwargs): # type: ignore
9890
9991 async def wrapper (* func_args : P .args , ** func_kwargs : P .kwargs ) -> R : # type: ignore
10092 with open_span (* func_args , ** func_kwargs ) as span :
101- if has_logfire_span_param :
102- func_kwargs ['logfire_span' ] = span
10393 result = await func (* func_args , ** func_kwargs )
10494 if record_return :
10595 # open_span returns a FastLogfireSpan, so we can't use span.set_attribute for complex types.
@@ -112,8 +102,6 @@ async def wrapper(*func_args: P.args, **func_kwargs: P.kwargs) -> R: # type: ig
112102 # Same as the above, but without the async/await
113103 def wrapper (* func_args : P .args , ** func_kwargs : P .kwargs ) -> R :
114104 with open_span (* func_args , ** func_kwargs ) as span :
115- if has_logfire_span_param :
116- func_kwargs ['logfire_span' ] = span
117105 result = func (* func_args , ** func_kwargs )
118106 if record_return :
119107 set_user_attributes_on_raw_span (span ._span , {'return' : result })
@@ -134,32 +122,27 @@ def get_open_span(
134122) -> Callable [P , AbstractContextManager [Any ]]:
135123 final_span_name : str = span_name or attributes [ATTRIBUTES_MESSAGE_TEMPLATE_KEY ] # type: ignore
136124
137- # Check if function has logfire_span parameter
138- sig = inspect .signature (func )
139- has_logfire_span_param = 'logfire_span' in sig .parameters
140-
141125 # This is the fast case for when there are no arguments to extract
142126 def open_span (* _ : P .args , ** __ : P .kwargs ): # type: ignore
143- if has_logfire_span_param :
144- return logfire ._span (final_span_name , attributes ) # type: ignore
145127 return logfire ._fast_span (final_span_name , attributes ) # type: ignore
146128
147129 if extract_args is True :
130+ sig = inspect .signature (func )
148131 if sig .parameters : # only extract args if there are any
149132
150133 def open_span (* func_args : P .args , ** func_kwargs : P .kwargs ):
151134 bound = sig .bind (* func_args , ** func_kwargs )
152135 bound .apply_defaults ()
153136 args_dict = bound .arguments
154- if has_logfire_span_param :
155- return logfire ._span (final_span_name , {** attributes , ** args_dict }) # type: ignore
156137 return logfire ._instrument_span_with_args ( # type: ignore
157138 final_span_name , attributes , args_dict
158139 )
159140
160141 return open_span
161142
162143 if extract_args : # i.e. extract_args should be an iterable of argument names
144+ sig = inspect .signature (func )
145+
163146 if isinstance (extract_args , str ):
164147 extract_args = [extract_args ]
165148
@@ -182,8 +165,6 @@ def open_span(*func_args: P.args, **func_kwargs: P.kwargs):
182165 # This line is the only difference from the extract_args=True case
183166 args_dict = {k : args_dict [k ] for k in extract_args_final }
184167
185- if has_logfire_span_param :
186- return logfire ._span (final_span_name , {** attributes , ** args_dict }) # type: ignore
187168 return logfire ._instrument_span_with_args ( # type: ignore
188169 final_span_name , attributes , args_dict
189170 )
0 commit comments