Uploaded image for project: 'Python Driver'
  1. Python Driver
  2. PYTHON-5306

New pymongo-4.12.0 with Elastic APM: NotImplementedError: Database objects do not implement truth value testing or bool()

    • Type: Icon: Bug Bug
    • Resolution: Fixed
    • Priority: Icon: Critical - P2 Critical - P2
    • 4.12.1
    • Affects Version/s: 4.12
    • Component/s: Query Operations
    • None
    • Python Drivers
    • Not Needed
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?
    • 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?

      1. Create a simple Linux Azure Function with a HTTP trigger like documented here.
      1. 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

            Assignee:
            noah.stapp@mongodb.com Noah Stapp
            Reporter:
            mihail.valchev@sew-eurodrive.de Mihail Valchev
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: