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