Skip to content

Commit 406b339

Browse files
committed
feat(pymongo): aggregate and getMore capture statements
1 parent 78300e9 commit 406b339

File tree

2 files changed

+73
-0
lines changed
  • instrumentation/opentelemetry-instrumentation-pymongo

2 files changed

+73
-0
lines changed

instrumentation/opentelemetry-instrumentation-pymongo/src/opentelemetry/instrumentation/pymongo/utils.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,6 @@
1717
"delete": "deletes",
1818
"update": "updates",
1919
"find": "filter",
20+
"getMore": "collection",
21+
"aggregate": "pipeline",
2022
}

instrumentation/opentelemetry-instrumentation-pymongo/tests/test_pymongo.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,77 @@ def test_no_op_tracer(self):
194194
spans_list = self.memory_exporter.get_finished_spans()
195195
self.assertEqual(len(spans_list), 0)
196196

197+
def test_capture_statement_getmore(self):
198+
command_attrs = {
199+
"command_name": "getMore",
200+
"collection": "test_collection",
201+
}
202+
command_tracer = CommandTracer(
203+
self.tracer, capture_statement=True
204+
)
205+
mock_event = MockEvent(command_attrs)
206+
command_tracer.started(event=mock_event)
207+
# pylint: disable=protected-access
208+
span = command_tracer._pop_span(mock_event)
209+
self.assertEqual(
210+
span.attributes[SpanAttributes.DB_STATEMENT],
211+
"getMore test_collection"
212+
)
213+
214+
def test_capture_statement_aggregate(self):
215+
pipeline = [{"$match": {"status": "active"}}, {"$group": {"_id": "$category", "count": {"$sum": 1}}}]
216+
command_attrs = {
217+
"command_name": "aggregate",
218+
"pipeline": pipeline,
219+
}
220+
command_tracer = CommandTracer(
221+
self.tracer, capture_statement=True
222+
)
223+
mock_event = MockEvent(command_attrs)
224+
command_tracer.started(event=mock_event)
225+
# pylint: disable=protected-access
226+
span = command_tracer._pop_span(mock_event)
227+
expected_statement = f"aggregate {pipeline}"
228+
self.assertEqual(
229+
span.attributes[SpanAttributes.DB_STATEMENT],
230+
expected_statement
231+
)
232+
233+
def test_capture_statement_disabled_getmore(self):
234+
command_attrs = {
235+
"command_name": "getMore",
236+
"collection": "test_collection",
237+
}
238+
command_tracer = CommandTracer(
239+
self.tracer, capture_statement=False
240+
)
241+
mock_event = MockEvent(command_attrs)
242+
command_tracer.started(event=mock_event)
243+
# pylint: disable=protected-access
244+
span = command_tracer._pop_span(mock_event)
245+
self.assertEqual(
246+
span.attributes[SpanAttributes.DB_STATEMENT],
247+
"getMore"
248+
)
249+
250+
def test_capture_statement_disabled_aggregate(self):
251+
pipeline = [{"$match": {"status": "active"}}]
252+
command_attrs = {
253+
"command_name": "aggregate",
254+
"pipeline": pipeline,
255+
}
256+
command_tracer = CommandTracer(
257+
self.tracer, capture_statement=False
258+
)
259+
mock_event = MockEvent(command_attrs)
260+
command_tracer.started(event=mock_event)
261+
# pylint: disable=protected-access
262+
span = command_tracer._pop_span(mock_event)
263+
self.assertEqual(
264+
span.attributes[SpanAttributes.DB_STATEMENT],
265+
"aggregate"
266+
)
267+
197268

198269
class MockCommand:
199270
def __init__(self, command_attrs):

0 commit comments

Comments
 (0)