Skip to content

Commit f2f40a3

Browse files
committed
Fix matching logic and tests for path entries
1 parent a17a380 commit f2f40a3

4 files changed

Lines changed: 31 additions & 15 deletions

File tree

webserver/observe/metrics/src/main/java/io/helidon/webserver/observe/metrics/AutoHttpMetricsConfigSupport.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,16 +69,14 @@ static boolean isMeasured(AutoHttpMetricsConfig config, Method method, UriPath u
6969
return true;
7070
}
7171

72-
boolean matchedPath = false;
7372
boolean latestResult = true;
7473

7574
for (AutoHttpMetricsPathConfig cfg : config.effectivePathConfigs()) {
7675
if (cfg.matchesPath(uriPath)) {
77-
matchedPath = true;
78-
latestResult = cfg.matchesMethod(method) && cfg.enabled();
76+
latestResult = cfg.matchesMethod(method) == cfg.enabled();
7977
}
8078
}
81-
return !matchedPath || latestResult;
79+
return latestResult;
8280
}
8381

8482
/**

webserver/observe/metrics/src/test/java/io/helidon/webserver/observe/metrics/TestAutoMetricsConfig.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ void testAutoMetricsConfig() {
4242
- path: "/greet"
4343
methods: ["GET","HEAD"]
4444
- path: "/greet/{name}"
45-
methods: ["GET","OPTION"]
45+
methods: ["GET","OPTIONS"]
46+
- path: "/stuff"
47+
methods: ["HEAD","OPTIONS"]
48+
enabled: false
4649
- path: "/hi"
4750
enabled: false
4851
""";
@@ -59,6 +62,8 @@ void testAutoMetricsConfig() {
5962

6063
assertThat("GET /metrics", config.isMeasured(Method.GET, UriPath.create("/metrics")), is(false));
6164

65+
assertThat("GET /stuff", config.isMeasured(Method.GET, UriPath.create("/stuff")), is(true));
66+
assertThat("HEAD /stuff", config.isMeasured(Method.HEAD, UriPath.create("/stuff")), is(false));
6267

6368
}
6469

webserver/observe/telemetry/metrics/src/main/java/io/helidon/webserver/observe/telemetry/metrics/OpenTelemetryMetricsHttpSemanticConventions.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,11 +162,11 @@ private void updateMetricsIfMeasured(RoutingRequest req,
162162
/*
163163
If we search for the meter and find it we avoid constructing the builder unnecessarily.
164164
*/
165-
meterRegistry.timer(TIMER_NAME, tags)
165+
var timer = meterRegistry.timer(TIMER_NAME, tags)
166166
.orElse(meterRegistry.getOrCreate(Timer.builder(TIMER_NAME)
167167
.tags(tags)
168-
.buckets(BUCKET_BOUNDARIES)))
169-
.record(endTime - startTime, TimeUnit.NANOSECONDS);
168+
.buckets(BUCKET_BOUNDARIES)));
169+
timer.record(endTime - startTime, TimeUnit.NANOSECONDS);
170170
}
171171

172172
private String errorType(RoutingResponse resp, Exception exception) {

webserver/observe/telemetry/metrics/src/test/java/io/helidon/webserver/observe/telemetry/metrics/TestOpenTelemetrySemanticConventions.java

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
import io.helidon.common.testing.junit5.MatcherWithRetry;
2929
import io.helidon.config.Config;
3030
import io.helidon.config.ConfigSources;
31+
import io.helidon.http.Method;
3132
import io.helidon.metrics.api.MeterRegistry;
32-
import io.helidon.metrics.api.Timer;
33-
import io.helidon.service.registry.Services;
33+
import io.helidon.metrics.api.Metrics;import io.helidon.metrics.api.Timer;
3434
import io.helidon.webclient.http1.Http1Client;
3535
import io.helidon.webclient.http1.Http1ClientResponse;
3636
import io.helidon.webserver.WebServer;
@@ -88,6 +88,10 @@ static void setupServer(WebServerConfig.Builder serverBuilder) {
8888
observers:
8989
metrics:
9090
auto:
91+
paths:
92+
- path: /greet
93+
methods: ["OPTIONS"]
94+
enabled: false
9195
sockets: ["@default","private"]
9296
opt-in: ["http.server.request.duration:server.address"]
9397
""";
@@ -98,15 +102,20 @@ static void setupServer(WebServerConfig.Builder serverBuilder) {
98102
.routing(r -> r.get("/greet/{name}",
99103
(req, resp) ->
100104
resp.send("Hello, " + req.path().segments().get(1).value() + "!")))
101-
.routing("private", r -> r.get("/greet",
102-
(req, resp) ->
103-
resp.send("Hello, World!")));
105+
.routing("private", r -> r.any("/greet",
106+
(req, resp) -> {
107+
switch (req.prologue().method().text()) {
108+
case Method.GET_NAME -> resp.send("Hello, World!");
109+
case Method.OPTIONS_NAME -> resp.send("Options, World!");
110+
default -> resp.next();
111+
}
112+
}));
104113

105114
}
106115

107116
@Test
108117
void checkAutoMetrics() {
109-
var meterRegistry = Services.get(MeterRegistry.class);
118+
var meterRegistry = Metrics.globalRegistry();
110119

111120
// Use default socket.
112121
try (Http1ClientResponse defaultResponse = defaultClient.get("/greet/Joe")
@@ -120,15 +129,20 @@ void checkAutoMetrics() {
120129
.request();
121130
Http1ClientResponse metricsOnDefaultResponse = defaultClient.get("/observe/metrics")
122131
.accept(MediaTypes.APPLICATION_JSON)
132+
.request();
133+
Http1ClientResponse greetOptionsResponse = privateClient.options("/greet")
134+
.accept(MediaTypes.TEXT_PLAIN)
123135
.request()) {
124136

125137
assertThat("Greet endpoint", defaultResponse.status().code(), is(200));
126138
assertThat("Private endpoint", privateResponse.status().code(), is(200));
127139
assertThat("Admin endpoint", adminResponse.status().code(), is(200));
128140
assertThat("Metrics endpoint via default socket", metricsOnDefaultResponse.status().code(), is(404));
141+
assertThat("Private endpoint HEAD", greetOptionsResponse.status().code(), is(200));
129142

130143
/*
131144
Helidon registers and updates the timer asynchronously, so tolerate some delay.
145+
Despite all the requests we sent, we expect only two timers to have been created (and updated).
132146
*/
133147
AtomicReference<List<Timer>> timersRef = requestTimers(meterRegistry, 2);
134148

@@ -205,7 +219,6 @@ void checkAutoMetrics() {
205219
socketNamesInTimers.forEach(unexpectedlyUntimedSockets::remove);
206220
assertThat("Unexpectedly untimed sockets", unexpectedlyUntimedSockets, hasSize(0));
207221

208-
209222
}
210223

211224
}

0 commit comments

Comments
 (0)