automatic tracking¶
logfire.install_auto_tracing All function calls in the specified module are tracked.
This is achieved by changing how these modules are imported, so the function must be called before importing the modules to be traced.
For example, suppose all your code is in an 应用程序 package, such as app.main,app.server,app.db, etc. You can create another file outside the 应用程序 package instead of using python app/main.py to launch the application, for example:
import logfire
logfire.configure()
logfire.install_auto_tracing(modules=['app'])
from app.main import main
main()
Filter modules to track¶
The modules parameter can be a list of module names. Any submodules within a given module will also be tracked, such as app.main and app.server. Other modules whose names start with the same prefix will not be tracked, such as 苹果.
If one of the strings in the list is not a valid module name, it is treated as a regular expression, so for example modules=['app.*'] except app.main, etc., will also track 苹果.
To gain more control,modules The parameter can be a function that returns for the module that should be traced True. This function will use [AutoTraceModule][logfire 调用。AutoTraceModule] Object that has name sum filename Attributes. For example, this should track all modules in a typical Python installation that are not part of the standard library or third-party package:
import pathlib
import logfire
PYTHON_LIB_ROOT = str(pathlib.Path(pathlib.__file__).parent)
def should_trace(module: logfire.AutoTraceModule) -> bool:
return not module.filename.startswith(PYTHON_LIB_ROOT)
logfire.install_auto_tracing(should_trace)
exclude functions from tracing¶
Once you have selected modules to trace, you may not want to trace every_function in_in those modules. To exclude functions from automatic tracing, add no_auto_trace decorator to them:
import logfire
@logfire.no_auto_trace
def my_function():
# 嵌套函数也将被排除在外
def inner_function():
...
return other_function()
# 此功能*不*从自动跟踪中排除。
# 即使从上面排除的 'my_function' 调用,它仍然会被追踪。
def other_function():
...
# 修饰类的所有方法也将被排除在外
@no_auto_trace
class MyClass:
def my_method(self):
...
Decorator detected on import. Only @no_auto_trace or @logfire.no_auto_trace is supported. Rename/alias functions or modules will not work. It will not be called indirectly through another function.
This decorator returns only unchanged parameters, so the runtime overhead is zero.
Track only functions that exceed the minimum duration¶
A more convenient way to exclude functions from tracing is to set [min_duration][logfire.Logfire.install_auto_tracing(min_duration)] Parameters such as:
# Only trace functions that take longer than 0.1 seconds
logfire.install_auto_tracing(modules=['app'], min_duration=0.1)
This means that you can automatically get observability for heavier parts of your application without too much overhead or data. Note that there are a few caveats:
-
Functions will only start to be traced after they have been run more than
min_durationtimes. This means:-
If it runs faster than the last few times,
min_durationYou will not be able to obtain data about these first calls. -
If you run over
min_durationfor the first time, you also don't get data about that call.
-
-
If a function runs more than
min_durationtimes, it is traced every time the function is called, no matter how long it takes. -
Measuring the duration of a function call still adds a small amount of overhead. For tiny functions that are called very frequently, it is still best to use the
@no_auto_tracedecorator to avoid any overhead. Automatic tracing usingmin_durationstill applies to other unmodified features.
本文总阅读量次