警报机制根据来自不同层的服务/实例/端点的度量来衡量系统性能。警报内核是一个内存中的、基于时间窗口的队列。
告警规则的定义分为三部分:
官方文档:https://github.com/apache/skywalking/blob/master/docs/en/setup/backend/backend-alarm.md
SkyWalking告警的核心由一组规则驱动,这些规则定义在 config/alarm-settings.yml文件中。
查看 config/alarm-settings.yml文件,Skywalking提供了一些默认的规则,只要我们的应用服务请求符合 alarm-setting.yml文件中的某一条规则就会触发告警通知。
rules:
# Rule unique name, must be ended with `_rule`.
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 value need to be long, double or int
metrics-name: service_sla
op: "<"
threshold: 8000
# The length of time to evaluate the metrics
period: 10
# How many times after the metrics match the condition, will trigger alarm
count: 2
# How many times of checks, the alarm keeps silence after alarm triggered, default as same as period.
silence-period: 3
message: Successful rate of service {name} is lower than 80% in 2 minutes of last 10 minutes
...
参数说明:更多规则说明查看官方文档
默认报警规则:它包括以下规则:
在应用服务中编写一个接口(慢查询)来测试告警。
@GetMapping("/timeout")
public UserDO timeout(Long seconds) throws InterruptedException {
if(seconds == null){
throw new RuntimeException("seconds 不能为空");
}
TimeUnit.SECONDS.sleep(seconds);
UserDO userDO = userService.getById(11L);
return userDO;
}
启动项目,访问接口:http://127.0.0.1:18081/user/timeout?seconds=5
然后我们可以在 SkyWalking UI界面的告警菜单中发现有了一条告警信息。说明请求满足了告警规则,并且SkyWalking收集到了告警信息。

上面针对接口,演示了 SkyWalking告警信息的规则触发。如果触发告警,我们该如何通知工作人员呢?
SkyWalking在 config/alarm-settings.yml文件中提供了回调接口。我们只需要配置我们的通知接口即可。
在 config/alarm-settings.yml中配置回调接口(我们应用服务的接口),并重启 skywalking服务。
webhooks:
# - http://127.0.0.1/notify/
# - http://127.0.0.1/go-wechat/
- http://127.0.0.1:18081/sw/alarm/notify/
- http://127.0.0.1:18081/sw/alarm/notify2/
在我们应用服务中编写接口。拿到告警信息,通过不同的方式通知工作人员。
简单点,控制台输出告警信息。
@RestController
@RequestMapping("/sw/alarm")
public class SkywalkingAlarmController {
/**
* Skywalking告警通知回调接口:http://127.0.0.1:18081/sw/alarm/notify/
*
* @param obj
* @return
*/
@RequestMapping("/notify")
public String notify(@RequestBody Object obj) {
// TODO 将告警信息通知给负责人。比如:通过发短信,钉钉消息,邮件,微信通知等方式发送给技术负责人
System.err.println("收到Skywalking告警信息:" + obj.toString());
return "notify successfully";
}
}
测试告警:

SpringBoot 实现发送邮件:https://blog.csdn.net/qq_42402854/article/details/110472398
/**
* 发送邮件
*
* Skywalking告警通知回调接口:http://127.0.0.1:18081/sw/alarm/notify2/
*
* @param obj
* @return
*/
@RequestMapping("/notify2")
public String notify2(@RequestBody Object obj) {
// TODO 将告警信息通知给负责人。比如:通过发短信,钉钉消息,邮件,微信通知等方式发送给技术负责人
System.err.println("notify2 收到Skywalking告警信息:" + obj.toString());
sendSimpleMail("xxx@qq.com", "Skywalking告警信息", obj.toString());
return "notify successfully";
}
private static final String SENDER = "xxx@163.com";
@Autowired
private JavaMailSender mailSender;
/**
* 发送普通邮件
*
* @param to 收件人邮箱
* @param subject 主题(标题)
* @param content 内容
*/
public void sendSimpleMail(String to, String subject, String content) {
SimpleMailMessage message = new SimpleMailMessage();
message.setFrom(SENDER);
message.setTo(to);
message.setSubject(subject);
message.setText(content);
try {
mailSender.send(message);
} catch (Exception e) {
System.out.println("发送普通邮件时发生异常!" + e);
}
}
测试告警:

– 求知若饥,虚心若愚。