-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feature] adding metric util and metric filter
PRD-221992
- Loading branch information
1 parent
af8b52a
commit 7cfcf6b
Showing
9 changed files
with
339 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
tosan-httpserver-spring-boot-sample/src/main/resources/application.properties
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
server.port = 8099 | ||
|
||
serviceLog.enabled=true | ||
http.log.format=json | ||
http.log.format=json | ||
|
||
metric.filter.enable=true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
...r-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/GaugeValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.tosan.http.server.starter.metrics; | ||
|
||
import java.util.concurrent.atomic.AtomicInteger; | ||
|
||
/** | ||
* @author Shahryar Safizadeh | ||
* @since 2/13/2024 | ||
*/ | ||
public class GaugeValue { | ||
private static final AtomicInteger value = new AtomicInteger(0); | ||
|
||
public void increment() { | ||
value.incrementAndGet(); | ||
} | ||
|
||
public void decrement() { | ||
value.decrementAndGet(); | ||
} | ||
|
||
public AtomicInteger getValue() { | ||
return value; | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...g-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/MeterFilterConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package com.tosan.http.server.starter.metrics; | ||
|
||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.validation.annotation.Validated; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* @author Shahryar Safizadeh | ||
* @since 2/17/2024 | ||
*/ | ||
@ConfigurationProperties(prefix = "metric.filter") | ||
@Validated | ||
public class MeterFilterConfig { | ||
|
||
private String[] excludedMeterNames; | ||
private Map<String, String> excludedMeterTags; | ||
|
||
public String[] getExcludedMeterNames() { | ||
return excludedMeterNames; | ||
} | ||
|
||
public void setExcludedMeterNames(String[] excludedMeterNames) { | ||
this.excludedMeterNames = excludedMeterNames; | ||
} | ||
|
||
public Map<String, String> getExcludedMeterTags() { | ||
return excludedMeterTags; | ||
} | ||
|
||
public void setExcludedMeterTags(Map<String, String> excludedMeterTags) { | ||
this.excludedMeterTags = excludedMeterTags; | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/MetricFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.tosan.http.server.starter.metrics; | ||
|
||
import io.micrometer.core.instrument.Meter; | ||
import io.micrometer.core.instrument.Tag; | ||
import io.micrometer.core.instrument.config.MeterFilter; | ||
import io.micrometer.core.instrument.config.MeterFilterReply; | ||
|
||
import java.util.Arrays; | ||
|
||
public class MetricFilter implements MeterFilter { | ||
|
||
private MeterFilterConfig meterFilterConfig; | ||
|
||
public MetricFilter(MeterFilterConfig meterFilterConfig) { | ||
this.meterFilterConfig = meterFilterConfig; | ||
} | ||
|
||
@Override | ||
public MeterFilterReply accept(Meter.Id id) { | ||
if (meterFilterConfig.getExcludedMeterNames() != null && Arrays.asList(meterFilterConfig.getExcludedMeterNames()).contains(id.getName())) { | ||
return MeterFilterReply.DENY; | ||
} | ||
if (meterFilterConfig.getExcludedMeterTags() != null && !meterFilterConfig.getExcludedMeterTags().isEmpty()) { | ||
for (String key : meterFilterConfig.getExcludedMeterTags().keySet()) { | ||
if (id.getTags().contains(Tag.of(key, meterFilterConfig.getExcludedMeterTags().get(key)))) { | ||
return MeterFilterReply.DENY; | ||
} | ||
} | ||
} | ||
return MeterFilterReply.NEUTRAL; | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ot-starter/src/main/java/com/tosan/http/server/starter/metrics/enumuration/MeterType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.tosan.http.server.starter.metrics.enumuration; | ||
|
||
/** | ||
* @author Shahryar Safizadeh | ||
* @since 2/13/2024 | ||
*/ | ||
public enum MeterType { | ||
TIMER, | ||
COUNTER, | ||
GAUGE; | ||
} |
148 changes: 148 additions & 0 deletions
148
...ring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/util/MeterUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
package com.tosan.http.server.starter.metrics.util; | ||
|
||
import com.tosan.http.server.starter.metrics.GaugeValue; | ||
import com.tosan.http.server.starter.metrics.enumuration.MeterType; | ||
import io.micrometer.core.instrument.*; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* @author Shahryar Safizadeh | ||
* @since 2/13/2024 | ||
*/ | ||
public class MeterUtil { | ||
|
||
private static final Logger LOGGER = LoggerFactory.getLogger(MeterUtil.class); | ||
private MeterRegistry meterRegistry; | ||
private final Map<String, Meter> meters = new HashMap<>(); | ||
private final Map<String, GaugeValue> gaugeMeters = new HashMap<>(); | ||
|
||
@Autowired | ||
public void setMeterRegistry(MeterRegistry meterRegistry) { | ||
this.meterRegistry = meterRegistry; | ||
} | ||
|
||
public Meter registerMeter(MeterType meterType, String meterName, String description, Tags tags) { | ||
String key = createKey(meterName, tags); | ||
if (!meters.containsKey(key)) { | ||
synchronized (meters) { | ||
meters.computeIfAbsent(key, ignore -> getMeter(meterType, meterName, description, tags)); | ||
} | ||
} | ||
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) { | ||
Meter meter = meters.get(key); | ||
((Timer) meter).record(duration, TimeUnit.MILLISECONDS); | ||
} | ||
} | ||
|
||
public void updateCounterMeter(String metricName, Tags tags) { | ||
String key = createKey(metricName, tags); | ||
if (!meters.isEmpty() && meters.get(key) != null) { | ||
Meter meter = meters.get(key); | ||
((Counter) meter).increment(); | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} | ||
|
||
public void updateGaugeMeterByDecrementing(String metricName, Tags tags) { | ||
String key = createKey(metricName, tags); | ||
if (!gaugeMeters.isEmpty() && gaugeMeters.get(key) != null) { | ||
GaugeValue gaugeValue = gaugeMeters.get(createKey(metricName, tags)); | ||
gaugeValue.decrement(); | ||
gaugeMeters.put(createKey(metricName, tags), gaugeValue); | ||
} | ||
} | ||
|
||
private Meter getMeter(MeterType meterType, String meterName, String description, Tags tags) { | ||
if (meterType != null) { | ||
switch (meterType) { | ||
case COUNTER: { | ||
return registerCounterMeter(meterName, description, tags); | ||
} | ||
case TIMER: { | ||
return registerTimerMeter(meterName, description, tags); | ||
} | ||
case GAUGE: { | ||
return registerGaugeMeter(meterName, description, tags); | ||
} | ||
} | ||
} | ||
LOGGER.error("No meterType selected."); | ||
return null; | ||
} | ||
|
||
private String createKey(String meterName, Tags tags) { | ||
if (meterName == null) { | ||
throw new RuntimeException("meter name is null"); | ||
} | ||
if (tags != null) { | ||
return String.join("_", meterName, tags.stream().map(Tag::getValue).collect(Collectors.joining("_"))); | ||
} else { | ||
return meterName; | ||
} | ||
} | ||
|
||
private Counter registerCounterMeter(String metricName, String description, Tags tags) { | ||
if (tags == null) { | ||
return Counter.builder(metricName) | ||
.description(description) | ||
.register(meterRegistry); | ||
} else { | ||
return Counter.builder(metricName) | ||
.description(description) | ||
.tags(tags) | ||
.register(meterRegistry); | ||
} | ||
} | ||
|
||
private Timer registerTimerMeter(String metricName, String description, Tags tags) { | ||
if (tags == null) { | ||
return Timer.builder(metricName) | ||
.description(description) | ||
.publishPercentiles(0.5, 0.95) | ||
.register(meterRegistry); | ||
} else { | ||
return Timer.builder(metricName) | ||
.description(description) | ||
.tags(tags) | ||
.publishPercentiles(0.5, 0.95) | ||
.register(meterRegistry); | ||
} | ||
} | ||
|
||
private Gauge registerGaugeMeter(String metricName, String desctiption, Tags tags) { | ||
GaugeValue gaugeValue = new GaugeValue(); | ||
Gauge gauge; | ||
if (tags == null) { | ||
gauge = Gauge.builder(metricName, gaugeValue::getValue) | ||
.description(desctiption) | ||
.register(meterRegistry); | ||
} else { | ||
gauge = Gauge.builder(metricName, gaugeValue::getValue) | ||
.description(desctiption) | ||
.tags(tags) | ||
.register(meterRegistry); | ||
} | ||
gaugeMeters.put(createKey(metricName, tags), gaugeValue); | ||
return gauge; | ||
} | ||
} |