Skip to content

Commit 359aa5e

Browse files
committed
Replace category-based filtering with regex-based metric filtering
Replace the fixed category enum approach with a flexible regex-based metric filter. Users can now specify include/exclude patterns via metric-filter.includes and metric-filter.excludes properties to freely configure which metrics are exported. Signed-off-by: Jiwon Park <jpark92@outlook.kr>
1 parent e7432db commit 359aa5e

File tree

10 files changed

+220
-304
lines changed

10 files changed

+220
-304
lines changed

amoro-metrics/amoro-metrics-prometheus/src/main/java/org/apache/amoro/metrics/promethues/MetricCategory.java

Lines changed: 0 additions & 84 deletions
This file was deleted.
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
package org.apache.amoro.metrics.promethues;
20+
21+
import java.util.Map;
22+
import java.util.regex.Pattern;
23+
24+
/** Regex-based metric filter for Prometheus exporter. */
25+
public class MetricFilter {
26+
27+
public static final String INCLUDES_KEY = "metric-filter.includes";
28+
public static final String EXCLUDES_KEY = "metric-filter.excludes";
29+
30+
public static final MetricFilter ACCEPT_ALL = new MetricFilter(null, null);
31+
32+
private final Pattern includePattern;
33+
private final Pattern excludePattern;
34+
35+
public MetricFilter(Pattern includePattern, Pattern excludePattern) {
36+
this.includePattern = includePattern;
37+
this.excludePattern = excludePattern;
38+
}
39+
40+
/** Parse metric filter from reporter properties. */
41+
public static MetricFilter fromProperties(Map<String, String> properties) {
42+
String includes = properties.get(INCLUDES_KEY);
43+
String excludes = properties.get(EXCLUDES_KEY);
44+
45+
if (includes == null && excludes == null) {
46+
return ACCEPT_ALL;
47+
}
48+
49+
Pattern includePattern = includes != null ? Pattern.compile(includes) : null;
50+
Pattern excludePattern = excludes != null ? Pattern.compile(excludes) : null;
51+
return new MetricFilter(includePattern, excludePattern);
52+
}
53+
54+
/** Check if a metric name passes the filter. */
55+
public boolean matches(String metricName) {
56+
if (includePattern != null && !includePattern.matcher(metricName).matches()) {
57+
return false;
58+
}
59+
if (excludePattern != null && excludePattern.matcher(metricName).matches()) {
60+
return false;
61+
}
62+
return true;
63+
}
64+
}

amoro-metrics/amoro-metrics-prometheus/src/main/java/org/apache/amoro/metrics/promethues/MetricsCollector.java

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
import org.slf4j.Logger;
3131
import org.slf4j.LoggerFactory;
3232

33-
import java.util.Collections;
3433
import java.util.List;
3534
import java.util.Map;
36-
import java.util.Set;
3735
import java.util.function.Function;
3836
import java.util.regex.Pattern;
3937
import java.util.stream.Collectors;
@@ -45,16 +43,15 @@ public class MetricsCollector extends Collector {
4543
private static final Pattern NAME_PATTERN = Pattern.compile("[a-zA-Z_:][a-zA-Z0-9_:]*");
4644
private static final Pattern LABEL_PATTERN = Pattern.compile("[a-zA-Z_][a-zA-Z0-9_]*");
4745
MetricSet metrics;
48-
private final Set<MetricCategory> disabledCategories;
46+
private final MetricFilter metricFilter;
4947

5048
public MetricsCollector(MetricSet metrics) {
51-
this(metrics, Collections.emptySet());
49+
this(metrics, MetricFilter.ACCEPT_ALL);
5250
}
5351

54-
public MetricsCollector(MetricSet metrics, Set<MetricCategory> disabledCategories) {
52+
public MetricsCollector(MetricSet metrics, MetricFilter metricFilter) {
5553
this.metrics = metrics;
56-
this.disabledCategories =
57-
disabledCategories != null ? disabledCategories : Collections.emptySet();
54+
this.metricFilter = metricFilter != null ? metricFilter : MetricFilter.ACCEPT_ALL;
5855
}
5956

6057
@Override
@@ -88,11 +85,8 @@ private boolean isValidMetric(MetricDefine define) {
8885
return false;
8986
}
9087

91-
if (!disabledCategories.isEmpty()) {
92-
MetricCategory category = MetricCategory.findCategory(define.getName());
93-
if (category != null && disabledCategories.contains(category)) {
94-
return false;
95-
}
88+
if (!metricFilter.matches(define.getName())) {
89+
return false;
9690
}
9791

9892
return true;

amoro-metrics/amoro-metrics-prometheus/src/main/java/org/apache/amoro/metrics/promethues/PrometheusMetricsReporter.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,16 @@
2323
import org.apache.amoro.metrics.MetricSet;
2424

2525
import java.io.IOException;
26-
import java.util.Collections;
2726
import java.util.Map;
2827
import java.util.Optional;
29-
import java.util.Set;
3028

3129
/** Prometheus exporter */
3230
public class PrometheusMetricsReporter implements MetricReporter {
3331

3432
public static final String PORT = "port";
3533

3634
private HTTPServer server;
37-
private Set<MetricCategory> disabledCategories = Collections.emptySet();
35+
private MetricFilter metricFilter = MetricFilter.ACCEPT_ALL;
3836

3937
@Override
4038
public void open(Map<String, String> properties) {
@@ -43,7 +41,7 @@ public void open(Map<String, String> properties) {
4341
.map(Integer::valueOf)
4442
.orElseThrow(() -> new IllegalArgumentException("Lack required property: " + PORT));
4543

46-
this.disabledCategories = MetricCategory.parseDisabledCategories(properties);
44+
this.metricFilter = MetricFilter.fromProperties(properties);
4745

4846
try {
4947
this.server = new HTTPServer(port);
@@ -64,7 +62,7 @@ public String name() {
6462

6563
@Override
6664
public void setGlobalMetricSet(MetricSet globalMetricSet) {
67-
MetricsCollector collector = new MetricsCollector(globalMetricSet, disabledCategories);
65+
MetricsCollector collector = new MetricsCollector(globalMetricSet, metricFilter);
6866
collector.register();
6967
}
7068
}

amoro-metrics/amoro-metrics-prometheus/src/test/java/org/apache/amoro/metrics/promethues/MetricCategoryTest.java

Lines changed: 0 additions & 136 deletions
This file was deleted.

0 commit comments

Comments
 (0)