Skip to content

Commit e3ff40e

Browse files
authored
f-string magic (#151)
1 parent ffde7b3 commit e3ff40e

File tree

12 files changed

+1225
-44
lines changed

12 files changed

+1225
-44
lines changed

logfire/_internal/config.py

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ def configure(
156156
fast_shutdown: bool = False,
157157
scrubbing_patterns: Sequence[str] | None = None,
158158
scrubbing_callback: ScrubCallback | None = None,
159+
inspect_arguments: bool | None = None,
159160
) -> None:
160161
"""Configure the logfire SDK.
161162
@@ -210,6 +211,9 @@ def configure(
210211
If it returns `None`, the value is redacted.
211212
Otherwise, the returned value replaces the matched value.
212213
The function accepts a single argument of type [`logfire.ScrubMatch`][logfire.ScrubMatch].
214+
inspect_arguments: Whether to enable f-string magic.
215+
If `None` uses the `LOGFIRE_INSPECT_ARGUMENTS` environment variable.
216+
Defaults to `True` if and only if the Python version is at least 3.11.
213217
"""
214218
GLOBAL_CONFIG.configure(
215219
base_url=base_url,
@@ -234,6 +238,7 @@ def configure(
234238
fast_shutdown=fast_shutdown,
235239
scrubbing_patterns=scrubbing_patterns,
236240
scrubbing_callback=scrubbing_callback,
241+
inspect_arguments=inspect_arguments,
237242
)
238243

239244

@@ -337,9 +342,10 @@ def _load_configuration(
337342
metric_readers: Sequence[MetricReader] | None,
338343
logfire_api_session: requests.Session | None,
339344
pydantic_plugin: PydanticPlugin | None,
340-
fast_shutdown: bool = False,
341-
scrubbing_patterns: Sequence[str] | None = None,
342-
scrubbing_callback: ScrubCallback | None = None,
345+
fast_shutdown: bool,
346+
scrubbing_patterns: Sequence[str] | None,
347+
scrubbing_callback: ScrubCallback | None,
348+
inspect_arguments: bool | None,
343349
) -> None:
344350
"""Merge the given parameters with the environment variables file configurations."""
345351
param_manager = ParamManager.create(config_dir)
@@ -357,6 +363,11 @@ def _load_configuration(
357363
self.show_summary = param_manager.load_param('show_summary', show_summary)
358364
self.data_dir = param_manager.load_param('data_dir', data_dir)
359365
self.collect_system_metrics = param_manager.load_param('collect_system_metrics', collect_system_metrics)
366+
self.inspect_arguments = param_manager.load_param('inspect_arguments', inspect_arguments)
367+
if self.inspect_arguments and sys.version_info[:2] <= (3, 8):
368+
raise LogfireConfigError(
369+
'Inspecting arguments is only supported in Python 3.9+ and only recommended in Python 3.11+.'
370+
)
360371

361372
# We save `scrubbing_patterns` and `scrubbing_callback` just so that they can be serialized and deserialized.
362373
self.scrubbing_patterns = scrubbing_patterns
@@ -425,6 +436,7 @@ def __init__(
425436
fast_shutdown: bool = False,
426437
scrubbing_patterns: Sequence[str] | None = None,
427438
scrubbing_callback: ScrubCallback | None = None,
439+
inspect_arguments: bool | None = None,
428440
) -> None:
429441
"""Create a new LogfireConfig.
430442
@@ -457,6 +469,7 @@ def __init__(
457469
fast_shutdown=fast_shutdown,
458470
scrubbing_patterns=scrubbing_patterns,
459471
scrubbing_callback=scrubbing_callback,
472+
inspect_arguments=inspect_arguments,
460473
)
461474
# initialize with no-ops so that we don't impact OTEL's global config just because logfire is installed
462475
# that is, we defer setting logfire as the otel global config until `configure` is called
@@ -490,9 +503,10 @@ def configure(
490503
metric_readers: Sequence[MetricReader] | None,
491504
logfire_api_session: requests.Session | None,
492505
pydantic_plugin: PydanticPlugin | None,
493-
fast_shutdown: bool = False,
494-
scrubbing_patterns: Sequence[str] | None = None,
495-
scrubbing_callback: ScrubCallback | None = None,
506+
fast_shutdown: bool,
507+
scrubbing_patterns: Sequence[str] | None,
508+
scrubbing_callback: ScrubCallback | None,
509+
inspect_arguments: bool | None,
496510
) -> None:
497511
with self._lock:
498512
self._initialized = False
@@ -519,6 +533,7 @@ def configure(
519533
fast_shutdown,
520534
scrubbing_patterns,
521535
scrubbing_callback,
536+
inspect_arguments,
522537
)
523538
self.initialize()
524539

logfire/_internal/config_params.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class _DefaultCallback:
104104
"""Set of items that should be excluded from Logfire Pydantic plugin instrumentation."""
105105
TRACE_SAMPLE_RATE = ConfigParam(env_vars=['LOGFIRE_TRACE_SAMPLE_RATE', 'OTEL_TRACES_SAMPLER_ARG'], allow_file_config=True, default=1.0, tp=float)
106106
"""Default sampling ratio for traces. Can be overridden by the `logfire.sample_rate` attribute of a span."""
107+
INSPECT_ARGUMENTS = ConfigParam(env_vars=['LOGFIRE_INSPECT_ARGUMENTS'], allow_file_config=True, default=sys.version_info[:2] >= (3, 11), tp=bool)
108+
"""Whether to enable the f-string magic feature. On by default for Python 3.11 and above."""
107109
# fmt: on
108110

109111
CONFIG_PARAMS = {
@@ -126,6 +128,7 @@ class _DefaultCallback:
126128
'pydantic_plugin_record': PYDANTIC_PLUGIN_RECORD,
127129
'pydantic_plugin_include': PYDANTIC_PLUGIN_INCLUDE,
128130
'pydantic_plugin_exclude': PYDANTIC_PLUGIN_EXCLUDE,
131+
'inspect_arguments': INSPECT_ARGUMENTS,
129132
}
130133

131134

0 commit comments

Comments
 (0)