@@ -63,27 +63,19 @@ def decorator(func: Callable[P, R]) -> Callable[P, R]:
63
63
attributes = get_attributes (func , msg_template , tags )
64
64
open_span = get_open_span (logfire , attributes , span_name , extract_args , func )
65
65
66
- # Check if function has logfire_span parameter
67
- sig = inspect .signature (func )
68
- has_logfire_span_param = 'logfire_span' in sig .parameters
69
-
70
66
if inspect .isgeneratorfunction (func ):
71
67
if not allow_generator :
72
68
warnings .warn (GENERATOR_WARNING_MESSAGE , stacklevel = 2 )
73
69
74
70
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 ):
78
72
yield from func (* func_args , ** func_kwargs )
79
73
elif inspect .isasyncgenfunction (func ):
80
74
if not allow_generator :
81
75
warnings .warn (GENERATOR_WARNING_MESSAGE , stacklevel = 2 )
82
76
83
77
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 ):
87
79
# `yield from` is invalid syntax in an async function.
88
80
# This loop is not quite equivalent, because `yield from` also handles things like
89
81
# sending values to the subgenerator.
@@ -98,8 +90,6 @@ async def wrapper(*func_args: P.args, **func_kwargs: P.kwargs): # type: ignore
98
90
99
91
async def wrapper (* func_args : P .args , ** func_kwargs : P .kwargs ) -> R : # type: ignore
100
92
with open_span (* func_args , ** func_kwargs ) as span :
101
- if has_logfire_span_param :
102
- func_kwargs ['logfire_span' ] = span
103
93
result = await func (* func_args , ** func_kwargs )
104
94
if record_return :
105
95
# 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
112
102
# Same as the above, but without the async/await
113
103
def wrapper (* func_args : P .args , ** func_kwargs : P .kwargs ) -> R :
114
104
with open_span (* func_args , ** func_kwargs ) as span :
115
- if has_logfire_span_param :
116
- func_kwargs ['logfire_span' ] = span
117
105
result = func (* func_args , ** func_kwargs )
118
106
if record_return :
119
107
set_user_attributes_on_raw_span (span ._span , {'return' : result })
@@ -134,32 +122,27 @@ def get_open_span(
134
122
) -> Callable [P , AbstractContextManager [Any ]]:
135
123
final_span_name : str = span_name or attributes [ATTRIBUTES_MESSAGE_TEMPLATE_KEY ] # type: ignore
136
124
137
- # Check if function has logfire_span parameter
138
- sig = inspect .signature (func )
139
- has_logfire_span_param = 'logfire_span' in sig .parameters
140
-
141
125
# This is the fast case for when there are no arguments to extract
142
126
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
145
127
return logfire ._fast_span (final_span_name , attributes ) # type: ignore
146
128
147
129
if extract_args is True :
130
+ sig = inspect .signature (func )
148
131
if sig .parameters : # only extract args if there are any
149
132
150
133
def open_span (* func_args : P .args , ** func_kwargs : P .kwargs ):
151
134
bound = sig .bind (* func_args , ** func_kwargs )
152
135
bound .apply_defaults ()
153
136
args_dict = bound .arguments
154
- if has_logfire_span_param :
155
- return logfire ._span (final_span_name , {** attributes , ** args_dict }) # type: ignore
156
137
return logfire ._instrument_span_with_args ( # type: ignore
157
138
final_span_name , attributes , args_dict
158
139
)
159
140
160
141
return open_span
161
142
162
143
if extract_args : # i.e. extract_args should be an iterable of argument names
144
+ sig = inspect .signature (func )
145
+
163
146
if isinstance (extract_args , str ):
164
147
extract_args = [extract_args ]
165
148
@@ -182,8 +165,6 @@ def open_span(*func_args: P.args, **func_kwargs: P.kwargs):
182
165
# This line is the only difference from the extract_args=True case
183
166
args_dict = {k : args_dict [k ] for k in extract_args_final }
184
167
185
- if has_logfire_span_param :
186
- return logfire ._span (final_span_name , {** attributes , ** args_dict }) # type: ignore
187
168
return logfire ._instrument_span_with_args ( # type: ignore
188
169
final_span_name , attributes , args_dict
189
170
)
0 commit comments