zentol commented on a change in pull request #19263: URL: https://github.com/apache/flink/pull/19263#discussion_r839398527
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/metrics/filter/DefaultMetricFilter.java ########## @@ -0,0 +1,131 @@ +/* + * 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.flink.runtime.metrics.filter; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.MetricOptions; +import org.apache.flink.metrics.Metric; +import org.apache.flink.metrics.MetricType; + +import java.util.Arrays; +import java.util.EnumSet; +import java.util.List; +import java.util.Locale; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Default {@link MetricFilter} implementation that filters metrics based on {@link + * MetricOptions#REPORTER_INCLUDES}/{@link MetricOptions#REPORTER_EXCLUDES}. + */ +public class DefaultMetricFilter implements MetricFilter { + + private static final EnumSet<MetricType> ALL_METRIC_TYPES = EnumSet.allOf(MetricType.class); + private static final EnumSet<MetricType> NO_METRIC_TYPES = EnumSet.noneOf(MetricType.class); + + private final List<FilterSpec> includes; + private final List<FilterSpec> excludes; + + private DefaultMetricFilter(List<FilterSpec> includes, List<FilterSpec> excludes) { + this.includes = includes; + this.excludes = excludes; + } + + @Override + public boolean filter(Metric metric, String name, String logicalScope) { + for (FilterSpec exclude : excludes) { + if (exclude.namePattern.matcher(name).matches() + && exclude.scopePattern.matcher(logicalScope).matches() + && exclude.types.contains(metric.getMetricType())) { + return false; + } + } + for (FilterSpec include : includes) { + if (include.namePattern.matcher(name).matches() + && include.scopePattern.matcher(logicalScope).matches() + && include.types.contains(metric.getMetricType())) { + return true; + } + } + return false; + } + + public static MetricFilter fromConfiguration(Configuration configuration) { + final List<String> includes = configuration.get(MetricOptions.REPORTER_INCLUDES); + final List<String> excludes = configuration.get(MetricOptions.REPORTER_EXCLUDES); + + final List<FilterSpec> includeFilters = + includes.stream().map(i -> parse(i)).collect(Collectors.toList()); + final List<FilterSpec> excludeFilters = + excludes.stream().map(e -> parse(e)).collect(Collectors.toList()); + + return new DefaultMetricFilter(includeFilters, excludeFilters); + } + + private static FilterSpec parse(String filter) { + final String[] split = filter.split(":"); + final Pattern scope = convertToPattern(split[0]); + final Pattern name = split.length > 1 ? convertToPattern(split[1]) : Pattern.compile(".+"); + final EnumSet<MetricType> type = + split.length > 2 ? parseMetricTypes(split[2]) : ALL_METRIC_TYPES; + + return new FilterSpec(scope, name, type); + } + + @VisibleForTesting + static Pattern convertToPattern(String scopeOrNameComponent) { + final String[] split = scopeOrNameComponent.split(";"); + + final String rawPattern = + Arrays.stream(split) + .map(s -> s.replaceAll("\\.", "\\.")) + .map(s -> s.replaceAll("\\*", ".+")) Review comment: It's not common, no. It isn't intuitive. I remember that I did intentionally switched to `+`, but that may have been due to an earlier iteration. I can't think of a reason to not use `*` now. ########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/metrics/filter/DefaultMetricFilter.java ########## @@ -0,0 +1,131 @@ +/* + * 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.flink.runtime.metrics.filter; + +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.configuration.MetricOptions; +import org.apache.flink.metrics.Metric; +import org.apache.flink.metrics.MetricType; + +import java.util.Arrays; +import java.util.EnumSet; +import java.util.List; +import java.util.Locale; +import java.util.regex.Pattern; +import java.util.stream.Collectors; + +/** + * Default {@link MetricFilter} implementation that filters metrics based on {@link + * MetricOptions#REPORTER_INCLUDES}/{@link MetricOptions#REPORTER_EXCLUDES}. + */ +public class DefaultMetricFilter implements MetricFilter { + + private static final EnumSet<MetricType> ALL_METRIC_TYPES = EnumSet.allOf(MetricType.class); + private static final EnumSet<MetricType> NO_METRIC_TYPES = EnumSet.noneOf(MetricType.class); + + private final List<FilterSpec> includes; + private final List<FilterSpec> excludes; + + private DefaultMetricFilter(List<FilterSpec> includes, List<FilterSpec> excludes) { + this.includes = includes; + this.excludes = excludes; + } + + @Override + public boolean filter(Metric metric, String name, String logicalScope) { + for (FilterSpec exclude : excludes) { + if (exclude.namePattern.matcher(name).matches() + && exclude.scopePattern.matcher(logicalScope).matches() + && exclude.types.contains(metric.getMetricType())) { + return false; + } + } + for (FilterSpec include : includes) { + if (include.namePattern.matcher(name).matches() + && include.scopePattern.matcher(logicalScope).matches() + && include.types.contains(metric.getMetricType())) { + return true; + } + } + return false; + } + + public static MetricFilter fromConfiguration(Configuration configuration) { + final List<String> includes = configuration.get(MetricOptions.REPORTER_INCLUDES); + final List<String> excludes = configuration.get(MetricOptions.REPORTER_EXCLUDES); + + final List<FilterSpec> includeFilters = + includes.stream().map(i -> parse(i)).collect(Collectors.toList()); + final List<FilterSpec> excludeFilters = + excludes.stream().map(e -> parse(e)).collect(Collectors.toList()); + + return new DefaultMetricFilter(includeFilters, excludeFilters); + } + + private static FilterSpec parse(String filter) { + final String[] split = filter.split(":"); + final Pattern scope = convertToPattern(split[0]); + final Pattern name = split.length > 1 ? convertToPattern(split[1]) : Pattern.compile(".+"); + final EnumSet<MetricType> type = + split.length > 2 ? parseMetricTypes(split[2]) : ALL_METRIC_TYPES; + + return new FilterSpec(scope, name, type); + } + + @VisibleForTesting + static Pattern convertToPattern(String scopeOrNameComponent) { + final String[] split = scopeOrNameComponent.split(";"); + + final String rawPattern = + Arrays.stream(split) + .map(s -> s.replaceAll("\\.", "\\.")) + .map(s -> s.replaceAll("\\*", ".+")) Review comment: It's not common, no. It isn't intuitive. I remember that I did intentionally switch to `+`, but that may have been due to an earlier iteration. I can't think of a reason to not use `*` now. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
