@@ -72,7 +72,11 @@ class PyMongoInstrumentation(AbstractInstrumentedModule):
7272 def call (self , module , method , wrapped , instance , args , kwargs ):
7373 cls_name , method_name = method .split ("." , 1 )
7474 signature = "." .join ([instance .full_name , method_name ])
75- host , port = instance .database .client .address
75+ nodes = instance .database .client .nodes
76+ if nodes :
77+ host , port = list (nodes )[0 ]
78+ else :
79+ host , port = None , None
7680 destination_info = {
7781 "address" : host ,
7882 "port" : port ,
@@ -97,7 +101,13 @@ class PyMongoBulkInstrumentation(AbstractInstrumentedModule):
97101 def call (self , module , method , wrapped , instance , args , kwargs ):
98102 collection = instance ._BulkOperationBuilder__bulk .collection
99103 signature = "." .join ([collection .full_name , "bulk.execute" ])
100- with capture_span (signature , span_type = "db" , span_subtype = "mongodb" , span_action = "query" ):
104+ with capture_span (
105+ signature ,
106+ span_type = "db" ,
107+ span_subtype = "mongodb" ,
108+ span_action = "query" ,
109+ extra = {"destination" : {"service" : {"name" : "mongodb" , "resource" : "mongodb" , "type" : "db" }}},
110+ ):
101111 return wrapped (* args , ** kwargs )
102112
103113
@@ -109,5 +119,18 @@ class PyMongoCursorInstrumentation(AbstractInstrumentedModule):
109119 def call (self , module , method , wrapped , instance , args , kwargs ):
110120 collection = instance .collection
111121 signature = "." .join ([collection .full_name , "cursor.refresh" ])
112- with capture_span (signature , span_type = "db" , span_subtype = "mongodb" , span_action = "query" ):
113- return wrapped (* args , ** kwargs )
122+ with capture_span (
123+ signature ,
124+ span_type = "db" ,
125+ span_subtype = "mongodb" ,
126+ span_action = "query" ,
127+ extra = {"destination" : {"service" : {"name" : "mongodb" , "resource" : "mongodb" , "type" : "db" }}},
128+ ) as span :
129+ response = wrapped (* args , ** kwargs )
130+ if span .context :
131+ host , port = instance .address
132+ span .context ["destination" ]["address" ] = host
133+ span .context ["destination" ]["port" ] = port
134+ else :
135+ pass
136+ return response
0 commit comments