查看指标和配额

在控制台中查看指标

您可以在 Google Cloud 控制台中查看您的 Cloud Functions 函数及其执行时间、执行次数和内存使用情况。Cloud Monitoring 中也提供了这些指标,您可以在该产品中对这些指标设置自定义提醒。如需了解详情,请参阅 Cloud Monitoring 文档

您可以在 Google Cloud 控制台的 API 概览页面中查看 API 调用的指标。

最后,您可以在 Google Cloud 控制台的 API 配额页面中查看 API 调用和函数执行的配额指标。您可以在 Cloud Monitoring 中设置配额错误提醒,只需过滤 STATUS 指标标签值为 out of quota 的执行项即可。如需了解详情,请参阅提醒简介

以编程方式读取指标

以下代码段展示了如何从代码读取指标。

Node.js

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

// Creates a client
const client = new monitoring.MetricServiceClient();

async function readTimeSeriesData() {
  /**
   * TODO(developer): Uncomment and edit the following lines of code.
   */
  // const projectId = 'YOUR_PROJECT_ID';
  // const filter = 'metric.type="compute.googleapis.com/instance/cpu/utilization"';

  const request = {
    name: client.projectPath(projectId),
    filter: filter,
    interval: {
      startTime: {
        // Limit results to the last 20 minutes
        seconds: Date.now() / 1000 - 60 * 20,
      },
      endTime: {
        seconds: Date.now() / 1000,
      },
    },
  };

  // Writes time series data
  const [timeSeries] = await client.listTimeSeries(request);
  timeSeries.forEach(data => {
    console.log(`${data.metric.labels.instance_name}:`);
    data.points.forEach(point => {
      console.log(JSON.stringify(point.value));
    });
  });
}
readTimeSeriesData();