@@ -75,8 +75,10 @@ def failed_hook(span, event):
75
75
76
76
"""
77
77
78
+ from __future__ import annotations
79
+
78
80
from logging import getLogger
79
- from typing import Callable , Collection
81
+ from typing import Any , Callable , Collection , TypeVar
80
82
81
83
from pymongo import monitoring
82
84
@@ -88,7 +90,7 @@ def failed_hook(span, event):
88
90
from opentelemetry .instrumentation .pymongo .version import __version__
89
91
from opentelemetry .instrumentation .utils import is_instrumentation_enabled
90
92
from opentelemetry .semconv .trace import DbSystemValues , SpanAttributes
91
- from opentelemetry .trace import SpanKind , get_tracer
93
+ from opentelemetry .trace import SpanKind , Tracer , get_tracer
92
94
from opentelemetry .trace .span import Span
93
95
from opentelemetry .trace .status import Status , StatusCode
94
96
@@ -98,14 +100,21 @@ def failed_hook(span, event):
98
100
ResponseHookT = Callable [[Span , monitoring .CommandSucceededEvent ], None ]
99
101
FailedHookT = Callable [[Span , monitoring .CommandFailedEvent ], None ]
100
102
103
+ CommandEvent = TypeVar (
104
+ "CommandEvent" ,
105
+ monitoring .CommandStartedEvent ,
106
+ monitoring .CommandSucceededEvent ,
107
+ monitoring .CommandFailedEvent ,
108
+ )
109
+
101
110
102
- def dummy_callback (span , event ): ...
111
+ def dummy_callback (span : Span , event : CommandEvent ): ...
103
112
104
113
105
114
class CommandTracer (monitoring .CommandListener ):
106
115
def __init__ (
107
116
self ,
108
- tracer ,
117
+ tracer : Tracer ,
109
118
request_hook : RequestHookT = dummy_callback ,
110
119
response_hook : ResponseHookT = dummy_callback ,
111
120
failed_hook : FailedHookT = dummy_callback ,
@@ -195,10 +204,12 @@ def failed(self, event: monitoring.CommandFailedEvent):
195
204
_LOG .exception (hook_exception )
196
205
span .end ()
197
206
198
- def _pop_span (self , event ) :
207
+ def _pop_span (self , event : CommandEvent ) -> Span | None :
199
208
return self ._span_dict .pop (_get_span_dict_key (event ), None )
200
209
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 :
202
213
statement = command_name
203
214
command_attribute = COMMAND_TO_ATTRIBUTE_MAPPING .get (command_name )
204
215
command = event .command .get (command_attribute )
@@ -207,14 +218,16 @@ def _get_statement_by_command_name(self, command_name, event):
207
218
return statement
208
219
209
220
210
- def _get_span_dict_key (event ):
221
+ def _get_span_dict_key (
222
+ event : CommandEvent ,
223
+ ) -> int | tuple [int , tuple [str , int | None ]]:
211
224
if event .connection_id is not None :
212
225
return event .request_id , event .connection_id
213
226
return event .request_id
214
227
215
228
216
229
class PymongoInstrumentor (BaseInstrumentor ):
217
- _commandtracer_instance = None # type CommandTracer
230
+ _commandtracer_instance : CommandTracer | None = None
218
231
# The instrumentation for PyMongo is based on the event listener interface
219
232
# https://api.mongodb.com/python/current/api/pymongo/monitoring.html.
220
233
# This interface only allows to register listeners and does not provide
@@ -225,7 +238,7 @@ class PymongoInstrumentor(BaseInstrumentor):
225
238
def instrumentation_dependencies (self ) -> Collection [str ]:
226
239
return _instruments
227
240
228
- def _instrument (self , ** kwargs ):
241
+ def _instrument (self , ** kwargs : Any ):
229
242
"""Integrate with pymongo to trace it using event listener.
230
243
https://api.mongodb.com/python/current/api/pymongo/monitoring.html
231
244
@@ -259,6 +272,6 @@ def _instrument(self, **kwargs):
259
272
# If already created, just enable it
260
273
self ._commandtracer_instance .is_enabled = True
261
274
262
- def _uninstrument (self , ** kwargs ):
275
+ def _uninstrument (self , ** kwargs : Any ):
263
276
if self ._commandtracer_instance is not None :
264
277
self ._commandtracer_instance .is_enabled = False
0 commit comments