Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature]adding fixed gauge meter and updating counter meter with amount #20

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -438,14 +438,27 @@ 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.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.<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 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) {
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 @@ -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;
}
}
Loading