21
21
* request_seconds_count{type, status, method, addr, isError}
22
22
* request_seconds_sum{type, status, method, addr, isError}
23
23
*
24
+ * The Histogram only works if buckets param was defined in web.xml
25
+ *
24
26
* Counter responseSize:
25
27
* response_size_bytes{type, status, method, addr, isError}
26
28
*
@@ -40,10 +42,12 @@ public enum MonitorMetrics {
40
42
41
43
private static final String REQUESTS_SECONDS_METRIC_NAME = "request_seconds" ;
42
44
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" ;
44
46
private static final String DEPENDENCY_UP_METRIC_NAME = "dependency_up" ;
45
47
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 };
47
51
48
52
public CollectorRegistry collectorRegistry = new CollectorRegistry (true );
49
53
@@ -55,50 +59,46 @@ public enum MonitorMetrics {
55
59
56
60
private DependencyCheckerExecutor dependencyCheckerExecutor = new DependencyCheckerExecutor ();
57
61
62
+ private boolean noBuckets = false ;
58
63
private boolean initialized ;
59
64
60
65
/**
61
66
* Initialize metric collectors
62
67
*
63
68
* @param collectJvmMetrics collect or not JVM metrics
64
69
* @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
66
71
*/
67
72
public void init (boolean collectJvmMetrics , String applicationVersion , double ... buckets ) {
68
73
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" );
71
76
}
72
77
if (buckets == null || buckets .length == 0 ) {
73
- buckets = DEFAULT_BUCKETS ;
78
+ noBuckets = true ;
74
79
}
75
80
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 );
81
86
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 );
92
95
93
96
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" )
96
98
.register (collectorRegistry );
97
99
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 );
102
102
// register the application version on application_info metric
103
103
applicationInfo .labels (applicationVersion ).set (1 );
104
104
@@ -120,8 +120,9 @@ public void init(boolean collectJvmMetrics, String applicationVersion, double...
120
120
* @param errorMessage the error message from a request with error
121
121
* @param elapsedSeconds how long time did the request has executed
122
122
*/
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 ) {
125
126
requestSeconds .labels (type , status , method , addr , Boolean .toString (isError ), errorMessage )
126
127
.observe (elapsedSeconds );
127
128
}
@@ -130,38 +131,40 @@ public void collectTime(String type, String status, String method, String addr,
130
131
/**
131
132
* Collect size metric response_size_bytes
132
133
*
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
140
141
*/
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 ) {
142
144
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 );
145
147
}
146
148
}
147
-
149
+
148
150
/**
149
151
* Collect latency metric dependency_request_seconds
150
152
*
151
- * @param name the name of the dependency
153
+ * @param name the name of the dependency
152
154
* @param type which request protocol was used (e.g. grpc or http)
153
155
* @param status the response status(e.g. response HTTP status code)
154
156
* @param method the request method(e.g. HTTP methods GET, POST, PUT)
155
157
* @param addr the requested endpoint address
156
158
* @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
158
160
* @param elapsedSeconds how long time did the request has executed
159
161
*/
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
+ }
165
168
}
166
169
167
170
/**
@@ -181,8 +184,8 @@ public void addDependencyChecker(final DependencyChecker checker, final long per
181
184
TimerTask task = new TimerTask () {
182
185
public void run () {
183
186
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.
186
189
return ;
187
190
}
188
191
DependencyState state = checker .run ();
@@ -195,20 +198,21 @@ public void run() {
195
198
/**
196
199
* Add a dependency event to be monitored
197
200
*
198
- * @param name the name of the dependency
201
+ * @param name the name of the dependency
199
202
* @param type which request protocol was used (e.g. http, grpc, etc)
200
203
* @param status the response status(e.g. response HTTP status code)
201
204
* @param method the request method(e.g. HTTP methods GET, POST, PUT)
202
205
* @param addr the requested endpoint address
203
206
* @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
205
208
* @param elapsedSeconds how long the request lasted in seconds
206
209
*/
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 );
213
217
}
214
218
}
0 commit comments