skywalking告警相關配置

告警基本流程
        skywalking發送告警的基本原理是每隔一段時間輪詢skywalking-collector收集到的鏈路追蹤的數據,再根據所配置的告警規則(如服務響應時間、服務響應時間百分比)等,如果達到閾值則發送響應的告警信息。發送告警信息是以線程池異步的方式調用webhook接口完成,(具體的webhook接口可以使用者自行定義),從而開發者可以在指定的webhook接口中自行編寫各種告警方式,釘釘告警、郵件告警等等。

告警相關配置
開啓skywalking相關告警配置,編輯 config/alarm-settings.yml,打開之後如下所示:rules即爲需要配置的告警規則的列表;第一個規則‘endpoint_percent_rule’,是規則名,不能重複且必須以’_rule’爲結尾;其中裏面的屬性:
屬性    含義
metrics-name    指定的規則(與規則名不同,這裏是對應的告警中的規則map,具體可查看https://github.com/apache/skywalking/blob/master/docs/en/setup/backend/backend-alarm.md#list-of-all-potential-metrics-name,其中一些常見的,endpoint_percent_rule——端點相應半分比告警,service_percent_rule——服務相應百分比告警)
threshold    閾值,與metrics-name和下面的比較符號相匹配
op    比較操作符,可以設定>,<,=,即如metrics-name: endpoint_percent, threshold: 75,op: < ,表示如果相應時長小於平均75%則發送告警
period    多久檢查一次當前的指標數據是否符合告警規則
counts    達到多少次告警後,發送告警消息
silence-period    在多久之內,忽略相同的告警消息
message    告警消息內容
include-names    使用本規則告警的服務列表

rules:
  # Rule unique name, must be ended with `_rule`.
  endpoint_percent_rule:
    # Metrics value need to be long, double or int
    metrics-name: endpoint_percent
    threshold: 75
    op: <
    # The length of time to evaluate the metrics
    period: 10
    # How many times after the metrics match the condition, will trigger alarm
    count: 3
    # How many times of checks, the alarm keeps silence after alarm triggered, default as same as period.
    silence-period: 10
    
  service_percent_rule:
    metrics-name: service_percent
    # [Optional] Default, match all services in this metrics
    include-names:
      - service_a
      - service_b
    threshold: 85
    op: <
    period: 10
    count: 4

webhooks:
 - http://127.0.0.1//alarm/test

webhook接口url的定義(地址自定義),除了規則制定之外,還有達到告警規則後,需要skywalking調用的webhook接口,如上所示的配置,一定要注意url的縮進,之前縮進兩個空格,一直沒生效。
        配置完成之後,重啓skywalking生效;

告警webhook接口對接
        編寫上述webhook對接的接口,http://127.0.0.1//alarm/test ,當前版本webhook接口調用的方式是post+requestbody,而body中的內容如下:

[
    {
        "scopeId":1,  //指的是告警的範圍類型(源碼中有定義常量org.apache.skywalking.oap.server.core.source.DefaultScopeDefine)
        "name":"gateway", //告警服務名稱
        "id0":3,  //與服務名稱一一匹配
        "id1":0,  //暫時未做使用 
        "alarmMessage":"Response time of service gateway is more than 1000ms in 3 minutes of last 10 minutes.",
        "startTime":1569552742633  //告警發起時間戳
    },
    {
        "scopeId":1,
        "name":"en-exercise",
        "id0":2,
        "id1":0,
        "alarmMessage":"Response time of service en-exercise is more than 1000ms in 3 minutes of last 10 minutes.",
        "startTime":1569552742633
    }
]

於是定義的接口如下:

@RequestMapping("/alarm")
@RestController
public class AlarmController {
    @Autowired
    AlarmService alarmService;

    @RequestMapping(value = "/test",method = RequestMethod.POST)
    public void alarm(@RequestBody List<AlarmMessageDto> alarmMessageList){
        System.out.println(alarmMessageList.toString());
        //具體處理告警信息
        alarmService.doAlarm(alarmMessageList);
    }
}
//實體類
@Data
public class AlarmMessageDto {
    private int scopeId;
    private String name;
    private int id0;
    private int id1;
    private String alarmMessage;
    private long startTime;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章