@Test public void init() throws Exception { FilterConfig cfg = mock(FilterConfig.class); when(cfg.getInitParameter(anyString())).thenReturn(null); String metricName = "foo"; when(cfg.getInitParameter(MetricsFilter.METRIC_NAME_PARAM)).thenReturn(metricName); when(cfg.getInitParameter(MetricsFilter.PATH_COMPONENT_PARAM)).thenReturn("4"); f.init(cfg); assertEquals(f.pathComponents, 4); HttpServletRequest req = mock(HttpServletRequest.class); when(req.getRequestURI()).thenReturn("/foo/bar/baz/bang/zilch/zip/nada"); when(req.getMethod()).thenReturn(HttpMethods.GET); HttpServletResponse res = mock(HttpServletResponse.class); FilterChain c = mock(FilterChain.class); f.doFilter(req, res, c); verify(c).doFilter(req, res); final Double sampleValue = CollectorRegistry.defaultRegistry.getSampleValue(metricName + "_count", new String[]{"path", "method"}, new String[]{"/foo/bar/baz/bang", HttpMethods.GET}); assertNotNull(sampleValue); assertEquals(1, sampleValue, 0.0001); }
@Override public void init(FilterConfig filterConfig) throws ServletException { Histogram.Builder builder = Histogram.build() .labelNames("path", "method"); if (filterConfig == null && isEmpty(metricName)) { throw new ServletException("No configuration object provided, and no metricName passed via constructor"); } if (filterConfig != null) { if (isEmpty(metricName)) { metricName = filterConfig.getInitParameter(METRIC_NAME_PARAM); if (isEmpty(metricName)) { throw new ServletException("Init parameter \"" + METRIC_NAME_PARAM + "\" is required; please supply a value"); } } if (!isEmpty(filterConfig.getInitParameter(HELP_PARAM))) { help = filterConfig.getInitParameter(HELP_PARAM); } if (!isEmpty(filterConfig.getInitParameter(PATH_COMPONENT_PARAM))) { pathComponents = Integer.valueOf(filterConfig.getInitParameter(PATH_COMPONENT_PARAM)); } if (!isEmpty(filterConfig.getInitParameter(BUCKET_CONFIG_PARAM))) { String[] bucketParams = filterConfig.getInitParameter(BUCKET_CONFIG_PARAM).split(","); buckets = new double[bucketParams.length]; for (int i = 0; i < bucketParams.length; i++) { buckets[i] = Double.parseDouble(bucketParams[i]); } } } if (buckets != null) { builder = builder.buckets(buckets); } histogram = builder .help(help) .name(metricName) .register(); statusCounter = Counter.build(metricName + "_status_total", "HTTP status codes of " + help) .labelNames("path", "method", "status") .register(); }
MetricsFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Histogram.Builder builder = Histogram.build() .labelNames("path", "method"); if (filterConfig == null && isEmpty(metricName)) { throw new ServletException("No configuration object provided, and no metricName passed via constructor"); } if (filterConfig != null) { if (isEmpty(metricName)) { metricName = filterConfig.getInitParameter(METRIC_NAME_PARAM); if (isEmpty(metricName)) { throw new ServletException("Init parameter \"" + METRIC_NAME_PARAM + "\" is required; please supply a value"); } } if (!isEmpty(filterConfig.getInitParameter(HELP_PARAM))) { help = filterConfig.getInitParameter(HELP_PARAM); } if (!isEmpty(filterConfig.getInitParameter(PATH_COMPONENT_PARAM))) { pathComponents = Integer.valueOf(filterConfig.getInitParameter(PATH_COMPONENT_PARAM)); } if (!isEmpty(filterConfig.getInitParameter(BUCKET_CONFIG_PARAM))) { String[] bucketParams = filterConfig.getInitParameter(BUCKET_CONFIG_PARAM).split(","); buckets = new double[bucketParams.length]; for (int i = 0; i < bucketParams.length; i++) { buckets[i] = Double.parseDouble(bucketParams[i]); } } } if (buckets != null) { builder = builder.buckets(buckets); } histogram = builder .help(help) .name(metricName) .register(); statusCounter = Counter.build(metricName + "_status_total", "HTTP status codes of " + help) .labelNames("path", "method", "status") .register(); } }
MetricsFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Histogram.Builder builder = Histogram.build() .labelNames("path", "method"); if (filterConfig == null && isEmpty(metricName)) { throw new ServletException("No configuration object provided, and no metricName passed via constructor"); } if (filterConfig != null) { if (isEmpty(metricName)) { metricName = filterConfig.getInitParameter(METRIC_NAME_PARAM); if (isEmpty(metricName)) { throw new ServletException("Init parameter \"" + METRIC_NAME_PARAM + "\" is required; please supply a value"); } } if (!isEmpty(filterConfig.getInitParameter(HELP_PARAM))) { help = filterConfig.getInitParameter(HELP_PARAM); } if (!isEmpty(filterConfig.getInitParameter(PATH_COMPONENT_PARAM))) { pathComponents = Integer.valueOf(filterConfig.getInitParameter(PATH_COMPONENT_PARAM)); } if (!isEmpty(filterConfig.getInitParameter(BUCKET_CONFIG_PARAM))) { String[] bucketParams = filterConfig.getInitParameter(BUCKET_CONFIG_PARAM).split(","); buckets = new double[bucketParams.length]; for (int i = 0; i < bucketParams.length; i++) { buckets[i] = Double.parseDouble(bucketParams[i]); } } } if (buckets != null) { builder = builder.buckets(buckets); } histogram = builder .help(help) .name(metricName) .register(); statusCounter = Counter.build(metricName + "_status_total", "HTTP status codes of " + help) .labelNames("path", "method", "status") .register(); } MetricsFilter();  MetricsFilter(
            String metricName,
            String help,
            Integer pathComponents,
            double[] buckets); }
MetricsFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { Histogram.Builder builder = Histogram.build() .labelNames("path", "method"); if (filterConfig == null && isEmpty(metricName)) { throw new ServletException("No configuration object provided, and no metricName passed via constructor"); } if (filterConfig != null) { if (isEmpty(metricName)) { metricName = filterConfig.getInitParameter(METRIC_NAME_PARAM); if (isEmpty(metricName)) { throw new ServletException("Init parameter \"" + METRIC_NAME_PARAM + "\" is required; please supply a value"); } } if (!isEmpty(filterConfig.getInitParameter(HELP_PARAM))) { help = filterConfig.getInitParameter(HELP_PARAM); } if (!isEmpty(filterConfig.getInitParameter(PATH_COMPONENT_PARAM))) { pathComponents = Integer.valueOf(filterConfig.getInitParameter(PATH_COMPONENT_PARAM)); } if (!isEmpty(filterConfig.getInitParameter(BUCKET_CONFIG_PARAM))) { String[] bucketParams = filterConfig.getInitParameter(BUCKET_CONFIG_PARAM).split(","); buckets = new double[bucketParams.length]; for (int i = 0; i < bucketParams.length; i++) { buckets[i] = Double.parseDouble(bucketParams[i]); } } } if (buckets != null) { builder = builder.buckets(buckets); } histogram = builder .help(help) .name(metricName) .register(); statusCounter = Counter.build(metricName + "_status_total", "HTTP status codes of " + help) .labelNames("path", "method", "status") .register(); } 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 init(FilterConfig filterConfig) throws ServletException { Histogram.Builder builder = Histogram.build() .labelNames("path", "method"); if (filterConfig == null && isEmpty(metricName)) { throw new ServletException("No configuration object provided, and no metricName passed via constructor"); } if (filterConfig != null) { if (isEmpty(metricName)) { metricName = filterConfig.getInitParameter(METRIC_NAME_PARAM); if (isEmpty(metricName)) { throw new ServletException("Init parameter \"" + METRIC_NAME_PARAM + "\" is required; please supply a value"); } } if (!isEmpty(filterConfig.getInitParameter(HELP_PARAM))) { help = filterConfig.getInitParameter(HELP_PARAM); } if (!isEmpty(filterConfig.getInitParameter(PATH_COMPONENT_PARAM))) { pathComponents = Integer.valueOf(filterConfig.getInitParameter(PATH_COMPONENT_PARAM)); } if (!isEmpty(filterConfig.getInitParameter(BUCKET_CONFIG_PARAM))) { String[] bucketParams = filterConfig.getInitParameter(BUCKET_CONFIG_PARAM).split(","); buckets = new double[bucketParams.length]; for (int i = 0; i < bucketParams.length; i++) { buckets[i] = Double.parseDouble(bucketParams[i]); } } } if (buckets != null) { builder = builder.buckets(buckets); } histogram = builder .help(help) .name(metricName) .register(); statusCounter = Counter.build(metricName + "_status_total", "HTTP status codes of " + help) .labelNames("path", "method", "status") .register(); } 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();  }