All Products
Search
Document Center

CloudMonitor:Handle alert notifications by using Function Compute

Last Updated:Aug 15, 2023

In webhook scenarios, CloudMonitor supports alert notification methods such as DingTalk, WeCom, Lark, and Slack and alert delivery methods such as Auto Scaling, Log Service, and Message Service. 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 Activate the Function Compute service.

  • 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 CloudMonitor alert notifications.

Step 1: Deploy a code function in Function Compute

  1. Create a service.

    Enter a service name. Use the default values of other parameters. For more information, see Step 2: Create a service.

  2. 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.

    Note

    The 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.

  3. Deploy the function code.

    1. On the Code tab, click Upload Code in the upper-right corner, and then select Upload JAR Package.

      Note

      Java 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 CloudMonitor 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 the StreamRequestHandler operation 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());
          }
      }
      Note

      For 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.

    2. In the Upload Code in ZIP, JAR, or WAR Package dialog box, upload the code package and click Save and Deploy.

  4. Test the function code.

    1. On the Code tab of the function, click Test Function.

    2. On the Test Function tab, click Configure test events, copy the following CloudMonitor 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 CloudMonitor console

  1. Log on to the CloudMonitor console.
  2. In the left-side navigation pane, choose Alerts > Alert Rules.

  3. On the Alert Rules page, click Create Alert Rule.

  4. In the Create Alert Rule panel, configure the basic information about the alert rule. Then, click Advanced Settings, turn on Function Compute, and select the region, service, and function of Function Compute.

    For more information about how to configure the parameters of an alert rule, see Create an alert rule.

    Note

    The region, service, and function that you select in this step must be the same as those created in Step 1.

  5. 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.

  1. Log on to the Function Compute console.

  2. In the left-side navigation pane, click Services & Functions.

  3. In the top navigation bar, select a region.

  4. On the Services page, click the name of the desired service.

  5. On the Functions page, click the name of the desired function.

  6. Click the Logs tab, click the Requests tab, and then specify a time range.

    Note

    The first time you view the invocation logs of a function, you must click Enable to enable the logging feature.

  7. Click Request Log in the Actions column of the desired invocation log.

    You can view the log details.