When system monitoring event-triggered tasks do not cover your business-specific metrics, you can report custom metrics to CloudMonitor and use custom monitoring event-triggered tasks to monitor them.
Report monitoring data to CloudMonitor
Before you create a custom monitoring event-triggered task, you must report your custom metrics to CloudMonitor.
CloudMonitor lets you define custom metrics and alarm rules. After you report your monitoring data, CloudMonitor processes it and triggers alarms based on your rules.
CloudMonitor supports three methods for reporting monitoring data: OpenAPI, the Java SDK, and the Alibaba Cloud command-line interface (CLI). The following example shows how to report monitoring data by using the Java SDK.
- Add the required JAR dependencies to your project. If you use Maven to manage your project, add the following dependencies:
<dependency> <groupId>com.aliyun</groupId> <artifactId>aliyun-java-sdk-core</artifactId> <version>3.2.6</version> </dependency> <dependency> <groupId>com.aliyun.openservices</groupId> <artifactId>aliyun-cms</artifactId> <version>0.2.4</version> </dependency> - Report a custom metric to CloudMonitor.
static String endPoint = "https://metrichub-cms-cn-hangzhou.aliyuncs.com"; CMSClient cmsClient = new CMSClient(endPoint, accAutoScalingKey, accAutoScalingSecret); CustomMetricUploadRequest request = CustomMetricUploadRequest.builder() .append(CustomMetric.builder() .setMetricName("myCustomMetric")//The name of the custom metric. .setGroupId(54504L)//The ID of the application group. .setTime(new Date())//The timestamp. .setType(CustomMetric.TYPE_VALUE)//The metric type. In this example, it is a raw value. .appendValue(MetricAttribute.VALUE, number)//The raw value. The key must be MetricAttribute.VALUE. .appendDimension("key1", "value1")//Add a dimension. .appendDimension("key2", "value2") .build()) .build(); CustomMetricUploadResponse response = cmsClient.putCustomMetric(request);//Report the metric.The preceding example reports a single data point to CloudMonitor. You must specify the
groupIdparameter, which is the ID of an application group. You can use an existing application group ID or create a new one. CloudMonitor automatically creates the application group if it does not exist. - Go to the CloudMonitor console. On the Application Group page, you can view information about your application groups. On the Custom Monitoring page, you can view the reported custom metric, which is displayed as a time series.
We recommend that you push custom monitoring data to an existing application group, which logically organizes multiple cloud services for better integration with CloudMonitor and other services.
CloudMonitor automatically aggregates reported monitoring data. If you push a large volume of data, aggregate it locally before reporting. For more information about limits, see Overview.