```html
Guidelines, metrics, alerts, and diagnostic logs
Azure Cosmos DB provides built‑in monitoring capabilities via Azure Monitor. You can track performance, detect anomalies, and set up automated alerts.
Current RU consumption per second.
Average write and read latency.
Number of requests throttled due to RU limits.
Create alerts based on metric thresholds to receive email, SMS, or webhook notifications.
5
for 5 minutes).Enable diagnostic settings to stream logs to Log Analytics, Event Hub, or Storage.
az monitor diagnostic-settings create \
--name cosmosdb-diagnostics \
--resource <resource-id> \
--logs '[{"category":"DataPlaneRequests","enabled":true}]' \
--metrics '[{"category":"AllMetrics","enabled":true}]' \
--workspace <log-analytics-workspace-id>
Use Log Analytics queries to create custom dashboards.
requests
| where TimeGenerated > ago(1h)
| summarize count() by bin(TimeGenerated, 5m), operationName
Retrieve metric data programmatically using the Azure SDK for JavaScript.
import { MonitorClient } from "@azure/arm-monitor";
import { DefaultAzureCredential } from "@azure/identity";
const credential = new DefaultAzureCredential();
const subscriptionId = process.env.AZURE_SUBSCRIPTION_ID;
const client = new MonitorClient(credential, subscriptionId);
async function getRuMetrics(resourceId) {
const result = await client.metrics.list(resourceId, {
metricnames: "TotalRequestUnits",
timespan: "PT1H",
aggregation: "Average",
interval: "PT5M"
});
console.log(JSON.stringify(result, null, 2));
}
getRuMetrics("/subscriptions/.../resourceGroups/.../providers/Microsoft.DocumentDB/databaseAccounts/yourCosmosDb");
For .NET, Java, Python, refer to the official documentation.