告警基本流程
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;
}