2525 * (including servlet context path), but can be configured with the {@code path-components} init parameter. Any number
2626 * provided that is less than 1 will provide the full path granularity (warning, this may affect performance).
2727 *
28+ * <p>The {@code strip-context-path} init parameter can be used to avoid including the leading path components which are
29+ * part of the context (i.e. the folder where the servlet is deployed) so that the same project deployed under different
30+ * paths can produce the same metrics.
31+ *
2832 * <p>The Histogram buckets can be configured with a {@code buckets} init parameter whose value is a comma-separated list
2933 * of valid {@code double} values.
3034 *
5054 * <param-name>path-components</param-name>
5155 * <param-value>0</param-value>
5256 * </init-param>
57+ * <!-- strip-context-path is optional, defaults to false -->
58+ * <init-param>
59+ * <param-name>strip-context-path</param-name>
60+ * <param-value>false</param-value>
61+ * </init-param>
5362 * </filter>
5463 * }</pre>
5564 *
@@ -60,6 +69,7 @@ public class MetricsFilter implements Filter {
6069 static final String HELP_PARAM = "help" ;
6170 static final String METRIC_NAME_PARAM = "metric-name" ;
6271 static final String BUCKET_CONFIG_PARAM = "buckets" ;
72+ static final String STRIP_CONTEXT_PATH_PARAM = "strip-context-path" ;
6373 static final String UNKNOWN_HTTP_STATUS_CODE = "" ;
6474
6575 private Histogram histogram = null ;
@@ -68,6 +78,7 @@ public class MetricsFilter implements Filter {
6878 // Package-level for testing purposes.
6979 int pathComponents = 1 ;
7080 private String metricName = null ;
81+ boolean stripContextPath = false ;
7182 private String help = "The time taken fulfilling servlet requests" ;
7283 private double [] buckets = null ;
7384
@@ -145,6 +156,10 @@ public void init(FilterConfig filterConfig) throws ServletException {
145156 buckets [i ] = Double .parseDouble (bucketParams [i ]);
146157 }
147158 }
159+
160+ if (!isEmpty (filterConfig .getInitParameter (STRIP_CONTEXT_PATH_PARAM ))) {
161+ stripContextPath = Boolean .parseBoolean (filterConfig .getInitParameter (STRIP_CONTEXT_PATH_PARAM ));
162+ }
148163 }
149164
150165 if (buckets != null ) {
@@ -171,6 +186,9 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
171186 HttpServletRequest request = (HttpServletRequest ) servletRequest ;
172187
173188 String path = request .getRequestURI ();
189+ if (stripContextPath ) {
190+ path = path .substring (request .getContextPath ().length ());
191+ }
174192
175193 String components = getComponents (path );
176194 String method = request .getMethod ();
0 commit comments