Skip to content

Instantly share code, notes, and snippets.

@chrismckelt
Last active March 14, 2024 01:36
Show Gist options
  • Save chrismckelt/bb864a44d50c9586097358c4d963f912 to your computer and use it in GitHub Desktop.
Save chrismckelt/bb864a44d50c9586097358c4d963f912 to your computer and use it in GitHub Desktop.
Cosmos DB Queries
// Consumed RU/s in last 24 hours
// Identify consumed RU/s on Cosmos databases and collections.
// To create an alert for this query, click '+ New alert rule'
//You can compare the RU/s consumption with your provisioned RU/s to determine if you should scale up or down RU/s based on your workload.
AzureDiagnostics
| where Category == "DataPlaneRequests"
//| where collectionName_s == "CollectionToAnalyze" //Replace to target the query to a collection
| summarize ConsumedRUsPerMinute = sum(todouble(requestCharge_s)) by collectionName_s, _ResourceId, bin(TimeGenerated, 1m)
| project TimeGenerated , ConsumedRUsPerMinute , collectionName_s, _ResourceId
| render timechart
// RU charges by
CDBPartitionKeyRUConsumption
| where DatabaseName == "dsp-db"
| summarize sum(RequestCharge) by PartitionKey
| order by sum_RequestCharge desc
// RU charges by
CDBPartitionKeyRUConsumption
| where DatabaseName == "dsp-db"
| summarize sum(RequestCharge) by PartitionKey
| order by sum_RequestCharge desc
// RU per query
CDBQueryRuntimeStatistics
| join kind=inner (
CDBDataPlaneRequests
| project RequestCharge, ActivityId
) on $left.ActivityId == $right.ActivityId
| distinct RequestCharge, QueryText
| order by RequestCharge desc
| limit 10
@chrismckelt
Copy link
Author

CDBPartitionKeyRUConsumption
| where DatabaseName == "dsp-db"
| where isnotempty(PartitionKey)
// Sum total request units consumed by logical partition key for each second
| project OperationName, CollectionName, PartitionKey, RequestCharge
| summarize sum(RequestCharge) by PartitionKey, CollectionName,OperationName
| order by sum_RequestCharge desc

@chrismckelt
Copy link
Author

//Determine what requests are returning 429 responses
CDBDataPlaneRequests
| summarize throttledOperations = dcountif(ActivityId, StatusCode == 429), totalOperations = dcount(ActivityId), totalConsumedRUPerMinute = sum(RequestCharge) by DatabaseName, CollectionName, OperationName, bin(TimeGenerated, 1min)
| extend averageRUPerOperation = 1.0 * totalConsumedRUPerMinute / totalOperations
| extend Most429s = 1.0 * throttledOperations / totalOperations
| order by Most429s desc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment