Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature:Configurable summarySetBlackList to set monitoring blacklist. #2165

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
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;
Expand All @@ -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";
Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 java.util.ArrayList;
import java.util.List;
import org.apache.zookeeper.metrics.MetricsProvider;

/**
* Base implementation of {@link MetricsProvider}.<br>
* Provide a default implementation that currently includes the summarySet blacklist feature.
*/
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use English, instead of Chinese

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👌

public class BaseMetricsProvider {
public static final String summarySetBlackList = "zookeeper.summarySetBlackList";

public static final List<String> getSummarySetBlackList() {
String summarySetBlackListConfig = System.getProperty(summarySetBlackList);
List<String> blackList = new ArrayList<>();
if (summarySetBlackListConfig != null) {
String[] list = summarySetBlackListConfig.split(",");
for (String name : list) {
if (!name.trim().isEmpty()) {
blackList.add(name.trim());
}
}
}
return blackList;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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 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;

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<String> 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());
}

}
Loading