Skip to content

Commit

Permalink
Makes socket name a tag and updates tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Santiago Pericas-Geertsen <[email protected]>
  • Loading branch information
spericas committed Jan 21, 2025
1 parent 63a9d14 commit 462205c
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package io.helidon.common.concurrency.limits;

import java.io.Serial;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
Expand All @@ -31,6 +32,7 @@
import io.helidon.metrics.api.MeterRegistry;
import io.helidon.metrics.api.Metrics;
import io.helidon.metrics.api.MetricsFactory;
import io.helidon.metrics.api.Tag;
import io.helidon.metrics.api.Timer;

import static io.helidon.metrics.api.Meter.Scope.VENDOR;
Expand Down Expand Up @@ -192,39 +194,61 @@ void initMetrics(String socketName, AimdLimitConfig config) {
if (config.enableMetrics()) {
MetricsFactory metricsFactory = MetricsFactory.getInstance();
MeterRegistry meterRegistry = Metrics.globalRegistry();
String namePrefix = (socketName.startsWith("@") ? socketName.substring(1) : socketName)
+ "_" + config.name();

// define tag if socket is not the default
Tag socketNameTag = null;
if (!socketName.equals("@default")) {
socketNameTag = Tag.create("socketName", socketName);
}

// actual value of limit at this time
Gauge.Builder<Integer> limitBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_limit", limit::get).scope(VENDOR);
config.name() + "_limit", limit::get).scope(VENDOR);
if (socketNameTag != null) {
limitBuilder.tags(List.of(socketNameTag));
}
meterRegistry.getOrCreate(limitBuilder);

// count of current requests running
Gauge.Builder<Integer> concurrentRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_concurrent_requests", concurrentRequests::get).scope(VENDOR);
config.name() + "_concurrent_requests", concurrentRequests::get).scope(VENDOR);
if (socketNameTag != null) {
concurrentRequestsBuilder.tags(List.of(socketNameTag));
}
meterRegistry.getOrCreate(concurrentRequestsBuilder);

// count of number of requests rejected
Gauge.Builder<Integer> rejectedRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_rejected_requests", rejectedRequests::get).scope(VENDOR);
config.name() + "_rejected_requests", rejectedRequests::get).scope(VENDOR);
if (socketNameTag != null) {
rejectedRequestsBuilder.tags(List.of(socketNameTag));
}
meterRegistry.getOrCreate(rejectedRequestsBuilder);

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

// histogram of round-trip times, excluding any time queued
Timer.Builder rttTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_rtt")
Timer.Builder rttTimerBuilder = metricsFactory.timerBuilder(config.name() + "_rtt")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
if (socketNameTag != null) {
rttTimerBuilder.tags(List.of(socketNameTag));
}
rttTimer = meterRegistry.getOrCreate(rttTimerBuilder);

// histogram of wait times for a permit in queue
Timer.Builder waitTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_queue_wait_time")
Timer.Builder waitTimerBuilder = metricsFactory.timerBuilder(config.name() + "_queue_wait_time")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
if (socketNameTag != null) {
waitTimerBuilder.tags(List.of(socketNameTag));
}
queueWaitTimer = meterRegistry.getOrCreate(waitTimerBuilder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package io.helidon.common.concurrency.limits;

import java.util.List;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.Semaphore;
Expand All @@ -30,6 +31,7 @@
import io.helidon.metrics.api.MeterRegistry;
import io.helidon.metrics.api.Metrics;
import io.helidon.metrics.api.MetricsFactory;
import io.helidon.metrics.api.Tag;
import io.helidon.metrics.api.Timer;

import static io.helidon.metrics.api.Meter.Scope.VENDOR;
Expand Down Expand Up @@ -260,36 +262,55 @@ public void init(String socketName) {
if (config.enableMetrics()) {
MetricsFactory metricsFactory = MetricsFactory.getInstance();
MeterRegistry meterRegistry = Metrics.globalRegistry();
String namePrefix = (socketName.startsWith("@") ? socketName.substring(1) : socketName)
+ "_" + config.name();

// define tag if socket is not the default
Tag socketNameTag = null;
if (!socketName.equals("@default")) {
socketNameTag = Tag.create("socketName", socketName);
}

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

// count of current requests running
Gauge.Builder<Integer> concurrentRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_concurrent_requests", concurrentRequests::get).scope(VENDOR);
config.name() + "_concurrent_requests", concurrentRequests::get).scope(VENDOR);
if (socketNameTag != null) {
concurrentRequestsBuilder.tags(List.of(socketNameTag));
}
meterRegistry.getOrCreate(concurrentRequestsBuilder);

// actual number of requests queued
Gauge.Builder<Integer> rejectedRequestsBuilder = metricsFactory.gaugeBuilder(
namePrefix + "_rejected_requests", rejectedRequests::get).scope(VENDOR);
config.name() + "_rejected_requests", rejectedRequests::get).scope(VENDOR);
if (socketNameTag != null) {
rejectedRequestsBuilder.tags(List.of(socketNameTag));
}
meterRegistry.getOrCreate(rejectedRequestsBuilder);

// histogram of round-trip times, excluding any time queued
Timer.Builder rttTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_rtt")
Timer.Builder rttTimerBuilder = metricsFactory.timerBuilder(config.name() + "_rtt")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
if (socketNameTag != null) {
rttTimerBuilder.tags(List.of(socketNameTag));
}
rttTimer = meterRegistry.getOrCreate(rttTimerBuilder);

// histogram of wait times for a permit in queue
Timer.Builder waitTimerBuilder = metricsFactory.timerBuilder(namePrefix + "_queue_wait_time")
Timer.Builder waitTimerBuilder = metricsFactory.timerBuilder(config.name() + "_queue_wait_time")
.scope(VENDOR)
.baseUnit(Timer.BaseUnits.MILLISECONDS);
if (socketNameTag != null) {
waitTimerBuilder.tags(List.of(socketNameTag));
}
queueWaitTimer = meterRegistry.getOrCreate(waitTimerBuilder);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@

package io.helidon.webserver.tests.resourcelimit;

import java.util.Collections;
import java.util.Optional;

import io.helidon.common.concurrency.limits.AimdLimit;
import io.helidon.metrics.api.MeterRegistry;
import io.helidon.metrics.api.MetricsFactory;
import io.helidon.metrics.api.Timer;
import io.helidon.webclient.api.HttpClientResponse;
import io.helidon.webclient.api.WebClient;
import io.helidon.webserver.WebServerConfig;
Expand All @@ -29,22 +35,23 @@

import org.junit.jupiter.api.Test;

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

@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"
"aimd_queue_length",
"aimd_rejected_requests",
"aimd_rtt_seconds",
"aimd_rtt_seconds_max",
"aimd_queue_wait_time_seconds",
"aimd_queue_wait_time_seconds_max",
"aimd_concurrent_requests",
"aimd_limit"
};

private final WebClient webClient;
Expand Down Expand Up @@ -80,12 +87,18 @@ 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));
}

MeterRegistry meterRegistry = MetricsFactory.getInstance().globalRegistry();
Optional<Timer> rtt = meterRegistry.timer("aimd_rtt", Collections.emptyList());
assertThat(rtt.isPresent(), is(true));
assertThat(rtt.get().count(), is(greaterThan(0L)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

package io.helidon.webserver.tests.resourcelimit;

import java.util.Collections;
import java.util.Optional;

import io.helidon.common.concurrency.limits.FixedLimit;
import io.helidon.metrics.api.MeterRegistry;
import io.helidon.metrics.api.MetricsFactory;
import io.helidon.metrics.api.Timer;
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;
Expand All @@ -30,21 +35,22 @@

import org.junit.jupiter.api.Test;

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

@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"
"fixed_queue_length",
"fixed_rejected_requests",
"fixed_rtt_seconds",
"fixed_rtt_seconds_max",
"fixed_queue_wait_time_seconds",
"fixed_queue_wait_time_seconds_max",
"fixed_concurrent_requests"
};

private final WebClient webClient;
Expand Down Expand Up @@ -77,12 +83,18 @@ 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));
}

MeterRegistry meterRegistry = MetricsFactory.getInstance().globalRegistry();
Optional<Timer> rtt = meterRegistry.timer("fixed_rtt", Collections.emptyList());
assertThat(rtt.isPresent(), is(true));
assertThat(rtt.get().count(), is(greaterThan(0L)));
}
}

0 comments on commit 462205c

Please sign in to comment.