From 4212885203edaf9b5e643fd1fb7bf2787bfa607d Mon Sep 17 00:00:00 2001 From: "S.Safizadeh" Date: Wed, 21 Feb 2024 19:05:10 +0330 Subject: [PATCH] [feature]adding fixed gauge meter and updating counter meter with amount --- README.md | 15 ++++++- .../server/starter/metrics/GaugeValue.java | 2 +- .../starter/metrics/util/MeterUtil.java | 41 ++++++++++++++++--- 3 files changed, 51 insertions(+), 7 deletions(-) diff --git a/README.md b/README.md index 425c090..286d85d 100644 --- a/README.md +++ b/README.md @@ -438,7 +438,16 @@ we have 3 types of metric : TIMER , COUNTER , GAUGE. you can register your custo "description", tags); ``` -as you see, you should specify your metric with a valid type and a name. additionally, if wanted, you can add description or tags to your registering metric. +as you see, you should specify your metric with a valid type and a name. additionally, if wanted, you can add description or tags to your registering metric.
+you can also register a gauge meter with specifying its initial value: +``` + meterUtil.registerFixedInitGaugeMeter("meterName", + "description", + tags, + initialValue); +``` +note that the initialValue is Long and this kind of gauge metric can not be updated by incrementing or decrementing its value. + #### updating metrics we have three types of metric that for each one, we provided a proper updating mechanism: 1. updating COUNTER meter: by updating this type of meter you will increment the value of it.
@@ -446,6 +455,10 @@ we have three types of metric that for each one, we provided a proper updating m ``` meterUtil.updateCounterMeter("meterName", tags); ``` +and if you want to increment your counter metric by an specified amount (double): +``` + meterUtil.updateCounterMeter("meterName", tags, amount); +``` - note : you can pass null instead of _tags_ if you did not specify tags for your metric in the first place. 2.updating TIMER meter: by updating the timer meter, you can record your wanted duration in milliseconds.
diff --git a/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/GaugeValue.java b/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/GaugeValue.java index 271e32c..35eb2bd 100644 --- a/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/GaugeValue.java +++ b/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/GaugeValue.java @@ -7,7 +7,7 @@ * @since 2/13/2024 */ public class GaugeValue { - private static final AtomicInteger value = new AtomicInteger(0); + private final AtomicInteger value = new AtomicInteger(0); public void increment() { value.incrementAndGet(); diff --git a/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/util/MeterUtil.java b/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/util/MeterUtil.java index 4bafbe0..01a4ac4 100644 --- a/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/util/MeterUtil.java +++ b/tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/util/MeterUtil.java @@ -38,6 +38,16 @@ public Meter registerMeter(MeterType meterType, String meterName, String descrip return meters.get(key); } + public Meter registerFixedInitGaugeMeter(String meterName, String description, Tags tags, Long value) { + String key = createKey(meterName, tags); + if (!meters.containsKey(key)) { + synchronized (meters) { + meters.computeIfAbsent(key, ignore -> registerFixedGaugeMeter(meterName, description, tags, value)); + } + } + return meters.get(key); + } + public void updateTimerMeter(String metricName, Tags tags, long duration) { String key = createKey(metricName, tags); if (!meters.isEmpty() && meters.get(key) != null) { @@ -54,12 +64,19 @@ public void updateCounterMeter(String metricName, Tags tags) { } } + public void updateCounterMeterByFixedAmount(String metricName, Tags tags, double amount) { + String key = createKey(metricName, tags); + if (!meters.isEmpty() && meters.get(key) != null) { + Meter meter = meters.get(key); + ((Counter) meter).increment(amount); + } + } + public void updateGaugeMeterByIncrementing(String metricName, Tags tags) { String key = createKey(metricName, tags); if (!meters.isEmpty() && gaugeMeters.get(key) != null) { GaugeValue gaugeValue = gaugeMeters.get(createKey(metricName, tags)); gaugeValue.increment(); - gaugeMeters.put(createKey(metricName, tags), gaugeValue); } } @@ -68,7 +85,6 @@ public void updateGaugeMeterByDecrementing(String metricName, Tags tags) { if (!gaugeMeters.isEmpty() && gaugeMeters.get(key) != null) { GaugeValue gaugeValue = gaugeMeters.get(createKey(metricName, tags)); gaugeValue.decrement(); - gaugeMeters.put(createKey(metricName, tags), gaugeValue); } } @@ -129,20 +145,35 @@ private Timer registerTimerMeter(String metricName, String description, Tags tag } } - private Gauge registerGaugeMeter(String metricName, String desctiption, Tags tags) { + private Gauge registerGaugeMeter(String metricName, String description, Tags tags) { GaugeValue gaugeValue = new GaugeValue(); Gauge gauge; if (tags == null) { gauge = Gauge.builder(metricName, gaugeValue::getValue) - .description(desctiption) + .description(description) .register(meterRegistry); } else { gauge = Gauge.builder(metricName, gaugeValue::getValue) - .description(desctiption) + .description(description) .tags(tags) .register(meterRegistry); } gaugeMeters.put(createKey(metricName, tags), gaugeValue); return gauge; } + + private Gauge registerFixedGaugeMeter(String metricName, String description, Tags tags, Long value) { + Gauge gauge; + if (tags == null) { + gauge = Gauge.builder(metricName, value::longValue) + .description(description) + .register(meterRegistry); + } else { + gauge = Gauge.builder(metricName, value::longValue) + .description(description) + .tags(tags) + .register(meterRegistry); + } + return gauge; + } }