```html Azure Cosmos DB Monitoring – Documentation

Monitoring Azure Cosmos DB

Guidelines, metrics, alerts, and diagnostic logs

Overview

Azure Cosmos DB provides built‑in monitoring capabilities via Azure Monitor. You can track performance, detect anomalies, and set up automated alerts.

  • Key metrics: RU consumption, latency, request units, throttling, storage usage.
  • Diagnostic logs: Request, query, and system logs.
  • Integration with Azure Dashboard, Power BI, and third‑party tools.

Metrics

Request Units (RU/s)

Current RU consumption per second.

Latency (ms)

Average write and read latency.

Throttled Requests

Number of requests throttled due to RU limits.

Alerts

Create alerts based on metric thresholds to receive email, SMS, or webhook notifications.

  1. Navigate to Azure Monitor → Alerts → New alert rule.
  2. Select the Cosmos DB account as the resource.
  3. Choose a metric (e.g., Throttled Requests).
  4. Define the condition (e.g., greater than 5 for 5 minutes).
  5. Configure action groups for notification channels.
Best practice: Combine multiple conditions (RU usage + latency) into a single alert rule for comprehensive monitoring.

Diagnostic Logs

Enable diagnostic settings to stream logs to Log Analytics, Event Hub, or Storage.

  • Request – Details about each request.
  • Query – Information on query execution.
  • Audit – Administrative actions.
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

SDK Samples

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.

```