-
Type:
Bug
-
Resolution: Fixed
-
Priority:
Critical - P2
-
Affects Version/s: 4.12
-
Component/s: Query Operations
-
None
-
Python Drivers
-
Not Needed
-
-
None
-
None
-
None
-
None
-
None
-
None
After updating pymongo to the newest 4.12.0 version from 8. April, the Elastic APM cause an exception when calling the pymongo method collection.aggregate()
This happens in a Linux Azure Function in Python 3.11 deployed in the cloud. Removing the Elastic APM or downgrading pymongo to 4.11.3 "solves" the issue.
Caution: testing the Azure Function locally under Windows 10 does not expose the issue! So for now it seems that it happens only under Linux. The python 3.11 Azure Functions run on a Debian 11 image as stated here.
The error I get is:
NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None
Traceback:
Result: Failure Exception: NotImplementedError: Database objects do not implement truth value testing or bool(). Please compare with None instead: database is not None Stack: File "/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py", line 671, in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.11/concurrent/futures/thread.py", line 58, in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/dispatcher.py", line 1001, in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.11/LINUX/X64/azure_functions_worker/extension.py", line 211, in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/function_app.py", line 25, in test cur = collection.aggregate( File "/home/site/wwwroot/.python_packages/lib/site-packages/elasticapm/instrumentation/packages/base.py", line 213, in call_if_sampling return self.call(module, method, wrapped, instance, args, kwargs) File "/home/site/wwwroot/.python_packages/lib/site-packages/elasticapm/instrumentation/packages/pymongo.py", line 78, in call if nodes: File "/home/site/wwwroot/.python_packages/lib/site-packages/pymongo/synchronous/database.py", line 342, in __bool__ raise NotImplementedError(
Detailed steps to reproduce the problem?
- Create a simple Linux Azure Function with a HTTP trigger like documented here.
- In the Function App file function_app.py paste the following code:
import elasticapm from elasticapm.contrib.serverless.azure import ElasticAPMExtension ElasticAPMExtension.configure() import pymongo from bson import ObjectId import os import azure.functions as func app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION) @app.route(route="test", auth_level=func.AuthLevel.ANONYMOUS) def test(req: func.HttpRequest) -> func.HttpResponse: client = pymongo.MongoClient(os.environ["MONGODB_CONNECTION"]) database = client[os.environ["DATABASE_NAME"]] collection = database.get_collection(os.environ["MEASUREMENTS_COLLECTION_NAME"]) cur = collection.aggregate( [ { "$match": { "_id": ObjectId("*******") } } ] ) return func.HttpResponse("Hi", status_code=200)
# Of course you have to set up a MongoDB with a few sample documents and set all the env vars needed by the Python script to work with it.
Definition of done: what must be done to consider the task complete?
Confirm that the issue is in the Elastic APM package or provide a fix in the next pymongo version.
The exact Python version used, with patch level: 3.11.9 on Windows / 3.11.11 on Linux
The exact version of PyMongo used, with patch level: 4.12.0
pymongo.has_c() = True
Describe how MongoDB is set up. Local vs Hosted, version, topology, load balanced, etc.
Not sure. If important I'll have to ask. Definitely not local, as far as I know it is on the Azure cloud.
The operating system and version (e.g. Windows 7, OSX 10.8, ...)
Windows 10 / Debian 11
Web framework or asynchronous network library used, if any, with version (e.g. Django 1.7, mod_wsgi 4.3.0, gevent 1.0.1, Tornado 4.0.2, ...)
NA
Security Vulnerabilities
If you’ve identified a security vulnerability in a driver or any other MongoDB project, please report it according to the instructions here
- is caused by
-
PYTHON-3636 MongoClient should perform SRV resolution lazily
-
- Closed
-
- related to
-
PYTHON-5242 MongoClient does not define all attributes in __init__
-
- Closed
-