本例介绍Pinpoint告警的相关内容,Pinpoint参考【APM】Pinpoint 安装部署(一)
Pinpoint Web会定期检查应用程序的状态,并在满足某些预配置条件(规则)的情况下触发警报。
这些条件(默认情况下)由Web模块中的后台批处理过程每3分钟检查一次(默认情况下),使用的是最后5分钟的数据。一旦满足条件,批处理过程就会向注册到用户组的用户发送短信/电子邮件。
Pinpoint告警原理
1、下载源码,通过源码解析:
Pinpoint中有一个匹处理类,BatchConfiguration.java,此类会进行批任务处理
1 @Configuration2 @Conditional(BatchConfiguration.Condition.class)3 @ImportResource("classpath:/batch/applicationContext-batch-schedule.xml")4 public classBatchConfiguration{5 static class Condition implementsConfigurationCondition {6 @Override7 public booleanmatches(ConditionContext context, AnnotatedTypeMetadata metadata) {8 ......9 Resource resource = context.getResourceLoader().getResource("classpath:/batch.properties")10 ......11 final String enable = properties.getProperty("batch.enable");12 ......13 }14 }15 }
Condition中会读取配置文件batch.properties中的配置项batch.enable,默认是false。因此如果要开启批处理功能,必须设置batch.enable=true。
2、在Pinpoint中,在批处理任务配置文件(applicationContext-batch-schedule.xml)中,文件路径为:pinpoint/web/src/main/resources/batch/applicationContext-batch-schedule.xml,可以找到定义的任务
1
2
3
4
5
6
7
配置文件中,定义了执行告警Job的时间。定义了一个线程池大小为1的调度器scheduler
3、在Pinpoint中,在告警任务配置文件(applicationContext-alarmJob.xml)中,文件路径为:pinpoint/web/src/main/resources/batch/applicationContext-alarmJob.xml,
reader:读取数据 => 通过用户配置的规则提供Checker,即异常校验器。
processor:处理数据 => 用Checker进行校验,标记异常状态。
writer:回写数据 => 判断Checker是否有异常情况,有则报警。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
4、在回写数据类中AlarmWriter.java中发现,是通过注入AlarmMessageSender来发送消息
1 public class AlarmWriter implements ItemWriter{2
3 @Autowired(required = false)4 private AlarmMessageSender alarmMessageSender = newEmptyMessageSend