1u0 commented on a change in pull request #9870: [FLINK-14350][metrics] Introduce dedicated MetricScope URL: https://github.com/apache/flink/pull/9870#discussion_r333874262
########## File path: flink-runtime/src/main/java/org/apache/flink/runtime/metrics/scope/InternalMetricScope.java ########## @@ -0,0 +1,89 @@ +package org.apache.flink.runtime.metrics.scope; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.metrics.CharacterFilter; +import org.apache.flink.metrics.MetricScope; +import org.apache.flink.runtime.metrics.DelimiterProvider; + +import java.util.Map; +import java.util.function.Supplier; + +/** + * Default scope implementation. Contains additional methods assembling identifiers based on reporter-specific delimiters. + */ +@Internal +public class InternalMetricScope implements MetricScope { + + private final DelimiterProvider delimiterProvider; + private final Supplier<Map<String, String>> variablesProvider; + + /** + * The map containing all variables and their associated values, lazily computed. + */ + protected volatile Map<String, String> variables; + + /** + * The metrics scope represented by this group. + * For example ["host-7", "taskmanager-2", "window_word_count", "my-mapper" ]. + */ + private final String[] scopeComponents; + + /** + * Array containing the metrics scope represented by this group for each reporter, as a concatenated string, lazily computed. + * For example: "host-7.taskmanager-2.window_word_count.my-mapper" + */ + private final String[] scopeStrings; + + public InternalMetricScope(DelimiterProvider delimiterProvider, String[] scopeComponents, Supplier<Map<String, String>> variablesProvider) { + this.delimiterProvider = delimiterProvider; + this.variablesProvider = variablesProvider; + this.scopeComponents = scopeComponents; + this.scopeStrings = new String[delimiterProvider.getNumberReporters()]; + } + + @Override + public Map<String, String> getAllVariables() { + if (variables == null) { // avoid synchronization for common case + synchronized (this) { + if (variables == null) { + variables = variablesProvider.get(); + } + } + } + return variables; + } + + public String[] geScopeComponents() { + return scopeStrings; + } Review comment: Also, you are returning `scopeStrings` here instead of `scopeComponents` (I hope this would be revealed by tests, once CI reaches them). Side note: I think a better name for `scopeStrings` would be something like `cachedIdentifierScope`. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services