Skip to content

Commit

Permalink
[feature]adding fixed gauge meter and updating counter meter with amount
Browse files Browse the repository at this point in the history
  • Loading branch information
S.Safizadeh committed Feb 21, 2024
1 parent 1a3a12f commit 88fd86d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 4 deletions.
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -440,14 +440,26 @@ 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.<br>
you can also register a gauge meter with specifying its initial value:
```
meterUtil.registerMeter("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.<br>
example:
```
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.<br>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,16 @@ public Meter registerMeter(MeterType meterType, String meterName, String descrip
return meters.get(key);
}

public Meter registerMeter(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) {
Expand All @@ -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);
}
}

Expand All @@ -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);
}
}

Expand Down Expand Up @@ -145,4 +161,19 @@ private Gauge registerGaugeMeter(String metricName, String desctiption, Tags tag
gaugeMeters.put(createKey(metricName, tags), gaugeValue);
return gauge;
}

private Gauge registerFixedGaugeMeter(String metricName, String desctiption, Tags tags, Long value) {
Gauge gauge;
if (tags == null) {
gauge = Gauge.builder(metricName, value::longValue)
.description(desctiption)
.register(meterRegistry);
} else {
gauge = Gauge.builder(metricName, value::longValue)
.description(desctiption)
.tags(tags)
.register(meterRegistry);
}
return gauge;
}
}

0 comments on commit 88fd86d

Please sign in to comment.