skip to Main Content

Any time I connect to our Azure-hosted CosmosDB Mongo database, we are given the following warning:

UserWarning: You appear to be connected to a CosmosDB cluster. For more information regarding feature compatibility and support please visit https://www.mongodb.com/supportability/cosmosdb

(mongo-python-driver GitHub source line for reference)

Nothing seems to broken by ignoring the warning and continuing, however running our test suite is now plagued with this in our CI output’s STDOUT upon every execution with an otherwise pristine output.

Why does this show as a warning when nothing is wrong? Does the mongo driver provide a way of disabling the message without silencing genuine warnings?

2

Answers


  1. UserWarning: You appear to be connected to a CosmosDB cluster.

    pymongo indicates that it has identified a Cosmos DB instance hosted on Azure being utilized as the MongoDB server. The error message is to inform users that Cosmos DB, while MongoDB-compatible, may not support all MongoDB features, especially those available in native MongoDB clusters. You can use warnings module to ignore the warnings. Below is an example on how you can ignore warnings.

    import warnings
    from pymongo import MongoClient
    
    warnings.filterwarnings("ignore", message="You appear to be connected to a CosmosDB cluster")
    
    client = MongoClient("<connection_string>")
    db = client.get_database("newdb1")
    
    print(db.list_collection_names())
    

    Output:

    ['newcoll1']
    
    Login or Signup to reply.
  2. Good self-investigation to find the cause. But this would need to be updated in pymongo itself, possibly with a flag like warn_external=False. Consider adding a Feature request on their Python driver JIRA board.

    A quick/partial workaround would be to suppress stdout during the connection. And it’s partial because you’re capturing the log’s stdout but not if it were writing to a file. If your CI/infra changed and you start using stream or file logging, this won’t work.

    Suppress or Redirect stdout:

    import contextlib
    
    with contextlib.redirect_stdout(io.StringIO()) as f:
        client = pymongo.MongoClient(...)
    _ = f.getvalue()  # clear out
    

    Optionally – and for a more robust workaround: Since _log_or_warn will warn if the INFO log message is below the threshold for the logger – you can raise the log level and suppress warnings before connecting:

    def _log_or_warn(logger: logging.Logger, message: str) -> None:
        if logger.isEnabledFor(logging.INFO):
            logger.info(message)
        else:
            # stacklevel=4 ensures that the warning is for the user's code.
            warnings.warn(message, UserWarning, stacklevel=4)
    

    So this method would be:

    1. Temporarily raise the Log level, to trigger the warning and then
    2. Temporarily Suppress Warnings

    USE WITH CAUTION! You may miss other warnings or log messages which are unrelated to "external DBs".

    import warnings
    import logging
    
    
    logger = logging.getLogger()
    
    with warnings.catch_warnings(action="ignore"):
        # get current log level and raise
        curr_level = logger.getEffectiveLevel()
        logger.setLevel(logging.ERROR)  # or logging.WARN, logging.CRITICAL
        
        client = pymongo.MongoClient(...)
        
        # reset to original level
        logger.setLevel(curr_level)
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search