If you have your own monitoring system and want to obtain some metrics of a specific business, but system monitoring event-triggered tasks cannot meet your monitoring requirements, you can report custom monitoring metrics to CloudMonitor and use custom monitoring event-triggered tasks to monitor these metrics.
Report monitoring data to CloudMonitor
Custom monitoring event-triggered tasks is a feature provided by Auto Scaling to monitor custom metrics. Before you create a custom monitoring event-triggered task, you must report custom metrics to CloudMonitor in advance.
CloudMonitor custom monitoring is a feature that allows you to customize metrics and alarm rules. You can monitor custom metrics that are related to your business requirements. The collected monitoring data is reported to CloudMonitor. CloudMonitor processes data and generates alarms based on processing results.
CloudMonitor provides API operations, SDKs for Java, and Alibaba Cloud command-line interface (CLI) for reporting data. In this example, SDK for Java is used to show how to report monitoring data.
- Import the JAR package to a project.
If you use Apache Maven to manage a project, you must add the following dependencies to the project:
<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 custom metrics 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")//Set the name of the custom metric. .setGroupId(54504L)//Set the ID of the application group. .setTime(new Date())//Set the time. .setType(CustomMetric.TYPE_VALUE)//Set the type to original value. .appendValue(MetricAttribute.VALUE, number)//The original value. The key must be an original value. .appendDimension("key1", "value1")//Add a dimension. .appendDimension("key2", "value2") .build()) .build(); CustomMetricUploadResponse response = cmsClient.putCustomMetric(request);//Report data.
The preceding example shows how to report a metric to CloudMonitor. When you report a metric, you must specify the groupId parameter that represents the ID of an application group in CloudMonitor. You can specify an application group that is already created in CloudMonitor or a group that does not exist. If the specified application group does not exist, the system will create one.
- You can go to the CloudMonitor console to view the details of the application group on the Application Groups page. You can view the custom metrics that you reported in chronological order on the Custom Monitoring page.
We recommend that you push custom monitoring data to an existing application group in CloudMonitor to increase the flexibility of CloudMonitor and other services. An application group in CloudMonitor is a logical group of multiple cloud services.
CloudMonitor automatically aggregates the monitoring data that you reported. If you want to report a large amount of data to CloudMonitor, you can also aggregate the data locally before you report it. The reported monitoring data has some limits. For more information, see Report monitoring data.