@@ -75,8 +75,10 @@ def failed_hook(span, event):
7575
7676"""
7777
78+ from __future__ import annotations
79+
7880from logging import getLogger
79- from typing import Callable , Collection
81+ from typing import Any , Callable , Collection , TypeVar
8082
8183from pymongo import monitoring
8284
@@ -88,7 +90,7 @@ def failed_hook(span, event):
8890from opentelemetry .instrumentation .pymongo .version import __version__
8991from opentelemetry .instrumentation .utils import is_instrumentation_enabled
9092from opentelemetry .semconv .trace import DbSystemValues , SpanAttributes
91- from opentelemetry .trace import SpanKind , get_tracer
93+ from opentelemetry .trace import SpanKind , Tracer , get_tracer
9294from opentelemetry .trace .span import Span
9395from opentelemetry .trace .status import Status , StatusCode
9496
@@ -98,14 +100,21 @@ def failed_hook(span, event):
98100ResponseHookT = Callable [[Span , monitoring .CommandSucceededEvent ], None ]
99101FailedHookT = 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
105114class 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
216229class 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
0 commit comments