-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
60 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from typing import TYPE_CHECKING, Any | ||
from typing import Any | ||
|
||
try: | ||
from opentelemetry.instrumentation.pymongo import PymongoInstrumentor | ||
from opentelemetry.instrumentation.pymongo import ( | ||
PymongoInstrumentor, | ||
dummy_callback, # type: ignore[reportUnknownVariableType] | ||
) | ||
|
||
from logfire.integrations.pymongo import FailedHook, RequestHook, ResponseHook | ||
except ImportError: | ||
raise RuntimeError( | ||
'`logfire.instrument_pymongo()` requires the `opentelemetry-instrumentation-pymongo` package.\n' | ||
'You can install this with:\n' | ||
" pip install 'logfire[pymongo]'" | ||
) | ||
|
||
if TYPE_CHECKING: | ||
from pymongo.monitoring import CommandFailedEvent, CommandStartedEvent, CommandSucceededEvent | ||
from typing_extensions import Protocol, TypedDict, Unpack | ||
|
||
class RequestHook(Protocol): | ||
def __call__(self, span: Any, event: CommandStartedEvent) -> None: ... | ||
|
||
class ResponseHook(Protocol): | ||
def __call__(self, span: Any, event: CommandSucceededEvent) -> None: ... | ||
|
||
class FailedHook(Protocol): | ||
def __call__(self, span: Any, event: CommandFailedEvent) -> None: ... | ||
|
||
class PymongoInstrumentKwargs(TypedDict, total=False): | ||
request_hook: RequestHook | None | ||
response_hook: ResponseHook | None | ||
failed_hook: FailedHook | None | ||
capture_statement: bool | None | ||
skip_dep_check: bool | ||
|
||
|
||
def instrument_pymongo(**kwargs: Unpack[PymongoInstrumentKwargs]) -> None: | ||
def instrument_pymongo( | ||
*, | ||
capture_statement: bool, | ||
request_hook: RequestHook | None, | ||
response_hook: ResponseHook | None, | ||
failed_hook: FailedHook | None, | ||
**kwargs: Any, | ||
) -> None: | ||
"""Instrument the `pymongo` module so that spans are automatically created for each operation. | ||
See the `Logfire.instrument_pymongo` method for details. | ||
""" | ||
PymongoInstrumentor().instrument(**kwargs) | ||
PymongoInstrumentor().instrument( | ||
request_hook=request_hook or dummy_callback, | ||
response_hook=response_hook or dummy_callback, | ||
failed_hook=failed_hook or dummy_callback, | ||
capture_statement=capture_statement, | ||
**kwargs, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
from typing import Callable | ||
|
||
from opentelemetry.trace import Span | ||
from pymongo.monitoring import CommandFailedEvent, CommandStartedEvent, CommandSucceededEvent | ||
|
||
RequestHook = Callable[[Span, CommandStartedEvent], None] | ||
"""A hook that is called when a command is started.""" | ||
|
||
ResponseHook = Callable[[Span, CommandSucceededEvent], None] | ||
"""A hook that is called when a command is succeeded.""" | ||
|
||
FailedHook = Callable[[Span, CommandFailedEvent], None] | ||
"""A hook that is called when a command is failed.""" |