Skywalking之告警

1.  概述

    原理:  每隔一段时间轮询skywalking-oap收集到的链路追踪的数据,再根据所配置的告警规则等,如果达到阈值则发送相应的告警信息。

            发送告警信息是以线程池异步的方式调用webhook接口完成的,具体的webhook接口由使用者自行定义,从而可以在指定的webhook接口中自行编写各种告警方式。    

2.  告警规则

    通过/usr/local/skywalking/config/oal/core.oal定义指标数据如何查询数据

    通过/usr/local/skywalking/config/alarm-settings.yml定义告警规则

rules:
  service_resp_time_rule:
    metrics-name: service_resp_time
    op: ">"
    threshold: 1000
    period: 10
    count: 3
    silence-period: 5
    message: Response time of service {name} is more than 1000ms in 3 minutes of last 10 minutes.
  service_sla_rule:
    metrics-name: service_sla
    op: "<"
    threshold: 8000
    period: 10
    count: 2
    silence-period: 3
    message: Successful rate of service {name} is lower than 80% in 2 minutes of last 10 minutes
  service_resp_time_percentile_rule:
    metrics-name: service_percentile
    op: ">"
    threshold: 1000,1000,1000,1000,1000
    period: 10
    count: 3
    silence-period: 5
    message: Percentile response time of service {name} alarm in 3 minutes of last 10 minutes, due to more than one condition of p50 > 1000, p75 > 1000, p90 > 1000, p95 > 1000, p99 > 1000
  service_instance_resp_time_rule:
    metrics-name: service_instance_resp_time
    op: ">"
    threshold: 1000
    period: 10
    count: 2
    silence-period: 5
    message: Response time of service instance {name} is more than 1000ms in 2 minutes of last 10 minutes
  database_access_resp_time_rule:
    metrics-name: database_access_resp_time
    threshold: 1000
    op: ">"
    period: 10
    count: 2
    message: Response time of database access {name} is more than 1000ms in 2 minutes of last 10 minutes
  endpoint_relation_resp_time_rule:
    metrics-name: endpoint_relation_resp_time
    threshold: 1000
    op: ">"
    period: 10
    count: 2
    message: Response time of endpoint relation {name} is more than 1000ms in 2 minutes of last 10 minutes

    rule name:  告警规则名称,如示例中的endpoint_relation_resp_time_rule,所有名称必须以_rule结尾,否则无法识别。

    metrics-name:  统计的指标,其实际值只能为int,double类型

    threshold:  metrics-name配置的这个指标触发告警的阈值,毫秒为单位

    op:  比较运算符。可以设置为大于,小于或等于

    period:  告警规则检查的时间窗口,分钟为单位

    count:  时间窗口内,如果触发告警的次数达到配置的次数,将会告警

    silence-period:  告警间隔时间,防止短时间内多次告警

3.  常用规则名

      

4.  Webhooks

    当告警规则达到触发条件后,需要通过Webhooks将告警信息发送到用户指定的Web服务上。发送的告警消息格式如下:

1.    采用http发送并且method为POST
2.    content-type为application/json,UTF-8编码
3.    数据结构定义为List<org.apache.skywalking.oap.server.core.alarm.AlarmMessage>
        public class AlarmMessage {
           private int scopeId;
           private string name;
           private int id0;
           private int id1;
           private string ruleName;
           private string alarmMessage;
           private long startTime;
}    
scopeId:类型Id;
name:目标 Scope 的实体名称;
id0:Scope 实体的 ID,与name在数据库中对应。
id1:未使用;
ruleName:您在 alarm-settings.yml 中配置的规则名;
alarmMessage. 告警消息内容;
startTime. 告警时间戳,当前时间与 UTC 1970/1/1 相差的毫秒数;

    1.  微信Hook

        1.  先在企业微信群里创建一个机器人,复制webhook的地址

            

        2.  找到skywalking的alarm-settings.yml文件

wechat:
  default:
    is-default: true
    text-template: |-
      {
        "msgtype": "text",
        "text": {
          "content": "Apache SkyWalking Alarm: \n %s."
        }
      }
    webhooks:
    - https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=dummy_key

        3.  重启skywalking服务   

    2.  钉钉Hook

        1.  设置钉钉机器人

            钉钉机器人只支持群设置,要先建一个群,然后找到自定义webhook的位置进行配置。           

                       

            

        2.  添加钉钉机器人                      

                        

                        

            

        3.  告警规则配置钉钉机器人            

                        

posted @ 2024-03-07 15:01  奋斗史  阅读(686)  评论(0)    收藏  举报