How to configure logging and connectivity with the Azure Functions trigger for Cosmos DB
APPLIES TO:
SQL API
This article describes advanced configuration options you can set when using the Azure Functions trigger for Cosmos DB.
Enabling trigger specific logs
The Azure Functions trigger for Cosmos DB uses the Change Feed Processor Library internally, and the library generates a set of health logs that can be used to monitor internal operations for troubleshooting purposes.
The health logs describe how the Azure Functions trigger for Cosmos DB behaves when attempting operations during load-balancing scenarios or initialization.
Enabling logging
To enable logging when using Azure Functions trigger for Cosmos DB, locate the host.json file in your Azure Functions project or Azure Functions App and configure the level of required logging. Enable the traces for Host.Triggers.CosmosDB as shown in the following sample:
{
"version": "2.0",
"logging": {
"fileLoggingMode": "always",
"logLevel": {
"Host.Triggers.CosmosDB": "Trace"
}
}
}
After the Azure Function is deployed with the updated configuration, you will see the Azure Functions trigger for Cosmos DB logs as part of your traces. You can view the logs in your configured logging provider under the Category Host.Triggers.CosmosDB.
Query the logs
Run the following query to query the logs generated by the Azure Functions trigger for Cosmos DB in Azure Application Insights' Analytics:
traces
| where customDimensions.Category == "Host.Triggers.CosmosDB"
Configuring the connection policy
There are two connection modes - Direct mode and Gateway mode. To learn more about these connection modes, see the connection modes article. By default, Gateway is used to establish all connections on the Azure Functions trigger for Cosmos DB. However, it might not be the best option for performance-driven scenarios.
Changing the connection mode and protocol
There are two key configuration settings available to configure the client connection policy – the connection mode and the connection protocol. You can change the default connection mode and protocol used by the Azure Functions trigger for Cosmos DB and all the Azure Cosmos DB bindings). To change the default settings, you need to locate the host.json file in your Azure Functions project or Azure Functions App and add the following extra setting:
{
"cosmosDB": {
"connectionMode": "Direct",
"protocol": "Tcp"
}
}
Where connectionMode must have the desired connection mode (Direct or Gateway) and protocol the desired connection protocol (Tcp for Direct mode or Https for Gateway mode).
If your Azure Functions project is working with Azure Functions V1 runtime, the configuration has a slight name difference, you should use documentDB instead of cosmosDB:
{
"documentDB": {
"connectionMode": "Direct",
"protocol": "Tcp"
}
}
Note
When hosting your function app in a Consumption plan, each instance has a limit in the amount of Socket Connections that it can maintain. When working with Direct / TCP mode, by design more connections are created and can hit the Consumption plan limit, in which case you can either use Gateway mode or instead host your function app in either a Premium plan or a Dedicated (App Service) plan.
Next steps
- Connection limits in Azure Functions
- Enable monitoring in your Azure Functions applications.
- Learn how to Diagnose and troubleshoot common issues when using the Azure Functions trigger for Cosmos DB.