Skip to content

Commit

Permalink
Add PARKED, INTERRUPTED and DELAYED to the Metric types #258 (#259)
Browse files Browse the repository at this point in the history
* Add PARKED, INTERRUPTED and DELAYED to the Metric types #258

* Add PARKED, INTERRUPTED and DELAYED to the Metric types #258
  • Loading branch information
MarcGiffing authored Mar 13, 2024
1 parent 5ae9285 commit cfd84bd
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 17 deletions.
1 change: 1 addition & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import java.util.List;

import io.github.bucket4j.BucketListener;
import lombok.Getter;

/**
* Marker Interface
*
*/
public class MetricBucketListener implements BucketListener {

private final String name;
@Getter
private final String name;
private final List<MetricTagResult> tags;
private final List<MetricHandler> metricHandlers;
private final List<MetricType> allowedTypes;
Expand All @@ -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));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

public interface MetricHandler {

void handle(MetricType type, String name, long tokens, List<MetricTagResult> tags);
void handle(MetricType type, String name, long counterIncrement, List<MetricTagResult> tags);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MetricTagResult> tags) {
public void handle(MetricType type, String name, long counterIncrement, List<MetricTagResult> tags) {

List<String> extendedTags = new ArrayList<>();
extendedTags.add("name");
Expand All @@ -33,13 +35,28 @@ public void handle(MetricType type, String name, long tokens, List<MetricTagResu
switch (type) {
case CONSUMED_COUNTER:
Metrics
.counter("bucket4j_summary_consumed", extendedTagsArray)
.increment(tokens);
.counter(METRIC_COUNTER_PREFIX + "consumed", extendedTagsArray)
.increment(counterIncrement);
break;
case REJECTED_COUNTER:
Metrics
.counter("bucket4j_summary_rejected", extendedTagsArray)
.increment(tokens);
.counter(METRIC_COUNTER_PREFIX + "rejected", extendedTagsArray)
.increment(counterIncrement);
break;
case PARKED_COUNTER:
Metrics
.counter(METRIC_COUNTER_PREFIX + "parked", extendedTagsArray)
.increment(counterIncrement);
break;
case INTERRUPTED_COUNTER:
Metrics
.counter(METRIC_COUNTER_PREFIX + "interrupted", extendedTagsArray)
.increment(counterIncrement);
break;
case DELAYED_COUNTER:
Metrics
.counter(METRIC_COUNTER_PREFIX + "delayed", extendedTagsArray)
.increment(counterIncrement);
break;
default:
throw new IllegalStateException("Unsupported metric type: " + type);
Expand Down

0 comments on commit cfd84bd

Please sign in to comment.