Stackdriver 错误记录 API - 错误代码": 400 "message": "Missing group_id." - 获取错误日志

Stackdriver Error Logging API - error code": 400 "message": "Missing group_id." - Get error logs

我将 Stackdriver 错误记录 REST API 与 Apps 脚本一起使用。

文档位于:

https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/list

查询参数字符串中需要组 ID。 我需要组 ID,但不知道从哪里获取组名或组 ID。我查看了 IAM 设置:

https://console.cloud.google.com/iam-admin/settings?authuser=0

IAM 设置有一个组部分,但我没有任何组。 我不知道从哪里获取组 ID。 在哪里可以找到组 ID?

我得到的错误是:

{
  "error": {
    "code": 400,
    "message": "Missing group_id.",
    "status": "INVALID_ARGUMENT"
  }
}

我使用的代码是:

function getStackdriverLogs(){
  var entries,groupId,httpResponse,logs,options,param,projectId,url;
    
  /*
    Get StackDriver Logs -
  */
  
  /*
    The Stackdriver Error Reporting API (Error Reporting API) must be enabled - 
  
    https://cloud.google.com/error-reporting/reference/rest/v1beta1/projects.events/list    
  */
  
  groupId = "Put group ID here";
  projectId = "Put the Cloud Project ID here";
  
  param = 'projects/' + projectId;
  url = "https://clouderrorreporting.googleapis.com/v1beta1/" + param + "/events";
  
  url = url + "?groupId=" + groupId;

  options = {};
  
  options.muteHttpExceptions = true;
  options.headers = {Authorization: 'Bearer ' + ScriptApp.getOAuthToken()};
  
  options.contentType = "application/json";
  options.method = 'get';
  
  httpResponse = UrlFetchApp.fetch(url,options);
  Logger.log('httpResponse.getResponseCode(): ' + httpResponse.getResponseCode());
  
  if (httpResponse.getResponseCode() !== 200) {
    return httpResponse.getContentText();
  }

  logs = httpResponse.getContentText();
  Logger.log('logs: ' + logs)
  
  logs = JSON.parse(logs);
  
  entries = logs.entries;
  Logger.log('pgTkn',pgTkn)
  
  
}

错误消息根据 set of criteria. Each group can be seen in Stack driver error reporting 分组。如果在仪表板中单击一个组,将打开该组错误消息并且 url 看起来像:

https://console.cloud.google.com/errors/[GROUP_ID]?project=[PROJECT_ID]&time=[TIME_DATA]

组ID可以从url推断出来。或者(最好),使用 projects.groupStats.list 端点获取给定项目中的所有组 ID。

相关: