Skip to content

Commit 03ff5e5

Browse files
authored
Move base_url, id_generator, and ns_timestamp_generator parameters of logfire.configure() into advanced: AdvancedOptions (#432)
1 parent ceb271a commit 03ff5e5

File tree

23 files changed

+164
-198
lines changed

23 files changed

+164
-198
lines changed

logfire-api/logfire_api/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,9 @@ def __init__(self, *args, **kwargs) -> None: ...
179179
class ScrubbingOptions:
180180
def __init__(self, *args, **kwargs) -> None: ...
181181

182+
class AdvancedOptions:
183+
def __init__(self, *args, **kwargs) -> None: ...
184+
182185
class PydanticPlugin:
183186
def __init__(self, *args, **kwargs) -> None: ...
184187

logfire-api/logfire_api/__init__.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from ._internal.auto_trace import AutoTraceModule as AutoTraceModule
22
from ._internal.auto_trace.rewrite_ast import no_auto_trace as no_auto_trace
3-
from ._internal.config import ConsoleOptions as ConsoleOptions, METRICS_PREFERRED_TEMPORALITY as METRICS_PREFERRED_TEMPORALITY, PydanticPlugin as PydanticPlugin, configure as configure
3+
from ._internal.config import AdvancedOptions as AdvancedOptions, ConsoleOptions as ConsoleOptions, METRICS_PREFERRED_TEMPORALITY as METRICS_PREFERRED_TEMPORALITY, PydanticPlugin as PydanticPlugin, configure as configure
44
from ._internal.constants import LevelName as LevelName
55
from ._internal.exporters.file import load_file as load_spans_from_file
66
from ._internal.main import Logfire as Logfire, LogfireSpan as LogfireSpan
@@ -11,7 +11,7 @@ from .integrations.structlog import LogfireProcessor as StructlogProcessor
1111
from .version import VERSION as VERSION
1212
from logfire.sampling import SamplingOptions as SamplingOptions
1313

14-
__all__ = ['Logfire', 'LogfireSpan', 'LevelName', 'ConsoleOptions', 'PydanticPlugin', 'configure', 'span', 'instrument', 'log', 'trace', 'debug', 'notice', 'info', 'warn', 'error', 'exception', 'fatal', 'force_flush', 'log_slow_async_callbacks', 'install_auto_tracing', 'instrument_fastapi', 'instrument_openai', 'instrument_anthropic', 'instrument_asyncpg', 'instrument_httpx', 'instrument_celery', 'instrument_requests', 'instrument_psycopg', 'instrument_django', 'instrument_flask', 'instrument_starlette', 'instrument_aiohttp_client', 'instrument_sqlalchemy', 'instrument_redis', 'instrument_pymongo', 'instrument_mysql', 'instrument_system_metrics', 'AutoTraceModule', 'with_tags', 'with_settings', 'shutdown', 'load_spans_from_file', 'no_auto_trace', 'METRICS_PREFERRED_TEMPORALITY', 'ScrubMatch', 'ScrubbingOptions', 'VERSION', 'suppress_instrumentation', 'StructlogProcessor', 'LogfireLoggingHandler', 'SamplingOptions']
14+
__all__ = ['Logfire', 'LogfireSpan', 'LevelName', 'AdvancedOptions', 'ConsoleOptions', 'PydanticPlugin', 'configure', 'span', 'instrument', 'log', 'trace', 'debug', 'notice', 'info', 'warn', 'error', 'exception', 'fatal', 'force_flush', 'log_slow_async_callbacks', 'install_auto_tracing', 'instrument_fastapi', 'instrument_openai', 'instrument_anthropic', 'instrument_asyncpg', 'instrument_httpx', 'instrument_celery', 'instrument_requests', 'instrument_psycopg', 'instrument_django', 'instrument_flask', 'instrument_starlette', 'instrument_aiohttp_client', 'instrument_sqlalchemy', 'instrument_redis', 'instrument_pymongo', 'instrument_mysql', 'instrument_system_metrics', 'AutoTraceModule', 'with_tags', 'with_settings', 'shutdown', 'load_spans_from_file', 'no_auto_trace', 'METRICS_PREFERRED_TEMPORALITY', 'ScrubMatch', 'ScrubbingOptions', 'VERSION', 'suppress_instrumentation', 'StructlogProcessor', 'LogfireLoggingHandler', 'SamplingOptions']
1515

1616
DEFAULT_LOGFIRE_INSTANCE = Logfire()
1717
span = DEFAULT_LOGFIRE_INSTANCE.span

logfire-api/logfire_api/_internal/config.pyi

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ class ConsoleOptions:
4848
include_timestamps: bool = ...
4949
verbose: bool = ...
5050
min_log_level: LevelName = ...
51+
show_project_link: bool = ...
52+
53+
@dataclass
54+
class AdvancedOptions:
55+
"""Options primarily used for testing by Logfire developers."""
56+
base_url: str = ...
57+
id_generator: IdGenerator = ...
58+
ns_timestamp_generator: Callable[[], int] = ...
5159

5260
@dataclass
5361
class PydanticPlugin:
@@ -58,7 +66,7 @@ class PydanticPlugin:
5866

5967
class DeprecatedKwargs(TypedDict): ...
6068

61-
def configure(*, send_to_logfire: bool | Literal['if-token-present'] | None = None, token: str | None = None, service_name: str | None = None, service_version: str | None = None, console: ConsoleOptions | Literal[False] | None = None, show_summary: bool | None = None, config_dir: Path | str | None = None, data_dir: Path | str | None = None, base_url: str | None = None, id_generator: IdGenerator | None = None, ns_timestamp_generator: Callable[[], int] | None = None, additional_span_processors: Sequence[SpanProcessor] | None = None, additional_metric_readers: Sequence[MetricReader] | None = None, pydantic_plugin: PydanticPlugin | None = None, fast_shutdown: bool = False, scrubbing: ScrubbingOptions | Literal[False] | None = None, inspect_arguments: bool | None = None, sampling: SamplingOptions | None = None, **deprecated_kwargs: Unpack[DeprecatedKwargs]) -> None:
69+
def configure(*, send_to_logfire: bool | Literal['if-token-present'] | None = None, token: str | None = None, service_name: str | None = None, service_version: str | None = None, console: ConsoleOptions | Literal[False] | None = None, config_dir: Path | str | None = None, data_dir: Path | str | None = None, additional_span_processors: Sequence[SpanProcessor] | None = None, additional_metric_readers: Sequence[MetricReader] | None = None, pydantic_plugin: PydanticPlugin | None = None, scrubbing: ScrubbingOptions | Literal[False] | None = None, inspect_arguments: bool | None = None, sampling: SamplingOptions | None = None, advanced: AdvancedOptions | None = None, **deprecated_kwargs: Unpack[DeprecatedKwargs]) -> None:
6270
"""Configure the logfire SDK.
6371
6472
Args:
@@ -72,26 +80,20 @@ def configure(*, send_to_logfire: bool | Literal['if-token-present'] | None = No
7280
console: Whether to control terminal output. If `None` uses the `LOGFIRE_CONSOLE_*` environment variables,
7381
otherwise defaults to `ConsoleOption(colors='auto', indent_spans=True, include_timestamps=True, verbose=False)`.
7482
If `False` disables console output. It can also be disabled by setting `LOGFIRE_CONSOLE` environment variable to `false`.
75-
show_summary: When to print a summary of the Logfire setup including a link to the dashboard. If `None` uses the `LOGFIRE_SHOW_SUMMARY` environment variable, otherwise
76-
defaults to `True`.
7783
config_dir: Directory that contains the `pyproject.toml` file for this project. If `None` uses the
7884
`LOGFIRE_CONFIG_DIR` environment variable, otherwise defaults to the current working directory.
7985
data_dir: Directory to store credentials, and logs. If `None` uses the `LOGFIRE_CREDENTIALS_DIR` environment variable, otherwise defaults to `'.logfire'`.
80-
base_url: Root URL for the Logfire API. If `None` uses the `LOGFIRE_BASE_URL` environment variable, otherwise defaults to https://logfire-api.pydantic.dev.
81-
id_generator: Generator for span IDs. Defaults to `RandomIdGenerator()` from the OpenTelemetry SDK.
82-
ns_timestamp_generator: Generator for nanosecond timestamps. Defaults to [`time.time_ns`][time.time_ns] from the
83-
Python standard library.
8486
additional_span_processors: Span processors to use in addition to the default processor which exports spans to Logfire's API.
8587
additional_metric_readers: Sequence of metric readers to be used in addition to the default reader
8688
which exports metrics to Logfire's API.
8789
pydantic_plugin: Configuration for the Pydantic plugin. If `None` uses the `LOGFIRE_PYDANTIC_PLUGIN_*` environment
8890
variables, otherwise defaults to `PydanticPlugin(record='off')`.
89-
fast_shutdown: Whether to shut down exporters and providers quickly, mostly used for tests. Defaults to `False`.
9091
scrubbing: Options for scrubbing sensitive data. Set to `False` to disable.
9192
inspect_arguments: Whether to enable
9293
[f-string magic](https://logfire.pydantic.dev/docs/guides/onboarding_checklist/add_manual_tracing/#f-strings).
9394
If `None` uses the `LOGFIRE_INSPECT_ARGUMENTS` environment variable.
9495
Defaults to `True` if and only if the Python version is at least 3.11.
96+
advanced: Advanced options primarily used for testing by Logfire developers.
9597
sampling: Sampling options. See the [sampling guide](https://logfire.pydantic.dev/docs/guides/advanced/sampling/).
9698
"""
9799

@@ -106,32 +108,28 @@ class _LogfireConfigData:
106108
`dataclasses.asdict` in `integrations/executors.py` to get a dict with just the attributes from
107109
`_LogfireConfigData`, and none of the attributes added in `LogfireConfig`.
108110
"""
109-
base_url: str
110111
send_to_logfire: bool | Literal['if-token-present']
111112
token: str | None
112113
service_name: str
113114
service_version: str | None
114115
console: ConsoleOptions | Literal[False] | None
115-
show_summary: bool
116116
data_dir: Path
117-
id_generator: IdGenerator
118-
ns_timestamp_generator: Callable[[], int]
119117
additional_span_processors: Sequence[SpanProcessor] | None
120118
pydantic_plugin: PydanticPlugin
121-
fast_shutdown: bool
122119
scrubbing: ScrubbingOptions | Literal[False]
123120
inspect_arguments: bool
124121
sampling: SamplingOptions
122+
advanced: AdvancedOptions
125123

126124
class LogfireConfig(_LogfireConfigData):
127-
def __init__(self, base_url: str | None = None, send_to_logfire: bool | None = None, token: str | None = None, service_name: str | None = None, service_version: str | None = None, console: ConsoleOptions | Literal[False] | None = None, show_summary: bool | None = None, config_dir: Path | None = None, data_dir: Path | None = None, id_generator: IdGenerator | None = None, ns_timestamp_generator: Callable[[], int] | None = None, additional_span_processors: Sequence[SpanProcessor] | None = None, additional_metric_readers: Sequence[MetricReader] | None = None, pydantic_plugin: PydanticPlugin | None = None, fast_shutdown: bool = False, scrubbing: ScrubbingOptions | Literal[False] | None = None, inspect_arguments: bool | None = None, sampling: SamplingOptions | None = None) -> None:
125+
def __init__(self, send_to_logfire: bool | None = None, token: str | None = None, service_name: str | None = None, service_version: str | None = None, console: ConsoleOptions | Literal[False] | None = None, config_dir: Path | None = None, data_dir: Path | None = None, additional_span_processors: Sequence[SpanProcessor] | None = None, additional_metric_readers: Sequence[MetricReader] | None = None, pydantic_plugin: PydanticPlugin | None = None, scrubbing: ScrubbingOptions | Literal[False] | None = None, inspect_arguments: bool | None = None, sampling: SamplingOptions | None = None, advanced: AdvancedOptions | None = None) -> None:
128126
"""Create a new LogfireConfig.
129127
130128
Users should never need to call this directly, instead use `logfire.configure`.
131129
132130
See `_LogfireConfigData` for parameter documentation.
133131
"""
134-
def configure(self, base_url: str | None, send_to_logfire: bool | Literal['if-token-present'] | None, token: str | None, service_name: str | None, service_version: str | None, console: ConsoleOptions | Literal[False] | None, show_summary: bool | None, config_dir: Path | None, data_dir: Path | None, id_generator: IdGenerator | None, ns_timestamp_generator: Callable[[], int] | None, additional_span_processors: Sequence[SpanProcessor] | None, additional_metric_readers: Sequence[MetricReader] | None, pydantic_plugin: PydanticPlugin | None, fast_shutdown: bool, scrubbing: ScrubbingOptions | Literal[False] | None, inspect_arguments: bool | None, sampling: SamplingOptions | None) -> None: ...
132+
def configure(self, send_to_logfire: bool | Literal['if-token-present'] | None, token: str | None, service_name: str | None, service_version: str | None, console: ConsoleOptions | Literal[False] | None, config_dir: Path | None, data_dir: Path | None, additional_span_processors: Sequence[SpanProcessor] | None, additional_metric_readers: Sequence[MetricReader] | None, pydantic_plugin: PydanticPlugin | None, scrubbing: ScrubbingOptions | Literal[False] | None, inspect_arguments: bool | None, sampling: SamplingOptions | None, advanced: AdvancedOptions | None) -> None: ...
135133
def initialize(self) -> ProxyTracerProvider:
136134
"""Configure internals to start exporting traces and metrics."""
137135
def force_flush(self, timeout_millis: int = 30000) -> bool:

logfire-api/logfire_api/_internal/config_params.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,25 +29,25 @@ class _DefaultCallback:
2929
"""
3030
callback: Callable[[], Any]
3131

32-
BASE_URL: Incomplete
3332
SEND_TO_LOGFIRE: Incomplete
3433
TOKEN: Incomplete
3534
SERVICE_NAME: Incomplete
3635
SERVICE_VERSION: Incomplete
37-
SHOW_SUMMARY: Incomplete
3836
CREDENTIALS_DIR: Incomplete
3937
CONSOLE: Incomplete
4038
CONSOLE_COLORS: Incomplete
4139
CONSOLE_SPAN_STYLE: Incomplete
4240
CONSOLE_INCLUDE_TIMESTAMP: Incomplete
4341
CONSOLE_VERBOSE: Incomplete
4442
CONSOLE_MIN_LOG_LEVEL: Incomplete
43+
CONSOLE_SHOW_PROJECT_LINK: Incomplete
4544
PYDANTIC_PLUGIN_RECORD: Incomplete
4645
PYDANTIC_PLUGIN_INCLUDE: Incomplete
4746
PYDANTIC_PLUGIN_EXCLUDE: Incomplete
4847
TRACE_SAMPLE_RATE: Incomplete
4948
INSPECT_ARGUMENTS: Incomplete
5049
IGNORE_NO_CONFIG: Incomplete
50+
BASE_URL: Incomplete
5151
CONFIG_PARAMS: Incomplete
5252

5353
@dataclass

logfire/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ._internal.auto_trace import AutoTraceModule
1010
from ._internal.auto_trace.rewrite_ast import no_auto_trace
11-
from ._internal.config import METRICS_PREFERRED_TEMPORALITY, ConsoleOptions, PydanticPlugin, configure
11+
from ._internal.config import METRICS_PREFERRED_TEMPORALITY, AdvancedOptions, ConsoleOptions, PydanticPlugin, configure
1212
from ._internal.constants import LevelName
1313
from ._internal.exporters.file import load_file as load_spans_from_file
1414
from ._internal.main import Logfire, LogfireSpan
@@ -84,6 +84,7 @@ def loguru_handler() -> dict[str, Any]:
8484
'Logfire',
8585
'LogfireSpan',
8686
'LevelName',
87+
'AdvancedOptions',
8788
'ConsoleOptions',
8889
'PydanticPlugin',
8990
'configure',

logfire/_internal/async_.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def log_slow_callbacks(logfire: Logfire, slow_duration: float) -> ContextManager
2727
"""
2828
original_run = asyncio.events.Handle._run
2929
logfire = logfire.with_settings(custom_scope_suffix='asyncio')
30-
timer = logfire.config.ns_timestamp_generator
30+
timer = logfire.config.advanced.ns_timestamp_generator
3131
slow_duration *= ONE_SECOND_IN_NANOSECONDS
3232

3333
def patched_run(self: asyncio.events.Handle) -> Any:

logfire/_internal/auto_trace/rewrite_ast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ def logfire_method_call_node(self, node: ast.FunctionDef | ast.AsyncFunctionDef,
113113
config = self.logfire_instance._config # type: ignore
114114

115115
# Local vars for fast access
116-
timer = config.ns_timestamp_generator
116+
timer = config.advanced.ns_timestamp_generator
117117
min_duration = self.min_duration
118118

119119
# This needs to be as fast as possible since it's the cost of auto-tracing a function

logfire/_internal/cli.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def parse_backfill(args: argparse.Namespace) -> None: # pragma: no cover
124124
sys.exit(1)
125125

126126
logfire_url = cast(str, args.logfire_url)
127-
logfire.configure(data_dir=data_dir, base_url=logfire_url)
127+
logfire.configure(data_dir=data_dir, advanced=logfire.AdvancedOptions(base_url=logfire_url))
128128
config = logfire_config.GLOBAL_CONFIG
129129
config.initialize()
130130
token = config.token
@@ -143,7 +143,7 @@ def reader() -> Iterator[bytes]:
143143
yield data
144144
progress.update(task, completed=f.tell())
145145

146-
url = urljoin(config.base_url, '/v1/backfill/traces')
146+
url = urljoin(config.advanced.base_url, '/v1/backfill/traces')
147147
response = requests.post(
148148
url, data=reader(), headers={'Authorization': token, 'User-Agent': f'logfire/{VERSION}'}
149149
)

0 commit comments

Comments
 (0)