本文介绍了Stackdriver Alerting Policy-需要过滤JSON有效负载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在App Engine下设置了一个Python脚本,该脚本在我们组织内的所有项目中运行,并在以下位置收集数据:

  • 未与快照策略关联的磁盘
  • 名称中没有每日"或每周"的快照策略(因此不在我们的命名约定之内)

以前者为例,Stackdriver日志中的JSON有效负载如下:

  jsonPayload:{DiskWithoutPolicy:真"diskId:"1234567891234567891"diskName:服务器磁盘3"项目:"projectID"} 

当我创建警报策略时,Metrics Explorer仅允许我过滤日志的资源标签(在此处列出:

在本例中,我们选择 jsonPayload.trace ,现在我们可以通过Metrics Explorer中的 trace 标签过滤自定义指标:

请注意,您可以直接从用户定义的指标列表中创建Stackdriver Monitoring警报(从指标创建警报):

I have a Python script set up under App Engine that runs through all of the projects within our organisation that collects data on:

  • The disks not associated with snapshot policies
  • The snapshot policies that do not have "daily" or "weekly" in their name (and are thus outside our naming convention)

Taking the former as the example, the JSON payload within the Stackdriver log is like:

jsonPayload: {
  DiskWithoutPolicy: "True"
  diskId: "1234567891234567891"
  diskName: "server-disk3"
  project: "projectID"
}

When I go to create the Alerting Policy, Metrics Explorer only allows me to filter on the log's resource labels (listed here: https://cloud.google.com/monitoring/api/resources):

resource: {
  labels: {
   module_id: "get_googlecloud_snapshotstatus"
   project_id: "projectID"
   version_id: ""
   zone: "europe-west1-d"
  }

Can Metrics Explorer filter on the JSON payload, so I can therefore see the 'diskId', 'diskName' and 'project'?

If not, is there any other way of achieving what I need?

解决方案

You can create your own user-defined metrics in Stackdriver Logging. This way, you can capture all logs matching a particular filter and expose the fields that you want as metric labels.

For example, I navigate to Stackdriver Logging -> Logs-based Metrics -> Create Metric and select a filter for a GAE application:

resource.type="gae_app"
logName=("projects/REDACTED/logs/appengine.googleapis.com%2Fstdout"
OR "projects/REDACTED/logs/appengine.googleapis.com%2Fstderr"
OR "projects/REDACTED/logs/appengine.googleapis.com%2Fnginx.request"
OR "projects/REDACTED/logs/appengine.googleapis.com%2Frequest_log")
resource.labels.module_id="image-demo"
httpRequest.requestMethod="GET"

Requests contain a generic jsonPayload such as:

jsonPayload: {
  appLatencySeconds: "0.000"
  latencySeconds: "0.001"
  trace: "4ff777572199f23f4fc97388e75c0acc"
 }

On the metric editor (right panel) under Labels there is a Field name dropdown selector that includes our jsonPayload fields:

In our case we select jsonPayload.trace and now we can filter our custom metric by trace label in the Metrics Explorer:

Note that you can create a Stackdriver Monitoring alert directly from the list of user-defined metrics (Create alert from metric):

这篇关于Stackdriver Alerting Policy-需要过滤JSON有效负载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 20:51