From 2637b19714b8bd6b2c5894c525aade5121552d6e Mon Sep 17 00:00:00 2001 From: ganzichen Date: Wed, 8 May 2024 17:14:39 +0800 Subject: [PATCH 1/4] =?UTF-8?q?feature=EF=BC=9AConfigurable=20summarySetBl?= =?UTF-8?q?ackList=20to=20set=20monitoring=20blacklist.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../prometheus/PrometheusMetricsProvider.java | 7 ++- .../metrics/impl/BaseMetricsProvider.java | 47 ++++++++++++++++++ .../metrics/impl/DefaultMetricsProvider.java | 5 +- .../metrics/impl/NullMetricsProvider.java | 4 +- .../metric/SummarySetBlackListTest.java | 48 +++++++++++++++++++ 5 files changed, 107 insertions(+), 4 deletions(-) create mode 100644 zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java create mode 100644 zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/SummarySetBlackListTest.java diff --git a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java index af4bff9d218..cfefccd9e50 100644 --- a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java +++ b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java @@ -54,6 +54,8 @@ import org.apache.zookeeper.server.RateLogger; import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; +import org.apache.zookeeper.metrics.impl.BaseMetricsProvider; +import org.apache.zookeeper.metrics.impl.NullMetricsProvider; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder; @@ -66,7 +68,7 @@ * * @since 3.6.0 */ -public class PrometheusMetricsProvider implements MetricsProvider { +public class PrometheusMetricsProvider extends BaseMetricsProvider implements MetricsProvider { private static final Logger LOG = LoggerFactory.getLogger(PrometheusMetricsProvider.class); private static final String LABEL = "key"; @@ -348,6 +350,9 @@ public Summary getSummary(String name, DetailLevel detailLevel) { @Override public SummarySet getSummarySet(String name, DetailLevel detailLevel) { + if (getSummarySetBlackList().contains(name)) { + return new NullMetricsProvider.NullSummarySet(); + } if (detailLevel == DetailLevel.BASIC) { return basicSummarySets.computeIfAbsent(name, (n) -> { if (summarySets.containsKey(n)) { diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java new file mode 100644 index 00000000000..fae8a46d2d6 --- /dev/null +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java @@ -0,0 +1,47 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.metrics.impl; + +import org.apache.zookeeper.metrics.MetricsProvider; + +import java.util.ArrayList; +import java.util.List; + +/** + * Base implementation of {@link MetricsProvider}.
+ * 提供默认的实现,当前包含summarySet黑名单功能。 + */ +public class BaseMetricsProvider { + public static String summarySetBlackList = "zookeeper.summarySetBlackList"; + + public static final List getSummarySetBlackList() { + String summarySetBlackListConfig = System.getProperty(summarySetBlackList); + List blackList = new ArrayList<>(); + if (summarySetBlackListConfig != null) { + String[] list = summarySetBlackListConfig.split(","); + for (String name : list) { + if (!name.trim().isEmpty()) { + blackList.add(name.trim()); + } + } + } + return blackList; + } + +} diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/DefaultMetricsProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/DefaultMetricsProvider.java index 634ec25d30f..c140f558b20 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/DefaultMetricsProvider.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/DefaultMetricsProvider.java @@ -46,7 +46,7 @@ * It is mostly useful to make the legacy 4 letter words interface work as * expected. */ -public class DefaultMetricsProvider implements MetricsProvider { +public class DefaultMetricsProvider extends BaseMetricsProvider implements MetricsProvider { private final DefaultMetricsContext rootMetricsContext = new DefaultMetricsContext(); @@ -155,6 +155,9 @@ public Summary getSummary(String name, DetailLevel detailLevel) { @Override public SummarySet getSummarySet(String name, DetailLevel detailLevel) { + if (getSummarySetBlackList().contains(name)) { + return new NullMetricsProvider.NullSummarySet(); + } if (detailLevel == DetailLevel.BASIC) { return basicSummarySets.computeIfAbsent(name, (n) -> { if (summarySets.containsKey(n)) { diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/NullMetricsProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/NullMetricsProvider.java index 8d07e91865d..5a0978b24c4 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/NullMetricsProvider.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/NullMetricsProvider.java @@ -36,7 +36,7 @@ public class NullMetricsProvider implements MetricsProvider { /** - * Instance of NullMetricsProvider useful for tests. + * Instance of NullMetricsProvider useful for tests and balck list. */ public static final MetricsProvider INSTANCE = new NullMetricsProvider(); @@ -146,7 +146,7 @@ public void add(long value) { } - private static final class NullSummarySet implements SummarySet { + public static final class NullSummarySet implements SummarySet { private static final NullSummarySet INSTANCE = new NullSummarySet(); diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/SummarySetBlackListTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/SummarySetBlackListTest.java new file mode 100644 index 00000000000..027dc50af42 --- /dev/null +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/SummarySetBlackListTest.java @@ -0,0 +1,48 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.zookeeper.server.metric; + +import org.apache.zookeeper.metrics.impl.BaseMetricsProvider; +import org.apache.zookeeper.metrics.impl.DefaultMetricsProvider; +import org.apache.zookeeper.server.ServerMetrics; +import org.junit.Test; + +import java.util.HashSet; +import java.util.Set; + +import static org.junit.Assert.assertTrue; + +public class SummarySetBlackListTest { + + @Test + public void testMetrics() { + System.setProperty(BaseMetricsProvider.summarySetBlackList, "write_per_namespace,read_per_namespace"); + ServerMetrics.metricsProviderInitialized(new DefaultMetricsProvider()); + ServerMetrics.getMetrics().WRITE_PER_NAMESPACE.add("testNamespace", 1); + ServerMetrics.getMetrics().READ_PER_NAMESPACE.add("testNamespace", 1); + + Set metricNames = new HashSet<>(); + ServerMetrics.getMetrics().getMetricsProvider().dump((k, v) -> metricNames.add(k)); + assertTrue(!metricNames.contains("cnt_testNamespace_write_per_namespace")); + assertTrue(!metricNames.contains("cnt_testNamespace_read_per_namespace")); + System.clearProperty(BaseMetricsProvider.summarySetBlackList); + ServerMetrics.metricsProviderInitialized(new DefaultMetricsProvider()); + } + +} From c2eee75713931442754ee1e8a4f5d85461c13f80 Mon Sep 17 00:00:00 2001 From: ganzichen Date: Thu, 16 May 2024 15:19:14 +0800 Subject: [PATCH 2/4] fix:MS_SHOULD_BE_FINAL --- .../org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java index fae8a46d2d6..c9247fee5ff 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java @@ -28,7 +28,7 @@ * 提供默认的实现,当前包含summarySet黑名单功能。 */ public class BaseMetricsProvider { - public static String summarySetBlackList = "zookeeper.summarySetBlackList"; + public static final String summarySetBlackList = "zookeeper.summarySetBlackList"; public static final List getSummarySetBlackList() { String summarySetBlackListConfig = System.getProperty(summarySetBlackList); From 4eb5e87e1650431741f547b8a43866664769ca3e Mon Sep 17 00:00:00 2001 From: ganzichen Date: Mon, 20 May 2024 11:50:56 +0800 Subject: [PATCH 3/4] fix:checkstyle --- .../zookeeper/metrics/impl/BaseMetricsProvider.java | 5 ++--- .../zookeeper/server/metric/SummarySetBlackListTest.java | 8 +++----- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java index c9247fee5ff..2eb43027b3d 100644 --- a/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java +++ b/zookeeper-server/src/main/java/org/apache/zookeeper/metrics/impl/BaseMetricsProvider.java @@ -18,14 +18,13 @@ package org.apache.zookeeper.metrics.impl; -import org.apache.zookeeper.metrics.MetricsProvider; - import java.util.ArrayList; import java.util.List; +import org.apache.zookeeper.metrics.MetricsProvider; /** * Base implementation of {@link MetricsProvider}.
- * 提供默认的实现,当前包含summarySet黑名单功能。 + * Provide a default implementation that currently includes the summarySet blacklist feature. */ public class BaseMetricsProvider { public static final String summarySetBlackList = "zookeeper.summarySetBlackList"; diff --git a/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/SummarySetBlackListTest.java b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/SummarySetBlackListTest.java index 027dc50af42..cf460044edd 100644 --- a/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/SummarySetBlackListTest.java +++ b/zookeeper-server/src/test/java/org/apache/zookeeper/server/metric/SummarySetBlackListTest.java @@ -18,16 +18,14 @@ package org.apache.zookeeper.server.metric; +import static org.junit.Assert.assertTrue; +import java.util.HashSet; +import java.util.Set; import org.apache.zookeeper.metrics.impl.BaseMetricsProvider; import org.apache.zookeeper.metrics.impl.DefaultMetricsProvider; import org.apache.zookeeper.server.ServerMetrics; import org.junit.Test; -import java.util.HashSet; -import java.util.Set; - -import static org.junit.Assert.assertTrue; - public class SummarySetBlackListTest { @Test From 1cd5f2b2a82263e1df80a1d19acbec72083aaaba Mon Sep 17 00:00:00 2001 From: ganzichen Date: Mon, 20 May 2024 14:15:38 +0800 Subject: [PATCH 4/4] fix:checkstyle zookeeper-metrics-providers --- .../metrics/prometheus/PrometheusMetricsProvider.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java index cfefccd9e50..d8424d6cbba 100644 --- a/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java +++ b/zookeeper-metrics-providers/zookeeper-prometheus-metrics/src/main/java/org/apache/zookeeper/metrics/prometheus/PrometheusMetricsProvider.java @@ -51,11 +51,11 @@ import org.apache.zookeeper.metrics.MetricsProviderLifeCycleException; import org.apache.zookeeper.metrics.Summary; import org.apache.zookeeper.metrics.SummarySet; +import org.apache.zookeeper.metrics.impl.BaseMetricsProvider; +import org.apache.zookeeper.metrics.impl.NullMetricsProvider; import org.apache.zookeeper.server.RateLogger; import org.eclipse.jetty.security.ConstraintMapping; import org.eclipse.jetty.security.ConstraintSecurityHandler; -import org.apache.zookeeper.metrics.impl.BaseMetricsProvider; -import org.apache.zookeeper.metrics.impl.NullMetricsProvider; import org.eclipse.jetty.server.Server; import org.eclipse.jetty.servlet.ServletContextHandler; import org.eclipse.jetty.servlet.ServletHolder;