/
AbstractHttpServerMetricsHandler.java
258 lines (219 loc) · 9.1 KB
/
AbstractHttpServerMetricsHandler.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
/*
* Copyright (c) 2021-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.http.server;
import io.netty.buffer.ByteBufHolder;
import io.netty5.buffer.api.Buffer;
import io.netty5.channel.ChannelHandlerAdapter;
import io.netty5.channel.ChannelHandlerContext;
import io.netty5.handler.codec.http.HttpRequest;
import io.netty5.handler.codec.http.HttpResponse;
import io.netty5.handler.codec.http.HttpResponseStatus;
import io.netty5.handler.codec.http.LastHttpContent;
import io.netty5.handler.codec.http2.Http2StreamChannel;
import io.netty5.util.concurrent.Future;
import reactor.netty.channel.ChannelOperations;
import reactor.util.Logger;
import reactor.util.Loggers;
import reactor.util.annotation.Nullable;
import java.time.Duration;
import java.util.function.Function;
/**
* @author Violeta Georgieva
* @since 1.0.8
*/
abstract class AbstractHttpServerMetricsHandler extends ChannelHandlerAdapter {
private static final Logger log = Loggers.getLogger(AbstractHttpServerMetricsHandler.class);
long dataReceived;
long dataSent;
long dataReceivedTime;
long dataSentTime;
final Function<String, String> uriTagValue;
protected AbstractHttpServerMetricsHandler(@Nullable Function<String, String> uriTagValue) {
this.uriTagValue = uriTagValue;
}
protected AbstractHttpServerMetricsHandler(AbstractHttpServerMetricsHandler copy) {
this.dataReceived = copy.dataReceived;
this.dataReceivedTime = copy.dataReceivedTime;
this.dataSent = copy.dataSent;
this.dataSentTime = copy.dataSentTime;
this.uriTagValue = copy.uriTagValue;
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
// For custom user recorders, we don't propagate the channelActive event, because this will be done
// by the ChannelMetricsHandler itself. ChannelMetricsHandler is only present when the recorder is
// not our MicrometerHttpServerMetricsRecorder. See HttpServerConfig class.
if (!(ctx.channel() instanceof Http2StreamChannel) && recorder() instanceof MicrometerHttpServerMetricsRecorder) {
try {
recorder().recordServerConnectionOpened(ctx.channel().localAddress());
}
catch (RuntimeException e) {
log.warn("Exception caught while recording metrics.", e);
// Allow request-response exchange to continue, unaffected by metrics problem
}
}
ctx.fireChannelActive();
}
@Override
public void channelInactive(ChannelHandlerContext ctx) {
if (!(ctx.channel() instanceof Http2StreamChannel) && recorder() instanceof MicrometerHttpServerMetricsRecorder) {
try {
recorder().recordServerConnectionClosed(ctx.channel().localAddress());
}
catch (RuntimeException e) {
log.warn("Exception caught while recording metrics.", e);
// Allow request-response exchange to continue, unaffected by metrics problem
}
}
ctx.fireChannelInactive();
}
@Override
public Future<Void> write(ChannelHandlerContext ctx, Object msg) {
try {
if (msg instanceof HttpResponse) {
if (((HttpResponse) msg).status().equals(HttpResponseStatus.CONTINUE)) {
return ctx.write(msg);
}
ChannelOperations<?, ?> channelOps = ChannelOperations.get(ctx.channel());
if (channelOps instanceof HttpServerOperations ops) {
startWrite(ops, uriTagValue == null ? ops.path : uriTagValue.apply(ops.path),
ops.method().name(), ops.status().codeAsText().toString());
}
}
dataSent += extractProcessedDataFromBuffer(msg);
if (msg instanceof LastHttpContent) {
return ctx.write(msg)
.addListener(future -> {
ChannelOperations<?, ?> channelOps = ChannelOperations.get(ctx.channel());
if (channelOps instanceof HttpServerOperations ops) {
try {
recordWrite(ops, uriTagValue == null ? ops.path : uriTagValue.apply(ops.path),
ops.method().name(), ops.status().codeAsText().toString());
}
catch (RuntimeException e) {
log.warn("Exception caught while recording metrics.", e);
// Allow request-response exchange to continue, unaffected by metrics problem
}
if (!ops.isHttp2() && ops.hostAddress() != null) {
// This metric is not applicable for HTTP/2
// ops.hostAddress() == null when request decoding failed, in this case
// we do not report active connection, so we do not report inactive connection
try {
recordInactiveConnection(ops);
}
catch (RuntimeException e) {
log.warn("Exception caught while recording metrics.", e);
// Allow request-response exchange to continue, unaffected by metrics problem
}
}
}
dataSent = 0;
});
}
}
catch (RuntimeException e) {
log.warn("Exception caught while recording metrics.", e);
// Allow request-response exchange to continue, unaffected by metrics problem
}
return ctx.write(msg);
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
try {
if (msg instanceof HttpRequest) {
ChannelOperations<?, ?> channelOps = ChannelOperations.get(ctx.channel());
if (channelOps instanceof HttpServerOperations ops) {
if (!ops.isHttp2()) {
// This metric is not applicable for HTTP/2
recordActiveConnection(ops);
}
startRead(ops, uriTagValue == null ? ops.path : uriTagValue.apply(ops.path), ops.method().name());
}
}
dataReceived += extractProcessedDataFromBuffer(msg);
if (msg instanceof LastHttpContent) {
ChannelOperations<?, ?> channelOps = ChannelOperations.get(ctx.channel());
if (channelOps instanceof HttpServerOperations ops) {
recordRead(ops, uriTagValue == null ? ops.path : uriTagValue.apply(ops.path), ops.method().name());
}
dataReceived = 0;
}
}
catch (RuntimeException e) {
log.warn("Exception caught while recording metrics.", e);
// Allow request-response exchange to continue, unaffected by metrics problem
}
ctx.fireChannelRead(msg);
}
@Override
public void channelExceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
try {
ChannelOperations<?, ?> channelOps = ChannelOperations.get(ctx.channel());
if (channelOps instanceof HttpServerOperations ops) {
// Always take the remote address from the operations in order to consider proxy information
recordException(ops, uriTagValue == null ? ops.path : uriTagValue.apply(ops.path));
}
}
catch (RuntimeException e) {
log.warn("Exception caught while recording metrics.", e);
// Allow request-response exchange to continue, unaffected by metrics problem
}
ctx.fireChannelExceptionCaught(cause);
}
private long extractProcessedDataFromBuffer(Object msg) {
if (msg instanceof ByteBufHolder) {
return ((ByteBufHolder) msg).content().readableBytes();
}
else if (msg instanceof Buffer) {
return ((Buffer) msg).readableBytes();
}
return 0;
}
protected abstract HttpServerMetricsRecorder recorder();
protected void recordException(HttpServerOperations ops, String path) {
// Always take the remote address from the operations in order to consider proxy information
recorder().incrementErrorsCount(ops.remoteAddress(), path);
}
protected void recordRead(HttpServerOperations ops, String path, String method) {
recorder().recordDataReceivedTime(path, method, Duration.ofNanos(System.nanoTime() - dataReceivedTime));
// Always take the remote address from the operations in order to consider proxy information
recorder().recordDataReceived(ops.remoteAddress(), path, dataReceived);
}
protected void recordWrite(HttpServerOperations ops, String path, String method, String status) {
Duration dataSentTimeDuration = Duration.ofNanos(System.nanoTime() - dataSentTime);
recorder().recordDataSentTime(path, method, status, dataSentTimeDuration);
if (dataReceivedTime != 0) {
recorder().recordResponseTime(path, method, status, Duration.ofNanos(System.nanoTime() - dataReceivedTime));
}
else {
recorder().recordResponseTime(path, method, status, dataSentTimeDuration);
}
// Always take the remote address from the operations in order to consider proxy information
recorder().recordDataSent(ops.remoteAddress(), path, dataSent);
}
protected void recordActiveConnection(HttpServerOperations ops) {
recorder().recordServerConnectionActive(ops.hostAddress());
}
protected void recordInactiveConnection(HttpServerOperations ops) {
recorder().recordServerConnectionInactive(ops.hostAddress());
}
protected void startRead(HttpServerOperations ops, String path, String method) {
dataReceivedTime = System.nanoTime();
}
protected void startWrite(HttpServerOperations ops, String path, String method, String status) {
dataSentTime = System.nanoTime();
}
}