From 6376b027f7ba0600180c7dabdddff24d1d7622f7 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 20 Dec 2024 21:19:53 +0100 Subject: [PATCH 1/2] Support PEP 561 to `opentelemetry-instrumentation-pymongo` --- CHANGELOG.md | 2 ++ .../instrumentation/pymongo/__init__.py | 31 +++++++++++++------ .../instrumentation/pymongo/py.typed | 0 3 files changed, 23 insertions(+), 10 deletions(-) create mode 100644 instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/py.typed diff --git a/CHANGELOG.md b/CHANGELOG.md index a243091b1d..83c80f7db7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#3100](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3100)) - Add support to database stability opt-in in `_semconv` utilities and add tests ([#3111](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3111)) +- `opentelemetry-opentelemetry-pymongo` Add `py.typed` file to enable PEP 561 + ([#3136](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3136)) ### Fixed diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py index e0721f2f2d..f59961945e 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py @@ -76,7 +76,7 @@ def failed_hook(span, event): """ from logging import getLogger -from typing import Callable, Collection +from typing import Any, Callable, Collection, TypeVar from pymongo import monitoring @@ -88,7 +88,7 @@ def failed_hook(span, event): from opentelemetry.instrumentation.pymongo.version import __version__ from opentelemetry.instrumentation.utils import is_instrumentation_enabled from opentelemetry.semconv.trace import DbSystemValues, SpanAttributes -from opentelemetry.trace import SpanKind, get_tracer +from opentelemetry.trace import SpanKind, Tracer, get_tracer from opentelemetry.trace.span import Span from opentelemetry.trace.status import Status, StatusCode @@ -98,14 +98,21 @@ def failed_hook(span, event): ResponseHookT = Callable[[Span, monitoring.CommandSucceededEvent], None] FailedHookT = Callable[[Span, monitoring.CommandFailedEvent], None] +CommandEvent = TypeVar( + "CommandEvent", + monitoring.CommandStartedEvent, + monitoring.CommandSucceededEvent, + monitoring.CommandFailedEvent, +) + -def dummy_callback(span, event): ... +def dummy_callback(span: Span, event: CommandEvent): ... class CommandTracer(monitoring.CommandListener): def __init__( self, - tracer, + tracer: Tracer, request_hook: RequestHookT = dummy_callback, response_hook: ResponseHookT = dummy_callback, failed_hook: FailedHookT = dummy_callback, @@ -195,10 +202,12 @@ def failed(self, event: monitoring.CommandFailedEvent): _LOG.exception(hook_exception) span.end() - def _pop_span(self, event): + def _pop_span(self, event: CommandEvent) -> Span | None: return self._span_dict.pop(_get_span_dict_key(event), None) - def _get_statement_by_command_name(self, command_name, event): + def _get_statement_by_command_name( + self, command_name: str, event: CommandEvent + ) -> str: statement = command_name command_attribute = COMMAND_TO_ATTRIBUTE_MAPPING.get(command_name) command = event.command.get(command_attribute) @@ -207,14 +216,16 @@ def _get_statement_by_command_name(self, command_name, event): return statement -def _get_span_dict_key(event): +def _get_span_dict_key( + event: CommandEvent, +) -> int | tuple[int, tuple[str, int | None]]: if event.connection_id is not None: return event.request_id, event.connection_id return event.request_id class PymongoInstrumentor(BaseInstrumentor): - _commandtracer_instance = None # type CommandTracer + _commandtracer_instance: CommandTracer | None = None # The instrumentation for PyMongo is based on the event listener interface # https://api.mongodb.com/python/current/api/pymongo/monitoring.html. # This interface only allows to register listeners and does not provide @@ -225,7 +236,7 @@ class PymongoInstrumentor(BaseInstrumentor): def instrumentation_dependencies(self) -> Collection[str]: return _instruments - def _instrument(self, **kwargs): + def _instrument(self, **kwargs: Any): """Integrate with pymongo to trace it using event listener. https://api.mongodb.com/python/current/api/pymongo/monitoring.html @@ -259,6 +270,6 @@ def _instrument(self, **kwargs): # If already created, just enable it self._commandtracer_instance.is_enabled = True - def _uninstrument(self, **kwargs): + def _uninstrument(self, **kwargs: Any): if self._commandtracer_instance is not None: self._commandtracer_instance.is_enabled = False diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/py.typed b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/py.typed new file mode 100644 index 0000000000..e69de29bb2 From 79089a2d1ef0d4147f3de038fc499f54c524228b Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 20 Dec 2024 21:38:34 +0100 Subject: [PATCH 2/2] add future --- .../src/opentelemetry/instrumentation/pymongo/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py index f59961945e..bb609a08c6 100644 --- a/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py +++ b/instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/__init__.py @@ -75,6 +75,8 @@ def failed_hook(span, event): """ +from __future__ import annotations + from logging import getLogger from typing import Any, Callable, Collection, TypeVar