Skip to content

Commit 6376b02

Browse files
committed
Support PEP 561 to opentelemetry-instrumentation-pymongo
1 parent 54cbf59 commit 6376b02

File tree

3 files changed

+23
-10
lines changed

3 files changed

+23
-10
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
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

2123
### Fixed
2224

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

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def failed_hook(span, event):
7676
"""
7777

7878
from logging import getLogger
79-
from typing import Callable, Collection
79+
from typing import Any, Callable, Collection, TypeVar
8080

8181
from pymongo import monitoring
8282

@@ -88,7 +88,7 @@ def failed_hook(span, event):
8888
from opentelemetry.instrumentation.pymongo.version import __version__
8989
from opentelemetry.instrumentation.utils import is_instrumentation_enabled
9090
from opentelemetry.semconv.trace import DbSystemValues, SpanAttributes
91-
from opentelemetry.trace import SpanKind, get_tracer
91+
from opentelemetry.trace import SpanKind, Tracer, get_tracer
9292
from opentelemetry.trace.span import Span
9393
from opentelemetry.trace.status import Status, StatusCode
9494

@@ -98,14 +98,21 @@ def failed_hook(span, event):
9898
ResponseHookT = Callable[[Span, monitoring.CommandSucceededEvent], None]
9999
FailedHookT = Callable[[Span, monitoring.CommandFailedEvent], None]
100100

101+
CommandEvent = TypeVar(
102+
"CommandEvent",
103+
monitoring.CommandStartedEvent,
104+
monitoring.CommandSucceededEvent,
105+
monitoring.CommandFailedEvent,
106+
)
107+
101108

102-
def dummy_callback(span, event): ...
109+
def dummy_callback(span: Span, event: CommandEvent): ...
103110

104111

105112
class CommandTracer(monitoring.CommandListener):
106113
def __init__(
107114
self,
108-
tracer,
115+
tracer: Tracer,
109116
request_hook: RequestHookT = dummy_callback,
110117
response_hook: ResponseHookT = dummy_callback,
111118
failed_hook: FailedHookT = dummy_callback,
@@ -195,10 +202,12 @@ def failed(self, event: monitoring.CommandFailedEvent):
195202
_LOG.exception(hook_exception)
196203
span.end()
197204

198-
def _pop_span(self, event):
205+
def _pop_span(self, event: CommandEvent) -> Span | None:
199206
return self._span_dict.pop(_get_span_dict_key(event), None)
200207

201-
def _get_statement_by_command_name(self, command_name, event):
208+
def _get_statement_by_command_name(
209+
self, command_name: str, event: CommandEvent
210+
) -> str:
202211
statement = command_name
203212
command_attribute = COMMAND_TO_ATTRIBUTE_MAPPING.get(command_name)
204213
command = event.command.get(command_attribute)
@@ -207,14 +216,16 @@ def _get_statement_by_command_name(self, command_name, event):
207216
return statement
208217

209218

210-
def _get_span_dict_key(event):
219+
def _get_span_dict_key(
220+
event: CommandEvent,
221+
) -> int | tuple[int, tuple[str, int | None]]:
211222
if event.connection_id is not None:
212223
return event.request_id, event.connection_id
213224
return event.request_id
214225

215226

216227
class PymongoInstrumentor(BaseInstrumentor):
217-
_commandtracer_instance = None # type CommandTracer
228+
_commandtracer_instance: CommandTracer | None = None
218229
# The instrumentation for PyMongo is based on the event listener interface
219230
# https://api.mongodb.com/python/current/api/pymongo/monitoring.html.
220231
# This interface only allows to register listeners and does not provide
@@ -225,7 +236,7 @@ class PymongoInstrumentor(BaseInstrumentor):
225236
def instrumentation_dependencies(self) -> Collection[str]:
226237
return _instruments
227238

228-
def _instrument(self, **kwargs):
239+
def _instrument(self, **kwargs: Any):
229240
"""Integrate with pymongo to trace it using event listener.
230241
https://api.mongodb.com/python/current/api/pymongo/monitoring.html
231242
@@ -259,6 +270,6 @@ def _instrument(self, **kwargs):
259270
# If already created, just enable it
260271
self._commandtracer_instance.is_enabled = True
261272

262-
def _uninstrument(self, **kwargs):
273+
def _uninstrument(self, **kwargs: Any):
263274
if self._commandtracer_instance is not None:
264275
self._commandtracer_instance.is_enabled = False

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

Whitespace-only changes.

0 commit comments

Comments
 (0)