/
Metrics.java
300 lines (240 loc) · 8.33 KB
/
Metrics.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
* Copyright (c) 2019-2022 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.netty;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.observation.TimerObservationHandler;
import io.micrometer.observation.ObservationHandler;
import io.micrometer.observation.ObservationRegistry;
import reactor.netty.observability.ReactorNettyTimerObservationHandler;
import reactor.util.annotation.Nullable;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
/**
* Constants and utilities around metrics.
*
* @author Violeta Georgieva
* @since 0.9
*/
public class Metrics {
public static final MeterRegistry REGISTRY = io.micrometer.core.instrument.Metrics.globalRegistry;
public static final ObservationRegistry OBSERVATION_REGISTRY = ObservationRegistry.create();
static {
OBSERVATION_REGISTRY.observationConfig().observationHandler(
new ObservationHandler.FirstMatchingCompositeObservationHandler(
new ReactorNettyTimerObservationHandler(REGISTRY),
new TimerObservationHandler(REGISTRY)));
}
// Names
/**
* Name prefix that will be used for the HTTP server's metrics
* registered in Micrometer's global registry
*/
public static final String HTTP_SERVER_PREFIX = "reactor.netty.http.server";
/**
* Name prefix that will be used for the HTTP client's metrics
* registered in Micrometer's global registry
*/
public static final String HTTP_CLIENT_PREFIX = "reactor.netty.http.client";
/**
* Name prefix that will be used for the TCP server's metrics
* registered in Micrometer's global registry
*/
public static final String TCP_SERVER_PREFIX = "reactor.netty.tcp.server";
/**
* Name prefix that will be used for the TCP client's metrics
* registered in Micrometer's global registry
*/
public static final String TCP_CLIENT_PREFIX = "reactor.netty.tcp.client";
/**
* Name prefix that will be used for the UDP server's metrics
* registered in Micrometer's global registry
*/
public static final String UDP_SERVER_PREFIX = "reactor.netty.udp.server";
/**
* Name prefix that will be used for the UDP client's metrics
* registered in Micrometer's global registry
*/
public static final String UDP_CLIENT_PREFIX = "reactor.netty.udp.client";
/**
* Name prefix that will be used for Event Loop Group metrics
* registered in Micrometer's global registry
*/
public static final String EVENT_LOOP_PREFIX = "reactor.netty.eventloop";
/**
* Name prefix that will be used for the PooledConnectionProvider's metrics
* registered in Micrometer's global registry
*/
public static final String CONNECTION_PROVIDER_PREFIX = "reactor.netty.connection.provider";
/**
* Name prefix that will be used for the ByteBufAllocator's metrics
* registered in Micrometer's global registry
*/
public static final String BYTE_BUF_ALLOCATOR_PREFIX = "reactor.netty.bytebuf.allocator";
// Metrics
/**
* Amount of the data received, in bytes
*/
public static final String DATA_RECEIVED = ".data.received";
/**
* Amount of the data sent, in bytes
*/
public static final String DATA_SENT = ".data.sent";
/**
* Number of errors that occurred
*/
public static final String ERRORS = ".errors";
/**
* Time spent for TLS handshake
*/
public static final String TLS_HANDSHAKE_TIME = ".tls.handshake.time";
/**
* Time spent for connecting to the remote address
*/
public static final String CONNECT_TIME = ".connect.time";
/**
* Time spent in consuming incoming data
*/
public static final String DATA_RECEIVED_TIME = ".data.received.time";
/**
* Time spent in sending outgoing data
*/
public static final String DATA_SENT_TIME = ".data.sent.time";
/**
* Total time for the request/response
*/
public static final String RESPONSE_TIME = ".response.time";
/**
* The number of all connections, whether they are active or idle
*/
public static final String CONNECTIONS_TOTAL = ".connections.total";
/**
* The number of connections that are currently in use
*/
public static final String CONNECTIONS_ACTIVE = ".connections.active";
// AddressResolverGroup Metrics
/**
* Time spent for resolving the address
*/
public static final String ADDRESS_RESOLVER = ".address.resolver";
// PooledConnectionProvider Metrics
/**
* The number of all connections, active or idle
*/
public static final String TOTAL_CONNECTIONS = ".total.connections";
/**
* The number of the connections that have been successfully acquired and are in active use
*/
public static final String ACTIVE_CONNECTIONS = ".active.connections";
/**
* The maximum number of active connections that are allowed
*/
public static final String MAX_CONNECTIONS = ".max.connections";
/**
* The number of the idle connections
*/
public static final String IDLE_CONNECTIONS = ".idle.connections";
/**
* The number of requests that are waiting for a connection
*/
public static final String PENDING_CONNECTIONS = ".pending.connections";
/**
* The maximum number of requests that will be queued while waiting for a ready connection
*/
public static final String MAX_PENDING_CONNECTIONS = ".max.pending.connections";
/**
* The number of the active HTTP/2 streams
*/
public static final String ACTIVE_STREAMS = ".active.streams";
/**
* The number of requests that are waiting for opening HTTP/2 stream
*/
public static final String PENDING_STREAMS = ".pending.streams";
// ByteBufAllocator Metrics
/**
* The number of bytes reserved by heap buffer allocator
*/
public static final String USED_HEAP_MEMORY = ".used.heap.memory";
/**
* The number of bytes reserved by direct buffer allocator
*/
public static final String USED_DIRECT_MEMORY = ".used.direct.memory";
/**
* The actual bytes consumed by in-use buffers allocated from heap buffer pools
*/
public static final String ACTIVE_HEAP_MEMORY = ".active.heap.memory";
/**
* The actual bytes consumed by in-use buffers allocated from direct buffer pools
*/
public static final String ACTIVE_DIRECT_MEMORY = ".active.direct.memory";
/**
* The number of heap arenas
*/
public static final String HEAP_ARENAS = ".heap.arenas";
/**
* The number of direct arenas
*/
public static final String DIRECT_ARENAS = ".direct.arenas";
/**
* The number of thread local caches
*/
public static final String THREAD_LOCAL_CACHES = ".threadlocal.caches";
/**
* The size of the small cache
*/
public static final String SMALL_CACHE_SIZE = ".small.cache.size";
/**
* The size of the normal cache
*/
public static final String NORMAL_CACHE_SIZE = ".normal.cache.size";
/**
* The chunk size for an arena
*/
public static final String CHUNK_SIZE = ".chunk.size";
// EventLoop Metrics
/**
* The number of tasks that are pending for processing on an event loop
*/
public static final String PENDING_TASKS = ".pending.tasks";
// HttpServer Metrics
/**
* The number of active HTTP/2 streams
*/
public static final String STREAMS_ACTIVE = ".streams.active";
// Tags
public static final String LOCAL_ADDRESS = "local.address";
public static final String REMOTE_ADDRESS = "remote.address";
public static final String URI = "uri";
public static final String STATUS = "status";
public static final String METHOD = "method";
public static final String ID = "id";
public static final String NAME = "name";
public static final String TYPE = "type";
public static final String SUCCESS = "SUCCESS";
public static final String ERROR = "ERROR";
@Nullable
public static String formatSocketAddress(@Nullable SocketAddress socketAddress) {
if (socketAddress != null) {
if (socketAddress instanceof InetSocketAddress) {
InetSocketAddress address = (InetSocketAddress) socketAddress;
return address.getHostString() + ":" + address.getPort();
}
else {
return socketAddress.toString();
}
}
return null;
}
}