Skip to content

Commit 05ed36a

Browse files
[feature] add metrics tag filter
Add filters for purpose metric.filter.include[i].name=http_server_requests_seconds metric.filter.include[i].tags.uri=/auth/login metric.filter.include[i].tags.method=GET
1 parent fcb1f62 commit 05ed36a

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.tosan.http.server.starter.metrics;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* @author Elahe Hajizade
8+
* @since 25/12/2025
9+
*/
10+
public class FilterRule {
11+
private String name;
12+
private Map<String, String> tags = new HashMap<>();
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public Map<String, String> getTags() {
23+
return tags;
24+
}
25+
26+
public void setTags(Map<String, String> tags) {
27+
this.tags = tags;
28+
}
29+
}

tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/MeterFilterConfig.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.springframework.boot.context.properties.ConfigurationProperties;
44
import org.springframework.validation.annotation.Validated;
55

6+
import java.util.ArrayList;
7+
import java.util.List;
68
import java.util.Map;
79

810
/**
@@ -15,6 +17,7 @@ public class MeterFilterConfig {
1517

1618
private String[] excludedMeterNames;
1719
private Map<String, String> excludedMeterTags;
20+
private List<FilterRule> include = new ArrayList<>();
1821

1922
public String[] getExcludedMeterNames() {
2023
return excludedMeterNames;
@@ -31,4 +34,12 @@ public Map<String, String> getExcludedMeterTags() {
3134
public void setExcludedMeterTags(Map<String, String> excludedMeterTags) {
3235
this.excludedMeterTags = excludedMeterTags;
3336
}
37+
38+
public List<FilterRule> getInclude() {
39+
return include;
40+
}
41+
42+
public void setInclude(List<FilterRule> include) {
43+
this.include = include;
44+
}
3445
}

tosan-httpserver-spring-boot-starter/src/main/java/com/tosan/http/server/starter/metrics/MetricFilter.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,21 @@ public MetricFilter(MeterFilterConfig meterFilterConfig) {
1717

1818
@Override
1919
public MeterFilterReply accept(Meter.Id id) {
20+
21+
if (meterFilterConfig.getInclude() != null) {
22+
for (FilterRule rule : meterFilterConfig.getInclude()) {
23+
24+
if (!id.getName().equals(rule.getName())) continue;
25+
26+
boolean tagsMatch = rule.getTags().entrySet().stream()
27+
.allMatch(e -> id.getTags().contains(Tag.of(e.getKey(), e.getValue())));
28+
29+
if (tagsMatch) {
30+
return MeterFilterReply.ACCEPT;
31+
}
32+
}
33+
}
34+
2035
if (meterFilterConfig.getExcludedMeterNames() != null && Arrays.asList(meterFilterConfig.getExcludedMeterNames()).contains(id.getName())) {
2136
return MeterFilterReply.DENY;
2237
}

0 commit comments

Comments
 (0)