@Test public void doFilter() throws Exception { HttpServletRequest req = mock(HttpServletRequest.class); final String path = "/foo/bar/baz/bang/zilch/zip/nada"; when(req.getRequestURI()).thenReturn(path); when(req.getMethod()).thenReturn(HttpMethods.GET); HttpServletResponse res = mock(HttpServletResponse.class); FilterChain c = mock(FilterChain.class); String name = "foo"; FilterConfig cfg = mock(FilterConfig.class); when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn(name); when(cfg.getInitParameter(MetricsFilter.PATH_COMPONENT_PARAM)).thenReturn("0"); f.init(cfg); f.doFilter(req, res, c); verify(c).doFilter(req, res); final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(name + "_count", new String[]{"path", "method"}, new String[]{path, HttpMethods.GET}); assertNotNull(sampleValue); assertEquals(1, sampleValue, 0.0001); }
@Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (!(servletRequest instanceof HttpServletRequest)) { filterChain.doFilter(servletRequest, servletResponse); return; } HttpServletRequest request = (HttpServletRequest) servletRequest; String path = request.getRequestURI(); String components = getComponents(path); String method = request.getMethod(); Histogram.Timer timer = histogram .labels(components, method) .startTimer(); try { filterChain.doFilter(servletRequest, servletResponse); } finally { timer.observeDuration(); statusCounter.labels(components, method, getStatusCode(servletResponse)).inc(); } }
MetricsFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (!(servletRequest instanceof HttpServletRequest)) { filterChain.doFilter(servletRequest, servletResponse); return; } HttpServletRequest request = (HttpServletRequest) servletRequest; String path = request.getRequestURI(); String components = getComponents(path); String method = request.getMethod(); Histogram.Timer timer = histogram .labels(components, method) .startTimer(); try { filterChain.doFilter(servletRequest, servletResponse); } finally { timer.observeDuration(); statusCounter.labels(components, method, getStatusCode(servletResponse)).inc(); } } }
MetricsFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (!(servletRequest instanceof HttpServletRequest)) { filterChain.doFilter(servletRequest, servletResponse); return; } HttpServletRequest request = (HttpServletRequest) servletRequest; String path = request.getRequestURI(); String components = getComponents(path); String method = request.getMethod(); Histogram.Timer timer = histogram .labels(components, method) .startTimer(); try { filterChain.doFilter(servletRequest, servletResponse); } finally { timer.observeDuration(); statusCounter.labels(components, method, getStatusCode(servletResponse)).inc(); } } MetricsFilter();  MetricsFilter(
            String metricName,
            String help,
            Integer pathComponents,
            double[] buckets); }
MetricsFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (!(servletRequest instanceof HttpServletRequest)) { filterChain.doFilter(servletRequest, servletResponse); return; } HttpServletRequest request = (HttpServletRequest) servletRequest; String path = request.getRequestURI(); String components = getComponents(path); String method = request.getMethod(); Histogram.Timer timer = histogram .labels(components, method) .startTimer(); try { filterChain.doFilter(servletRequest, servletResponse); } finally { timer.observeDuration(); statusCounter.labels(components, method, getStatusCode(servletResponse)).inc(); } } MetricsFilter();  MetricsFilter(
            String metricName,
            String help,
            Integer pathComponents,
            double[] buckets); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain); @Override void destroy(); }
MetricsFilter implements Filter { @Override public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { if (!(servletRequest instanceof HttpServletRequest)) { filterChain.doFilter(servletRequest, servletResponse); return; } HttpServletRequest request = (HttpServletRequest) servletRequest; String path = request.getRequestURI(); String components = getComponents(path); String method = request.getMethod(); Histogram.Timer timer = histogram .labels(components, method) .startTimer(); try { filterChain.doFilter(servletRequest, servletResponse); } finally { timer.observeDuration(); statusCounter.labels(components, method, getStatusCode(servletResponse)).inc(); } } MetricsFilter();  MetricsFilter(
            String metricName,
            String help,
            Integer pathComponents,
            double[] buckets); @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain); @Override void destroy();  }