@@ -76,7 +76,7 @@ def failed_hook(span, event):
7676"""
7777
7878from logging import getLogger
79- from typing import Callable , Collection
79+ from typing import Any , Callable , Collection , TypeVar
8080
8181from pymongo import monitoring
8282
@@ -88,7 +88,7 @@ def failed_hook(span, event):
8888from opentelemetry .instrumentation .pymongo .version import __version__
8989from opentelemetry .instrumentation .utils import is_instrumentation_enabled
9090from opentelemetry .semconv .trace import DbSystemValues , SpanAttributes
91- from opentelemetry .trace import SpanKind , get_tracer
91+ from opentelemetry .trace import SpanKind , Tracer , get_tracer
9292from opentelemetry .trace .span import Span
9393from opentelemetry .trace .status import Status , StatusCode
9494
@@ -98,14 +98,21 @@ def failed_hook(span, event):
9898ResponseHookT = Callable [[Span , monitoring .CommandSucceededEvent ], None ]
9999FailedHookT = 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
105112class 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
216227class 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
0 commit comments