springboot集成prometheus
生活随笔
收集整理的這篇文章主要介紹了
springboot集成prometheus
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 Maven pom.xml引入依賴
<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_spring_boot</artifactId> </dependency>2 啟動類引入注解
import io.prometheus.client.spring.boot.EnablePrometheusEndpoint; import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication @EnablePrometheusEndpoint @EnableSpringBootMetricsCollector public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}3 Controller類寫需要監控的指標,比如Counter
import io.prometheus.client.Counter; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.Random;@RestController public class SampleController {private static Random random = new Random();private static final Counter requestTotal = Counter.build().name("my_sample_counter").labelNames("status").help("A simple Counter to illustrate custom Counters in Spring Boot and Prometheus").register();@RequestMapping("/endpoint")public void endpoint() {if (random.nextInt(2) > 0) {requestTotal.labels("success").inc();} else {requestTotal.labels("error").inc();}} }4 設置springboot應用的服務名和端口,在application.properties
spring.application.name=mydemo server.port=88885 配置prometheus.yml
global:scrape_interval: 15s # By default, scrape targets every 15 seconds.evaluation_interval: 15s # By default, scrape targets every 15 seconds.# scrape_timeout is set to the global default (10s).# Attach these labels to any time series or alerts when communicating with# external systems (federation, remote storage, Alertmanager).external_labels:monitor: 'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files:# - "first.rules"# - "second.rules"# A scrape configuration containing exactly one endpoint to scrape: # Here it's Prometheus itself. scrape_configs:# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.- job_name: 'prometheus'# Override the global default and scrape targets from this job every 5 seconds.scrape_interval: 5s# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ['localhost:9090']- job_name: 'mydemo'# Override the global default and scrape targets from this job every 5 seconds.scrape_interval: 5smetrics_path: '/prometheus'# scheme defaults to 'http'.static_configs:- targets: ['10.94.20.52:8888']最關鍵的配置就是targets: [‘10.94.20.52:8888’],就是springboot應用的ip和端口
注:在application.xml里設置屬性:spring.metrics.servo.enabled=false,去掉重復的metrics,不然在prometheus的控制臺的targets頁簽里,會一直顯示此endpoint為down狀態。
6 多次訪問 http://localhost:8888/mydemo/endpoint,然后在prometheus控制臺查看相關metrics信息,my_sample_counter,2個頁簽:Graph,Console
總結
以上是生活随笔為你收集整理的springboot集成prometheus的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【docker】docker run命令
- 下一篇: 关系数据库是如何工作的