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>
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..."); } }
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的信息了
不积跬步无以至千里