Skip to content

Commit cfda4e0

Browse files
committed
Provide the context of the interaction when reporting client requests and HTTP requests.
Motivation: The metrics SPI before the tracing SPI, it does not take in account the request context unlike the tracing SPI. We can definitely introduce its support by passing the context as part of the metrics SPI event signal. Changes: - Overload appropriate metrics SPI events with a signature augmented with the Vert.x context - Deprecate for removal in Vertx 6.0 the original versions - Implement and test the behavior
1 parent 970dc22 commit cfda4e0

File tree

14 files changed

+83
-34
lines changed

14 files changed

+83
-34
lines changed

vertx-core/src/main/java/io/vertx/core/http/impl/http1x/Http1xClientConnection.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ private void beginRequest(Stream stream, io.vertx.core.http.impl.HttpRequestHead
255255
inflight.addLast(stream);
256256
this.isConnect = connect;
257257
if (this.metrics != null) {
258-
stream.metric = this.metrics.requestBegin(request.uri, new ObservableRequest(request));
258+
stream.metric = this.metrics.requestBegin(stream.context, request.uri, new ObservableRequest(request));
259259
}
260260
VertxTracer tracer = stream.context.tracer();
261261
if (tracer != null) {

vertx-core/src/main/java/io/vertx/core/http/impl/http1x/Http1xServerRequest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ private void reportRequestComplete() {
609609
private void reportRequestBegin() {
610610
HttpServerMetrics metrics = conn.metrics;
611611
if (metrics != null) {
612-
metric = metrics.requestBegin(conn.metric(), this);
612+
metric = metrics.requestBegin(context, conn.metric(), this);
613613
}
614614
VertxTracer tracer = context.tracer();
615615
if (tracer != null) {

vertx-core/src/main/java/io/vertx/core/http/impl/http2/DefaultHttp2ClientStream.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ public void onPush(DefaultHttp2ClientStream pushStream, int promisedStreamId, Ht
197197
HttpClientPush push = new HttpClientPush(new HttpRequestHead(headers.scheme(), headers.method(), headers.path(), headers, headers.authority(), null, null), pushStream);
198198
pushStream.init(promisedStreamId, writable);
199199
if (pushStream.observable != null) {
200-
pushStream.observable.observePush(headers);
200+
pushStream.observable.observePush(pushStream.context, headers);
201201
}
202202
context.execute(push, this::handlePush);
203203
}

vertx-core/src/main/java/io/vertx/core/http/impl/http2/codec/Http2ServerConnectionImpl.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import io.vertx.core.http.impl.http2.Http2ServerConnection;
3333
import io.vertx.core.http.impl.http2.Http2ServerStream;
3434
import io.vertx.core.internal.ContextInternal;
35+
import io.vertx.core.internal.PromiseInternal;
3536
import io.vertx.core.net.HostAndPort;
3637
import io.vertx.core.spi.metrics.HttpServerMetrics;
3738
import io.vertx.core.spi.metrics.TransportMetrics;
@@ -229,7 +230,7 @@ private synchronized void doSendPush(int streamId, HostAndPort authority, HttpMe
229230
this,
230231
metrics,
231232
metric(),
232-
context,
233+
((PromiseInternal)promise).context(),
233234
new HttpRequestHeaders(headers_),
234235
method,
235236
path,

vertx-core/src/main/java/io/vertx/core/http/impl/observability/ClientStreamObserver.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ public ClientStreamObserver(ContextInternal context, TracingPolicy tracingPolicy
4040
this.clientMetrics = clientMetrics;
4141
}
4242

43-
public void observePush(HttpRequestHeaders headers) {
43+
public void observePush(ContextInternal context, HttpRequestHeaders headers) {
4444
if (clientMetrics != null) {
45-
Object metric = clientMetrics.requestBegin(headers.path().toString(), observableRequest(headers, remoteAddress));
45+
Object metric = clientMetrics.requestBegin(context, headers.path().toString(), observableRequest(headers, remoteAddress));
4646
this.metric = metric;
4747
clientMetrics.requestEnd(metric, 0L);
4848
}
@@ -56,7 +56,7 @@ public void observeUpgrade(Object metric, Object trace) {
5656
public void observeOutboundHeaders(HttpHeaders headers) {
5757
HttpRequestHeaders r = (HttpRequestHeaders) headers;
5858
if (clientMetrics != null) {
59-
metric = clientMetrics.requestBegin(r.path(), observableRequest(r, remoteAddress));
59+
metric = clientMetrics.requestBegin(context, r.path(), observableRequest(r, remoteAddress));
6060
}
6161
VertxTracer tracer = context.tracer();
6262
if (tracer != null) {

vertx-core/src/main/java/io/vertx/core/http/impl/observability/ServerStreamObserver.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public void observeInboundTrailers(long bytesRead) {
5151

5252
public void observeInboundHeaders(HttpHeaders headers) {
5353
if (serverMetrics != null) {
54-
metric = serverMetrics.requestBegin(socketMetric, observableRequest((HttpRequestHeaders) headers, remoteAddress));
54+
metric = serverMetrics.requestBegin(context, socketMetric, observableRequest((HttpRequestHeaders) headers, remoteAddress));
5555
}
5656
VertxTracer tracer = context.tracer();
5757
if (tracer != null) {
@@ -85,7 +85,7 @@ public void observeRoute(String route) {
8585

8686
public void observePush(HttpResponseHeaders headers, HttpMethod method, String uri) {
8787
if (serverMetrics != null) {
88-
metric = serverMetrics.responsePushed(socketMetric, method, uri, observableResponse(headers, remoteAddress));
88+
metric = serverMetrics.responsePushed(context, socketMetric, method, uri, observableResponse(headers, remoteAddress));
8989
}
9090
}
9191
}

vertx-core/src/main/java/io/vertx/core/spi/metrics/ClientMetrics.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,26 +11,37 @@
1111

1212
package io.vertx.core.spi.metrics;
1313

14+
import io.vertx.core.Context;
15+
1416
/**
1517
* The client metrics SPI that Vert.x will use to call when client events occur.<p/>
1618
*
1719
* @author <a href="mailto:[email protected]">Julien Viet</a>
1820
*/
1921
public interface ClientMetrics<M, Req, Resp> extends Metrics {
2022

23+
/**
24+
* @deprecated instead override {@link #requestBegin(Context, String, Object)}, this will be removed in Vert.x 6
25+
*/
26+
@Deprecated(forRemoval = true)
27+
default M requestBegin(String uri, Req request) {
28+
return null;
29+
}
30+
2131
/**
2232
* Called when a client request begins. Vert.x will invoke {@link #requestEnd} when the request
2333
* has ended or {@link #requestReset} if the request/response has failed before.
2434
*
2535
* <p>The request uri is an arbitrary URI that depends on the client, e.g an HTTP request uri,
2636
* a SQL query, etc...
2737
*
38+
* @param context the vertx context associated with the request
2839
* @param uri an arbitrary uri
2940
* @param request the request object
3041
* @return the request metric
3142
*/
32-
default M requestBegin(String uri, Req request) {
33-
return null;
43+
default M requestBegin(Context context, String uri, Req request) {
44+
return requestBegin(uri, request);
3445
}
3546

3647
/**

vertx-core/src/main/java/io/vertx/core/spi/metrics/HttpServerMetrics.java

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
package io.vertx.core.spi.metrics;
1313

14+
import io.vertx.core.Context;
1415
import io.vertx.core.http.HttpMethod;
1516
import io.vertx.core.http.ServerWebSocket;
1617
import io.vertx.core.spi.observability.HttpRequest;
@@ -37,16 +38,25 @@
3738
*/
3839
public interface HttpServerMetrics<R, W, S> extends TransportMetrics<S> {
3940

41+
/**
42+
* @deprecated instead override {@link #requestBegin(Context, Object, HttpRequest)}, this will be removed in Vert.x 6
43+
*/
44+
@Deprecated(forRemoval = true)
45+
default R requestBegin(S socketMetric, HttpRequest request) {
46+
return null;
47+
}
48+
4049
/**
4150
* Called when an http server request begins. Vert.x will invoke {@link #responseEnd} when the response has ended
4251
* or {@link #requestReset} if the request/response has failed before.
4352
*
53+
* @param context the vertx context associated with the request
4454
* @param socketMetric the socket metric
4555
* @param request the http server reuqest
4656
* @return the request metric
4757
*/
48-
default R requestBegin(S socketMetric, HttpRequest request) {
49-
return null;
58+
default R requestBegin(Context context, S socketMetric, HttpRequest request) {
59+
return requestBegin(socketMetric, request);
5060
}
5161

5262
/**
@@ -76,16 +86,26 @@ default void requestReset(R requestMetric) {
7686
default void responseBegin(R requestMetric, HttpResponse response) {
7787
}
7888

89+
/**
90+
* @deprecated instead override {@link #responsePushed(Context, Object, HttpMethod, String, HttpResponse)},
91+
* this will be removed in Vert.x 6
92+
*/
93+
@Deprecated(forRemoval = true)
94+
default R responsePushed(S socketMetric, HttpMethod method, String uri, HttpResponse response) {
95+
return null;
96+
}
97+
7998
/**
8099
* Called when an http server response is pushed.
81100
*
101+
* @param context the vertx context associated with the request
82102
* @param socketMetric the socket metric
83103
* @param method the pushed response method
84104
* @param uri the pushed response uri
85105
* @param response the http server response @return the request metric
86106
*/
87-
default R responsePushed(S socketMetric, HttpMethod method, String uri, HttpResponse response) {
88-
return null;
107+
default R responsePushed(Context context, S socketMetric, HttpMethod method, String uri, HttpResponse response) {
108+
return responsePushed(socketMetric, method, uri, response);
89109
}
90110

91111
/**

vertx-core/src/test/java/io/vertx/test/fakemetrics/EndpointMetric.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
package io.vertx.test.fakemetrics;
1313

14+
import io.vertx.core.Context;
1415
import io.vertx.core.spi.metrics.ClientMetrics;
1516
import io.vertx.core.spi.observability.HttpRequest;
1617
import io.vertx.core.spi.observability.HttpResponse;
@@ -32,9 +33,9 @@ public EndpointMetric() {
3233
}
3334

3435
@Override
35-
public HttpClientMetric requestBegin(String uri, HttpRequest request) {
36+
public HttpClientMetric requestBegin(Context context, String uri, HttpRequest request) {
3637
requestCount.incrementAndGet();
37-
HttpClientMetric metric = new HttpClientMetric(this, request);
38+
HttpClientMetric metric = new HttpClientMetric(this, context, request);
3839
requests.put(request, metric);
3940
return metric;
4041
}

vertx-core/src/test/java/io/vertx/test/fakemetrics/FakeHttpServerMetrics.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
package io.vertx.test.fakemetrics;
1313

14+
import io.vertx.core.Context;
1415
import io.vertx.core.http.HttpMethod;
1516
import io.vertx.core.http.HttpServerRequest;
1617
import io.vertx.core.http.ServerWebSocket;
@@ -45,8 +46,8 @@ public HttpServerMetric getResponseMetric(String uri) {
4546
}
4647

4748
@Override
48-
public HttpServerMetric requestBegin(SocketMetric socketMetric, HttpRequest request) {
49-
HttpServerMetric metric = new HttpServerMetric(request, socketMetric);
49+
public HttpServerMetric requestBegin(Context context, SocketMetric socketMetric, HttpRequest request) {
50+
HttpServerMetric metric = new HttpServerMetric(context, request, socketMetric);
5051
requests.add(metric);
5152
return metric;
5253
}
@@ -58,8 +59,8 @@ public void requestEnd(HttpServerMetric requestMetric, HttpRequest request, long
5859
}
5960

6061
@Override
61-
public HttpServerMetric responsePushed(SocketMetric socketMetric, HttpMethod method, String uri, HttpResponse response) {
62-
HttpServerMetric requestMetric = new HttpServerMetric(uri, socketMetric);
62+
public HttpServerMetric responsePushed(Context context, SocketMetric socketMetric, HttpMethod method, String uri, HttpResponse response) {
63+
HttpServerMetric requestMetric = new HttpServerMetric(context, uri, socketMetric);
6364
requestMetric.response.set(response);
6465
requests.add(requestMetric);
6566
return requestMetric;

0 commit comments

Comments
 (0)