From 2a5e4c17cd6b5b31085d6afb179cf82a41f4fa92 Mon Sep 17 00:00:00 2001 From: Benjamin Wohlwend Date: Thu, 30 Apr 2020 16:31:03 +0200 Subject: [PATCH] work around issue on pymongo < 3.0 instance.address is not set on those versions --- elasticapm/instrumentation/packages/pymongo.py | 2 +- tests/instrumentation/pymongo_tests.py | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/elasticapm/instrumentation/packages/pymongo.py b/elasticapm/instrumentation/packages/pymongo.py index c6d599b11..de2fb1786 100644 --- a/elasticapm/instrumentation/packages/pymongo.py +++ b/elasticapm/instrumentation/packages/pymongo.py @@ -127,7 +127,7 @@ def call(self, module, method, wrapped, instance, args, kwargs): extra={"destination": {"service": {"name": "mongodb", "resource": "mongodb", "type": "db"}}}, ) as span: response = wrapped(*args, **kwargs) - if span.context: + if span.context and instance.address: host, port = instance.address span.context["destination"]["address"] = host span.context["destination"]["port"] = port diff --git a/tests/instrumentation/pymongo_tests.py b/tests/instrumentation/pymongo_tests.py index f7160655a..029f9d589 100644 --- a/tests/instrumentation/pymongo_tests.py +++ b/tests/instrumentation/pymongo_tests.py @@ -203,11 +203,12 @@ def test_collection_find(instrument, elasticapm_client, mongo_database): assert span["subtype"] == "mongodb" assert span["action"] == "query" assert span["name"] == "elasticapm_test.blogposts.cursor.refresh" - assert span["context"]["destination"] == { - "address": os.environ.get("MONGODB_HOST", "localhost"), - "port": int(os.environ.get("MONGODB_PORT", 27017)), - "service": {"name": "mongodb", "resource": "mongodb", "type": "db"}, - } + if not pymongo.version_tuple < (3, 0): + assert span["context"]["destination"] == { + "address": os.environ.get("MONGODB_HOST", "localhost"), + "port": int(os.environ.get("MONGODB_PORT", 27017)), + "service": {"name": "mongodb", "resource": "mongodb", "type": "db"}, + } @pytest.mark.integrationtest