Google cloud functions 我如何访问谷歌云功能执行指标?

Google cloud functions 我如何访问谷歌云功能执行指标?,google-cloud-functions,monitoring,Google Cloud Functions,Monitoring,我希望通过编程获得与Google云函数执行相关的指标(例如调用、延迟等)。是否有(最好是简单的)方法来实现这一点(例如通过API)?Cloudfunctions正在使用Stackdriver来捕获和显示这些指标。(我假设您正在寻找控制台中每个函数上显示的相同指标。)StackDriver有一个API和客户端库,您可以从中提取指标。更多信息请访问 下面是一个在node.js中提取这些内容的小示例 const util = require("util"); // Imports the Google

我希望通过编程获得与Google云函数执行相关的指标(例如调用、延迟等)。是否有(最好是简单的)方法来实现这一点(例如通过API)?

Cloudfunctions正在使用Stackdriver来捕获和显示这些指标。(我假设您正在寻找控制台中每个函数上显示的相同指标。)StackDriver有一个API和客户端库,您可以从中提取指标。更多信息请访问

下面是一个在node.js中提取这些内容的小示例

const util = require("util");
// Imports the Google Cloud client library
const monitoring = require("@google-cloud/monitoring");

// Your Google Cloud Platform project ID
const projectId = "<YOUR PROJECT ID>";

// Creates a client
const client = new monitoring.MetricServiceClient({
  keyFilename: "<YOUR CREDENTIALS>"
});

// list of metrics also avaliable at https://cloud.google.com/monitoring/api/metrics_gcp#gcp-cloudfunctions
// list all metrics avaliable
client
  .listMetricDescriptors({
    // name: `projects/${projectId}/monitoredResourceDescriptors/cloud_function`,
    name: `projects/${projectId}`,
    filter: 'metric.type = starts_with("cloudfunctions.googleapis.com")'
  })
  .then(results => {
    console.log(results);
  })
  .catch(err => {
    console.error("ERROR:", err);
  });

const currentTime = new Date();
endTime = currentTime.toISOString();
startTime = new Date(currentTime - 1 * 60 * 60 * 1000).toISOString();
interval = {
  startTime: {
    // Limit results to the last 20 minutes
    seconds: Date.now() / 1000 - 60 * 60
  },
  endTime: {
    seconds: Date.now() / 1000
  }
};

// get the executions accross time
client
  .listTimeSeries({
    name: `projects/${projectId}`,
    filter:
      'metric.type = "cloudfunctions.googleapis.com/function/execution_count" AND resource.label.function_name = "<YOUR FUNCTION NAME>"',
    view: "FULL",
    interval
  })
  .then(results => {
    // console.log(results);
    console.log(util.inspect(results, { showHidden: true, depth: 5 }));
  })
  .catch(err => {
    console.error("ERROR:", err);
  });
const util=require(“util”);
//导入Google云客户端库
const monitoring=require(“@google cloud/monitoring”);
//您的Google云平台项目ID
const projectd=“”;
//创建一个客户端
const client=new monitoring.MetricServiceClient({
密钥文件名:“
});
//指标列表也可在https://cloud.google.com/monitoring/api/metrics_gcp#gcp-云函数
//列出所有可用的指标
客户
.listMetricDescriptors({
//名称:`projects/${projectId}/monitoredResourceDescriptors/cloud\u function`,
名称:`projects/${projectId}`,
过滤器:“metric.type=以(“cloudfunctions.googleapis.com”)开头”
})
。然后(结果=>{
控制台日志(结果);
})
.catch(错误=>{
console.error(“error:,err”);
});
const currentTime=新日期();
endTime=currentTime.toISOString();
startTime=新日期(currentTime-1*60*60*1000).toISOString();
间隔={
开始时间:{
//将结果限制在最后20分钟内
秒:Date.now()/1000-60*60
},
结束时间:{
秒:Date.now()/1000
}
};
//按时拿到死刑
客户
.listTimeSeries({
名称:`projects/${projectId}`,
过滤器:
'metric.type=“cloudfunctions.googleapis.com/function/execution\u count”和resource.label.function\u name=“”,
视图:“完整”,
间隔
})
。然后(结果=>{
//控制台日志(结果);
log(util.inspect(结果,{showHidden:true,depth:5}));
})
.catch(错误=>{
console.error(“error:,err”);
});

谢谢你,R.赖特!此时,有一些关于调用、执行时间、网络和内存消耗的度量。控制台中出现的“延迟”度量是否与API的“执行时间”度量相对应(以便此API完全包含GUI中的信息)?是的。谷歌在控制台上甚至有点不一致。谷歌云控制台称之为执行时间,而Firebase控制台称之为延迟。