Prometheus 普罗米修斯监控
周末在家无聊 看新闻 看到关于监控的东西 拿来玩玩 试一下 感觉还蛮有意思 特此记录一下
这里只介绍客户端的配置
1:首先在POM中添加依赖
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>${prometheus.version}</version>
</dependency>
<!-- Hotspot JVM metrics-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>${prometheus.version}</version>
</dependency>
<!-- Exposition servlet-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>${prometheus.version}</version>
</dependency>
<!-- Pushgateway exposition-->
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_pushgateway</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_web</artifactId>
<version>${prometheus.version}</version>
</dependency>
2:在web.xml文件里添加servlet拦截器
<servlet>
<servlet-name>PrometheusServlet</servlet-name>
<servlet-class>io.prometheus.client.exporter.MetricsServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PrometheusServlet</servlet-name>
<url-pattern>/metrics</url-pattern>
</servlet-mapping>
web.xml
3:添加拦截器class
public class WebInterceptor extends HandlerInterceptorAdapter { private static Logger logger = Logger.getLogger(WebInterceptor.class); private static final String SYSTEM_ID= "BLOAN-GJJ"; private static final String APP_ID= "gjj-ics-credit"; private static final String TYPE = "java"; private static final Histogram requestLatency = Histogram.build()
.name("service_requests_latency_seconds")
.help("Request latency in seconds.")
.labelNames("systemId", "appId", "type", "name", "method").register(); private ThreadLocal<Histogram.Timer> timerThreadLocal; @Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
return super.preHandle(request, response, handler);
} @Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
String name = this.getName(request, handler).toLowerCase();
String method = request.getMethod().toUpperCase();
timerThreadLocal = new ThreadLocal<>();
timerThreadLocal.set(requestLatency.labels(SYSTEM_ID, APP_ID, TYPE, name, method).startTimer());
super.postHandle(request, response, handler, modelAndView);
} @Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
super.afterCompletion(request, response, handler, ex);
if (timerThreadLocal.get() != null) {
timerThreadLocal.get().observeDuration();
}
} @Override
public void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
super.afterConcurrentHandlingStarted(request, response, handler);
} private String getName(HttpServletRequest request, Object handler) {
String name = "";
try {
if (handler != null
&& handler instanceof HandlerMethod) {
HandlerMethod method = (HandlerMethod) handler;
String className = ((HandlerMethod) handler).getBeanType().getName();
name = className + "." + method.getMethod().getName();
} else {
name = request.getRequestURI();
}
} catch (Exception ex) {
logger.error("getName", ex);
} finally {
return name;
}
}
}
4:配置初始化的类
@Component
public class PrometheusConfig { private static Logger logger = Logger.getLogger(PrometheusConfig.class); @PostConstruct
public void initialize() {
logger.info("prometheus init...");
DefaultExports.initialize();
logger.info("prometheus has been initialized...");
} }
PrometheusConfig.class
5:springmvc.xml中 添加拦截器
<mvc:interceptor>
<bean class="com.pingan.credit.interceptor.WebInterceptor"/>
</mvc:interceptor>
6:spring-application.xml 初始化 初始化类中的方法
<bean id="prometheusConfig" class="com.pingan.credit.service.PrometheusConfig" init-method="initialize" />
7:添加controller
@Controller
public class MetricsController {
private CollectorRegistry registry; /**
* Construct a MetricsServlet for the default registry.
*/
public MetricsController() {
this(CollectorRegistry.defaultRegistry);
} /**
* Construct a MetricsServlet for the given registry.
*/
private MetricsController(CollectorRegistry registry) {
this.registry = registry;
} @RequestMapping(value="/metrics",method= RequestMethod.GET)
public void metrics(final HttpServletRequest req,
final HttpServletResponse resp) throws ServletException,
IOException {
resp.setStatus(HttpServletResponse.SC_OK);
resp.setContentType(TextFormat.CONTENT_TYPE_004); try (Writer writer = resp.getWriter()) {
TextFormat.write004(writer,
registry.filteredMetricFamilySamples(parse(req)));
writer.flush();
}
} private Set<String> parse(HttpServletRequest req) {
String[] includedParam = req.getParameterValues("name[]");
if (includedParam == null) {
return Collections.emptySet();
} else {
return new HashSet<>(Arrays.asList(includedParam));
}
}
}
启动tomcat 以及你的project 然后进入localhost:8080/metrics就可以看到JVM的信息了
Prometheus 普罗米修斯监控的更多相关文章
- Prometheus监控学习笔记之Prometheus普罗米修斯监控入门
0x00 概述 视频讲解通过链接网易云课堂·IT技术快速入门学院进入,更多关于Prometheus的文章. Prometheus是最近几年开始流行的一个新兴监控告警工具,特别是kubernetes的流 ...
- Prometheus普罗米修斯快速入门
欢迎来到普罗米修斯! Prometheus是一个监控平台,通过从监控目标的抓取HTTP端点上获取指标. 本指南将展示如何使用和安装Promethues,配置和监视第一个资源.还将下载并安装导出器Exp ...
- 监控神器-普罗米修斯Prometheus的安装
搬砖党的福音:普罗米修斯-监控神器 功能: 在业务层用作埋点系统 Prometheus支持多种语言(Go,java,python,ruby官方提供客户端,其他语言有第三方开源客户端).我们可以通过客户 ...
- 普罗米修斯+grafana监控k8s
其实现原理有点类似ELK.node-exporter组件负责收集节点上的metrics监控数据,并将数据推送给prometheus, prometheus负责存储这些数据,grafana将这些数据通过 ...
- 当ABAP遇见普罗米修斯
Jerry每次在工作场合中同Prometheus(普罗米修斯)打交道时,都会"出戏",因为这个单词给我的第一印象,并不是用go语言实现的微服务监控利器,而是名导雷德利·斯科特(Ri ...
- 一步步教你用Prometheus搭建实时监控系统系列(一)——上帝之火,普罗米修斯的崛起
上帝之火 本系列讲述的是开源实时监控告警解决方案Prometheus,这个单词很牛逼.每次我都能联想到带来上帝之火的希腊之神,普罗米修斯.而这个开源的logo也是火,个人挺喜欢这个logo的设计. 本 ...
- 普罗米修斯!Ubuntu下prometheus监控软件安装使用
*Prometheus* 是一个开源的服务监控系统和时间序列数据库 官方网站:prometheus.io 一.安装prometheus cd /usr/local/ #进入安装目录 wg ...
- 普罗米修斯Prometheus监控安装
普罗米修斯Prometheus监控安装 架构: 服务端:192.168.0.204 客户端:192.168.0.206 环境准备:所有节点安装go 语言环境 rz go1.12.linux-amd64 ...
- 在Grafana使用普罗米修斯
aaarticlea/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IE ...
随机推荐
- POJ 3666 Making the Grade(数列变成非降序/非升序数组的最小代价,dp)
传送门: http://poj.org/problem?id=3666 Making the Grade Time Limit: 1000MS Memory Limit: 65536K Total ...
- WSASocket()与Socket()的区别 转
/**************************************************** WSASocket是Windows专用,支持异步操作:socket是unix标准,只能同步操 ...
- laravel5项目安装debugbar
链接:https://github.com/barryvdh/laravel-debugbar 1.项目目录运行 composer require barryvdh/laravel-debugbar ...
- ListItem Updating事件监视有没有上传附件
using System; using System.Collections.Generic; using System.Text; using Microsoft.SharePoint; using ...
- c# 开发可替换的通用序列化库
开篇继续吹牛.... 其实没有什么可吹的了,哈哈哈哈哈 主要是写一个通用库,既可以直接用,又方便替换,我的序列化都是采用第三方的哈. 我不上完整代码了,只是给大家讲讲过程. 1.写一个序列化的类,我是 ...
- 【TOJ 5103】Electric Fence(皮克定理)
描述 In this problem, `lattice points' in the plane are points with integer coordinates. In order to c ...
- This system is registered to Red Hat Subscription Management, but is not receiving updates. You can use subscription-manager to assign subscriptions.
Wrong date and time, reset the date and time in the system properly. It may also happen that system ...
- 利用nginx使ftp可以通过http访问
./nginx 启动服务./nginx -s stop 关闭服务./nginx -s reload 重新加载配置文件 搭建nginx映射ftp服务:打开nginx的配置文件nginx.conf(位于n ...
- 浅谈Linux下mv和cp命令的区别
1.功能上的区别 mv:用户可以使用该命令为文件或目录重命名或将文件由一个目录移入另一个目录中. cp: 该命令的功能是将给出的文件或目录拷贝到另一文件或目录中. 2.从inode角度来区分 mv:会 ...
- Win7下如何安装python pygame的whl包
看了小甲鱼的python教程,对那个python版本的打飞机游戏很感兴趣,尝试运行,居然报错了,提示缺少pygame包: 仔细一看需要安装一个pygame的包,默认安装好python是不包括这个包的, ...