普罗米修斯java_Prometheus 普罗米修斯监控

本文介绍了如何在Java应用中集成Prometheus监控,通过WebInterceptor类实现请求延迟的度量,利用Histogram记录并报告请求处理的延迟时间,同时提供labels进行细粒度分类。
摘要由CSDN通过智能技术生成

public class WebInterceptor extendsHandlerInterceptorAdapter {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 ThreadLocaltimerThreadLocal;

@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throwsException {return super.preHandle(request, response, handler);

}

@Overridepublic void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throwsException {

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);

}

@Overridepublic void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throwsException {super.afterCompletion(request, response, handler, ex);if (timerThreadLocal.get() != null) {

timerThreadLocal.get().observeDuration();

}

}

@Overridepublic void afterConcurrentHandlingStarted(HttpServletRequest request, HttpServletResponse response, Object handler) throwsException {super.afterConcurrentHandlingStarted(request, response, handler);

}privateString getName(HttpServletRequest request, Object handler) {

String name= "";try{if (handler != null

&& handler instanceofHandlerMethod) {

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{returnname;

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值