周末在家无聊 看新闻 看到关于监控的东西 拿来玩玩 试一下 感觉还蛮有意思 特此记录一下

这里只介绍客户端的配置

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 普罗米修斯监控的更多相关文章

  1. Prometheus监控学习笔记之Prometheus普罗米修斯监控入门

    0x00 概述 视频讲解通过链接网易云课堂·IT技术快速入门学院进入,更多关于Prometheus的文章. Prometheus是最近几年开始流行的一个新兴监控告警工具,特别是kubernetes的流 ...

  2. Prometheus普罗米修斯快速入门

    欢迎来到普罗米修斯! Prometheus是一个监控平台,通过从监控目标的抓取HTTP端点上获取指标. 本指南将展示如何使用和安装Promethues,配置和监视第一个资源.还将下载并安装导出器Exp ...

  3. 监控神器-普罗米修斯Prometheus的安装

    搬砖党的福音:普罗米修斯-监控神器 功能: 在业务层用作埋点系统 Prometheus支持多种语言(Go,java,python,ruby官方提供客户端,其他语言有第三方开源客户端).我们可以通过客户 ...

  4. 普罗米修斯+grafana监控k8s

    其实现原理有点类似ELK.node-exporter组件负责收集节点上的metrics监控数据,并将数据推送给prometheus, prometheus负责存储这些数据,grafana将这些数据通过 ...

  5. 当ABAP遇见普罗米修斯

    Jerry每次在工作场合中同Prometheus(普罗米修斯)打交道时,都会"出戏",因为这个单词给我的第一印象,并不是用go语言实现的微服务监控利器,而是名导雷德利·斯科特(Ri ...

  6. 一步步教你用Prometheus搭建实时监控系统系列(一)——上帝之火,普罗米修斯的崛起

    上帝之火 本系列讲述的是开源实时监控告警解决方案Prometheus,这个单词很牛逼.每次我都能联想到带来上帝之火的希腊之神,普罗米修斯.而这个开源的logo也是火,个人挺喜欢这个logo的设计. 本 ...

  7. 普罗米修斯!Ubuntu下prometheus监控软件安装使用

    *Prometheus* 是一个开源的服务监控系统和时间序列数据库 官方网站:prometheus.io 一.安装prometheus cd /usr/local/        #进入安装目录 wg ...

  8. 普罗米修斯Prometheus监控安装

    普罗米修斯Prometheus监控安装 架构: 服务端:192.168.0.204 客户端:192.168.0.206 环境准备:所有节点安装go 语言环境 rz go1.12.linux-amd64 ...

  9. 在Grafana使用普罗米修斯

    aaarticlea/svg+xml;base64,PD94bWwgdmVyc2lvbj0iMS4wIiBlbmNvZGluZz0idXRmLTgiPz4KPCEtLSBHZW5lcmF0b3I6IE ...

随机推荐

  1. centOS 7 更改root密码

    Linux忘记密码怎么办,不用重装系统,进入emergency mode 更改root密码即可. 首先重启系统,按下 向下 按钮, 定位在第一个,摁 e ,进行编辑 找到 ro , 把ro改为 rw ...

  2. 指纹获取 Fingerprint2

    指纹插件  Fingerprint2 import Fingerprint2 from 'fingerprintjs2' new Fingerprint2().get(function(result, ...

  3. Struts-Core jar包

    密码t6mp https://pan.baidu.com/share/init?surl=E--zExzI9-VY1zaT8F9i9w

  4. iOS | CAShapeLayer转场动画

    什么也不说了,作为一名乐于分享技术的小开发,直接先上个样式最为直观贴切,有需要的朋友可以直接拿过去用. 需要demo请点击这里 :github 在这个demo中,核心为选用画布CAShapeLayer ...

  5. Docker 入坑教程笔记

    Docker 入坑教程笔记 视频网址B站:点这里 查询命令 man docker 简单启动和退出 docker run --name [容器名] -i -t ubuntu /bin/bash 交互启动 ...

  6. python3.5怎么打包编译

    问题:用Python开发的小工具有时需要编译打包为Windows(*.exe).Mac等操作系统下的可执行性文件以供非程序员使用. 解决方案: 一.py2exe 目前只支持到Python3.4,暂不支 ...

  7. 【ISIS(中间系统到中间系统)路由链路状态信息协议初识】

    ISIS单区域的基本配置 一:根据项目需求,考虑到组网的规模和条件,部署ISIS单区域的拓扑图如下: 二:配置 1:首先对RTA进行配置,在系统视图创建ISIS进程:进入ISIS配置视图,指定IS的级 ...

  8. php-5.6.26源代码 - hash存储结构 - hash算法

    // zend_inline_hash_func 实现在文件“php-5.6.26\Zend\zend_hash.h” h = zend_inline_hash_func(arKey, nKeyLen ...

  9. js onsubmit和return false的关系

    一直以来,我都是以为onsubmit=“return false”就不会进行提交,但经过项目之后才知道return false只是避免了之后的跳转,但onsubmit已经是正在进行了,故onsubmi ...

  10. ruby 数据类型Range

    范围(Range)无处不在:a 到 z. 0 到 9.等等.Ruby 支持范围,并允许我们以不同的方式使用范围: 作为序列的范围 作为条件的范围 作为间隔的范围 作为序列的范围 (1..5) #==& ...