我正在Wildlfy Server 8.1.0.Final上部署WS,并且遇到以下问题。
我正在使用普罗米修斯作为指标,并且尝试通过ContainerResponseFilter获取请求的http响应代码。问题是我为某些请求获取了错误的状态代码。
例如,我的WS使用/ ping端点进行健康检查,它以201而不是200进行响应。当还有许多其他请求以201进行响应时,就会发生这种情况。
ContainerResponseFilter可能无法每秒处理大量请求并混淆响应者代码。
@Provider
public class CountAndLatencyMetrics implements ContainerResponseFilter, ContainerRequestFilter {
@Context
ResourceInfo resourceInfo;
@Context
SecurityContext securityContext;
private static final Counter httpRequestsTotal = Counter
.build("http_requests_total", "Total number of HTTP requests")
.labelNames("request", "user", "responseCode")
.register();
private static final Histogram httpRequestsLatency = Histogram
.build("http_requests_latency", "Total latency of HTTP requests")
.labelNames("request", "user")
.register();
private Histogram.Timer timer;
private String userName;
private String javaMethodName;
private String endpointName;
private String request;
private int responseCode;
@Override
public void filter(ContainerRequestContext requestContext, ContainerResponseContext responseContext) {
timer.observeDuration();
responseCode = responseContext.getStatus();
httpRequestsTotal.labels(request, userName, String.valueOf(responseCode)).inc();
}
@Override
public void filter(ContainerRequestContext requestContext) {
userName = securityContext.getUserPrincipal().getName();
javaMethodName = resourceInfo.getResourceMethod().getName();
endpointName = resourceInfo.getResourceMethod().getAnnotation(Path.class).value();
request = javaMethodName + " - " + endpointName;
timer = httpRequestsLatency.labels(request, userName).startTimer();
}
}