CosmosDB - query for consumed RU/s programmatically?

Viewed 1042

I am trying to get information about consumption of RU/second on the collection level, unfortunately when I query for collection information with node.js documentdb module, with code like

var client = new DocumentDBClient(host, { masterKey: masterKey });
client.readCollection(collection._self, function(err, item) {
   if (err) {
      console.log(err);
    } else {
      console.log(item);
    }
});

I get only basic information like below

{ id: 'myCollection',
  indexingPolicy:
   { indexingMode: 'consistent',
     automatic: true,
     includedPaths: [ [Object] ],
     excludedPaths: [] },
  _rid: 'ku8SANRCfwA=',
  _ts: 1505295711,
  _self: 'dbs/ku8SAA==/colls/ku8SANRCfwA=/',
  _etag: '"00008b00-0000-0000-0000-59b8fd5f0000"',
  _docs: 'docs/',
  _sprocs: 'sprocs/',
  _triggers: 'triggers/',
  _udfs: 'udfs/',
  _conflicts: 'conflicts/' }

Is there any other way to get RU consumption information per collection? This data is available in the portal in Metrics -> Throughput blade, but how to get it through API is a mystery to me. Azure Monitor provides only a metric averaged on the whole database level, so using Azure Monitor API also isn't a way to go.

2 Answers

You can query the consumed RU/s using the @azure/arm-monitor and @azure/ms-rest-nodeauth packages. You'll need to provide the caller (function app running in Azure if using MSI auth or a service principal) with 'API Management Service Reader' and 'Monitoring Reader' privileges on the resource group in the Azure portal. The code looks something like this in TypeScript:

import * as moment from 'moment'
import {
  loginWithAppServiceMSI
} from '@azure/ms-rest-nodeauth'
import {
  MonitorManagementClient, MonitorManagementModels
} from '@azure/arm-monitor'
import { MetricsListResponse } from '@azure/arm-monitor/esm/models'

// alternatively, you can use a service principal creds, @azure/ms-rest-nodeauth -> loginWithServicePrincipalSecretWithAuthResponse()
const credentials = await loginWithAppServiceMSI({
  msiEndpoint: process.env.MSI_ENDPOINT,
  msiSecret: process.env.MSI_SECRET,
  resource: 'https://management.azure.com/'
})
const now = moment()
const end = now.toISOString()
const start = now.subtract(1, 'minute').toISOString()
const client = new MonitorManagementClient(credentials, SUBSCRIPTION_ID)

const options: MonitorManagementModels.MetricsListOptionalParams = {
  metricnames: 'TotalRequestUnits',
  metricnamespace: 'Microsoft.DocumentDB/databaseAccounts',
  filter: `CollectionName eq '${CONTAINER_NAME}'`,
  interval: 'PT1M',
  timespan: `${start}/${end}`
}
const url = `subscriptions/${SUBSCRIPTION_ID}/resourceGroups/${RESOURCE_GROUP_NAME}/providers/Microsoft.DocumentDB/databaseAccounts/${DATABASE_NAME}`
const response: MetricsListResponse = (await client.metrics.list(
  url,
  options
))

Related