【问题标题】:Google cloud tasks NodeJS api: Get queue stats?谷歌云任务 NodeJS api:获取队列统计信息?
【发布时间】:2021-04-14 21:42:48
【问题描述】:

我想使用 nodejs 客户端库@google-cloud/tasks 获取谷歌云任务中队列的统计字段。 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: '*'}})

【问题讨论】:

    标签: node.js google-api google-api-client beta google-cloud-tasks


    【解决方案1】:

    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
      }
    }
    */
    

    列表队列

    const { v2beta3 } = require('@google-cloud/tasks');
    const tasksClient = new v2beta3.CloudTasksClient();
    
    async function main() {
        const request = {
            parent: 'projects/PROJECT/locations/LOCATION',
            readMask: { paths: ['name', 'stats'] }
        };
    
        const [response] = await tasksClient.listQueues(request);
    
        console.log(response);
    }
    main();
    
    /*
    [
      {
        name: 'projects/PROJECT/locations/LOCATION/queues/QUEUE',
        ...
        stats: {
          tasksCount: '1771',
          oldestEstimatedArrivalTime: [Object],
          executedLastMinuteCount: '0',
          concurrentDispatchesCount: '0',
          effectiveExecutionRate: 500
        }
      },
      ...
    ]
    */
    
    

    【讨论】:

      【解决方案2】:

      通过查看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 字段获得的所有相关字段)。

      下图应该对你有帮助。

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

      2020 年 1 月 23 日编辑

      更正了文档以告知以表达:

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

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

      【讨论】:

      • 真可惜。您提出的代码确实需要一个 API 密钥和客户端 ID,这是额外的配置。
      • 是的。你是对的!客户端库的好处是它们通过环境变量在后台处理身份验证和授权,但我没有看到使用 JS 的任何其他可能性。不过,您可以使用 Python 客户端库获取 Queue.stats 字段。如果您觉得有帮助,我可以发布一些代码。
      猜你喜欢
      • 2020-01-15
      • 1970-01-01
      • 2020-09-22
      • 1970-01-01
      • 2012-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多