Skip to content

Commit 03ca18f

Browse files
KarineValencaeabili0
authored andcommitted
added errorMessage in responseSize and requestTime metrics
1 parent 9e0b158 commit 03ca18f

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

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

+9-6
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ public void init(boolean collectJvmMetrics, String applicationVersion, double...
7575

7676
requestSeconds = Histogram.build().name(REQUESTS_SECONDS_METRIC_NAME)
7777
.help("records in a histogram the number of http requests and their duration in seconds")
78-
.labelNames("type", "status", "method", "addr", "isError")
78+
.labelNames("type", "status", "method", "addr", "isError", "errorMessage")
7979
.buckets(buckets)
8080
.register(collectorRegistry);
8181

8282
responseSize = Counter.build().name(RESPONSE_SIZE_METRIC_NAME)
8383
.help("counts the size of each http response")
84-
.labelNames("type", "status", "method", "addr", "isError")
84+
.labelNames("type", "status", "method", "addr", "isError", "errorMessage")
8585
.register(collectorRegistry);
8686

8787
dependencyRequestSeconds = Histogram.build().name(DEPENDENCY_REQUESTS_SECONDS_METRIC_NAME)
@@ -117,11 +117,12 @@ public void init(boolean collectJvmMetrics, String applicationVersion, double...
117117
* @param method the request method(e.g. HTTP methods GET, POST, PUT)
118118
* @param addr the requested endpoint address
119119
* @param isError if the status code reported is an error or not
120+
* @param errorMessage the error message from a request with error
120121
* @param elapsedSeconds how long time did the request has executed
121122
*/
122-
public void collectTime(String type, String status, String method, String addr, boolean isError, double elapsedSeconds) {
123+
public void collectTime(String type, String status, String method, String addr, boolean isError, String errorMessage, double elapsedSeconds) {
123124
if (initialized) {
124-
requestSeconds.labels(type, status, method, addr, Boolean.toString(isError))
125+
requestSeconds.labels(type, status, method, addr, Boolean.toString(isError), errorMessage)
125126
.observe(elapsedSeconds);
126127
}
127128
}
@@ -134,11 +135,13 @@ public void collectTime(String type, String status, String method, String addr,
134135
* @param method the request method(e.g. HTTP methods GET, POST, PUT)
135136
* @param addr the requested endpoint address
136137
* @param isError if the status code reported is an error or not
138+
* @param errorMessage the error message from a request with error
137139
* @param size the response content size
138140
*/
139-
public void collectSize(String type, String status, String method, String addr, boolean isError, final long size) {
141+
public void collectSize(String type, String status, String method, String addr, boolean isError, String errorMessage, final long size) {
140142
if (initialized) {
141-
MonitorMetrics.INSTANCE.responseSize.labels(type, status, method, addr, Boolean.toString(isError)).inc(size);
143+
MonitorMetrics.INSTANCE.responseSize.labels(type, status, method, addr, Boolean.toString(isError), errorMessage)
144+
.inc(size);
142145
}
143146
}
144147

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -184,14 +184,12 @@ private void collect(HttpServletRequest httpRequest, CountingServletResponse cou
184184
final String method = httpRequest.getMethod();
185185
final String status = Integer.toString(counterResponse.getStatus());
186186
final boolean isError = isErrorStatus(counterResponse.getStatus());
187-
if (isError) {
188-
System.out.println(httpRequest.getAttribute(errorMessageParam));
189-
}
187+
final String errorMessage = (String) httpRequest.getAttribute(errorMessageParam);
190188
final long count = counterResponse.getByteCount();
191189
final String scheme = httpRequest.getScheme();
192190
DebugUtil.debug(path, " ; bytes count = ", count);
193-
MonitorMetrics.INSTANCE.collectTime(scheme, status, method, path, isError, elapsedSeconds);
194-
MonitorMetrics.INSTANCE.collectSize(scheme, status, method, path, isError, count);
191+
MonitorMetrics.INSTANCE.collectTime(scheme, status, method, path, isError, errorMessage, elapsedSeconds);
192+
MonitorMetrics.INSTANCE.collectSize(scheme, status, method, path, isError, errorMessage, count);
195193
}
196194

197195
/**

0 commit comments

Comments
 (0)