springboot集成pom
<!-- SpringBoot Web容器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
这个是前几天网上一位道友的代码,是在是找不到记录了。
全局配置异常切面拦截器
@ControllerAdvice
public class GlobalExceptionHandler {
@Resource
private PrometheusCustomMonitor monitor;
@ResponseBody
@ExceptionHandler(value = Exception.class)
public String handle(Exception e) {
monitor.getRequestErrorCount().increment();
return "error, message: " + e.getMessage();
}
}
监控项编写
@Component
public class PrometheusCustomMonitor {
/**
* 记录请求出错次数
*/
private Counter requestErrorCount;
/**
* 订单发起次数
*/
private Counter orderCount;
/**
* 金额统计
*/
private DistributionSummary amountSum;
private final MeterRegistry registry;
@Autowired
public PrometheusCustomMonitor(MeterRegistry registry) {
this.registry = registry;
}
@PostConstruct
private void init() {
requestErrorCount = (Counter) registry.counter("requests_error_total", "status", "error");
orderCount = (Counter) registry.counter("order_request_count", "order", "test-svc");
amountSum = registry.summary("order_amount_sum", "orderAmount", "test-svc");
}
public Counter getRequestErrorCount() {
return requestErrorCount;
}
public Counter getOrderCount() {
return orderCount;
}
public DistributionSummary getAmountSum() {
return amountSum;
}
}
网路请求接口
@RestController
public class TestController {
@Resource
private PrometheusCustomMonitor monitor;
//....
@RequestMapping("/order")
public String order(@RequestParam(defaultValue = "0") String flag) throws Exception {
// 统计下单次数
monitor.getOrderCount().increment();
if ("1".equals(flag)) {
throw new Exception("出错啦");
}
Random random = new Random();
int amount = random.nextInt(100);
// 统计金额
monitor.getAmountSum().record(amount);
return "下单成功, 金额: " + amount;
}
}
启动配置项重要
@SpringBootApplication
public class Application {
public static void main(String[] args)
{
SpringApplication.run(Application.class, args);
}
// 注意,这个是注册的核心代码块
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
项目启动文件配置
spring:
profiles:
active: dev
application:
name: prometheus-example
management:
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}
现在就完成了集成 启动默认地址
http://127.0.0.1:8080/actuator/prometheus
下面显示已经注入成功
rules配置参考文章 普罗米修斯钉钉推送(新版)
接下来操作Grafana
选择code 将自定义加入自定义公式
自定义失败率
sum(rate(requests_error_total{application="prometheus-example"}[10m])) / sum(rate(order_request_count_total{application="prometheus-example"}[10m])) * 100
不需要计算的总额、错误数可以直接选择
当将项目部署到服务器后,自定义的expr会自动展示出来也就是Metric
想看的话可以在Grafana的alert-rules里面看到
在自己的普罗米修斯的alert里面也可以看到,不过Grafana可以看到钉钉的推送及结果看着更友好
普罗米修斯自己的也可以不咋地
application事自己的服务 在普罗米修斯的配置文件里面有
请求4次失败 接口
看到变化了
收到消息