Class MetricsFilter

  • All Implemented Interfaces:
    jakarta.servlet.Filter

    public class MetricsFilter
    extends java.lang.Object
    implements jakarta.servlet.Filter
    The MetricsFilter class provides a high-level filter that enables tunable collection of metrics for Servlet performance. This is the Jakarta version of the MetricsFilter. If you are using Javax Servlet, there is a Javax version available in simpleclient-servlet.

    The metric name itself is required, and configured with a metric-name init parameter.

    The help parameter, configured with the help init parameter, is not required but strongly recommended.

    The Histogram buckets can be configured with a buckets init parameter whose value is a comma-separated list * of valid double values. If omitted, the default buckets from Histogram are used.

    By default, this filter will provide metrics that distinguish only 1 level deep for the request path (including servlet context path), but can be configured with the path-components init parameter. Any number provided that is less than 1 will provide the full path granularity (warning, this may affect performance).

    The strip-context-path init parameter can be used to avoid including the leading path components which are part of the context (i.e. the folder where the servlet is deployed) so that the same project deployed under different paths can produce the same metrics.

    HTTP statuses will be aggregated via Counter. The name for this counter will be derived from the metric-name init parameter.

    
     <filter>
       <filter-name>prometheusFilter</filter-name>
       <!-- This example shows the javax version. For Jakarta you would use -->
       <!-- <filter-class>io.prometheus.client.filter.servlet.jakarta.MetricsFilter</filter-class> -->
       <filter-class>io.prometheus.client.filter.MetricsFilter</filter-class>
       <init-param>
         <param-name>metric-name</param-name>
         <param-value>webapp_metrics_filter</param-value>
       </init-param>
       <!-- help is optional, defaults to the message below -->
       <init-param>
         <param-name>help</param-name>
         <param-value>This is the help for your metrics filter</param-value>
       </init-param>
       <!-- buckets is optional, unless specified the default buckets from io.prometheus.client.Histogram are used -->
       <init-param>
         <param-name>buckets</param-name>
         <param-value>0.005,0.01,0.025,0.05,0.075,0.1,0.25,0.5,0.75,1,2.5,5,7.5,10</param-value>
       </init-param>
       <!-- path-components is optional, anything less than 1 (1 is the default) means full granularity -->
       <init-param>
         <param-name>path-components</param-name>
         <param-value>1</param-value>
       </init-param>
       <!-- strip-context-path is optional, defaults to false -->
       <init-param>
         <param-name>strip-context-path</param-name>
         <param-value>false</param-value>
       </init-param>
     </filter>
    
     <!-- You will most likely want this to be the first filter in the chain
     (therefore the first <filter-mapping> in the web.xml file), so that you can get
     the most accurate measurement of latency. -->
     <filter-mapping>
       <filter-name>prometheusFilter</filter-name>
       <url-pattern>/*</url-pattern>
     </filter-mapping>
     
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private Filter delegate  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void destroy()  
      void doFilter​(jakarta.servlet.ServletRequest servletRequest, jakarta.servlet.ServletResponse servletResponse, jakarta.servlet.FilterChain filterChain)  
      void init​(jakarta.servlet.FilterConfig filterConfig)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • delegate

        private final Filter delegate
    • Constructor Detail

      • MetricsFilter

        public MetricsFilter()
    • Method Detail

      • init

        public void init​(jakarta.servlet.FilterConfig filterConfig)
                  throws jakarta.servlet.ServletException
        Specified by:
        init in interface jakarta.servlet.Filter
        Throws:
        jakarta.servlet.ServletException
      • doFilter

        public void doFilter​(jakarta.servlet.ServletRequest servletRequest,
                             jakarta.servlet.ServletResponse servletResponse,
                             jakarta.servlet.FilterChain filterChain)
                      throws java.io.IOException,
                             jakarta.servlet.ServletException
        Specified by:
        doFilter in interface jakarta.servlet.Filter
        Throws:
        java.io.IOException
        jakarta.servlet.ServletException
      • destroy

        public void destroy()
        Specified by:
        destroy in interface jakarta.servlet.Filter