Skip to content

Commit 15b4b02

Browse files
committed
Revert "param version"
This reverts commit 33a7b63.
1 parent 33a7b63 commit 15b4b02

File tree

3 files changed

+5
-95
lines changed

3 files changed

+5
-95
lines changed

docs/guides/onboarding-checklist/add-manual-tracing.md

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -234,17 +234,6 @@ my_function(3, 4)
234234
# Logs: Applying my_function to x=3 and y=4
235235
```
236236

237-
You can also access the span directly within your instrumented function by adding a `logfire_span` parameter:
238-
239-
```python
240-
@logfire.instrument('Processing {x=}')
241-
def process_data(x: int, logfire_span: logfire.LogfireSpan | None = None) -> int:
242-
# Access and modify the span directly
243-
if logfire_span:
244-
logfire_span.message = f'Custom message for x={x}'
245-
return x * 2
246-
```
247-
248237
!!! note
249238

250239
- The [`@logfire.instrument`][logfire.Logfire.instrument] decorator MUST be applied first, i.e., UNDER any other decorators.

logfire/_internal/instrument.py

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -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
)

tests/test_logfire.py

Lines changed: 0 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1273,66 +1273,6 @@ def run(a: str) -> Model:
12731273
)
12741274

12751275

1276-
def test_instrument_with_logfire_span_parameter(exporter: TestExporter):
1277-
@logfire.instrument('Calling foo with {x=}')
1278-
def foo(x: int, logfire_span: logfire.LogfireSpan | None = None) -> int:
1279-
# Test that we can access the span and modify its message
1280-
assert logfire_span is not None
1281-
logfire_span.message = f'Modified message for x={x}'
1282-
return x * 2
1283-
1284-
result = foo(5)
1285-
assert result == 10
1286-
1287-
spans = exporter.exported_spans_as_dict(_strip_function_qualname=False)
1288-
assert len(spans) == 1
1289-
span = spans[0]
1290-
assert span['attributes']['logfire.msg'] == 'Modified message for x=5'
1291-
assert span['attributes']['x'] == 5
1292-
1293-
1294-
def test_instrument_with_logfire_span_parameter_async(exporter: TestExporter):
1295-
@logfire.instrument('Calling async foo with {x=}')
1296-
async def foo(x: int, logfire_span: logfire.LogfireSpan | None = None) -> int:
1297-
# Test that we can access the span and modify its message
1298-
assert logfire_span is not None
1299-
logfire_span.message = f'Async modified message for x={x}'
1300-
return x * 3
1301-
1302-
async def run_test():
1303-
return await foo(7)
1304-
1305-
import asyncio
1306-
1307-
result = asyncio.run(run_test())
1308-
assert result == 21
1309-
1310-
spans = exporter.exported_spans_as_dict(_strip_function_qualname=False)
1311-
assert len(spans) == 1
1312-
span = spans[0]
1313-
assert span['attributes']['logfire.msg'] == 'Async modified message for x=7'
1314-
assert span['attributes']['x'] == 7
1315-
1316-
1317-
def test_instrument_with_logfire_span_parameter_extract_args_false(exporter: TestExporter):
1318-
@logfire.instrument('Calling foo', extract_args=False)
1319-
def foo(x: int, logfire_span: logfire.LogfireSpan | None = None) -> int:
1320-
# Test that we can access the span and modify its message
1321-
assert logfire_span is not None
1322-
logfire_span.message = f'Extract args false message for x={x}'
1323-
return x * 4
1324-
1325-
result = foo(3)
1326-
assert result == 12
1327-
1328-
spans = exporter.exported_spans_as_dict(_strip_function_qualname=False)
1329-
assert len(spans) == 1
1330-
span = spans[0]
1331-
assert span['attributes']['logfire.msg'] == 'Extract args false message for x=3'
1332-
# x should not be in attributes since extract_args=False
1333-
assert 'x' not in span['attributes']
1334-
1335-
13361276
def test_validation_error_on_span(exporter: TestExporter) -> None:
13371277
class Model(BaseModel, plugin_settings={'logfire': {'record': 'off'}}):
13381278
a: int

0 commit comments

Comments
 (0)