Skip to content

Commit d16afc7

Browse files
authored
if possible, get pymongo address from socket, otherwise from nodes (#797)
1 parent 054f224 commit d16afc7

File tree

2 files changed

+39
-6
lines changed

2 files changed

+39
-6
lines changed

elasticapm/instrumentation/packages/pymongo.py

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,11 @@ class PyMongoInstrumentation(AbstractInstrumentedModule):
7272
def call(self, module, method, wrapped, instance, args, kwargs):
7373
cls_name, method_name = method.split(".", 1)
7474
signature = ".".join([instance.full_name, method_name])
75-
host, port = instance.database.client.address
75+
nodes = instance.database.client.nodes
76+
if nodes:
77+
host, port = list(nodes)[0]
78+
else:
79+
host, port = None, None
7680
destination_info = {
7781
"address": host,
7882
"port": port,
@@ -97,7 +101,13 @@ class PyMongoBulkInstrumentation(AbstractInstrumentedModule):
97101
def call(self, module, method, wrapped, instance, args, kwargs):
98102
collection = instance._BulkOperationBuilder__bulk.collection
99103
signature = ".".join([collection.full_name, "bulk.execute"])
100-
with capture_span(signature, span_type="db", span_subtype="mongodb", span_action="query"):
104+
with capture_span(
105+
signature,
106+
span_type="db",
107+
span_subtype="mongodb",
108+
span_action="query",
109+
extra={"destination": {"service": {"name": "mongodb", "resource": "mongodb", "type": "db"}}},
110+
):
101111
return wrapped(*args, **kwargs)
102112

103113

@@ -109,5 +119,18 @@ class PyMongoCursorInstrumentation(AbstractInstrumentedModule):
109119
def call(self, module, method, wrapped, instance, args, kwargs):
110120
collection = instance.collection
111121
signature = ".".join([collection.full_name, "cursor.refresh"])
112-
with capture_span(signature, span_type="db", span_subtype="mongodb", span_action="query"):
113-
return wrapped(*args, **kwargs)
122+
with capture_span(
123+
signature,
124+
span_type="db",
125+
span_subtype="mongodb",
126+
span_action="query",
127+
extra={"destination": {"service": {"name": "mongodb", "resource": "mongodb", "type": "db"}}},
128+
) as span:
129+
response = wrapped(*args, **kwargs)
130+
if span.context:
131+
host, port = instance.address
132+
span.context["destination"]["address"] = host
133+
span.context["destination"]["port"] = port
134+
else:
135+
pass
136+
return response

tests/instrumentation/pymongo_tests.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,17 +192,22 @@ def test_collection_find(instrument, elasticapm_client, mongo_database):
192192
blogposts.append({"author": "Tom", "comments": i})
193193
mongo_database.blogposts.insert(blogposts)
194194
r = mongo_database.blogposts.insert(blogpost)
195-
elasticapm_client.events[TRANSACTION]
196195
elasticapm_client.begin_transaction("transaction.test")
197196
r = list(mongo_database.blogposts.find({"comments": {"$gt": 995}}))
198197

199198
elasticapm_client.end_transaction("transaction.test")
200199
transactions = elasticapm_client.events[TRANSACTION]
201-
span = _get_pymongo_span(elasticapm_client.spans_for_transaction(transactions[0]))
200+
spans = elasticapm_client.spans_for_transaction(transactions[0])
201+
span = _get_pymongo_span(spans)
202202
assert span["type"] == "db"
203203
assert span["subtype"] == "mongodb"
204204
assert span["action"] == "query"
205205
assert span["name"] == "elasticapm_test.blogposts.cursor.refresh"
206+
assert span["context"]["destination"] == {
207+
"address": os.environ.get("MONGODB_HOST", "localhost"),
208+
"port": int(os.environ.get("MONGODB_PORT", 27017)),
209+
"service": {"name": "mongodb", "resource": "mongodb", "type": "db"},
210+
}
206211

207212

208213
@pytest.mark.integrationtest
@@ -220,6 +225,11 @@ def test_collection_find_one(instrument, elasticapm_client, mongo_database):
220225
assert span["subtype"] == "mongodb"
221226
assert span["action"] == "query"
222227
assert span["name"] == "elasticapm_test.blogposts.find_one"
228+
assert span["context"]["destination"] == {
229+
"address": os.environ.get("MONGODB_HOST", "localhost"),
230+
"port": int(os.environ.get("MONGODB_PORT", 27017)),
231+
"service": {"name": "mongodb", "resource": "mongodb", "type": "db"},
232+
}
223233

224234

225235
@pytest.mark.integrationtest

0 commit comments

Comments
 (0)