Skip to content

Commit 8d7778d

Browse files
authored
Remove custom_scope_suffix parameter of Logfire.log (#399)
1 parent 95a43d4 commit 8d7778d

File tree

9 files changed

+43
-29
lines changed

9 files changed

+43
-29
lines changed

logfire-api/logfire_api/_internal/main.pyi

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ class Logfire:
240240
span_name: The span name. If not provided, the `msg_template` will be used.
241241
extract_args: Whether to extract arguments from the function signature and log them as span attributes.
242242
"""
243-
def log(self, level: LevelName | int, msg_template: str, attributes: dict[str, Any] | None = None, tags: Sequence[str] | None = None, exc_info: ExcInfo = False, console_log: bool | None = None, custom_scope_suffix: str | None = None) -> None:
243+
def log(self, level: LevelName | int, msg_template: str, attributes: dict[str, Any] | None = None, tags: Sequence[str] | None = None, exc_info: ExcInfo = False, console_log: bool | None = None) -> None:
244244
"""Log a message.
245245
246246
```py
@@ -261,12 +261,6 @@ class Logfire:
261261
262262
Set to `True` to use the currently handled exception.
263263
console_log: Whether to log to the console, defaults to `True`.
264-
custom_scope_suffix: A custom suffix to append to `logfire.` e.g. `logfire.loguru`.
265-
266-
It should only be used when instrumenting another library with Logfire, such as structlog or loguru.
267-
268-
See the `instrumenting_module_name` parameter on
269-
[TracerProvider.get_tracer][opentelemetry.sdk.trace.TracerProvider.get_tracer] for more info.
270264
"""
271265
def with_tags(self, *tags: str) -> Logfire:
272266
"""A new Logfire instance which always uses the given tags.

logfire-api/logfire_api/integrations/logging.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .. import Logfire as Logfire
12
from .._internal.constants import ATTRIBUTES_LOGGING_ARGS_KEY as ATTRIBUTES_LOGGING_ARGS_KEY, ATTRIBUTES_MESSAGE_KEY as ATTRIBUTES_MESSAGE_KEY, ATTRIBUTES_MESSAGE_TEMPLATE_KEY as ATTRIBUTES_MESSAGE_TEMPLATE_KEY, LOGGING_TO_OTEL_LEVEL_NUMBERS as LOGGING_TO_OTEL_LEVEL_NUMBERS
23
from .._internal.utils import is_instrumentation_suppressed as is_instrumentation_suppressed
34
from _typeshed import Incomplete
@@ -10,7 +11,8 @@ class LogfireLoggingHandler(LoggingHandler):
1011
"""A logging handler that sends logs to **Logfire**."""
1112
custom_scope_suffix: ClassVar[str]
1213
fallback: Incomplete
13-
def __init__(self, level: int | str = ..., fallback: LoggingHandler = ...) -> None: ...
14+
logfire_instance: Incomplete
15+
def __init__(self, level: int | str = ..., fallback: LoggingHandler = ..., logfire_instance: Logfire | None = None) -> None: ...
1416
def emit(self, record: LogRecord) -> None:
1517
"""Send the log to Logfire.
1618

logfire-api/logfire_api/integrations/structlog.pyi

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from .. import Logfire as Logfire
12
from .._internal.constants import ATTRIBUTES_MESSAGE_KEY as ATTRIBUTES_MESSAGE_KEY
23
from _typeshed import Incomplete
34
from structlog.types import EventDict, WrappedLogger
@@ -7,6 +8,7 @@ RESERVED_ATTRS: Incomplete
78
class LogfireProcessor:
89
"""Logfire processor for structlog."""
910
console_log: Incomplete
10-
def __init__(self, *, console_log: bool = False) -> None: ...
11+
logfire_instance: Incomplete
12+
def __init__(self, *, console_log: bool = False, logfire_instance: Logfire | None = None) -> None: ...
1113
def __call__(self, logger: WrappedLogger, name: str, event_dict: EventDict) -> EventDict:
1214
"""A middleware to process structlog event, and send it to **Logfire**."""

logfire/_internal/main.py

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,6 @@ def log(
553553
tags: Sequence[str] | None = None,
554554
exc_info: ExcInfo = False,
555555
console_log: bool | None = None,
556-
custom_scope_suffix: str | None = None,
557556
) -> None:
558557
"""Log a message.
559558
@@ -575,12 +574,6 @@ def log(
575574
576575
Set to `True` to use the currently handled exception.
577576
console_log: Whether to log to the console, defaults to `True`.
578-
custom_scope_suffix: A custom suffix to append to `logfire.` e.g. `logfire.loguru`.
579-
580-
It should only be used when instrumenting another library with Logfire, such as structlog or loguru.
581-
582-
See the `instrumenting_module_name` parameter on
583-
[TracerProvider.get_tracer][opentelemetry.sdk.trace.TracerProvider.get_tracer] for more info.
584577
"""
585578
with handle_internal_errors():
586579
stack_info = get_user_stack_info()
@@ -640,12 +633,7 @@ def log(
640633
otlp_attributes[DISABLE_CONSOLE_KEY] = True
641634
start_time = self._config.ns_timestamp_generator()
642635

643-
if custom_scope_suffix:
644-
tracer = self._get_tracer(is_span_tracer=False, otel_scope=f'logfire.{custom_scope_suffix}')
645-
else:
646-
tracer = self._logs_tracer
647-
648-
span = tracer.start_span(
636+
span = self._logs_tracer.start_span(
649637
msg_template,
650638
attributes=otlp_attributes,
651639
start_time=start_time,

logfire/integrations/logging.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from __future__ import annotations
44

55
from logging import NOTSET, Handler as LoggingHandler, LogRecord, StreamHandler
6-
from typing import Any, ClassVar, Mapping, cast
6+
from typing import TYPE_CHECKING, Any, ClassVar, Mapping, cast
77

88
import logfire
99

@@ -45,15 +45,26 @@
4545
]
4646
)
4747

48+
if TYPE_CHECKING:
49+
from .. import Logfire
50+
4851

4952
class LogfireLoggingHandler(LoggingHandler):
5053
"""A logging handler that sends logs to **Logfire**."""
5154

5255
custom_scope_suffix: ClassVar[str] = 'stdlib.logging'
5356

54-
def __init__(self, level: int | str = NOTSET, fallback: LoggingHandler = StreamHandler()) -> None:
57+
def __init__(
58+
self,
59+
level: int | str = NOTSET,
60+
fallback: LoggingHandler = StreamHandler(),
61+
logfire_instance: Logfire | None = None,
62+
) -> None:
5563
super().__init__(level=level)
5664
self.fallback = fallback
65+
self.logfire_instance = (logfire_instance or logfire.DEFAULT_LOGFIRE_INSTANCE).with_settings(
66+
custom_scope_suffix=self.custom_scope_suffix
67+
)
5768

5869
def emit(self, record: LogRecord) -> None:
5970
"""Send the log to Logfire.
@@ -67,11 +78,10 @@ def emit(self, record: LogRecord) -> None:
6778

6879
attributes = self.fill_attributes(record)
6980

70-
logfire.log(
81+
self.logfire_instance.log(
7182
msg_template=attributes.pop(ATTRIBUTES_MESSAGE_TEMPLATE_KEY, record.msg),
7283
level=LOGGING_TO_OTEL_LEVEL_NUMBERS.get(record.levelno, record.levelno),
7384
attributes=attributes,
74-
custom_scope_suffix=self.custom_scope_suffix,
7585
exc_info=record.exc_info,
7686
)
7787

logfire/integrations/structlog.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,27 +14,36 @@
1414
if TYPE_CHECKING:
1515
from structlog.types import EventDict, WrappedLogger
1616

17+
from .. import Logfire
18+
1719
RESERVED_ATTRS = LOGGING_RESERVED_ATTRS | {'level', 'event', 'timestamp'}
1820
"""Attributes to strip from the event before sending to Logfire."""
1921

2022

2123
class LogfireProcessor:
2224
"""Logfire processor for structlog."""
2325

24-
def __init__(self, *, console_log: bool = False) -> None:
26+
def __init__(
27+
self,
28+
*,
29+
console_log: bool = False,
30+
logfire_instance: Logfire | None = None,
31+
) -> None:
2532
self.console_log = console_log
33+
self.logfire_instance = (logfire_instance or logfire.DEFAULT_LOGFIRE_INSTANCE).with_settings(
34+
custom_scope_suffix='structlog'
35+
)
2636

2737
def __call__(self, logger: WrappedLogger, name: str, event_dict: EventDict) -> EventDict:
2838
"""A middleware to process structlog event, and send it to **Logfire**."""
2939
attributes = {k: v for k, v in event_dict.items() if k not in RESERVED_ATTRS}
3040
level = event_dict.get('level', 'info').lower()
3141
# NOTE: An event can be `None` in structlog. We may want to create a default msg in those cases.
3242
attributes[ATTRIBUTES_MESSAGE_KEY] = message = event_dict.get('event') or 'structlog event'
33-
logfire.log(
43+
self.logfire_instance.log(
3444
level=level, # type: ignore
3545
msg_template=message,
3646
attributes=attributes,
3747
console_log=self.console_log,
38-
custom_scope_suffix='structlog',
3948
)
4049
return event_dict

tests/test_loguru.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,6 @@ def test_loguru(exporter: TestExporter) -> None:
113113
},
114114
]
115115
)
116+
117+
for span in exporter.exported_spans:
118+
assert span.instrumentation_scope.name == 'logfire.loguru' # type: ignore

tests/test_stdlib_logging.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ def test_logging_from_opentelemetry(exporter: TestExporter) -> None:
324324
]
325325
)
326326

327+
for span in exporter.exported_spans:
328+
assert span.instrumentation_scope.name == 'logfire.stdlib.logging' # type: ignore
329+
327330

328331
def test_logging_non_string(exporter: TestExporter, logger: Logger):
329332
logger.error(123)

tests/test_structlog.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,3 +71,6 @@ def test_structlog(exporter: TestExporter, logger: Logger) -> None:
7171
},
7272
]
7373
)
74+
75+
for span in exporter.exported_spans:
76+
assert span.instrumentation_scope.name == 'logfire.structlog' # type: ignore

0 commit comments

Comments
 (0)