Description
Describe the bug: 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
And it points out to elasticapm/instrumentation/packages/base.py, line 213, in call_if_sampling
return self.call(module, method, wrapped, instance, args, kwargs)
Trace:
Click to expand
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(
To Reproduce
-
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.
Environment (please complete the following information)
- OS: Linux (Debian 11)
- Python version: 3.11.9 on Windows 10, 3.11.11 on Linux
- Framework and version: Azure Functions v4, python package version is 1.23.0
- APM Server version: 8.11.0
- Agent version: 6.23.0
Additional context
Add any other context about the problem here.
-
requirements.txt
:Click to expand
azure-functions==1.23.0 elastic-apm==6.23.0 pymongo==4.12.0
-
I've set the following elastic relevant env vars on the Function App:
-
"ELASTIC_APM_SERVER_URL"
-
"ELASTIC_APM_CLOUD_PROVIDER": "azure"
-
"ELASTIC_APM_LOG_ECS_REFORMATTING": "override"
-
"ELASTIC_APM_SERVICE_NAME"
-
"ELASTIC_APM_SECRET_TOKEN"
-
"ELASTIC_APM_ENVIRONMENT"
-
"ELASTIC_APM_CAPTURE_BODY"