上报原始数据 CMSClientInit.groupId = 101L;//设置公共的应用分组ID。
CMSClient cmsClient = new CMSClient(endpoint, accKey, secret);//初始化client。
CustomMetricUploadRequest request = CustomMetricUploadRequest.builder()
.append(CustomMetric.builder()
.setMetricName("testMetric")//指标名称。
.setGroupId(102L)//设置应用分组ID。
.setTime(new Date())
.setType(CustomMetric.TYPE_VALUE)//类型为原始值。
.appendValue(MetricAttribute.VALUE, 1f)//原始值,key只能是该value,不能自定义。
.appendDimension("key", "value")//添加维度。
.appendDimension("ip", "127.0.0.1")//添加维度。
.build())
.build();
CustomMetricUploadResponse response = cmsClient.putCustomMetric(request);//上报数据。
System.out.println(JSONObject.toJSONString(response));
上报聚合数据 CMSClientInit.groupId = 101L;
CMSClient cmsClient = new CMSClient(endpoint, accKey, secret);
CustomMetricUploadRequest request = CustomMetricUploadRequest.builder()
.append(CustomMetric.builder()
.setMetricName("customTest")
.setTime(new Date())
.setType(CustomMetric.TYPE_AGG)//类型为聚合。
.setPeriod(CustomMetric.PERIOD_1M)//周期为1分钟。
.appendDimension("test", "testValue")//设置维度。
.appendDimension("dimension", "dimensionValue")//设置维度。
.appendValue(MetricAttribute.SUM, 100)//设置求和。
.appendValue(MetricAttribute.MAX, 20)//设置最大值。
.appendValue(MetricAttribute.MIN, 0.1)//设置最小值。
.appendValue(MetricAttribute.COUNT, 20)//设置计数。
.appendValue(MetricAttribute.AVG, 5)//设置平均值。
.appendValue(MetricAttribute.LAST, 10)//设置周期最后一个值。
.appendValue(MetricAttribute.P50, 10)//设置P50。
.appendValue(MetricAttribute.P90, 17)//设置P90。
.appendValue(MetricAttribute.P99, 19)//设置P99。
.build())
.build();
CustomMetricUploadResponse response = cmsClient.putCustomMetric(request);
System.out.println(JSONObject.toJSONString(response));