Skip to content

Commit 091bc6f

Browse files
authored
Add metrics parameter to logfire.configure() (#444)
1 parent 2304f95 commit 091bc6f

File tree

12 files changed

+143
-49
lines changed

12 files changed

+143
-49
lines changed

logfire-api/logfire_api/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ def __init__(self, *args, **kwargs) -> None: ...
182182
class AdvancedOptions:
183183
def __init__(self, *args, **kwargs) -> None: ...
184184

185+
class MetricsOptions:
186+
def __init__(self, *args, **kwargs) -> None: ...
187+
185188
class PydanticPlugin:
186189
def __init__(self, *args, **kwargs) -> None: ...
187190

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 AdvancedOptions as AdvancedOptions, 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, MetricsOptions as MetricsOptions, 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', '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']
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', 'MetricsOptions']
1515

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

logfire-api/logfire_api/_internal/config.pyi

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ from logfire.exceptions import LogfireConfigError as LogfireConfigError
2525
from logfire.sampling import SamplingOptions as SamplingOptions
2626
from logfire.sampling._tail_sampling import TailSamplingProcessor as TailSamplingProcessor
2727
from logfire.version import VERSION as VERSION
28-
from opentelemetry import metrics
28+
from opentelemetry.metrics import Meter
2929
from opentelemetry.sdk.metrics.export import MetricReader as MetricReader
3030
from opentelemetry.sdk.trace import SpanProcessor
3131
from opentelemetry.sdk.trace.id_generator import IdGenerator
@@ -64,9 +64,17 @@ class PydanticPlugin:
6464
include: set[str] = ...
6565
exclude: set[str] = ...
6666

67+
@dataclass
68+
class MetricsOptions:
69+
"""Configuration of metrics.
70+
71+
This only has one option for now, but it's a place to add more related options in the future.
72+
"""
73+
additional_readers: Sequence[MetricReader] = ...
74+
6775
class DeprecatedKwargs(TypedDict): ...
6876

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:
77+
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, metrics: MetricsOptions | Literal[False] | 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:
7078
"""Configure the logfire SDK.
7179
7280
Args:
@@ -84,8 +92,8 @@ def configure(*, send_to_logfire: bool | Literal['if-token-present'] | None = No
8492
`LOGFIRE_CONFIG_DIR` environment variable, otherwise defaults to the current working directory.
8593
data_dir: Directory to store credentials, and logs. If `None` uses the `LOGFIRE_CREDENTIALS_DIR` environment variable, otherwise defaults to `'.logfire'`.
8694
additional_span_processors: Span processors to use in addition to the default processor which exports spans to Logfire's API.
87-
additional_metric_readers: Sequence of metric readers to be used in addition to the default reader
88-
which exports metrics to Logfire's API.
95+
metrics: Set to `False` to disable sending all metrics,
96+
or provide a `MetricsOptions` object to configure metrics, e.g. additional metric readers.
8997
pydantic_plugin: Configuration for the Pydantic plugin. If `None` uses the `LOGFIRE_PYDANTIC_PLUGIN_*` environment
9098
variables, otherwise defaults to `PydanticPlugin(record='off')`.
9199
scrubbing: Options for scrubbing sensitive data. Set to `False` to disable.
@@ -122,14 +130,14 @@ class _LogfireConfigData:
122130
advanced: AdvancedOptions
123131

124132
class LogfireConfig(_LogfireConfigData):
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:
133+
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, metrics: MetricsOptions | Literal[False] | 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:
126134
"""Create a new LogfireConfig.
127135
128136
Users should never need to call this directly, instead use `logfire.configure`.
129137
130138
See `_LogfireConfigData` for parameter documentation.
131139
"""
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: ...
140+
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, metrics: MetricsOptions | Literal[False] | None, pydantic_plugin: PydanticPlugin | None, scrubbing: ScrubbingOptions | Literal[False] | None, inspect_arguments: bool | None, sampling: SamplingOptions | None, advanced: AdvancedOptions | None) -> None: ...
133141
def initialize(self) -> ProxyTracerProvider:
134142
"""Configure internals to start exporting traces and metrics."""
135143
def force_flush(self, timeout_millis: int = 30000) -> bool:
@@ -159,7 +167,7 @@ class LogfireConfig(_LogfireConfigData):
159167
"""
160168
def warn_if_not_initialized(self, message: str): ...
161169
@cached_property
162-
def meter(self) -> metrics.Meter:
170+
def meter(self) -> Meter:
163171
"""Get a meter from this `LogfireConfig`.
164172
165173
This is used internally and should not be called by users of the SDK.

logfire/__init__.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,14 @@
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, AdvancedOptions, ConsoleOptions, PydanticPlugin, configure
11+
from ._internal.config import (
12+
METRICS_PREFERRED_TEMPORALITY,
13+
AdvancedOptions,
14+
ConsoleOptions,
15+
MetricsOptions,
16+
PydanticPlugin,
17+
configure,
18+
)
1219
from ._internal.constants import LevelName
1320
from ._internal.exporters.file import load_file as load_spans_from_file
1421
from ._internal.main import Logfire, LogfireSpan
@@ -134,4 +141,5 @@ def loguru_handler() -> dict[str, Any]:
134141
'StructlogProcessor',
135142
'LogfireLoggingHandler',
136143
'SamplingOptions',
144+
'MetricsOptions',
137145
)

0 commit comments

Comments
 (0)