使用 Google Cloud Monitoring v3 api 写入自定义时间序列

Writing to custom timeseries with the Google Cloud Monitoring v3 api

Google 已警告 v2 监控 api 现已弃用,很快就会消失。然而,事实证明迁移到 v3 有点困难。我正在尝试编写自定义指标并收到以下错误响应:

服务 > Google 监控 API v3 > monitoring.projects.timeSeries.create

{
    "timeSeries": [{
        "metric": {
            "type": "custom.googleapis.com/test_metric",
            "labels": {
                "payment_type": "Paypal"
            }
        },
        "resource": {
            "type": "custom.googleapis.com/test_metric",
            "labels": {
                "payment_type": "Paypal"
            }
        },
        "metricKind": "GAUGE",
        "valueType": "INT64",
        "points": [{
            "interval": {
                "endTime": "2016-03-20T15:01:23.045123456Z",
                "startTime": "2016-03-20T15:01:23.045123456Z"
            },
            "value": {
                "int64Value": "2"
            }
        }]
    }]
}

{
  "error": {
  "code": 400,
  "message": "Field timeSeries[0].resource.type had an invalid value of \"custom.googleapis.com/test_metric\": Unrecognized resource name.",
  "status": "INVALID_ARGUMENT"
}

"resource" 字段是必需的,文档说它是 "MonitoredResource"... 但我没有看到任何 api 用于创建一个,仅用于列表。大胆猜测并将其设置为 "global" 似乎让我更进一步并给了我这个不同的错误:

{
 "error": {
  "code": 400,
  "message": "Field timeSeries[0].resource.labels[0] had an invalid value of \"payment_type\": Unrecognized resource label.",
  "status": "INVALID_ARGUMENT"
 }
}

列出指标描述符显示 payment_type 存在:

服务 > Google 监控 API v3 > monitoring.projects.metricDescriptors.list

{
 "name": "projects/gearlaunch-hub-sandbox/metricDescriptors/custom.googleapis.com/test_metric",
 "labels": [
  {
   "key": "payment_type"
  }
 ],
 "metricKind": "GAUGE",
 "valueType": "INT64",
 "description": "Test",
 "type": "custom.googleapis.com/test_metric"
}

我已经通读了迁移指南和相关文档,但仍然受阻。有人知道我在这里缺少什么吗?

更新:虽然看起来可以通过从 json 中删除 "resource.labels" 来实现这一点,但我仍在寻找一种方法通过 java 客户端 api.

使其工作

更新 2:接受的(自我回答的)问题显示了如何使用 java api.

看起来答案是使用 "resource" of "type": "global" 但是不用 ​​"labels":

{
    "timeSeries": [{
        "metric": {
            "type": "custom.googleapis.com/test_metric",
            "labels": {
                "payment_type": "Paypal"
            }
        },
        "resource": {
            "type": "global"
        },
        "metricKind": "GAUGE",
        "valueType": "INT64",
        "points": [{
            "interval": {
                "endTime": "2016-03-23T01:01:23.045123456Z",
                "startTime": "2016-03-23T01:01:23.045123456Z"
            },
            "value": {
                "int64Value": "2"
            }
        }]
    }]
}

这给了我 200 OK 响应并将数据添加到时间序列。

这直接在 api 资源管理器中工作。使用 java 客户端 api 的等效代码是:

public String writeCustomMetricValue(final String name, final Map<String, String> labels, final Long value) {
    Preconditions.checkNotNull(name);
    Preconditions.checkNotNull(labels);
    Preconditions.checkNotNull(value);

    final String now = DateTime.now().withZone(DateTimeZone.UTC).toString();

    final TimeInterval interval = new TimeInterval();
    interval.setStartTime(now);
    interval.setEndTime(now);

    final TypedValue pointValue = new TypedValue();
    pointValue.setInt64Value(value);

    final Point point = new Point();
    point.setInterval(interval);
    point.setValue(pointValue);

    final MonitoredResource resource = new MonitoredResource();
    resource.setType("global");

    final Metric metric = new Metric();
    metric.setType("custom.googleapis.com/" + name);

    final TimeSeries series = new TimeSeries();
    series.setMetric(metric);
    series.setPoints(Arrays.asList(point));
    series.setResource(resource);
    series.setMetricKind("GAUGE");

    final List<TimeSeries> timeseries = new ArrayList<>();
    timeseries.add(series);

    final CreateTimeSeriesRequest content = new CreateTimeSeriesRequest();
    content.setTimeSeries(timeseries);

    metric.setLabels(labels);

    try {
        return service().projects().timeSeries().create("projects/" + env.getProjectId().getId(), content).execute().toPrettyString();

    } catch (Exception e) {
        throw new RuntimeException("Name=" + name + ", labels=" + labels + ", value=" + value, e);
    }
}

使用:

<dependency>
    <groupId>com.google.apis</groupId>
    <artifactId>google-api-services-monitoring</artifactId>
    <version>v3-rev3-1.21.0</version>
</dependency>

写入数据点时,必须同时指定Metric和MonitoredResource来标识唯一的时间序列。 |全球| MonitoredResource 没有标签,因此它已被完全指定。您的自定义指标类型似乎是 |custom.googleapis.com/test_metric|有一个名为 |payment_type| 的标签。为了完全指定指标,您必须为 |payment_type| 分配一个值场.

试试看,让我知道它是如何工作的。