1414
1515import os
1616
17- from pymongo import MongoClient
17+ from pymongo import MongoClient , IndexModel , ASCENDING
1818
1919from opentelemetry import trace as trace_api
2020from opentelemetry .instrumentation .pymongo import PymongoInstrumentor
@@ -113,6 +113,30 @@ def test_find(self):
113113 expected_db_statement = "find {'name': 'testName'}"
114114 self .validate_spans (expected_db_statement )
115115
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+
116140 def test_delete (self ):
117141 """Should create a child span for delete"""
118142 with self ._tracer .start_as_current_span ("rootSpan" ):
@@ -123,6 +147,15 @@ def test_delete(self):
123147 )
124148 self .validate_spans (expected_db_statement )
125149
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+
126159 def test_find_without_capture_statement (self ):
127160 """Should create a child span for find"""
128161 self .instrumentor ._commandtracer_instance .capture_statement = False
0 commit comments