Skip to content

Commit 70187ff

Browse files
authored
Pymongo capture collection name (#1555)
1 parent d1dec92 commit 70187ff

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- `opentelemetry-instrumentation-aws-lambda` Adds an option to configure `disable_aws_context_propagation` by
1515
environment variable: `OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION`
1616
([#1507](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1507))
17-
17+
- Fix pymongo to collect the property DB_MONGODB_COLLECTION
18+
([#1555](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/1555))
1819

1920
## Version 1.15.0/0.36b0 (2022-12-10)
2021

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

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ def started(self, event: monitoring.CommandStartedEvent):
126126
statement = event.command_name
127127
if command:
128128
statement += " " + str(command)
129+
collection = event.command.get(event.command_name)
129130

130131
try:
131132
span = self._tracer.start_span(name, kind=SpanKind.CLIENT)
@@ -135,6 +136,10 @@ def started(self, event: monitoring.CommandStartedEvent):
135136
)
136137
span.set_attribute(SpanAttributes.DB_NAME, event.database_name)
137138
span.set_attribute(SpanAttributes.DB_STATEMENT, statement)
139+
if collection:
140+
span.set_attribute(
141+
SpanAttributes.DB_MONGODB_COLLECTION, collection
142+
)
138143
if event.connection_id is not None:
139144
span.set_attribute(
140145
SpanAttributes.NET_PEER_NAME, event.connection_id[0]

tests/opentelemetry-docker-tests/tests/pymongo/test_pymongo_functional.py

+4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ def validate_spans(self):
6868
self.assertEqual(
6969
pymongo_span.attributes[SpanAttributes.NET_PEER_PORT], MONGODB_PORT
7070
)
71+
self.assertEqual(
72+
pymongo_span.attributes[SpanAttributes.DB_MONGODB_COLLECTION],
73+
MONGODB_COLLECTION_NAME,
74+
)
7175

7276
def test_insert(self):
7377
"""Should create a child span for insert"""

0 commit comments

Comments
 (0)