Skip to content

Commit 3865f77

Browse files
authored
Merge pull request #33 from labbsr0x/feature/CustomMessageFilter
Implements filter on ErrorMessage
2 parents a86a3fe + 3f1f98f commit 3865f77

File tree

2 files changed

+45
-9
lines changed

2 files changed

+45
-9
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ Your java code should look like this:
174174
req.setAttribute("error-info", "Page not found");
175175
```
176176

177+
###### Improving error messages
178+
179+
It is possible to filter the error message to avoid long messages or personal info exposed in the metrics. To do it, two params may be used: `error-info-regex` and `error-info-max-size`. The first will set the regex to apply in the message, with `[^A-zÀ-ú .,]+` as the default value. The second, `error-info-max-size`, defines the max size of the message to be truncated and has `50` as the default value.
180+
177181
#### Setting application version
178182

179183
##### Manually

src/main/java/br/com/labbs/monitor/filter/MetricsCollectorFilter.java

+41-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
import java.util.ArrayList;
1818
import java.util.List;
1919
import java.util.Properties;
20-
20+
import java.util.regex.Matcher;
21+
import java.util.regex.Pattern;
22+
import java.util.logging.Level;
23+
import java.util.logging.Logger;
2124
/**
2225
* The MetricsFilter class provides a high-level filter that enables collection of (latency, amount and response
2326
* size metrics) for Servlet performance, based on schema, status code, HTTP method and URI path.
@@ -57,7 +60,14 @@ public class MetricsCollectorFilter implements Filter {
5760
private static final String ERROR_MESSAGE_PARAM = "error-message";
5861
private static final String DEBUG = "debug";
5962
private static final String APPLICATION_VERSION = "application-version";
63+
private static final String DEFAULT_FILTER_REGEX = "[^A-zÀ-ú .,]+";
64+
private static final String FILTER_REGEX_PARAM = "error-info-regex";
65+
private static final String FILTER_MAX_SIZE_PARAM = "error-info-max-size";
66+
private static final Logger LOGGER = Logger.getLogger(MetricsCollectorFilter.class.getName());
6067
private final List<String> exclusions = new ArrayList<String>();
68+
private int filter_max_size = 50;
69+
private String filter_regex = "";
70+
6171

6272
private int pathDepth = 0;
6373
private String errorMessageParam = "";
@@ -109,6 +119,11 @@ public void init(FilterConfig filterConfig) {
109119
exportJvmMetrics = Boolean.parseBoolean(exportJvmMetricsStr);
110120
}
111121
exportApplicationVersion = filterConfig.getInitParameter(APPLICATION_VERSION);
122+
123+
filter_max_size = filterConfig.getInitParameter(FILTER_MAX_SIZE_PARAM) != null ?
124+
Integer.valueOf(filterConfig.getInitParameter(FILTER_MAX_SIZE_PARAM)) : filter_max_size;
125+
filter_regex = filterConfig.getInitParameter(FILTER_REGEX_PARAM) != null ?
126+
filterConfig.getInitParameter(FILTER_REGEX_PARAM) : DEFAULT_FILTER_REGEX;
112127
}
113128
String version = isNotEmpty(exportApplicationVersion) ? exportApplicationVersion : getApplicationVersionFromPropertiesFile();
114129
// Allow users to capture error messages
@@ -209,20 +224,37 @@ private boolean isErrorStatus(int status) {
209224
/**
210225
* Get the error message from a request.
211226
* If error message is null, sets the string to empty string.
227+
* If a regex is defined, use it to filter message
228+
*
229+
* Default regex: [^A-zÀ-ú .,]+
230+
* Default max size: 50
212231
*
213232
* @param httpRequest request
214233
* @return string with the error message or empty string if error message not found.
215234
*/
216235
private String getErrorMessage(HttpServletRequest httpRequest) {
217-
if (errorMessageParam == null) {
218-
return "";
219-
}
220-
String errorMessage = (String) httpRequest.getAttribute(errorMessageParam);
221-
if (errorMessage == null) {
222-
return "";
223-
}
224236

225-
return errorMessage;
237+
if (errorMessageParam == null) {
238+
return "";
239+
}
240+
String errorMessage = (String) httpRequest.getAttribute(errorMessageParam);
241+
String result = "";
242+
if (errorMessage == null) {
243+
return result;
244+
}
245+
246+
try {
247+
// apply whitelist filter
248+
result = errorMessage.replaceAll(filter_regex, "");
249+
if (result.length() > filter_max_size) {
250+
result = result.substring(0, filter_max_size);
251+
}
252+
} catch (Exception e) {
253+
// avoid invalid regex or invalid matcher group index
254+
result = "";
255+
LOGGER.warning("Invalid regex: " + e.getMessage());
256+
}
257+
return result;
226258
}
227259

228260
/**

0 commit comments

Comments
 (0)