Monitoring: Metrics, Logs, and Alerts
Monitor Cosmos DB health with current throughput and connection-mode latency metrics, SDK request duration, diagnostic logs, Azure Monitor workbooks and application-specific alert rules.
Why monitoring matters
Think of monitoring as the dashboard gauges in your car. You need to know when the engine is overheating (throttling), when youβre running low on fuel (RU/s budget), and when something unusual happens (error spikes). Without gauges, youβre driving blind.
Marcusβs monitoring mission
βοΈ Marcus at FinSecure runs Cosmos DB in three production environments with SOC 2 compliance requirements. He needs to:
- Detect 429 throttling within minutes
- Track query performance trends
- Maintain audit logs for compliance
- Get alerted when latency exceeds SLA thresholds
Key metrics
Treat alert thresholds as application-specific design choices, not universal Cosmos DB SLA limits. Check the current monitoring reference for supported names, dimensions and aggregations.
| Metric | What It Tells You | Alert Threshold |
|---|---|---|
| NormalizedRUConsumption | % of provisioned RU/s consumed (0-100%); inspect partition dimensions | Example warning at >80% sustained, then check actual 429s |
| TotalRequests | Number of requests (including 429s) | Alert on sudden spikes |
| TotalRequestUnits | Total RU consumed | Track for cost trends |
| ServerSideLatencyDirect / ServerSideLatencyGateway | Server processing latency for the corresponding connection mode | Use supported Average/Maximum aggregations and a workload-specific threshold |
| DataUsage / IndexUsage | Data and index storage usage | Track growth; inspect logical-key storage separately |
| MetadataRequests | Metadata requests such as resource enumeration | Investigate unexpected increases |
Exam tip: NormalizedRUConsumption is the key metric
NormalizedRUConsumption helps detect throughput pressure. A physical partition at 100% is at its RU budget; check actual 429 responses and request distribution rather than assuming every request is throttled.
Key details:
- Itβs per-physical-partition β a hot partition can show 100% while the account average is 30%
- An example sustained >80% warning should trigger investigation of demand, throttling and partition skew before a capacity change
- The exam often presents this metric in scenarios asking βwhat should you monitor for throttling?β
Monitor the whole request
Backend processing time is not end-to-end application latency. Track SDK request duration and application percentiles against the service objective; network time, retries, connection setup and client resource pressure can all add delay. Capture diagnostics for slow requests and correlate region, activity id, status and RU charge before deciding whether the database backend is the bottleneck.
The retired AvailableStorage and ServerSideLatency metrics are not the current alert targets. Use the connection-mode latency metrics above. For logical-partition storage, enable PartitionKeyStatistics logs and monitor growth toward the standard 20 GB complete-key boundary. Those statistics sample large keys and are approximate, not a complete inventory of every key.
Diagnostic logging
Enable diagnostic logs to capture detailed operation data. This example exports to resource-specific tables used by the KQL below; replace the placeholder resource identifiers:
az monitor diagnostic-settings create \
--name "cosmos-diagnostics" \
--resource "/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.DocumentDB/databaseAccounts/finsecure-cosmos" \
--workspace "/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.OperationalInsights/workspaces/finsecure-logs" \
--export-to-resource-specific true \
--logs '[
{"category": "QueryRuntimeStatistics", "enabled": true},
{"category": "DataPlaneRequests", "enabled": true},
{"category": "PartitionKeyStatistics", "enabled": true},
{"category": "PartitionKeyRUConsumption", "enabled": true},
{"category": "ControlPlaneRequests", "enabled": true}
]'
| Log Category | What It Captures |
|---|---|
| DataPlaneRequests | Every read, write, query with RU cost and latency |
| QueryRuntimeStatistics | Query text and identifiers for correlation with request logs |
| PartitionKeyStatistics | Approximate sampled large logical-partition storage usage |
| PartitionKeyRUConsumption | RU consumption broken down by partition key |
| ControlPlaneRequests | Account-level operations (create, delete, scale) |
Query text and parameters can be obfuscated by default. Review data-handling requirements and logging costs before enabling more detailed query text.
KQL query examples
Query your diagnostic logs with Kusto Query Language (KQL) in Log Analytics:
// Find the most expensive queries in the last hour
let requestCharges = CDBDataPlaneRequests
| where TimeGenerated > ago(1h)
| project TimeGenerated, ActivityId, RequestCharge;
CDBQueryRuntimeStatistics
| where TimeGenerated > ago(1h)
| project ActivityId, QueryText, DatabaseName, CollectionName
| join kind=inner requestCharges on ActivityId
| project DatabaseName, CollectionName, QueryText, RequestCharge, TimeGenerated
| order by RequestCharge desc
| take 10
// Detect 429 throttling events
CDBDataPlaneRequests
| where TimeGenerated > ago(24h)
| where StatusCode == 429
| summarize ThrottledCount = count() by bin(TimeGenerated, 5m), CollectionName
| order by TimeGenerated desc
// Identify hot partitions
CDBPartitionKeyRUConsumption
| where TimeGenerated > ago(1h)
| summarize TotalRU = sum(RequestCharge) by PartitionKey
| order by TotalRU desc
| take 10
Azure Monitor workbooks
Azure provides built-in Cosmos DB insights β pre-built dashboards in the Azure portal:
- Overview: Throughput, requests, storage, availability at a glance
- Throughput: NormalizedRUConsumption with partition-level breakdown
- Requests: Status code distribution, latency percentiles
- Storage: Per-partition storage usage, index size
- Failures: 429 rates, timeout rates, error categorisation
Dianaβs tip: π Diana, Marcusβs security auditor, uses the ControlPlaneRequests log category to track who made configuration changes β required for SOC 2 audit trails.
Alert rules
# Alert when NormalizedRUConsumption exceeds 80% for 5 minutes
az monitor metrics alert create \
--name "cosmos-throttle-warning" \
--resource-group "<resource-group>" \
--scopes "/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.DocumentDB/databaseAccounts/finsecure-cosmos" \
--condition "max NormalizedRUConsumption > 80" \
--window-size 5m \
--evaluation-frequency 1m \
--action "/subscriptions/<subscription-id>/resourceGroups/<resource-group>/providers/Microsoft.Insights/actionGroups/ops-team"
| Alert | Condition | Severity |
|---|---|---|
| Throttling warning | NormalizedRUConsumption > 80% for 5min | Warning |
| Throttling critical | NormalizedRUConsumption > 95% for 5min | Critical |
| Latency spike | Average connection-mode server latency exceeds the applicationβs chosen threshold for 10min; investigate SDK percentiles separately | Warning |
| Logical-partition growth | Log alert from PartitionKeyStatistics before a sampled key reaches the standard 20 GB boundary | Warning |
| Error rate | HTTP 5xx count > 10 in 5min | Critical |
π¬ Video walkthrough
Flashcards
Knowledge Check
Marcus notices NormalizedRUConsumption at 95% sustained for his orders container. What should he do first?
Diana needs an audit trail of who changed the Cosmos DB account configuration for SOC 2 compliance. Which diagnostic log category should she enable?
Marcus wants to find the top 10 most expensive queries in the last hour. Which tool and approach should he use?
Next up: Backup and Restore β choosing between periodic and continuous backup, configuring policies, and point-in-time restore.