Skip to content

Commit

Permalink
New tests for limit metrics.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Dec 18, 2024
1 parent 1eb7329 commit 3fc680a
Show file tree
Hide file tree
Showing 5 changed files with 216 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,27 +195,33 @@ void initMetrics(String socketName, AimdLimitConfig config) {
String namePrefix = (socketName.startsWith("@") ? socketName.substring(1) : socketName)
+ "_" + config.name();

// actual value of limit at this time
Gauge.Builder<Integer> limitBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_limit", limit::get).scope(VENDOR);
meterRegistry.getOrCreate(limitBuilder);

// count of current requests running
Gauge.Builder<Integer> concurrentRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_concurrent_requests", concurrentRequests::get).scope(VENDOR);
meterRegistry.getOrCreate(concurrentRequestsBuilder);

// count of number of requests rejected
Gauge.Builder<Integer> rejectedRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_rejected_requests", rejectedRequests::get).scope(VENDOR);
meterRegistry.getOrCreate(rejectedRequestsBuilder);

// actual number of requests queued
Gauge.Builder<Integer> queueLengthBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_queue_length", semaphore::getQueueLength).scope(VENDOR);
meterRegistry.getOrCreate(queueLengthBuilder);

// histogram of round-trip times, excluding any time queued
Timer.Builder rttTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_rtt")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
rttTimer = meterRegistry.getOrCreate(rttTimerBuilder);

// histogram of wait times for a permit in queue
Timer.Builder waitTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_queue_wait_time")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,28 +261,33 @@ public void init(String socketName) {
+ "_" + config.name();

if (semaphore != null) {
// actual number of requests queued
Gauge.Builder<Integer> queueLengthBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_queue_length", semaphore::getQueueLength).scope(VENDOR);
meterRegistry.getOrCreate(queueLengthBuilder);

Gauge.Builder<Integer> concurrentRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_concurrent_requests", concurrentRequests::get).scope(VENDOR);
meterRegistry.getOrCreate(concurrentRequestsBuilder);

Gauge.Builder<Integer> rejectedRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_rejected_requests", rejectedRequests::get).scope(VENDOR);
meterRegistry.getOrCreate(rejectedRequestsBuilder);

Timer.Builder rttTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_rtt")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
rttTimer = meterRegistry.getOrCreate(rttTimerBuilder);

Timer.Builder waitTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_queue_wait_time")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
queueWaitTimer = meterRegistry.getOrCreate(waitTimerBuilder);
}

// count of current requests running
Gauge.Builder<Integer> concurrentRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_concurrent_requests", concurrentRequests::get).scope(VENDOR);
meterRegistry.getOrCreate(concurrentRequestsBuilder);

// actual number of requests queued
Gauge.Builder<Integer> rejectedRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_rejected_requests", rejectedRequests::get).scope(VENDOR);
meterRegistry.getOrCreate(rejectedRequestsBuilder);

// histogram of round-trip times, excluding any time queued
Timer.Builder rttTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_rtt")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
rttTimer = meterRegistry.getOrCreate(rttTimerBuilder);

// histogram of wait times for a permit in queue
Timer.Builder waitTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_queue_wait_time")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
queueWaitTimer = meterRegistry.getOrCreate(waitTimerBuilder);
}
}
}
8 changes: 8 additions & 0 deletions webserver/tests/resource-limits/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,14 @@
<groupId>io.helidon.webserver</groupId>
<artifactId>helidon-webserver-websocket</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver.observe</groupId>
<artifactId>helidon-webserver-observe</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver.observe</groupId>
<artifactId>helidon-webserver-observe-metrics</artifactId>
</dependency>
<dependency>
<groupId>io.helidon.webserver.testing.junit5</groupId>
<artifactId>helidon-webserver-testing-junit5</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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
*
* http://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 io.helidon.webserver.tests.resourcelimit;

import io.helidon.common.concurrency.limits.AimdLimit;
import io.helidon.webclient.api.HttpClientResponse;
import io.helidon.webclient.api.WebClient;
import io.helidon.webserver.WebServerConfig;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.observe.ObserveFeature;
import io.helidon.webserver.observe.metrics.MetricsObserver;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;
import io.helidon.webserver.testing.junit5.SetUpServer;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

@ServerTest
class AimdLimitMetricsTest {

private static final String[] METRIC_NAMES = {
"default_aimd_queue_length",
"default_aimd_rejected_requests",
"default_aimd_rtt_seconds",
"default_aimd_rtt_seconds_max",
"default_aimd_queue_wait_time_seconds",
"default_aimd_queue_wait_time_seconds_max",
"default_aimd_concurrent_requests",
"default_aimd_limit"
};

private final WebClient webClient;

AimdLimitMetricsTest(WebClient webClient) {
this.webClient = webClient;
}

@SetUpServer
static void serverSetup(WebServerConfig.Builder builder) {
ObserveFeature observe = ObserveFeature.builder()
.addObserver(MetricsObserver.create())
.build();
builder.concurrencyLimit(AimdLimit.builder()
.minLimit(1)
.initialLimit(1)
.maxLimit(1)
.queueLength(1)
.enableMetrics(true)
.build())
.addFeature(observe);
}

@SetUpRoute
static void routeSetup(HttpRules rules) {
rules.get("/greet", (req, res) -> {
res.send("hello");
});
}

@Test
void testMetrics() {
try (HttpClientResponse res = webClient.get("/greet").request()) {
assertThat(res.status().code(), is(200));
}
try (HttpClientResponse res = webClient.get("/observe/metrics").request()) {
String s = res.as(String.class);
for (String metricName : METRIC_NAMES) {
assertThat(s, containsString(metricName));
}
assertThat(res.status().code(), is(200));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (c) 2024 Oracle and/or its affiliates.
*
* 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
*
* http://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 io.helidon.webserver.tests.resourcelimit;

import io.helidon.common.concurrency.limits.FixedLimit;
import io.helidon.webclient.api.HttpClientResponse;
import io.helidon.webclient.api.WebClient;

import io.helidon.webserver.WebServerConfig;
import io.helidon.webserver.http.HttpRules;
import io.helidon.webserver.observe.ObserveFeature;
import io.helidon.webserver.observe.metrics.MetricsObserver;
import io.helidon.webserver.testing.junit5.ServerTest;
import io.helidon.webserver.testing.junit5.SetUpRoute;
import io.helidon.webserver.testing.junit5.SetUpServer;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;

@ServerTest
class FixedLimitMetricsTest {

private static final String[] METRIC_NAMES = {
"default_fixed_queue_length",
"default_fixed_rejected_requests",
"default_fixed_rtt_seconds",
"default_fixed_rtt_seconds_max",
"default_fixed_queue_wait_time_seconds",
"default_fixed_queue_wait_time_seconds_max",
"default_fixed_concurrent_requests"
};

private final WebClient webClient;

FixedLimitMetricsTest(WebClient webClient) {
this.webClient = webClient;
}

@SetUpServer
static void serverSetup(WebServerConfig.Builder builder) {
ObserveFeature observe = ObserveFeature.builder()
.addObserver(MetricsObserver.create())
.build();
builder.concurrencyLimit(FixedLimit.builder()
.permits(1)
.enableMetrics(true)
.build())
.addFeature(observe);
}

@SetUpRoute
static void routeSetup(HttpRules rules) {
rules.get("/greet", (req, res) -> {
res.send("hello");
});
}

@Test
void testMetrics() {
try (HttpClientResponse res = webClient.get("/greet").request()) {
assertThat(res.status().code(), is(200));
}
try (HttpClientResponse res = webClient.get("/observe/metrics").request()) {
String s = res.as(String.class);
for (String metricName : METRIC_NAMES) {
assertThat(s, containsString(metricName));
}
assertThat(res.status().code(), is(200));
}
}
}

0 comments on commit 3fc680a

Please sign in to comment.