dep*_*her 7 google-app-engine google-cloud-platform stackdriver google-cloud-monitoring
谷歌警告说,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"
}
Run Code Online (Sandbox Code Playgroud)
"资源"字段是必需的,文档称它是"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"
}
}
Run Code Online (Sandbox Code Playgroud)
列出度量标准描述符会显示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"
}
Run Code Online (Sandbox Code Playgroud)
我已经阅读了迁移指南和相关文档,但仍然受到阻碍.谁知道我在这里缺少什么?
更新:虽然看起来可以通过从json中删除"resource.labels"来实现这一点,但我仍然在寻找一种方法来通过java客户端api实现这一点.
更新2:接受(自我回答)的问题显示如何使用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"
}
}]
}]
}
Run Code Online (Sandbox Code Playgroud)
这给了我200 OK的响应,并将数据添加到时间序列中.
这直接来自api explorer.使用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);
}
}
Run Code Online (Sandbox Code Playgroud)
使用:
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-monitoring</artifactId>
<version>v3-rev3-1.21.0</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1763 次 |
最近记录: |