Skip to content

Commit 243e55b

Browse files
feat(pymongo): Add MongoDB collection span tag (#3182)
Adds the MongoDB collection as a tag on pymongo query spans. The semantics are set to match what is provided by OpenTelemetry: https://opentelemetry.io/docs/specs/semconv/database/mongodb/ --------- Co-authored-by: Anton Pirker <[email protected]>
1 parent a293450 commit 243e55b

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

sentry_sdk/consts.py

+7
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,13 @@ class SPANDATA:
231231
Example: postgresql
232232
"""
233233

234+
DB_MONGODB_COLLECTION = "db.mongodb.collection"
235+
"""
236+
The MongoDB collection being accessed within the database.
237+
See: https://github.com/open-telemetry/semantic-conventions/blob/main/docs/database/mongodb.md#attributes
238+
Example: public.users; customers
239+
"""
240+
234241
CACHE_HIT = "cache.hit"
235242
"""
236243
A boolean indicating whether the requested data was found in the cache.

sentry_sdk/integrations/pymongo.py

+1
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ def started(self, event):
130130
"db.name": event.database_name,
131131
SPANDATA.DB_SYSTEM: "mongodb",
132132
SPANDATA.DB_OPERATION: event.command_name,
133+
SPANDATA.DB_MONGODB_COLLECTION: command.get(event.command_name),
133134
}
134135

135136
try:

tests/integrations/pymongo/test_pymongo.py

+5
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ def test_transactions(sentry_init, capture_events, mongo_server, with_pii):
7474
assert find["description"].startswith("{'find")
7575
assert insert_success["description"].startswith("{'insert")
7676
assert insert_fail["description"].startswith("{'insert")
77+
78+
assert find["tags"][SPANDATA.DB_MONGODB_COLLECTION] == "test_collection"
79+
assert insert_success["tags"][SPANDATA.DB_MONGODB_COLLECTION] == "test_collection"
80+
assert insert_fail["tags"][SPANDATA.DB_MONGODB_COLLECTION] == "erroneous"
7781
if with_pii:
7882
assert "1" in find["description"]
7983
assert "2" in insert_success["description"]
@@ -125,6 +129,7 @@ def test_breadcrumbs(sentry_init, capture_events, mongo_server, with_pii):
125129
"db.operation": "find",
126130
"net.peer.name": mongo_server.host,
127131
"net.peer.port": str(mongo_server.port),
132+
"db.mongodb.collection": "test_collection",
128133
}
129134

130135

0 commit comments

Comments
 (0)