繼上一篇配置了skywalking+nacos+gateway+demoservice以後,這一篇主要提供一個skywalking告警規則的配置,及dingding告警相關數據的發送。git
skywalking發送告警的基本原理是每隔一段時間輪詢skywalking-collector收集到的鏈路追蹤的數據,再根據所配置的告警規則(如服務響應時間、服務響應時間百分比)等,若是達到閾值則發送響應的告警信息。發送告警信息是以線程池異步的方式調用webhook接口完成,(具體的webhook接口可使用者自行定義),從而開發者能夠在指定的webhook接口中自行編寫各類告警方式,釘釘告警、郵件告警等等。github
屬性 | 含義 |
---|---|
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
複製代碼
配置完成以後,重啓skywalking生效;web
編寫上述webhook對接的接口,http://127.0.0.1//alarm/test ,當前版本webhook接口調用的方式是post+requestbody,而body中的內容以下:apache
[
{
"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
}
]
複製代碼
因而定義的接口以下:bash
@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;
}
複製代碼
如上所示,咱們在webhook接口拿到數據後,在service中處理相關邏輯alarmService.doAlarm(alarmMessageList);所以能夠自行制定告警發送的地方,如釘釘、郵件等,具體接入方式釘釘文檔及博客均有描述,就再也不過多闡述。app
這篇文章主要介紹了skywalking告警的簡單原理、告警規則配置、webhook接收接口編寫等,後續還會對skywalking進行個性化開發,但願與你們多多交流。異步
參考:github.com/apache/skyw…post