You can use Log Service to collect logs in Knative. Besides, you can also set alerts based on logs.

Prerequisites

Procedure

  1. Configure search conditions.
    For more information, see Real-time analysis.
    1. Log on to the Log Service console and click the target project. On the Logstores tab, find the Logstore created from step 1.
    2. Click Search in the Search column.
    3. Enter the following command and click Search & Analytics.
      To set an alert on the number of errors, you can enter the following SQL statement:
      * | select 'ERROR' , count(1) as total group by 'ERROR'
  2. Configure alert settings.
    For more information, see Configure an alert.
    1. In the upper-right corner, click Save as Alert. The Create Alert page appears on the right.
      Note In the Trigger Condition field, enter a conditional expression that determines whether the alert is triggered. For more information, see Set an alarm condition expression. In this example, set Search Period to 1 Minute, Check Frequency to 1 Minute, and Trigger Condition to total > 3. This will search once every two minutes and an alert will be triggered if three errors are found in one minute.
    2. Set the alert parameters and click Next.
      Item 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 an SQL query statement.
      Search Period The Search Period parameter specifies the time range of log data that the server reads when it performs a query. You can select either 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, the server 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, the server reads the log data that was written from 14:15:00 to 14:30:00.
      Check Frequency The time when the server performs a check

      .

      Note Currently, the server checks only the first 100 data entries generated during the time specified by the search period.
      Trigger Condition The conditional expression that determines whether the alert is triggered. When the condition is met, the server sends an alert notification based on the specified Check Frequency and Notification Interval.

      For example, you can enter pv%100 > 0 && uv > 0.

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

      How do I check the sequence number of a chart?

      Notification Trigger Threshold When the cumulative number of times that the trigger condition is met exceeds this threshold, alert notifications will be sent based on the specified Notification Interval. If the trigger condition is not met, the overall count does not change.

      The default value of Notification Trigger Threshold is 1. This means that each time the specified Trigger Condition is met, the server checks whether the specified Notification Interval arrives.

      You can also enter a custom value so that the server sends an alert notification only after the trigger condition is met multiple times. For example, if you set the value to 100, the server checks whether the specified Notification Interval arrives only after the trigger condition is met 100 times. If the specified Notification Trigger Threshold is reached and the specified Notification Interval arrives, the server sends an alert notification. The overall count is then reset to zero. If the server fails to check log data due to exceptions such as network failures, the overall count does not change.
      Notification Interval

      The time interval at which the server sends an alert notification.

      If the cumulative number of times that the trigger condition is met exceeds the specified Notification Trigger Threshold and the specified notification interval arrives, the server sends an alert notification. If you set the value to 5 minutes, you can receive up to one alert notification in every five-minute period. The default value is No Interval.

      Note You can use the Notification Trigger Threshold and Notification Interval parameters to control the number of alert notifications that you receive.
    3. Specify the notification details, and then click Submit.
      Notification method Description
      DingTalk

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

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

      For more information about how to configure DingTalk Chatbot and obtain the request URL, see Configure an alert.

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

      Sends alert notifications to a custom WebHook URL through a specific method. To use this notification method, you must set Request URL, Request Method, and Request Content.

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

      Notifications

      Sends alert notifications to specific contacts through the notification method specified in Alibaba Cloud Message Center. To use this notification method, you must set Content. Enter the content of the notification in the Content field, which must be 1 to 500 characters in length. Template variables are supported.

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

  3. Run the following command to access the Hello World service.
    An alert will be triggered.
    $ curl -H "Host: helloworld-go.default.example.com" http://112.124.XX.XX
    Hello Knative!
  4. The following figure shows the alert information sent by email.

Summary

After Log Service is enabled on Knative, you can set up alerts to monitor the exceptions that may have occurred in applications and notify related O&M engineers and developers at the earliest opportunity. This helps ensure the availability of your service.