普罗米修斯java_springboot集成普罗米修斯(Prometheus)的方法

普罗⽶修斯java_springboot集成普罗⽶修斯(Prometheus)
的⽅法
Prometheus 是⼀套开源的系统监控报警框架。它由⼯作在 SoundCloud 的 员⼯创建,并在 2015 年正式发布的开源项⽬。2016
年,Prometheus 正式加⼊ Cloud Native Computing Foundation,⾮常的受欢迎。
简介
Prometheus 具有以下特点:
⼀个多维数据模型,其中包含通过度量标准名称和键/值对标识的时间序列数据
PromQL,⼀种灵活的查询语⾔,可利⽤此维度
不依赖分布式存储; 单服务器节点是⾃治的
时间序列收集通过HTTP上的拉模型进⾏
通过中间⽹关⽀持推送时间序列
通过服务发现或静态配置发现⽬标
多种图形和仪表板⽀持模式
Prometheus 组成及架构
声明:该⼩节参考了⽂章[Prometheus ⼊门与实践]
Prometheus ⽣态圈中包含了多个组件,其中许多组件是可选的:
Prometheus Server: ⽤于收集和存储时间序列数据。
Client Library: 客户端库,为需要监控的服务⽣成相应的 metrics 并暴露给 Prometheus server。当 Prometheus server 来 pull 时,直接返回实时状态的 metrics。
Push Gateway: 主要⽤于短期的 jobs。由于这类 jobs 存在时间较短,可能在 Prometheus 来 pull 之前就消失了。为此,这次 jobs 可以直接向 Prometheus server 端推送它们的 metrics。这种⽅式主要⽤于服务层⾯的 metrics,对于机器层⾯的 metrices,需要使⽤
node exporter。
Exporters: ⽤于暴露已有的第三⽅服务的 metrics 给 Prometheus。
Alertmanager: 从 Prometheus server 端接收到 alerts 后,会进⾏去除重复数据,分组,并路由到对收的接受⽅式,发出报警。常见的接收⽅式有:电⼦邮件,pagerduty,OpsGenie, webhook 等。
⼀些其他的⼯具。
其⼤概的⼯作流程是:
1.Prometheus server 定期从配置好的 jobs 或者 exporters 中拉 metrics,或者接收来⾃ Pushgateway 发过来的 metrics,或者从其他的 Prometheus server 中拉 metrics。
2.Prometheus server 在本地存储收集到的 metrics,并运⾏已定义好的 alert.rules,记录新的时间序列或者向 Alertmanager 推送警报。
3.Alertmanager 根据配置⽂件,对接收到的警报进⾏处理,发出告警。
4.在图形界⾯中,可视化采集数据。
springboot 集成prometheus
在spring boot⼯程中引⼊actuator的起步依赖,以及micrometer-registry-prometheus的依赖。
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-actuator
io.micrometer
micrometer-registry-prometheus
暴露prometheus的接⼝;暴露metrics.tags,和spring.application.name⼀致。
server:
port: 8081
spring:
application:
name: my-prometheus
玻璃钢料塔management:
endpoints:
web:
exposure:
include: 'prometheus'
metrics:
tags:
application: ${spring.application.name}
写⼀个API接⼝,⽤作测试。代码如下:
隐蔽微型摄像机
@RestController
public class TestController {
Logger logger = Logger(TestController.class);
@GetMapping("/test")
public String test() {
logger.info("test");
return "ok";
}
@GetMapping("")
public String home() {
logger.info("home");
实验室用振荡器return "ok";
}
}
半轴螺栓# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="my-prometheus",} 2.863661056E9
# HELP http_server_requests_seconds
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{application="my-
prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0
http_server_requests_seconds_sum{application="my-
媒体播放prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327 # HELP http_server_requests_seconds_max
# TYPE http_server_requests_seconds_max gauge
http_server_requests_seconds_max{application="my-
prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327 # HELP jvm_threads_states_threads The current number of threads having NEW state
# TYPE jvm_threads_states_threads gauge
jvm_threads_states_threads{application="my-prometheus",state="waiting",} 12.0
jvm_threads_states_threads{application="my-prometheus",state="runnable",} 8.0
jvm_threads_states_threads{application="my-prometheus",state="timed-waiting",} 2.0
jvm_threads_states_threads{application="my-prometheus",state="terminated",} 0.0
jvm_threads_states_threads{application="my-prometheus",state="blocked",} 0.0
jvm_threads_states_threads{application="my-prometheus",state="new",} 0.0
# HELP process_files_open_files The open file descriptor count
# TYPE process_files_open_files gauge
...省略更多
安装Prometheus
tar xvfz prometheus-*.
cd prometheus-*
修改Prometheus的配置⽂件l,代码如下:
global:
scrape_interval: 15s # By default, scrape targets every 15 seconds.
# Attach these labels to any time series or alerts when communicating with
# external systems (federation, remote storage, Alertmanager).
external_labels:
monitor: 'codelab-monitor'
# 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=` 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
static_configs:
- targets: ['localhost:9090']
- job_name: 'springboot_prometheus'
scrape_interval: 5s
metrics_path: '/actuator/prometheus'
static_configs:
粉碎机器人- targets: ['127.0.0.1:8081']
config.job_name,配置job的名称
config.scrape_interval,配置多久抓⼀次监控信息
config.static_configs.targets配置获取监控信息的地址。
使⽤以下的命令启动prometheus,并通过–config.file指定配置⽂件
./prometheus --config.l
prometheus提供了⼀些可视化图,⽐如使⽤柱状图来展⽰每秒请求数:
安装grafana

本文发布于:2024-09-21 14:44:41,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/144888.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:时间   配置   序列   数据   监控   服务
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议