This topic describes how to use Log Service to collect log data from Knative and configure alerts based on the collected log data.

Prerequisites

Procedure

  1. Configure search conditions.
    For more information, see Real-time log analysis.
    1. Log on to the Log Service console. Create a Log Service project named helloworld and a Logstore named helloworld-alert. For more information, see Enable Log Service on Knative.
    2. In the Log Service console, find and click the helloworld project. On the details page of the project, find and click the helloworld-alert Logstore.
    3. Enter the following command into the bar below helloworld-alert and click Search & Analyze on the right side of the page:
      To set an alert based on the number of errors that have occurred, you can enter the following SQL statement:
      * | select 'ERROR' , count(1) as total group by 'ERROR'
  2. Configure alert settings.
    For more information, see Create an alert rule.
    1. In the upper-right corner of the helloworld-alert tab, click Save as Alert. The Create Alert panel appears.
      Note In the Trigger Condition field, enter a conditional expression that determines whether to trigger an alert. For more information, see Syntax of trigger conditions in alert rules. In this example, set Search Period to 1 minute, Check Frequency to 1 minute, and Trigger Condition to total > 3. This means that a search is performed every other minute. If three or more errors are found from the log data within the searched period, an alert is triggered.
    2. Set the alert parameters and click Next.
      Parameter Description
      Alert Name The name of the alert. The name must be 1 to 64 characters in length.
      Add to New Dashboard Add the chart created based on the search results to a dashboard.
      Chart Name The name of the chart. The name must be 1 to 64 characters in length.
      Query Enter a SQL statement.
      Search Period The Search Period parameter specifies the time range of log data that the server reads when a search is performed. You can select a relative time or a time frame. For example, if you set Search Period to 15 minutes (relative) and start the query at 14:30:06, Log Service reads the log data that was written from 14:15:06 to 14:30:06. If you set Search Period to 15 minutes (time frame) and start the query at 14:30:06, Log Service reads the log data that was written from 14:15:00 to 14:30:00.
      Check Frequency The time interval at which the server checks log data based on the alert configuration.
      Note The server collects and checks only the first 100 data entries that are generated during the time specified by the search period.
      Trigger Condition The conditional expression that determines whether to trigger an alert. If the condition is met, Log Service sends notifications based on Check Frequency and Notification Interval.

      For example, you can enter pv%100 > 0 && uv > 0 into the Trigger Condition field.

      Note In the conditional expression, you can use $[sequence number] to differentiate charts. For example, you can use $0 to identify the chart whose sequence number is 0.

      For more information, see How can I view the serial number of a chart?

      Notification Trigger Threshold When the cumulative number of times that the trigger condition is met reaches the threshold, notifications are sent based on Notification Interval.

      The default value of Notification Trigger Threshold is 1. This means that each time Trigger Condition is met, Log Service checks Notification Interval to decide whether to send a notification.

      You can also set Log Service to send only one notification after the trigger condition is met a specified number of times. For example, if you set the value to 100, Log Service checks Notification Interval to decide whether to send a notification only when the trigger condition is met 100 times. If both Notification Trigger Threshold and Notification Interval are reached, Log Service sends a notification. After Log Service sends the notification, it resets the counter to 0. Query failures that are caused by network errors are not counted.
      Notification Interval

      The time interval at which Log Service sends notifications.

      If the cumulative number of times that the trigger condition is met reaches Notification Trigger Threshold and the length of time since the last notification reaches the specified notification interval, Log Service sends a notification to the specified recipients. If you set this parameter to 5 minutes, you receive only one notification every 5 minutes. The default value is No Interval.

      Note You can use the Notification Trigger Threshold and Notification Interval parameters to control the number of notifications that you can receive during a specified time period.
    3. Specify the notification recipients and content, and click Submit.
      Notification method Description
      DingTalk

      Sends notifications through DingTalk. When an alert is triggered, a DingTalk chatbot sends a notification to a DingTalk group. To use this notification method, you must specify Request URL and Content.

      Enter the content of the DingTalk message into the Content field. The content must be 1 to 500 characters in length. Template variables are supported.

      For more information about how to configure the DingTalk chatbot and obtain Request URL, see Create an alert rule.

      Note Each DingTalk chatbot can send up to 20 notifications per minute.
      WebHook

      Sends notifications to a custom webhook URL in a specific method. To use this notification method, you must specify Request URL, Request Method, and Content.

      Valid values of Request Method: GET, PUT, POST, DELETE, and OPTIONS. Enter the notification content into the Content field. The content must be 1 to 500 characters in length. Template variables are supported.

      Notifications

      Sends notifications to specific contacts by using the notification method specified in Alibaba Cloud Message Center. To use this notification method, you must specify Content. Enter the notification content into the Content field. The content must be 1 to 500 characters in length. Template variables are supported.

      In addition, you must specify contacts and the notification method in Message Center.

  3. Run the following command to access the Hello World application:
    An alert is triggered.
    curl -H "Host: helloworld-go.default.example.com" http://112.124.XX.XX
    Hello Knative!
  4. You are notified by email if setting up email notifications.

Summary

After Log Service is enabled on Knative, you can detect anomalies of applications, and then immediately alert operations and maintenance (O&M) engineers and developers. This ensures business continuity.