Skip to content

Commit b0ccf1e

Browse files
authored
Merge pull request #47887 from geoand/#47886
Add a way to access request context data in `HttpServerMetricsTagsContributor`
2 parents 09d452a + c9509a5 commit b0ccf1e

File tree

5 files changed

+23
-5
lines changed

5 files changed

+23
-5
lines changed

extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/HttpServerMetricsTagsContributor.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,14 @@ interface Context {
2323
HttpServerRequest request();
2424

2525
HttpResponse response();
26+
27+
/**
28+
* Gives access to the contextual data that was added while the HTTP request was active.
29+
* This can be found of doing something like {@link io.smallrye.common.vertx.ContextLocals#get(String)},
30+
* however this method is needed because {@link io.smallrye.common.vertx.ContextLocals#get(String)} won't
31+
* work when {@link HttpServerMetricsTagsContributor#contribute(Context)} is called as the HTTP request has
32+
* already gone away.
33+
*/
34+
<T> T requestContextLocalData(Object key);
2635
}
2736
}

extensions/micrometer/runtime/src/main/java/io/quarkus/micrometer/runtime/binder/vertx/VertxHttpServerMetrics.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import io.vertx.core.http.HttpServerOptions;
2626
import io.vertx.core.http.HttpServerRequest;
2727
import io.vertx.core.http.ServerWebSocket;
28+
import io.vertx.core.http.impl.HttpServerRequestInternal;
2829
import io.vertx.core.spi.metrics.HttpServerMetrics;
2930
import io.vertx.core.spi.observability.HttpRequest;
3031
import io.vertx.core.spi.observability.HttpResponse;
@@ -269,5 +270,9 @@ public void disconnected(LongTaskTimer.Sample websocketMetric) {
269270

270271
private record DefaultContext(HttpServerRequest request,
271272
HttpResponse response) implements HttpServerMetricsTagsContributor.Context {
273+
@Override
274+
public <T> T requestContextLocalData(Object key) {
275+
return ((HttpServerRequestInternal) request).context().getLocal(key);
276+
}
272277
}
273278
}

integration-tests/micrometer-prometheus/src/main/java/io/quarkus/DummyTag.java renamed to integration-tests/micrometer-prometheus/src/main/java/io/quarkus/ContextLocalTag.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
import io.quarkus.micrometer.runtime.HttpServerMetricsTagsContributor;
77

88
@Singleton
9-
public class DummyTag implements HttpServerMetricsTagsContributor {
9+
public class ContextLocalTag implements HttpServerMetricsTagsContributor {
1010

1111
@Override
1212
public Tags contribute(Context context) {
13-
return Tags.of("dummy", "value");
13+
String contextLocalData = context.requestContextLocalData("context-local");
14+
return Tags.of("dummy", contextLocalData != null ? contextLocalData : "value");
1415
}
1516
}

integration-tests/micrometer-prometheus/src/main/java/io/quarkus/it/micrometer/prometheus/PathTemplateResource.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@
44
import jakarta.ws.rs.Path;
55
import jakarta.ws.rs.PathParam;
66

7+
import io.smallrye.common.vertx.ContextLocals;
8+
79
@Path("template/path/{value}")
810
public class PathTemplateResource {
911
@GET
1012
public String get(@PathParam("value") String value) {
13+
ContextLocals.put("context-local", "val-" + value);
1114
return "Received: " + value;
1215
}
1316
}

integration-tests/micrometer-prometheus/src/test/java/io/quarkus/it/micrometer/prometheus/PrometheusMetricsRegistryTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@ void testPrometheusScrapeEndpointTextPlain() {
132132
.body(containsString("status=\"200\",uri=\"/secured/item/{id}\""))
133133
.body(containsString("status=\"401\",uri=\"/secured/item/{id}\""))
134134
.body(containsString("outcome=\"SUCCESS\""))
135-
.body(containsString("dummy=\"value\""))
135+
.body(containsString("dummy="))
136136
.body(containsString("foo=\"bar\""))
137137
.body(containsString("foo_response=\"value\""))
138138
.body(containsString("uri=\"/message/match/{id}/{sub}\""))
139139
.body(containsString("uri=\"/message/match/{other}\""))
140140

141141
.body(containsString(
142-
"http_server_requests_seconds_count{dummy=\"value\",env=\"test\",env2=\"test\",foo=\"UNSET\",foo_response=\"UNSET\",method=\"GET\",outcome=\"SUCCESS\",registry=\"prometheus\",status=\"200\",uri=\"/template/path/{value}\""))
142+
"http_server_requests_seconds_count{dummy=\"val-anything\",env=\"test\",env2=\"test\",foo=\"UNSET\",foo_response=\"UNSET\",method=\"GET\",outcome=\"SUCCESS\",registry=\"prometheus\",status=\"200\",uri=\"/template/path/{value}\""))
143143

144144
.body(containsString(
145145
"http_server_requests_seconds_count{dummy=\"value\",env=\"test\",env2=\"test\",foo=\"UNSET\",foo_response=\"UNSET\",method=\"GET\",outcome=\"SUCCESS\",registry=\"prometheus\",status=\"200\",uri=\"/root/{rootParam}/sub/{subParam}\""))
@@ -234,7 +234,7 @@ void testPrometheusScrapeEndpointOpenMetrics() {
234234
.body(containsString("uri=\"/message/match/{other}\""))
235235

236236
.body(containsString(
237-
"http_server_requests_seconds_count{dummy=\"value\",env=\"test\",env2=\"test\",foo=\"UNSET\",foo_response=\"UNSET\",method=\"GET\",outcome=\"SUCCESS\",registry=\"prometheus\",status=\"200\",uri=\"/template/path/{value}\""))
237+
"http_server_requests_seconds_count{dummy=\"val-anything\",env=\"test\",env2=\"test\",foo=\"UNSET\",foo_response=\"UNSET\",method=\"GET\",outcome=\"SUCCESS\",registry=\"prometheus\",status=\"200\",uri=\"/template/path/{value}\""))
238238

239239
// Verify Hibernate Metrics
240240
.body(containsString(

0 commit comments

Comments
 (0)