谷歌云任务 NodeJS api:获取队列统计信息?

问题描述

我想使用 nodejs 客户端库 @google-cloud/tasks 获取 google 云任务中队列的 stats 字段。 stats 字段仅存在于 v2beta3 版本中,但是为了获得它,我们需要传递一个查询参数 readMask=*,但我不知道如何使用客户端库传递它。 我尝试使用 otherArgs 参数,但它不起作用。

const tasks = require('@google-cloud/tasks');

const client = new tasks.v2beta3.GoogleCloudTasks()

// Get queue containing stats
const queue = await client.getQueue({name: '..'},{otherArgs: {readMask: '*'}})

解决方法

readMask 指定要获取响应对象的哪些路径。响应将包含所有可能的路径以及占位符(如 null、UNSPECIFIED 等),然后包含您想要的实际值。

const request = {
    ...
    readMask: { paths: ['name','stats','state',...] }
};

获取队列

const { v2beta3 } = require('@google-cloud/tasks');
const tasksClient = new v2beta3.CloudTasksClient();

async function main() {
    const request = {
        name: 'projects/PROJECT/locations/LOCATION/queues/QUEUE',readMask: { paths: ['name','stats'] }
    };

    const [response] = await tasksClient.getQueue(request);

    console.log(response);
}
main();

/*
{
  name: 'projects/PROJECT/locations/LOCATION/queues/QUEUE',...
  stats: {
    tasksCount: '113',oldestEstimatedArrivalTime: null,executedLastMinuteCount: '0',concurrentDispatchesCount: '0',effectiveExecutionRate: 500
  }
}
*/

listQueues

const { v2beta3 } = require('@google-cloud/tasks');
const tasksClient = new v2beta3.CloudTasksClient();

async function main() {
    const request = {
        parent: 'projects/PROJECT/locations/LOCATION','stats'] }
    };

    const [response] = await tasksClient.listQueues(request);

    console.log(response);
}
main();

/*
[
  {
    name: 'projects/PROJECT/locations/LOCATION/queues/QUEUE',...
    stats: {
      tasksCount: '1771',oldestEstimatedArrivalTime: [Object],effectiveExecutionRate: 500
    }
  },...
]
*/

,

通过查看 source code for the client library,我看不到 v2beta3 version of the REST API projects.locations.queues.get method 上指定的 readMask 参数的参考。

NodeJS 客户端库 getQueue() 上的相关方法需要一种类型的请求 IGetQueueRequest,它没有 readMask 参数并且只需要 name 属性。

尽管如此,此实现将来可能会发生变化,以包含获取统计信息的相关方法。

关于 REST API 本身,readMask section 上的公共文档存在错误,因为 * 不是有效字符。如果您想获得 Queue.stats 字段,您只需在 readMask 参数上输入 stats。如果您想获取所有相关字段,则应输入所有相关字段(例如,name,rateLimits,retryConfig,state,taskTtl,tombstoneTtl,type,stats 应获取通过调用方法获得的所有相关字段 + Queue.stats 字段)。

以下图片应该对您有所帮助。only return the Queue.stats field

作为一种解决方法,如果您单击相关方法文档的 Try this API 部分上的展开符号,您可以单击 JAVASCRIPT 部分并获取有关如何构建请求的相关代码,如下图所示.code in JS

编辑 2020 年 1 月 23 日

更正文档以告知:

[Queue.stats] 只有在掩码中明确指定时才会返回。

这意味着只需在 readMask 字段下写入 stats 将返回统计信息。