Merge branch 'stable-2.16'
* stable-2.16:
Precompile exclusion patterns for better performance
Improve documentation of the metrics.exclude parameter
Wrap MetricRegistry to handle exclusion rules
Rename package to metricsreporterprometheus
Change-Id: I449f9df001ab2035b51ba540da36c241dcc3e370
diff --git a/BUILD b/BUILD
index 89cb6c3..ee7c4fb 100644
--- a/BUILD
+++ b/BUILD
@@ -5,8 +5,8 @@
srcs = glob(["src/main/java/**/*.java"]),
manifest_entries = [
"Gerrit-PluginName: metrics-reporter-prometheus",
- "Gerrit-Module: com.googlesource.gerrit.plugins.metricsreporters.GerritPrometheusModule",
- "Gerrit-HttpModule: com.googlesource.gerrit.plugins.metricsreporters.GerritPrometheusHttpModule",
+ "Gerrit-Module: com.googlesource.gerrit.plugins.metricsreporterprometheus.GerritPrometheusModule",
+ "Gerrit-HttpModule: com.googlesource.gerrit.plugins.metricsreporterprometheus.GerritPrometheusHttpModule",
],
resources = glob(["src/main/resources/**/*"]),
deps = [
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/CapabilityChecker.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/CapabilityChecker.java
similarity index 96%
rename from src/main/java/com/googlesource/gerrit/plugins/metricsreporters/CapabilityChecker.java
rename to src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/CapabilityChecker.java
index 603e7cc..f1b936e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/CapabilityChecker.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/CapabilityChecker.java
@@ -11,7 +11,7 @@
// 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.metricsreporters;
+package com.googlesource.gerrit.plugins.metricsreporterprometheus;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.extensions.annotations.PluginName;
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/metricsreporters/GerritPrometheusExporter.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusExporter.java
similarity index 78%
rename from src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritPrometheusExporter.java
rename to src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusExporter.java
index 7387838..b7c96cc 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritPrometheusExporter.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusExporter.java
@@ -11,10 +11,10 @@
// 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.metricsreporters;
+package com.googlesource.gerrit.plugins.metricsreporterprometheus;
-import com.codahale.metrics.Metric;
-import com.codahale.metrics.MetricFilter;
+import static java.util.stream.Collectors.toList;
+
import com.codahale.metrics.MetricRegistry;
import com.google.common.base.Strings;
import com.google.common.net.HttpHeaders;
@@ -27,9 +27,9 @@
import io.prometheus.client.exporter.MetricsServlet;
import java.io.IOException;
import java.util.Arrays;
-import java.util.HashSet;
+import java.util.List;
import java.util.Optional;
-import java.util.Set;
+import java.util.regex.Pattern;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
@@ -55,24 +55,14 @@
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);
+ List<Pattern> excludes =
+ Arrays.stream(cfgFactory.getFromGerritConfig(pluginName).getStringList(EXCLUDE_KEY))
+ .map(Pattern::compile)
+ .collect(toList());
- 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 -> excludes.stream().anyMatch(e -> e.matcher(s).matches()));
// Hook the Dropwizard registry into the Prometheus registry
// via the DropwizardExports collector.
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritPrometheusHttpModule.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusHttpModule.java
similarity index 92%
rename from src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritPrometheusHttpModule.java
rename to src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusHttpModule.java
index 5764c38..0a2a625 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritPrometheusHttpModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusHttpModule.java
@@ -11,7 +11,7 @@
// 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.metricsreporters;
+package com.googlesource.gerrit.plugins.metricsreporterprometheus;
import com.google.inject.servlet.ServletModule;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritPrometheusModule.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusModule.java
similarity index 93%
rename from src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritPrometheusModule.java
rename to src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusModule.java
index 36c8663..591e81e 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/GerritPrometheusModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/GerritPrometheusModule.java
@@ -11,7 +11,7 @@
// 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.metricsreporters;
+package com.googlesource.gerrit.plugins.metricsreporterprometheus;
import com.google.gerrit.extensions.annotations.Exports;
import com.google.gerrit.extensions.config.CapabilityDefinition;
diff --git a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/ViewMetricsCapability.java b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/ViewMetricsCapability.java
similarity index 92%
rename from src/main/java/com/googlesource/gerrit/plugins/metricsreporters/ViewMetricsCapability.java
rename to src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/ViewMetricsCapability.java
index c3d22e0..8bb5157 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/metricsreporters/ViewMetricsCapability.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/metricsreporterprometheus/ViewMetricsCapability.java
@@ -11,7 +11,7 @@
// 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.metricsreporters;
+package com.googlesource.gerrit.plugins.metricsreporterprometheus;
import com.google.gerrit.extensions.config.CapabilityDefinition;
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index f45f16f..070151f 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -25,11 +25,12 @@
for how to configure the integration with Prometheus.
plugin.@PLUGIN@.excludeMetrics
-: String used to exclude metrics from the report. It can be specified multiple times.
- Parsed as regular expression. Note, ^ and $ are automatically added around the string.
+: Regex pattern used to exclude metrics from the report. It can be specified multiple times.
+ Note that pattern matching is done on the whole metric name, not only on a part of it.
By default no metric is excluded.
For example, to exclude all cache metrics, use: `excludeMetrics = cache.*`
+
[Back to @PLUGIN@ documentation index][index]
[index]: index.html