From f8d4c391a83229fab32938397d8bab3f90f62fc4 Mon Sep 17 00:00:00 2001 From: Alex Hall Date: Fri, 27 Sep 2024 14:59:28 +0200 Subject: [PATCH] Replace `pydantic_plugin` in `logfire.configure()` with `logfire.instrument_pydantic()` (#453) --- docs/integrations/pydantic.md | 28 +++++----- docs/integrations/third-party/mirascope.md | 5 +- docs/why-logfire/pydantic.md | 5 +- logfire-api/logfire_api/__init__.py | 3 ++ logfire-api/logfire_api/__init__.pyi | 3 +- logfire-api/logfire_api/_internal/config.pyi | 16 +++--- .../logfire_api/_internal/config_params.pyi | 2 - logfire-api/logfire_api/_internal/main.pyi | 21 +++++++- logfire-api/logfire_api/_internal/utils.pyi | 19 +++++++ .../logfire_api/integrations/pydantic.pyi | 6 ++- logfire/__init__.py | 2 + logfire/_internal/config.py | 40 ++++++-------- logfire/_internal/config_params.py | 7 +-- logfire/_internal/main.py | 51 +++++++++++++++++- logfire/integrations/pydantic.py | 23 +++++--- tests/conftest.py | 6 +++ tests/test_configure.py | 54 +++++++++++++------ tests/test_pydantic_plugin.py | 21 ++++---- 18 files changed, 215 insertions(+), 97 deletions(-) diff --git a/docs/integrations/pydantic.md b/docs/integrations/pydantic.md index 443fd381d..ca133d32f 100644 --- a/docs/integrations/pydantic.md +++ b/docs/integrations/pydantic.md @@ -1,6 +1,6 @@ # Pydantic -Logfire has a [Pydantic plugin][pydantic-plugin] to instrument [Pydantic][pydantic] models. +Logfire has a Pydantic plugin to instrument [Pydantic][pydantic] models. The plugin provides logs and metrics about model validation. To enable the plugin, do one of the following: @@ -13,35 +13,36 @@ To enable the plugin, do one of the following: pydantic_plugin_record = "all" ``` -- Use the [`pydantic_plugin`][logfire.configure(pydantic_plugin)] parameter in `logfire.configure`, e.g: +- Call [`logfire.instrument_pydantic`][logfire.Logfire.instrument_pydantic] with the desired configuration, e.g: ```py import logfire -logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record='all')) +logfire.instrument_pydantic() # Defaults to record='all' ``` -Note that if you only use the last option then only models defined and imported *after* calling `logfire.configure` +Note that if you only use the last option then only model classes defined and imported *after* calling `logfire.instrument_pydantic` will be instrumented. +!!! note + Remember to call [`logfire.configure()`][logfire.configure] at some point, whether before or after + calling `logfire.instrument_pydantic` and defining model classes. + Model validations will only start being logged after calling `logfire.configure()`. + ## Third party modules By default, third party modules are not instrumented by the plugin to avoid noise. You can enable instrumentation for those using the [`include`][logfire.PydanticPlugin.include] configuration. ```py -import logfire - -logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record='all', include={'openai'})) +logfire.instrument_pydantic(include={'openai'}) ``` You can also disable instrumentation for your own modules using the [`exclude`][logfire.PydanticPlugin.exclude] configuration. ```py -import logfire - -logfire.configure(pydantic_plugin=logfire.PydanticPlugin(record='all', exclude={'app.api.v1'})) +logfire.instrument_pydantic(exclude={'app.api.v1'}) ``` ## Model configuration @@ -60,13 +61,13 @@ class Foo(BaseModel, plugin_settings=PluginSettings(logfire={'record': 'failure' ### Record -The [`record`][logfire.integrations.pydantic.LogfireSettings.record] is used to configure what to record. +The [`record`][logfire.integrations.pydantic.LogfireSettings.record] argument is used to configure what to record. It can be one of the following values: - * `off`: Disable instrumentation. This is default value. - * `all`: Send traces and metrics for all events. + * `all`: Send traces and metrics for all events. This is default value for `logfire.instrument_pydantic`. * `failure`: Send metrics for all validations and traces only for validation failures. * `metrics`: Send only metrics. + * `off`: Disable instrumentation.