From cfd84bde38d09d7b8383704f159d0070dc212372 Mon Sep 17 00:00:00 2001 From: MarcGiffing Date: Wed, 13 Mar 2024 15:58:31 +0100 Subject: [PATCH] Add PARKED, INTERRUPTED and DELAYED to the Metric types #258 (#259) * Add PARKED, INTERRUPTED and DELAYED to the Metric types #258 * Add PARKED, INTERRUPTED and DELAYED to the Metric types #258 --- README.adoc | 1 + .../context/metrics/MetricBucketListener.java | 22 ++++++++------- .../context/metrics/MetricHandler.java | 2 +- .../starter/context/metrics/MetricType.java | 25 ++++++++++++++++- .../actuator/Bucket4jMetricHandler.java | 27 +++++++++++++++---- 5 files changed, 60 insertions(+), 17 deletions(-) diff --git a/README.adoc b/README.adoc index 7798d0bb..65ca48a8 100644 --- a/README.adoc +++ b/README.adoc @@ -497,6 +497,7 @@ This section is meant to help you migrate your application to new version of thi ==== Spring Boot Starter Bucket4j 0.12 * Removed deprecated expression property +* three new metric counter are added per default (PARKED, INTERRUPTED and DELAYED) ==== Spring Boot Starter Bucket4j 0.9 diff --git a/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricBucketListener.java b/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricBucketListener.java index 800e98bc..e655b9e5 100644 --- a/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricBucketListener.java +++ b/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricBucketListener.java @@ -3,6 +3,7 @@ import java.util.List; import io.github.bucket4j.BucketListener; +import lombok.Getter; /** * Marker Interface @@ -10,7 +11,8 @@ */ public class MetricBucketListener implements BucketListener { - private final String name; + @Getter + private final String name; private final List tags; private final List metricHandlers; private final List allowedTypes; @@ -36,26 +38,26 @@ public void onRejected(long tokens) { } } - public String getName() { - return name; - } - @Override public void onParked(long nanos) { - // TODO check if we should provide this information - + if(allowedTypes.contains(MetricType.PARKED_COUNTER)) { + metricHandlers.forEach(metricHandler -> metricHandler.handle(MetricType.PARKED_COUNTER, name, 1, tags)); + } } @Override public void onInterrupted(InterruptedException e) { - // TODO check if we should provide this information + if(allowedTypes.contains(MetricType.INTERRUPTED_COUNTER)) { + metricHandlers.forEach(metricHandler -> metricHandler.handle(MetricType.INTERRUPTED_COUNTER, name, 1, tags)); + } } @Override public void onDelayed(long nanos) { - // TODO check if we should provide this information - + if(allowedTypes.contains(MetricType.DELAYED_COUNTER)) { + metricHandlers.forEach(metricHandler -> metricHandler.handle(MetricType.DELAYED_COUNTER, name, 1, tags)); + } } } diff --git a/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricHandler.java b/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricHandler.java index d5783125..19a3a299 100644 --- a/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricHandler.java +++ b/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricHandler.java @@ -4,6 +4,6 @@ public interface MetricHandler { - void handle(MetricType type, String name, long tokens, List tags); + void handle(MetricType type, String name, long counterIncrement, List tags); } diff --git a/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricType.java b/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricType.java index 0d52260d..a15d1e25 100644 --- a/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricType.java +++ b/bucket4j-spring-boot-starter-context/src/main/java/com/giffing/bucket4j/spring/boot/starter/context/metrics/MetricType.java @@ -2,6 +2,29 @@ public enum MetricType { + /** + * Count token consumption. + */ CONSUMED_COUNTER, - REJECTED_COUNTER + + /** + * Count whenever consumption request for tokens is rejected. + */ + REJECTED_COUNTER, + + /** + * Count parked threads which wait of tokens refill in result of interaction with Bucket4js BlockingBucket. + */ + PARKED_COUNTER, + + /** + * Count interrupted threads during the wait of tokens refill in result of interaction with Bucket4js BlockingBucket + */ + INTERRUPTED_COUNTER, + + /** + * Count delayed tasks was submit to java.util.concurrent.ScheduledExecutorService + * because of wait for tokens refill in result of interaction with SchedulingBucket + */ + DELAYED_COUNTER } diff --git a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/metrics/actuator/Bucket4jMetricHandler.java b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/metrics/actuator/Bucket4jMetricHandler.java index 1da1d5d7..f309b1ad 100644 --- a/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/metrics/actuator/Bucket4jMetricHandler.java +++ b/bucket4j-spring-boot-starter/src/main/java/com/giffing/bucket4j/spring/boot/starter/config/metrics/actuator/Bucket4jMetricHandler.java @@ -16,8 +16,10 @@ @Primary public class Bucket4jMetricHandler implements MetricHandler { + public static final String METRIC_COUNTER_PREFIX = "bucket4j_summary_"; + @Override - public void handle(MetricType type, String name, long tokens, List tags) { + public void handle(MetricType type, String name, long counterIncrement, List tags) { List extendedTags = new ArrayList<>(); extendedTags.add("name"); @@ -33,13 +35,28 @@ public void handle(MetricType type, String name, long tokens, List