gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 15:21:39 25 4
gpt4 key购买 nike

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

服务 > Google Monitoring 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"
}

“资源”字段是必需的,文档说它是“MonitoredResource”...但我没有看到任何用于创建一个的 api,仅用于列表。大胆猜测并将其设置为“全局”似乎让我走得更远,并给了我这个不同的错误:

{
"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 Monitoring 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:接受的( self 回答的)问题显示了如何使用 java api 执行此操作。

最佳答案

看起来答案是使用“类型”的“资源”:“全局”但不要使用“标签”:

{
"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>

关于google-app-engine - 使用 Google Cloud Monitoring v3 api 写入自定义时间序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36164939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com