Skip to content

Commit 8222f32

Browse files
authored
Merge pull request #34 from w0lf-46/master
Histogram only works if buckets param was defined in web.xml file
2 parents 0c3e1f6 + 2db91da commit 8222f32

File tree

2 files changed

+64
-61
lines changed

2 files changed

+64
-61
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ dependency_request_seconds_count{name, type, status, isError, errorMessage, meth
1717
dependency_request_seconds_sum{name, type, status, isError, errorMessage, method, add}
1818
application_info{version}
1919
```
20+
**Attention, Buckets/Histogram only work if It was defined in web.xml file**
2021

2122
Details:
22-
2323
1. The `request_seconds_bucket` metric defines the histogram of how many requests are falling into the well-defined buckets represented by the label `le`;
2424

2525
2. The `request_seconds_count` is a counter that counts the overall number of requests with those exact label occurrences;
@@ -101,10 +101,9 @@ the most accurate measurement of latency and response size. -->
101101

102102
It is possible to use the following properties to configure the Metrics Collector Filter by init parameters on the web.xml file.
103103

104-
##### Override default buckets
104+
##### Configure buckets metrics
105105

106-
The number of buckets is optionally overridable and can be configured by passing a comma-separated string of doubles as the `buckets` init parameter.
107-
The `buckets` default value is `0.1, 0.3, 1.5, 10.5`.
106+
The number of buckets is only available and can be configured by passing a comma-separated string of doubles as the `buckets` init parameter.
108107

109108
e.g.
110109
```xml

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

+61-57
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* request_seconds_count{type, status, method, addr, isError}
2222
* request_seconds_sum{type, status, method, addr, isError}
2323
*
24+
* The Histogram only works if buckets param was defined in web.xml
25+
*
2426
* Counter responseSize:
2527
* response_size_bytes{type, status, method, addr, isError}
2628
*
@@ -40,10 +42,12 @@ public enum MonitorMetrics {
4042

4143
private static final String REQUESTS_SECONDS_METRIC_NAME = "request_seconds";
4244
private static final String RESPONSE_SIZE_METRIC_NAME = "response_size_bytes";
43-
private static final String DEPENDENCY_REQUESTS_SECONDS_METRIC_NAME ="dependency_request_seconds";
45+
private static final String DEPENDENCY_REQUESTS_SECONDS_METRIC_NAME = "dependency_request_seconds";
4446
private static final String DEPENDENCY_UP_METRIC_NAME = "dependency_up";
4547
private static final String APPLICATION_INFO_METRIC_NAME = "application_info";
46-
private static double[] DEFAULT_BUCKETS = {0.1D, 0.3D, 1.5D, 10.5D};
48+
49+
/* Not used anymore */
50+
private static double[] DEFAULT_BUCKETS = { 0.1D, 0.3D, 1.5D, 10.5D };
4751

4852
public CollectorRegistry collectorRegistry = new CollectorRegistry(true);
4953

@@ -55,50 +59,46 @@ public enum MonitorMetrics {
5559

5660
private DependencyCheckerExecutor dependencyCheckerExecutor = new DependencyCheckerExecutor();
5761

62+
private boolean noBuckets = false;
5863
private boolean initialized;
5964

6065
/**
6166
* Initialize metric collectors
6267
*
6368
* @param collectJvmMetrics collect or not JVM metrics
6469
* @param applicationVersion which version of your app handled the request
65-
* @param buckets the numbers of buckets
70+
* @param buckets the numbers of buckets if defined
6671
*/
6772
public void init(boolean collectJvmMetrics, String applicationVersion, double... buckets) {
6873
if (initialized) {
69-
throw new IllegalStateException("The MonitorMetrics instance has already been initialized. " +
70-
"The MonitorMetrics.INSTANCE.init method must be executed only once");
74+
throw new IllegalStateException("The MonitorMetrics instance has already been initialized. "
75+
+ "The MonitorMetrics.INSTANCE.init method must be executed only once");
7176
}
7277
if (buckets == null || buckets.length == 0) {
73-
buckets = DEFAULT_BUCKETS;
78+
noBuckets = true;
7479
}
7580

76-
requestSeconds = Histogram.build().name(REQUESTS_SECONDS_METRIC_NAME)
77-
.help("records in a histogram the number of http requests and their duration in seconds")
78-
.labelNames("type", "status", "method", "addr", "isError", "errorMessage")
79-
.buckets(buckets)
80-
.register(collectorRegistry);
81+
if (!noBuckets) {
82+
requestSeconds = Histogram.build().name(REQUESTS_SECONDS_METRIC_NAME)
83+
.help("records in a histogram the number of http requests and their duration in seconds")
84+
.labelNames("type", "status", "method", "addr", "isError", "errorMessage").buckets(buckets)
85+
.register(collectorRegistry);
8186

82-
responseSize = Counter.build().name(RESPONSE_SIZE_METRIC_NAME)
83-
.help("counts the size of each http response")
84-
.labelNames("type", "status", "method", "addr", "isError", "errorMessage")
85-
.register(collectorRegistry);
86-
87-
dependencyRequestSeconds = Histogram.build().name(DEPENDENCY_REQUESTS_SECONDS_METRIC_NAME)
88-
.help("records in a histogram the number of requests of a dependency and their duration in seconds")
89-
.labelNames("name", "type", "status", "method", "addr", "isError", "errorMessage")
90-
.buckets(buckets)
91-
.register(collectorRegistry);
87+
dependencyRequestSeconds = Histogram.build().name(DEPENDENCY_REQUESTS_SECONDS_METRIC_NAME)
88+
.help("records in a histogram the number of requests of a dependency and their duration in seconds")
89+
.labelNames("name", "type", "status", "method", "addr", "isError", "errorMessage").buckets(buckets)
90+
.register(collectorRegistry);
91+
}
92+
93+
responseSize = Counter.build().name(RESPONSE_SIZE_METRIC_NAME).help("counts the size of each http response")
94+
.labelNames("type", "status", "method", "addr", "isError", "errorMessage").register(collectorRegistry);
9295

9396
dependencyUp = Gauge.build().name(DEPENDENCY_UP_METRIC_NAME)
94-
.help("records if a dependency is up or down. 1 for up, 0 for down")
95-
.labelNames("name")
97+
.help("records if a dependency is up or down. 1 for up, 0 for down").labelNames("name")
9698
.register(collectorRegistry);
9799

98-
applicationInfo = Gauge.build().name(APPLICATION_INFO_METRIC_NAME)
99-
.help("static info of the application")
100-
.labelNames("version")
101-
.register(collectorRegistry);
100+
applicationInfo = Gauge.build().name(APPLICATION_INFO_METRIC_NAME).help("static info of the application")
101+
.labelNames("version").register(collectorRegistry);
102102
// register the application version on application_info metric
103103
applicationInfo.labels(applicationVersion).set(1);
104104

@@ -120,8 +120,9 @@ public void init(boolean collectJvmMetrics, String applicationVersion, double...
120120
* @param errorMessage the error message from a request with error
121121
* @param elapsedSeconds how long time did the request has executed
122122
*/
123-
public void collectTime(String type, String status, String method, String addr, boolean isError, String errorMessage, double elapsedSeconds) {
124-
if (initialized) {
123+
public void collectTime(String type, String status, String method, String addr, boolean isError,
124+
String errorMessage, double elapsedSeconds) {
125+
if (initialized && !noBuckets) {
125126
requestSeconds.labels(type, status, method, addr, Boolean.toString(isError), errorMessage)
126127
.observe(elapsedSeconds);
127128
}
@@ -130,38 +131,40 @@ public void collectTime(String type, String status, String method, String addr,
130131
/**
131132
* Collect size metric response_size_bytes
132133
*
133-
* @param type which request protocol was used (e.g. grpc or http)
134-
* @param status the response status(e.g. response HTTP status code)
135-
* @param method the request method(e.g. HTTP methods GET, POST, PUT)
136-
* @param addr the requested endpoint address
137-
* @param isError if the status code reported is an error or not
138-
* @param errorMessage the error message from a request with error
139-
* @param size the response content size
134+
* @param type which request protocol was used (e.g. grpc or http)
135+
* @param status the response status(e.g. response HTTP status code)
136+
* @param method the request method(e.g. HTTP methods GET, POST, PUT)
137+
* @param addr the requested endpoint address
138+
* @param isError if the status code reported is an error or not
139+
* @param errorMessage the error message from a request with error
140+
* @param size the response content size
140141
*/
141-
public void collectSize(String type, String status, String method, String addr, boolean isError, String errorMessage, final long size) {
142+
public void collectSize(String type, String status, String method, String addr, boolean isError,
143+
String errorMessage, final long size) {
142144
if (initialized) {
143-
MonitorMetrics.INSTANCE.responseSize.labels(type, status, method, addr, Boolean.toString(isError), errorMessage)
144-
.inc(size);
145+
MonitorMetrics.INSTANCE.responseSize
146+
.labels(type, status, method, addr, Boolean.toString(isError), errorMessage).inc(size);
145147
}
146148
}
147-
149+
148150
/**
149151
* Collect latency metric dependency_request_seconds
150152
*
151-
* @param name the name of the dependency
153+
* @param name the name of the dependency
152154
* @param type which request protocol was used (e.g. grpc or http)
153155
* @param status the response status(e.g. response HTTP status code)
154156
* @param method the request method(e.g. HTTP methods GET, POST, PUT)
155157
* @param addr the requested endpoint address
156158
* @param isError if the status code reported is an error or not
157-
* @param errorMessage the error message of a request
159+
* @param errorMessage the error message of a request
158160
* @param elapsedSeconds how long time did the request has executed
159161
*/
160-
public void collectDependencyTime(String name, String type, String status, String method, String addr, boolean isError, String errorMessage, double elapsedSeconds) {
161-
if(initialized) {
162-
dependencyRequestSeconds.labels(name, type, status, method, addr, Boolean.toString(isError), errorMessage)
163-
.observe(elapsedSeconds);
164-
}
162+
public void collectDependencyTime(String name, String type, String status, String method, String addr,
163+
boolean isError, String errorMessage, double elapsedSeconds) {
164+
if (initialized && !noBuckets) {
165+
dependencyRequestSeconds.labels(name, type, status, method, addr, Boolean.toString(isError), errorMessage)
166+
.observe(elapsedSeconds);
167+
}
165168
}
166169

167170
/**
@@ -181,8 +184,8 @@ public void addDependencyChecker(final DependencyChecker checker, final long per
181184
TimerTask task = new TimerTask() {
182185
public void run() {
183186
if (!initialized) {
184-
//skipping, the MonitorMetrics instance has not been initialized yet.
185-
//MonitorMetrics.INSTANCE.init must be executed once.
187+
// skipping, the MonitorMetrics instance has not been initialized yet.
188+
// MonitorMetrics.INSTANCE.init must be executed once.
186189
return;
187190
}
188191
DependencyState state = checker.run();
@@ -195,20 +198,21 @@ public void run() {
195198
/**
196199
* Add a dependency event to be monitored
197200
*
198-
* @param name the name of the dependency
201+
* @param name the name of the dependency
199202
* @param type which request protocol was used (e.g. http, grpc, etc)
200203
* @param status the response status(e.g. response HTTP status code)
201204
* @param method the request method(e.g. HTTP methods GET, POST, PUT)
202205
* @param addr the requested endpoint address
203206
* @param isError if the status code reported is an error or not
204-
* @param errorMessage the error message of a request
207+
* @param errorMessage the error message of a request
205208
* @param elapsedSeconds how long the request lasted in seconds
206209
*/
207-
public void addDependencyEvent(String name, String type, String status, String method, String addr, boolean isError, String errorMessage, double elapsedSeconds) {
208-
if (elapsedSeconds <= 0) {
209-
//skipping, elapsed seconds cannot be minor than zero
210-
return;
211-
}
212-
collectDependencyTime(name, type, status, method, addr, isError, errorMessage, elapsedSeconds);
210+
public void addDependencyEvent(String name, String type, String status, String method, String addr, boolean isError,
211+
String errorMessage, double elapsedSeconds) {
212+
if (elapsedSeconds <= 0) {
213+
// skipping, elapsed seconds cannot be minor than zero
214+
return;
215+
}
216+
collectDependencyTime(name, type, status, method, addr, isError, errorMessage, elapsedSeconds);
213217
}
214218
}

0 commit comments

Comments
 (0)