Skip to content

Commit 3ebdb63

Browse files
authored
Support PEP 561 to opentelemetry-instrumentation-pymongo (#3136)
1 parent a606fab commit 3ebdb63

File tree

3 files changed

+25
-10
lines changed

3 files changed

+25
-10
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1717
([#3100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3100))
1818
- Add support to database stability opt-in in `_semconv` utilities and add tests
1919
([#3111](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3111))
20+
- `opentelemetry-opentelemetry-pymongo` Add `py.typed` file to enable PEP 561
21+
([#3136](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3136))
2022
- `opentelemetry-opentelemetry-requests` Add `py.typed` file to enable PEP 561
2123
([#3135](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3135))
2224
- `opentelemetry-instrumentation-system-metrics` Add `py.typed` file to enable PEP 561

instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py

+23-10
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,10 @@ def failed_hook(span, event):
7575
7676
"""
7777

78+
from __future__ import annotations
79+
7880
from logging import getLogger
79-
from typing import Callable, Collection
81+
from typing import Any, Callable, Collection, TypeVar
8082

8183
from pymongo import monitoring
8284

@@ -88,7 +90,7 @@ def failed_hook(span, event):
8890
from opentelemetry.instrumentation.pymongo.version import __version__
8991
from opentelemetry.instrumentation.utils import is_instrumentation_enabled
9092
from opentelemetry.semconv.trace import DbSystemValues, SpanAttributes
91-
from opentelemetry.trace import SpanKind, get_tracer
93+
from opentelemetry.trace import SpanKind, Tracer, get_tracer
9294
from opentelemetry.trace.span import Span
9395
from opentelemetry.trace.status import Status, StatusCode
9496

@@ -98,14 +100,21 @@ def failed_hook(span, event):
98100
ResponseHookT = Callable[[Span, monitoring.CommandSucceededEvent], None]
99101
FailedHookT = Callable[[Span, monitoring.CommandFailedEvent], None]
100102

103+
CommandEvent = TypeVar(
104+
"CommandEvent",
105+
monitoring.CommandStartedEvent,
106+
monitoring.CommandSucceededEvent,
107+
monitoring.CommandFailedEvent,
108+
)
109+
101110

102-
def dummy_callback(span, event): ...
111+
def dummy_callback(span: Span, event: CommandEvent): ...
103112

104113

105114
class CommandTracer(monitoring.CommandListener):
106115
def __init__(
107116
self,
108-
tracer,
117+
tracer: Tracer,
109118
request_hook: RequestHookT = dummy_callback,
110119
response_hook: ResponseHookT = dummy_callback,
111120
failed_hook: FailedHookT = dummy_callback,
@@ -195,10 +204,12 @@ def failed(self, event: monitoring.CommandFailedEvent):
195204
_LOG.exception(hook_exception)
196205
span.end()
197206

198-
def _pop_span(self, event):
207+
def _pop_span(self, event: CommandEvent) -> Span | None:
199208
return self._span_dict.pop(_get_span_dict_key(event), None)
200209

201-
def _get_statement_by_command_name(self, command_name, event):
210+
def _get_statement_by_command_name(
211+
self, command_name: str, event: CommandEvent
212+
) -> str:
202213
statement = command_name
203214
command_attribute = COMMAND_TO_ATTRIBUTE_MAPPING.get(command_name)
204215
command = event.command.get(command_attribute)
@@ -207,14 +218,16 @@ def _get_statement_by_command_name(self, command_name, event):
207218
return statement
208219

209220

210-
def _get_span_dict_key(event):
221+
def _get_span_dict_key(
222+
event: CommandEvent,
223+
) -> int | tuple[int, tuple[str, int | None]]:
211224
if event.connection_id is not None:
212225
return event.request_id, event.connection_id
213226
return event.request_id
214227

215228

216229
class PymongoInstrumentor(BaseInstrumentor):
217-
_commandtracer_instance = None # type CommandTracer
230+
_commandtracer_instance: CommandTracer | None = None
218231
# The instrumentation for PyMongo is based on the event listener interface
219232
# https://api.mongodb.com/python/current/api/pymongo/monitoring.html.
220233
# This interface only allows to register listeners and does not provide
@@ -225,7 +238,7 @@ class PymongoInstrumentor(BaseInstrumentor):
225238
def instrumentation_dependencies(self) -> Collection[str]:
226239
return _instruments
227240

228-
def _instrument(self, **kwargs):
241+
def _instrument(self, **kwargs: Any):
229242
"""Integrate with pymongo to trace it using event listener.
230243
https://api.mongodb.com/python/current/api/pymongo/monitoring.html
231244
@@ -259,6 +272,6 @@ def _instrument(self, **kwargs):
259272
# If already created, just enable it
260273
self._commandtracer_instance.is_enabled = True
261274

262-
def _uninstrument(self, **kwargs):
275+
def _uninstrument(self, **kwargs: Any):
263276
if self._commandtracer_instance is not None:
264277
self._commandtracer_instance.is_enabled = False

instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/py.typed

Whitespace-only changes.

0 commit comments

Comments
 (0)