All Downloads are FREE. Search and download functionalities are using the official Maven repository.

manual.servlet.rst Maven / Gradle / Ivy

.. _manual-servlet:

##############################
Instrumenting Web Applications
##############################

The ``metrics-servlet`` module provides a Servlet filter which has meters for status codes, a
counter for the number of active requests, and a timer for request duration. By default the filter
will use ``com.codahale.metrics.servlet.InstrumentedFilter`` as the base name of the metrics.
You can use the filter in your ``web.xml`` like this:

.. code-block:: xml

    
        instrumentedFilter
        com.codahale.metrics.servlet.InstrumentedFilter
    
    
        instrumentedFilter
        /*
    


An optional filter init-param ``name-prefix`` can be specified to override the base name
of the metrics associated with the filter mapping. This can be helpful if you need to instrument
multiple url patterns and give each a unique name.

.. code-block:: xml

    
        instrumentedFilter
        com.codahale.metrics.servlet.InstrumentedFilter
        
            name-prefix
            authentication
        
    
    
        instrumentedFilter
        /auth/*
    

You will need to add your ``MetricRegistry`` to the servlet context as an attribute named
``com.codahale.metrics.servlet.InstrumentedFilter.registry``. You can do this using the Servlet API
by extending ``InstrumentedFilterContextListener``:

.. code-block:: java

    public class MyInstrumentedFilterContextListener extends InstrumentedFilterContextListener {
        public static final MetricRegistry REGISTRY = new MetricRegistry();

        @Override
        protected MetricRegistry getMetricRegistry() {
            return REGISTRY;
        }
    }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy