Wrap MetricRegistry to handle exclusion rules
This change fixes the same issue for this plugin like
Iaf0b4fa03293ccc2313ab0332f70a3a41ea30a25 for the metrics-reporter-jmx
plugin.
Since exporting metrics to prometheus works differently from exporting
metrics to JXM (collect vs listen) we need a different way of filtering
here than in the referenced change for the other plugin.
Change-Id: I55a441fa30b0fd8739d557ae24571de69d91ec99
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/FilteredMetricRegistry.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/FilteredMetricRegistry.java
new file mode 100644
index 0000000..87cdcbd
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/FilteredMetricRegistry.java
@@ -0,0 +1,61 @@
+// Copyright (C) 2020 The Android Open Source Project
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package com.googlesource.gerrit.plugins.metricsreporterprometheus;
+
+import com.codahale.metrics.Counter;
+import com.codahale.metrics.Gauge;
+import com.codahale.metrics.Histogram;
+import com.codahale.metrics.Meter;
+import com.codahale.metrics.MetricFilter;
+import com.codahale.metrics.MetricRegistry;
+import com.codahale.metrics.Timer;
+import java.util.SortedMap;
+import java.util.function.Predicate;
+
+public class FilteredMetricRegistry extends MetricRegistry {
+ private final MetricRegistry registry;
+ private final MetricFilter nonExcluded;
+
+ FilteredMetricRegistry(MetricRegistry registry, Predicate<String> exclusionFilter) {
+ this.registry = registry;
+ this.nonExcluded = (n, m) -> !exclusionFilter.test(n);
+ }
+
+ @Override
+ @SuppressWarnings("rawtypes")
+ public SortedMap<String, Gauge> getGauges() {
+ return registry.getGauges(nonExcluded);
+ }
+
+ @Override
+ public SortedMap<String, Counter> getCounters() {
+ return registry.getCounters(nonExcluded);
+ }
+
+ @Override
+ public SortedMap<String, Histogram> getHistograms() {
+ return registry.getHistograms(nonExcluded);
+ }
+
+ @Override
+ public SortedMap<String, Timer> getTimers() {
+ return registry.getTimers(nonExcluded);
+ }
+
+ @Override
+ public SortedMap<String, Meter> getMeters() {
+ return registry.getMeters(nonExcluded);
+ }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusExporter.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusExporter.java
index 728acb3..31b6152 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusExporter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusExporter.java
@@ -13,8 +13,6 @@
// limitations under the License.
package com.googlesource.gerrit.plugins.metricsreporterprometheus;
-import com.codahale.metrics.Metric;
-import com.codahale.metrics.MetricFilter;
import com.codahale.metrics.MetricRegistry;
import com.google.common.base.Strings;
import com.google.common.net.HttpHeaders;
@@ -55,24 +53,13 @@
this.prometheusBearerToken =
cfgFactory.getFromGerritConfig(pluginName).getString(PROMETHEUS_BEARER_TOKEN);
- /* Copy the registry to avoid filtering the global one */
- MetricRegistry filteredRegistry = new MetricRegistry();
- filteredRegistry.registerAll(registry);
-
Set<String> excludedMetrics = new HashSet<>();
excludedMetrics.addAll(
Arrays.asList(cfgFactory.getFromGerritConfig(pluginName).getStringList(EXCLUDE_KEY)));
- excludedMetrics.forEach(
- exclude -> {
- filteredRegistry.removeMatching(
- new MetricFilter() {
- @Override
- public boolean matches(String name, Metric metric) {
- return name.matches(exclude);
- }
- });
- });
+ FilteredMetricRegistry filteredRegistry =
+ new FilteredMetricRegistry(
+ registry, s -> excludedMetrics.stream().anyMatch(e -> s.matches(e)));
// Hook the Dropwizard registry into the Prometheus registry
// via the DropwizardExports collector.