14
14
15
15
import os
16
16
17
- from pymongo import MongoClient
17
+ from pymongo import MongoClient , IndexModel , ASCENDING
18
18
19
19
from opentelemetry import trace as trace_api
20
20
from opentelemetry .instrumentation .pymongo import PymongoInstrumentor
@@ -113,6 +113,30 @@ def test_find(self):
113
113
expected_db_statement = "find {'name': 'testName'}"
114
114
self .validate_spans (expected_db_statement )
115
115
116
+ def test_find_one_and_update (self ):
117
+ """Should create a child span for find_one_and_update"""
118
+ with self ._tracer .start_as_current_span ("rootSpan" ):
119
+ self ._collection .find_one_and_update (
120
+ {"name" : "testName" }, {"$set" : {"value" : "someOtherValue" }}
121
+ )
122
+
123
+ expected_db_statement = "findAndModify {'name': 'testName'} {'$set': {'value': 'someOtherValue'}}"
124
+ self .validate_spans (expected_db_statement )
125
+
126
+ def test_aggregate (self ):
127
+ """Should create a child span for aggregate"""
128
+ with self ._tracer .start_as_current_span ("rootSpan" ):
129
+ self ._collection .aggregate (
130
+ [
131
+ {"$match" : {"field" : "value" }},
132
+ {"$group" : {"_id" : "$anotherField" , "count" : {"$sum" : 1 }}},
133
+ ]
134
+ )
135
+
136
+
137
+ expected_db_statement = "aggregate [{'$match': {'field': 'value'}}, {'$group': {'_id': '$anotherField', 'count': {'$sum': 1}}}]"
138
+ self .validate_spans (expected_db_statement )
139
+
116
140
def test_delete (self ):
117
141
"""Should create a child span for delete"""
118
142
with self ._tracer .start_as_current_span ("rootSpan" ):
@@ -123,6 +147,15 @@ def test_delete(self):
123
147
)
124
148
self .validate_spans (expected_db_statement )
125
149
150
+ def test_create_indexes (self ):
151
+ """Should create a child span for create_indexes"""
152
+ some_index = IndexModel ([("anotherField" , ASCENDING )], name = "some_index" )
153
+ with self ._tracer .start_as_current_span ("rootSpan" ):
154
+ self ._collection .create_indexes ([some_index ])
155
+
156
+ expected_db_statement = "createIndexes [{'name': 'some_index', 'key': SON([('anotherField', 1)])}]"
157
+ self .validate_spans (expected_db_statement )
158
+
126
159
def test_find_without_capture_statement (self ):
127
160
"""Should create a child span for find"""
128
161
self .instrumentor ._commandtracer_instance .capture_statement = False
0 commit comments