Skip to content

Commit 8f9c15d

Browse files
Merge pull request #35 from labbsr0x/dependency_regex
Dependency error regex
2 parents 8222f32 + 0a05b2d commit 8f9c15d

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

src/main/java/br/com/labbs/monitor/MonitorMetrics.java

+19-3
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,17 @@ public enum MonitorMetrics {
6262
private boolean noBuckets = false;
6363
private boolean initialized;
6464

65+
private String dependencyErrorRegex;
66+
6567
/**
6668
* Initialize metric collectors
6769
*
6870
* @param collectJvmMetrics collect or not JVM metrics
6971
* @param applicationVersion which version of your app handled the request
72+
* @param dependencyErrorRegex
7073
* @param buckets the numbers of buckets if defined
7174
*/
72-
public void init(boolean collectJvmMetrics, String applicationVersion, double... buckets) {
75+
public void init(boolean collectJvmMetrics, String applicationVersion, String dependencyErrorRegex, double... buckets) {
7376
if (initialized) {
7477
throw new IllegalStateException("The MonitorMetrics instance has already been initialized. "
7578
+ "The MonitorMetrics.INSTANCE.init method must be executed only once");
@@ -106,6 +109,7 @@ public void init(boolean collectJvmMetrics, String applicationVersion, double...
106109
DefaultExports.register(collectorRegistry);
107110
}
108111

112+
this.dependencyErrorRegex = dependencyErrorRegex;
109113
initialized = true;
110114
}
111115

@@ -162,12 +166,24 @@ public void collectSize(String type, String status, String method, String addr,
162166
public void collectDependencyTime(String name, String type, String status, String method, String addr,
163167
boolean isError, String errorMessage, double elapsedSeconds) {
164168
if (initialized && !noBuckets) {
165-
dependencyRequestSeconds.labels(name, type, status, method, addr, Boolean.toString(isError), errorMessage)
169+
String errorToWrite = errorMessage;
170+
if (dependencyErrorRegex != null) {
171+
errorToWrite = sanitizeError(errorMessage);
172+
}
173+
dependencyRequestSeconds.labels(name, type, status, method, addr, Boolean.toString(isError), errorToWrite)
166174
.observe(elapsedSeconds);
167175
}
168176
}
169177

170-
/**
178+
private String sanitizeError(String errorMessage) {
179+
try {
180+
return errorMessage.replaceAll(this.dependencyErrorRegex, "");
181+
} catch (Exception e) {
182+
return "#ERROR INVALID REGEX#";
183+
}
184+
}
185+
186+
/**
171187
* Cancel all scheduled dependency checkers and terminates the executor timer.
172188
*/
173189
public void cancelAllDependencyCheckers() {

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

+24-13
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
package br.com.labbs.monitor.filter;
22

3-
import br.com.labbs.monitor.MonitorMetrics;
4-
import io.prometheus.client.SimpleTimer;
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Properties;
8+
import java.util.logging.Logger;
59

610
import javax.servlet.Filter;
711
import javax.servlet.FilterChain;
@@ -12,15 +16,9 @@
1216
import javax.servlet.ServletResponse;
1317
import javax.servlet.http.HttpServletRequest;
1418
import javax.servlet.http.HttpServletResponse;
15-
import java.io.IOException;
16-
import java.io.InputStream;
17-
import java.util.ArrayList;
18-
import java.util.List;
19-
import java.util.Properties;
20-
import java.util.regex.Matcher;
21-
import java.util.regex.Pattern;
22-
import java.util.logging.Level;
23-
import java.util.logging.Logger;
19+
20+
import br.com.labbs.monitor.MonitorMetrics;
21+
import io.prometheus.client.SimpleTimer;
2422
/**
2523
* The MetricsFilter class provides a high-level filter that enables collection of (latency, amount and response
2624
* size metrics) for Servlet performance, based on schema, status code, HTTP method and URI path.
@@ -62,11 +60,13 @@ public class MetricsCollectorFilter implements Filter {
6260
private static final String APPLICATION_VERSION = "application-version";
6361
private static final String DEFAULT_FILTER_REGEX = "[^A-zÀ-ú .,]+";
6462
private static final String FILTER_REGEX_PARAM = "error-info-regex";
63+
private static final String ENABLE_ERROR_REGEX_DEPENDENCY = "enable-error-regex-dependency";
6564
private static final String FILTER_MAX_SIZE_PARAM = "error-info-max-size";
6665
private static final Logger LOGGER = Logger.getLogger(MetricsCollectorFilter.class.getName());
6766
private final List<String> exclusions = new ArrayList<String>();
6867
private int filter_max_size = 50;
6968
private String filter_regex = "";
69+
private boolean enableErrorDependencyRegex = true; //defaut
7070

7171

7272
private int pathDepth = 0;
@@ -118,6 +118,12 @@ public void init(FilterConfig filterConfig) {
118118
if (isNotEmpty(exportJvmMetricsStr)) {
119119
exportJvmMetrics = Boolean.parseBoolean(exportJvmMetricsStr);
120120
}
121+
122+
String enableErrorRegexDependencyParam = filterConfig.getInitParameter(ENABLE_ERROR_REGEX_DEPENDENCY);
123+
if ("false".equalsIgnoreCase(enableErrorRegexDependencyParam)) {
124+
enableErrorDependencyRegex = false;
125+
}
126+
121127
exportApplicationVersion = filterConfig.getInitParameter(APPLICATION_VERSION);
122128

123129
filter_max_size = filterConfig.getInitParameter(FILTER_MAX_SIZE_PARAM) != null ?
@@ -128,8 +134,13 @@ public void init(FilterConfig filterConfig) {
128134
String version = isNotEmpty(exportApplicationVersion) ? exportApplicationVersion : getApplicationVersionFromPropertiesFile();
129135
// Allow users to capture error messages
130136
errorMessageParam = filterConfig.getInitParameter(ERROR_MESSAGE_PARAM);
131-
132-
MonitorMetrics.INSTANCE.init(exportJvmMetrics, version, buckets);
137+
138+
String dependencyErrorRegex = null;
139+
if (enableErrorDependencyRegex) {
140+
dependencyErrorRegex = filter_regex;
141+
}
142+
143+
MonitorMetrics.INSTANCE.init(exportJvmMetrics, version, dependencyErrorRegex, buckets);
133144
}
134145

135146
/**

0 commit comments

Comments
 (0)