prometheus会定时从pushGateway组件中拉取信息,所以此处将埋点信息推送至PushGateway看起来类似于将埋点信息推送至prometheus
- 引入插件
<dependency>
<groupId>pers.hyx</groupId>
<artifactId>metrics-monitor</artifactId>
<version>0.0.1-beta</version>
</dependency>
- 配置metrics-monitor 在application.properties中配置metrics-monitor相关配置
// pushGateway地址
metrics.config.push-gateway=xxx:xxx:xxx:9091
// server名称,标识具体服务,建议为服务名(或其他辨识度高的标识)
metrics.config.server=test
// 推送间隔,即产生埋点后多久进行推送,若不配置默认为1分钟
metrics.config.push-rate=5000
- 启用metrics-monitor 在启动类或者配置类中加载注解 @EnableMetricsMonitor
@SpringBootApplication
@EnableMetricsMonitor
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
- 注解使用metrics-monitor 可在相关方法中加载注解**@MetricsMonitor**
@Service
public class DemoService {
@MetricsMonitor(type = MonitorType.OTHER,
remark = "test remark",
monitorFor = {NullPointerException.class, ServerErrorException.class},
noMonitorFor = {IllegalArgumentException.class, NumberFormatException.class})
public void demo() throws Exception {
//do something
}
}
注解参数说明 - type : metrics类型,可选RPC、SERVER、CUSTOM、OTHER - remark : 备注标识(可选) - monitorFor : 需埋点异常类型(可选,可多选) - noMonitorFor : 不需要埋点异常类型(可选,可多选)
- 代码块使用metrics-monitor 使用MonitorBlock类进行生成局部代码块的方式进行埋点
public void block() {
new MonitorBlock().block(new MonitorBlock.Runnable() {
@Override
public void run() {
//do something
}
}, MonitorType.RPC, "method or identification", "params like json", "remark");
}
!! 此处代码块中抛出任意异常,均会被捕获并进行埋点
- 手动添加metrics-monitor Monitor中metrics类型默认为SERVER
public void monitor() throws IOException {
//method 1
Monitor.build("method or identification", "params like json", "remark").push();
//method 2
TagProduction production = SpringUtil.getBean(TagProduction.class);
Monitor.build().setTag(production.structureTag("method or identification", "params like json", "remark")).setGraph(MonitorType.RPC).push();
//method 3
MonitorTag monitorTag = new MonitorTag();
monitorTag.setIp("host address");
monitorTag.setServer("server information");
monitorTag.setMethod("method or identification");
monitorTag.setData("params like json");
monitorTag.setRemark("remark");
Monitor.build().setTag(monitorTag).setGraph(MonitorType.RPC).push();
}
###如何查看?
- 埋点成功后可打开配置的pushGateway地址,找到job-name = exception_alert相关数据,打开即可看到埋点信息