簡體   English   中英

CosmosDB - 以編程方式查詢消耗的 RU/s?

[英]CosmosDB - query for consumed RU/s programmatically?

我正在嘗試獲取有關收集級別上 RU/秒消耗的信息,不幸的是,當我使用 node.js documentdb 模塊查詢收集信息時,代碼如下

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

我只得到如下的基本信息

{ 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/' }

有沒有其他方法可以獲取每個集合的 RU 消耗信息? 此數據可在Metrics -> Throughput刀片中的門戶中獲得,但如何通過 API 獲取它對我來說是個謎。 Azure Monitor僅提供整個數據庫級別的平均指標,因此使用Azure Monitor API也不是一種可行的方法。

這在您的方案中可能不可行,但您絕對可以使用其他一些 Azure 產品推出自己的解決方案。 我同意用於拉動 RU 消耗的本機 API 會更理想。

如果您想推出自己的產品,可以通過使用事件中心和流分析來實現。 圍繞您的 Cosmos 查詢編寫一個小包裝器,將其 RU 成本發布到您的事件中心(您執行的每個操作都會從 Cosmos 返回 RU 成本)。 從那里,流分析內置了用於從您的 Hub 中提取消息的集成。 只需創建一個簡單的查詢,將 RU 成本匯總到一秒個桶中即可。 從那里你可以做各種很酷的事情,包括在給定的閾值下發送警報,甚至觸發自動縮放操作以響應你的實時使用情況來優化你的 Cosmos 成本。

根據您利用 Cosmos 的程度以及您是否觀察到峰值工作負載,進行智能擴展的成本節省將超過抵消事件中心 + 流分析的額外支出。

您可以使用 @azure/arm-monitor 和 @azure/ms-rest-nodeauth 包查詢消耗的 RU/s。 您需要為調用方(如果使用 MSI 身份驗證或服務主體,則在 Azure 中運行的函數應用程序)提供對 Azure 門戶中的資源組的“API 管理服務讀取器”和“監視讀取器”權限。 代碼在 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 ))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM