Alert notifications can be managed based on Function Compute. In webhook scenarios, Cloud Monitor supports alert notification methods such as DingTalk, WeCom, Lark, and Slack and alert delivery methods such as Auto Scaling, Simple Log Service, and Simple Message Queue (SMQ, formerly MNS). If the existing alert notification methods and alert delivery methods cannot meet your requirements, you can use Function Compute to write code so that other tools can receive alert notifications.
Prerequisites
Function Compute is activated. For more information, see Step 1: Activate Function Compute.
If you expect other tools to receive alert notifications, prepare their webhook URLs in advance.
Scenarios
Use Function Compute to process the content format of alert notifications so that other tools can receive alert notifications.
Use Function Compute to customize the content of Cloud Monitor alert notifications.
Step 1: Deploy a code function in Function Compute
Create a service.
Enter a service name. Use the default values of other parameters. For more information, see Step 2: Create a service.
Create a function.
In the Basic Settings section, specify Function Name and set Handler Type to Event Handler. Use the default values of other parameters. For more information, see Step 3: Create a function.
In this example, Runtime is set to Java 8.
NoteThe setting of Handler in the Advanced Settings section must be consistent with the function code. For example, the default value of Handler for the function is example.App::handleRequest. In the default value, example identifies a package name, App identifies a class name, and handleRequest identifies a class method. To view the requirements for other programming languages, click the "Event handler" link provided in Function Compute runtimes.
Deploy the function code.
On the Code tab, click Upload Code in the upper-right corner, and then select Upload JAR Package.
NoteJava is a compiled programming language and does not support online code viewing and editing. You must develop the code locally and then upload the code by using an Object Storage Service (OSS) or JAR package. For more information about how to edit and deploy a code package in the Java environment, see Compile and deploy code packages.
The following sample code shows a JAR package in the function code. The sample code indicates that the Cloud Monitor alert notification data is first transmitted to the function by running
inputStream. Then, you can modify the data format and send the data to different webhook URLs for business logic processing. The sample code implements theStreamRequestHandleroperation and prints only the alert notification data.package example; import com.aliyun.fc.runtime.Context; import com.aliyun.fc.runtime.FunctionInitializer; import com.aliyun.fc.runtime.StreamRequestHandler; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Hello world! */ public class App implements StreamRequestHandler, FunctionInitializer { public void initialize(Context context) throws IOException { //TODO } @Override public void handleRequest( InputStream inputStream, OutputStream outputStream, Context context) throws IOException { int n; StringBuilder sb = new StringBuilder(); while ((n = inputStream.read()) != -1) { sb.append((char) n); } System.out.println("alert param"); System.out.println(sb.toString()); } }NoteFor code development, see the following documentation:
For more information about how to develop code in multiple programming languages, see Overview.
For more information about the event functions in the sample code, see Event functions. You can download a project from the sample code repository of Function Compute and then develop the function code quickly.
For more information about Java development, see Event handlers.
In the Upload Code in ZIP, JAR, or WAR Package dialog box, upload the code package and click Save and Deploy.
Test the function code.
On the Code tab of the function, click Test Function.
On the Test Function tab, click Configure test events, copy the following Cloud Monitor alert notification data to the Create New Test Event section, and then click Test Function.
After the execution is successful, you can view the execution result, log output, and business verification result.
{ "product":"KVstore", "metricName":"ShardingCpuUsage", "instance":{ "instanceId":"r-bp1wt5u2ov6e5w****", "resource":"r-bp1wt5u2ov6e5w****", "instanceName":"online", "vpc":{ "vswitchInstanceId":"vsw-bp1rpjm10in1wgxd4****", "vpcInstanceId":"vpc-bp1n5sng5dntm3daf****" }, "aliUid":173651113438****, "category":"KVSTORE", "region":{ "regionId":"cn-hangzhou", "availabilityZone":"" }, "networkType":"VPC", "dimension":{ }, "desc":"", "tags":[ { "value":"1", "key":"__level__" } ] }, "Average":0.483, "userId":"173651113438****", "curLevel":"INFO", "Maximum":0.483, "__ts__":168612****, "instanceId":"r-bp1wt5u2ov6e5w****", "escalation":{ "comparisonOperator":"GreaterThanOrEqualToThreshold", "expression":"$Average >= 0.1", "level":4, "statistics":"Average", "tag":"INFO", "threshold":"0.1", "times":1 }, "ruleName":"test-tair", "nodeId":"r-bp1wt5u2ov6e5w****-db-1", "timestamp":1686123300000 }
Step 2: Configure an alert rule in the Cloud Monitor console
Log on to the Cloud Monitor console.
In the left-side navigation pane, choose .
On the Alert Rules page, click Create Alert Rule.
In the Create Alert Rule panel, configure the basic information about the alert rule. Then, click Advanced Settings, turn on Function Compute, and select its region, service, and function.
For more information about how to configure the parameters of an alert rule, see Create an alert rule.
NoteThe region, service, and function that you select in this step must be the same as those created in Step 1.
Click Confirm.
Step 3: View the alert triggering result
When an alert is triggered based on the alert rule for which Function Compute is turned on, the specified function is called. You can view the function invocation logs in the Function Compute console and determine whether the logs meet your expectations.
Log on to the Function Compute 3.0 console.
In the left-side navigation pane, click Functions. In the top navigation bar, select a region.
Click the name of the desired function.
In the middle of the page, click the Logs tab, then click the Invocation Requests tab, and select a time range.
NoteIf you view invocation logs for a function for the first time, click Enable to enable the log feature.
Click Request Log in the Actions column to view the details of the desired invocation log.