25
25
* (including servlet context path), but can be configured with the {@code path-components} init parameter. Any number
26
26
* provided that is less than 1 will provide the full path granularity (warning, this may affect performance).
27
27
*
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
+ *
28
32
* <p>The Histogram buckets can be configured with a {@code buckets} init parameter whose value is a comma-separated list
29
33
* of valid {@code double} values.
30
34
*
50
54
* <param-name>path-components</param-name>
51
55
* <param-value>0</param-value>
52
56
* </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>
53
62
* </filter>
54
63
* }</pre>
55
64
*
@@ -60,6 +69,7 @@ public class MetricsFilter implements Filter {
60
69
static final String HELP_PARAM = "help" ;
61
70
static final String METRIC_NAME_PARAM = "metric-name" ;
62
71
static final String BUCKET_CONFIG_PARAM = "buckets" ;
72
+ static final String STRIP_CONTEXT_PATH_PARAM = "strip-context-path" ;
63
73
static final String UNKNOWN_HTTP_STATUS_CODE = "" ;
64
74
65
75
private Histogram histogram = null ;
@@ -68,6 +78,7 @@ public class MetricsFilter implements Filter {
68
78
// Package-level for testing purposes.
69
79
int pathComponents = 1 ;
70
80
private String metricName = null ;
81
+ boolean stripContextPath = false ;
71
82
private String help = "The time taken fulfilling servlet requests" ;
72
83
private double [] buckets = null ;
73
84
@@ -145,6 +156,10 @@ public void init(FilterConfig filterConfig) throws ServletException {
145
156
buckets [i ] = Double .parseDouble (bucketParams [i ]);
146
157
}
147
158
}
159
+
160
+ if (!isEmpty (filterConfig .getInitParameter (STRIP_CONTEXT_PATH_PARAM ))) {
161
+ stripContextPath = Boolean .parseBoolean (filterConfig .getInitParameter (STRIP_CONTEXT_PATH_PARAM ));
162
+ }
148
163
}
149
164
150
165
if (buckets != null ) {
@@ -171,6 +186,9 @@ public void doFilter(ServletRequest servletRequest, ServletResponse servletRespo
171
186
HttpServletRequest request = (HttpServletRequest ) servletRequest ;
172
187
173
188
String path = request .getRequestURI ();
189
+ if (stripContextPath ) {
190
+ path = path .substring (request .getContextPath ().length ());
191
+ }
174
192
175
193
String components = getComponents (path );
176
194
String method = request .getMethod ();
0 commit comments