You can use the Java SDK to report custom monitoring data to CloudMonitor, either as raw data or locally aggregated data.
The Java SDK supports the following methods for reporting monitoring data:
- Directly report monitoring data to CloudMonitor.
- Aggregate data locally and then report the aggregated results.
The aggregation period is 60 seconds or 300 seconds.
Install the Java SDK
To install the Java SDK using Maven, add the following dependency:
<dependency>
<groupId>com.aliyun.openservices</groupId>
<artifactId>aliyun-cms</artifactId>
<version>0.2.4</version>
</dependency>Code examples
The following examples demonstrate how to report monitoring data:
- Report raw data
CMSClientInit.groupId = 101L;// Set the public application group ID. CMSClient cmsClient = new CMSClient(endpoint, accKey, secret);// Initialize the client. CustomMetricUploadRequest request = CustomMetricUploadRequest.builder() .append(CustomMetric.builder() .setMetricName("testMetric")// Metric name. .setGroupId(102L)// Set the application group ID. .setTime(new Date()) .setType(CustomMetric.TYPE_VALUE)// The type is raw value. .appendValue(MetricAttribute.VALUE, 1f)// Raw value. The key can only be this value and cannot be customized. .appendDimension("key", "value")// Add a dimension. .appendDimension("ip", "127.0.0.1")// Add a dimension. .build()) .build(); CustomMetricUploadResponse response = cmsClient.putCustomMetric(request);// Report the data. System.out.println(JSONObject.toJSONString(response)); - Report aggregate data
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)// The type is aggregation. .setPeriod(CustomMetric.PERIOD_1M)// The period is 1 minute. .appendDimension("test", "testValue")// Set a dimension. .appendDimension("dimension", "dimensionValue")// Set a dimension. .appendValue(MetricAttribute.SUM, 100)// Set the sum. .appendValue(MetricAttribute.MAX, 20)// Set the maximum value. .appendValue(MetricAttribute.MIN, 0.1)// Set the minimum value. .appendValue(MetricAttribute.COUNT, 20)// Set the count. .appendValue(MetricAttribute.AVG, 5)// Set the average value. .appendValue(MetricAttribute.LAST, 10)// Set the last value of the period. .appendValue(MetricAttribute.P50, 10)// Set P50. .appendValue(MetricAttribute.P90, 17)// Set P90. .appendValue(MetricAttribute.P99, 19)// Set P99. .build()) .build(); CustomMetricUploadResponse response = cmsClient.putCustomMetric(request); System.out.println(JSONObject.toJSONString(response));
Note For the service endpoints of Cloud Monitor in each region, see Service endpoints for reporting monitoring data.
Response example
A successful request returns a response similar to the following example:
{
"Message": "success",
"RequestId": "E25EE651-9C97-4EFD-AF22-A753B674E8D4",
"Code": "200"
}A status code of 200 indicates a successful request.
Multi-period aggregate reporting
You can use the Java SDK to aggregate data locally before reporting. The following table describes the supported data types.
| Data type | Description | Aggregated values | Memory consumption |
| value | General value type | All properties except LastValue | About 4 KB |
| gauge | Sampled value | LastValue | 4 bytes |
| meter | Sum, rate | Sum, SumPerSecond | 50 bytes |
| counter | Count | SampleCount | 10 bytes |
| timer | Time calculation | SampleCount, CountPerSecond, Average, Maximum, Minimum, PXX (P10-P99) | About 4 KB |
| histogram | Distribution | SampleCount, Average, Maximum, Minimum, PXX (P10-P99) | About 4 KB |
Note The memory consumption values are for a single time series and a single aggregation period.
The following example shows how to use each data type:
// Initialization
CMSClientInit.groupId = 0L;
CMSClient cmsClient = new CMSClient(accKey, secret, endpoint);// Create a client.
CMSMetricRegistryBuilder builder = new CMSMetricRegistryBuilder();
builder.setCmsClient(cmsClient);
// Create a registry that includes two aggregation periods.
final MetricRegistry registry = builder.build();
// Or, create a registry that includes only the 1-minute aggregation period.
final MetricRegistry registry = builder.build(RecordLevel._60S);
// Use value.
ValueWrapper value = registry.value(MetricName.build("value"));
value.update(6.5);
// Use meter.
MeterWrapper meter = registry.meter(MetricName.build("meter"));
meter.update(7.2);
// Use counter.
CounterWrapper counter = registry.counter(MetricName.build("counter"));
counter.inc(20);
counter.dec(5);
// Use timer.
TimerWrapper timer = registry.timer(MetricName.build("timer"));
timer.update(30, TimeUnit.MILLISECONDS);
// Use histogram.
HistogramWrapper histogram = registry.histogram(MetricName.build("histogram"));
histogram.update(20);
// Use gauge.
final List list = new ArrayList();
registry.gauge(MetricName.build("gauge"), new Gauge() {
@Override
public Number getValue() {
return list.size();
}
});