Skip to content

Commit 6be22a1

Browse files
authored
Remove default min_duration for install_auto_tracing (#446)
1 parent 091bc6f commit 6be22a1

File tree

5 files changed

+34
-34
lines changed

5 files changed

+34
-34
lines changed

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ you could create another file outside of the `app` package, e.g:
1414
import logfire
1515

1616
logfire.configure()
17-
logfire.install_auto_tracing(modules=['app'])
17+
logfire.install_auto_tracing(modules=['app'], min_duration=0.01)
1818

1919
from app.main import main
2020

@@ -24,6 +24,20 @@ main()
2424
!!! note
2525
Generator functions will not be traced for reasons explained [here](../advanced/generators.md).
2626

27+
## Only tracing functions above a minimum duration
28+
29+
In most situations you don't want to trace every single function call in your application.
30+
The most convenient way to exclude functions from tracing is with the [`min_duration`][logfire.Logfire.install_auto_tracing(min_duration)] argument. For example, the code snippet above will only trace functions that take longer than 0.01 seconds.
31+
This means you automatically get observability for the heavier parts of your application without too much overhead or data. Note that there are some caveats:
32+
33+
- A function will only start being traced after it runs longer than `min_duration` once. This means that:
34+
- If it runs faster than `min_duration` the first few times, you won't get data about those first calls.
35+
- The first time that it runs longer than `min_duration`, you also won't get data about that call.
36+
- After a function runs longer than `min_duration` once, it will be traced every time it's called afterwards, regardless of how long it takes.
37+
- Measuring the duration of a function call still adds a small overhead. For tiny functions that are called very frequently, it's best to still use the `@no_auto_trace` decorator to avoid any overhead. Auto-tracing with `min_duration` will still work for other undecorated functions.
38+
39+
If you want to trace all function calls from the beginning, set `min_duration=0`.
40+
2741
## Filtering modules to trace
2842

2943
The `modules` argument can be a list of module names.
@@ -89,20 +103,3 @@ Renaming/aliasing either the function or module won't work.
89103
Neither will calling this indirectly via another function.
90104

91105
This decorator simply returns the argument unchanged, so there is zero runtime overhead.
92-
93-
## Only tracing functions above a minimum duration
94-
95-
A more convenient way to exclude functions from tracing is to set the [`min_duration`][logfire.Logfire.install_auto_tracing(min_duration)] argument, e.g:
96-
97-
```python
98-
# Only trace functions that take longer than 0.1 seconds
99-
logfire.install_auto_tracing(modules=['app'], min_duration=0.1)
100-
```
101-
102-
This means you automatically get observability for the heavier parts of your application without too much overhead or data. Note that there are some caveats:
103-
104-
- A function will only start being traced after it runs longer than `min_duration` once. This means that:
105-
- If it runs faster than `min_duration` the first few times, you won't get data about those first calls.
106-
- The first time that it runs longer than `min_duration`, you also won't get data about that call.
107-
- After a function runs longer than `min_duration` once, it will be traced every time it's called afterwards, regardless of how long it takes.
108-
- Measuring the duration of a function call still adds a small overhead. For tiny functions that are called very frequently, it's best to still use the `@no_auto_trace` decorator to avoid any overhead. Auto-tracing with `min_duration` will still work for other undecorated functions.

logfire/_internal/auto_trace/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ def install_auto_tracing(
1616
logfire: Logfire,
1717
modules: Sequence[str] | Callable[[AutoTraceModule], bool],
1818
*,
19+
min_duration: float,
1920
check_imported_modules: Literal['error', 'warn', 'ignore'] = 'error',
20-
min_duration: float = 0,
2121
) -> None:
2222
"""Install automatic tracing.
2323

logfire/_internal/main.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -763,11 +763,14 @@ def install_auto_tracing(
763763
self,
764764
modules: Sequence[str] | Callable[[AutoTraceModule], bool],
765765
*,
766+
min_duration: float,
766767
check_imported_modules: Literal['error', 'warn', 'ignore'] = 'error',
767-
min_duration: float = 0,
768768
) -> None:
769769
"""Install automatic tracing.
770770
771+
See the [Auto-Tracing guide](https://logfire.pydantic.dev/docs/guides/onboarding_checklist/add_auto_tracing/)
772+
for more info.
773+
771774
This will trace all non-generator function calls in the modules specified by the modules argument.
772775
It's equivalent to wrapping the body of every function in matching modules in `with logfire.span(...):`.
773776
@@ -786,13 +789,13 @@ def install_auto_tracing(
786789
Args:
787790
modules: List of module names to trace, or a function which returns True for modules that should be traced.
788791
If a list is provided, any submodules within a given module will also be traced.
792+
min_duration: A minimum duration in seconds for which a function must run before it's traced.
793+
Setting to `0` causes all functions to be traced from the beginning.
794+
Otherwise, the first time(s) each function is called, it will be timed but not traced.
795+
Only after the function has run for at least `min_duration` will it be traced in subsequent calls.
789796
check_imported_modules: If this is `'error'` (the default), then an exception will be raised if any of the
790797
modules in `sys.modules` (i.e. modules that have already been imported) match the modules to trace.
791798
Set to `'warn'` to issue a warning instead, or `'ignore'` to skip the check.
792-
min_duration: An optional minimum duration in seconds for which a function must run before it's traced.
793-
The default is `0`, which means all functions are traced from the beginning.
794-
Otherwise, the first time(s) each function is called, it will be timed but not traced.
795-
Only after the function has run for at least `min_duration` will it be traced in subsequent calls.
796799
"""
797800
install_auto_tracing(self, modules, check_imported_modules=check_imported_modules, min_duration=min_duration)
798801

tests/test_auto_trace.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from inline_snapshot import snapshot
99

1010
import logfire
11-
from logfire import DEFAULT_LOGFIRE_INSTANCE, AutoTraceModule, install_auto_tracing
11+
from logfire import DEFAULT_LOGFIRE_INSTANCE, AutoTraceModule
1212
from logfire._internal.auto_trace import (
1313
AutoTraceModuleAlreadyImportedException,
1414
AutoTraceModuleAlreadyImportedWarning,
@@ -22,9 +22,9 @@
2222
def test_auto_trace_sample(exporter: TestExporter) -> None:
2323
meta_path = sys.meta_path.copy()
2424

25-
logfire.with_tags('testing', 'auto-tracing').install_auto_tracing('tests.auto_trace_samples')
25+
logfire.with_tags('testing', 'auto-tracing').install_auto_tracing('tests.auto_trace_samples', min_duration=0)
2626
# Check that having multiple LogfireFinders doesn't break things
27-
install_auto_tracing('tests.blablabla')
27+
logfire.install_auto_tracing('tests.blablabla', min_duration=0)
2828

2929
assert sys.meta_path[2:] == meta_path
3030
finder = sys.meta_path[1]
@@ -40,7 +40,7 @@ def test_auto_trace_sample(exporter: TestExporter) -> None:
4040
from tests.auto_trace_samples import foo
4141

4242
# Check ignoring imported modules
43-
install_auto_tracing('tests.auto_trace_samples', check_imported_modules='ignore')
43+
logfire.install_auto_tracing('tests.auto_trace_samples', check_imported_modules='ignore', min_duration=0)
4444

4545
loader = foo.__loader__
4646
assert isinstance(loader, LogfireLoader)
@@ -144,16 +144,16 @@ def test_check_already_imported() -> None:
144144
meta_path = sys.meta_path.copy()
145145

146146
with pytest.raises(AutoTraceModuleAlreadyImportedException, match=r"The module 'tests.*' matches modules to trace"):
147-
install_auto_tracing(['tests'])
147+
logfire.install_auto_tracing(['tests'], min_duration=0)
148148

149149
with pytest.raises(ValueError):
150-
install_auto_tracing(['tests'], check_imported_modules='other') # type: ignore
150+
logfire.install_auto_tracing(['tests'], check_imported_modules='other', min_duration=0) # type: ignore
151151

152152
# No tracing installed.
153153
assert sys.meta_path == meta_path
154154

155155
with pytest.warns(AutoTraceModuleAlreadyImportedWarning, match=r"The module 'tests.*' matches modules to trace"):
156-
install_auto_tracing(['tests'], check_imported_modules='warn')
156+
logfire.install_auto_tracing(['tests'], check_imported_modules='warn', min_duration=0)
157157

158158
# The tracing was installed, undo it.
159159
assert sys.meta_path[1:] == meta_path
@@ -438,7 +438,7 @@ def test_generators():
438438

439439

440440
def test_min_duration(exporter: TestExporter):
441-
install_auto_tracing('tests.auto_trace_samples.simple_nesting', min_duration=5)
441+
logfire.install_auto_tracing('tests.auto_trace_samples.simple_nesting', min_duration=5)
442442

443443
from tests.auto_trace_samples import simple_nesting
444444

@@ -491,4 +491,4 @@ def test_min_duration(exporter: TestExporter):
491491

492492
def test_wrong_type_modules():
493493
with pytest.raises(TypeError, match='modules must be a list of strings or a callable'):
494-
install_auto_tracing(123) # type: ignore
494+
logfire.install_auto_tracing(123, min_duration=0) # type: ignore

tests/test_logfire_api.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def test_runtime(logfire_api_factory: Callable[[], ModuleType], module_name: str
113113
logfire__all__.remove('log_slow_async_callbacks')
114114

115115
assert hasattr(logfire_api, 'install_auto_tracing')
116-
logfire_api.install_auto_tracing(modules=['all'])
116+
logfire_api.install_auto_tracing(modules=['all'], min_duration=0)
117117
logfire__all__.remove('install_auto_tracing')
118118

119119
assert hasattr(logfire_api, 'instrument')

0 commit comments

Comments
 (0)