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
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 usewarnings
module to ignore the warnings. Below is an example on how you can ignore warnings.Output:
Good self-investigation to find the cause. But this would need to be updated in
pymongo
itself, possibly with a flag likewarn_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’sstdout
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
: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:So this method would be:
USE WITH CAUTION! You may miss other warnings or log messages which are unrelated to "external DBs".