Prometheus+Grafana+SpringBoot业务埋点可视化监控
引入依赖
<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>
application.yml文件
server:
port: 8010
spring:
application:
name: spring-boot-prometheus-grafana
management:
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
controller中埋点
@RestController
@RequestMapping("/api")
public class MainController {
@Autowired
MeterRegistry registry;
private Counter counter;
private Counter failCounter;
@PostConstruct
private void init(){
counter = registry.counter("requests_add_total","save","carson");
failCounter= registry.counter("requests_add_fail_total","save","carson");
}
@GetMapping("/add")
public String add(String name,Integer age) {
counter.increment();
if(RandomUtils.nextInt(0,1000)<185){
failCounter.increment();
return "error";
}else{
return "name="+name+" age="+age;
}
}
}
在prometheus.yml添加
- job_name: 'spring_boot_8010'
metrics_path: /actuator/prometheus
static_configs:
- targets: ['127.0.0.1:8010']
在grafana中添加展示图表