EdColeman commented on code in PR #4459: URL: https://github.com/apache/accumulo/pull/4459#discussion_r1572925408
########## server/base/src/main/java/org/apache/accumulo/server/metrics/MetricsInfoImpl.java: ########## @@ -0,0 +1,340 @@ +/* + * 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 + * + * https://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.accumulo.server.metrics; + +import static org.apache.accumulo.core.conf.Property.GENERAL_ARBITRARY_PROP_PREFIX; +import static org.apache.accumulo.core.spi.metrics.MeterRegistryFactory.METRICS_PROP_SUBSTRING; +import static org.apache.hadoop.util.StringUtils.getTrimmedStrings; + +import java.lang.reflect.InvocationTargetException; +import java.time.Duration; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.locks.Lock; +import java.util.concurrent.locks.ReentrantLock; +import java.util.stream.Collectors; + +import org.apache.accumulo.core.classloader.ClassLoaderUtil; +import org.apache.accumulo.core.conf.AccumuloConfiguration; +import org.apache.accumulo.core.conf.Property; +import org.apache.accumulo.core.metrics.MetricsInfo; +import org.apache.accumulo.core.metrics.MetricsProducer; +import org.apache.accumulo.core.spi.common.ServiceEnvironment; +import org.apache.accumulo.core.util.HostAndPort; +import org.apache.accumulo.server.ServerContext; +import org.apache.accumulo.server.ServiceEnvironmentImpl; +import org.checkerframework.checker.nullness.qual.NonNull; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.annotations.VisibleForTesting; + +import io.micrometer.core.instrument.Meter; +import io.micrometer.core.instrument.MeterRegistry; +import io.micrometer.core.instrument.Metrics; +import io.micrometer.core.instrument.Tag; +import io.micrometer.core.instrument.binder.jvm.ClassLoaderMetrics; +import io.micrometer.core.instrument.binder.jvm.JvmGcMetrics; +import io.micrometer.core.instrument.binder.jvm.JvmMemoryMetrics; +import io.micrometer.core.instrument.binder.jvm.JvmThreadMetrics; +import io.micrometer.core.instrument.binder.system.ProcessorMetrics; +import io.micrometer.core.instrument.composite.CompositeMeterRegistry; +import io.micrometer.core.instrument.config.MeterFilter; +import io.micrometer.core.instrument.distribution.DistributionStatisticConfig; + +public class MetricsInfoImpl implements MetricsInfo { + + private static final Logger LOG = LoggerFactory.getLogger(MetricsInfoImpl.class); + + private final ServerContext context; + + private final Lock lock = new ReentrantLock(); + + private final Map<String,Tag> commonTags; + + // JvmGcMetrics are declared with AutoCloseable - keep reference to use with close() + private JvmGcMetrics jvmGcMetrics; + + private final boolean metricsEnabled; + + private CompositeMeterRegistry composite = null; + private final List<MeterRegistry> pendingRegistries = new ArrayList<>(); + + private final List<MetricsProducer> producers = new ArrayList<>(); + + public MetricsInfoImpl(final ServerContext context) { + this.context = context; + printMetricsConfig(); + metricsEnabled = context.getConfiguration().getBoolean(Property.GENERAL_MICROMETER_ENABLED); + commonTags = new HashMap<>(); + Tag t = MetricsInfo.instanceNameTag(context.getInstanceName()); + commonTags.put(t.getKey(), t); + } + + private void printMetricsConfig() { + final boolean micrometerEnabled = + context.getConfiguration().getBoolean(Property.GENERAL_MICROMETER_ENABLED); + final boolean jvmMetricsEnabled = + context.getConfiguration().getBoolean(Property.GENERAL_MICROMETER_JVM_METRICS_ENABLED); + LOG.info("micrometer metrics enabled: {}", micrometerEnabled); + if (jvmMetricsEnabled) { + if (micrometerEnabled) { + LOG.info("detailed jvm metrics enabled: {}", jvmMetricsEnabled); + } else { + LOG.info("requested jvm metrics, but micrometer metrics are disabled."); + } + } + if (micrometerEnabled) { + LOG.info("metrics registry factories: {}", + context.getConfiguration().get(Property.GENERAL_MICROMETER_FACTORY)); + } + } + + @Override + public boolean metricsEnabled() { + return metricsEnabled; + } + + /** + * Common tags for all services. + */ + @Override + public void addServiceTags(final String applicationName, final HostAndPort hostAndPort) { + List<Tag> tags = new ArrayList<>(); + + if (applicationName != null && !applicationName.isEmpty()) { + tags.add(MetricsInfo.processTag(applicationName)); + } + if (hostAndPort != null) { + tags.addAll(MetricsInfo.addressTags(hostAndPort)); + } + addCommonTags(tags); + } + + @Override + public void addCommonTags(List<Tag> updates) { + lock.lock(); + try { + if (composite != null) { + LOG.warn( + "Common tags after registry has been initialized may be ignored. Current common tags: {}", + commonTags); + return; + } + updates.forEach(t -> commonTags.put(t.getKey(), t)); + } finally { + lock.unlock(); + } + } + + @Override + public Collection<Tag> getCommonTags() { + lock.lock(); + try { + return Collections.unmodifiableCollection(commonTags.values()); + } finally { + lock.unlock(); + } + } + + @Override + public void addRegistry(MeterRegistry registry) { + lock.lock(); + try { + if (composite != null) { + composite.add(registry); + } else { + // defer until composite is initialized + pendingRegistries.add(registry); + } + + } finally { + lock.unlock(); + } + } + + @Override + public void addMetricsProducers(MetricsProducer... producer) { + lock.lock(); + try { + if (composite == null) { + producers.addAll(Arrays.asList(producer)); + } else { + Arrays.stream(producer).forEach(p -> p.registerMetrics(composite)); + } + } finally { + lock.unlock(); + } + } + + @Override + public MeterRegistry getRegistry() { + lock.lock(); + try { + if (composite == null) { + throw new IllegalStateException("metrics have not been initialized, call init() first"); + } + } finally { + lock.unlock(); + } + return composite; + } + + @Override + public void init() { Review Comment: That is a good observation and essentially correct. I'd need to think about a builder and maybe do it as a follow-on. I didn't consider adding a builder to server context and passing that around. The key thing is that common tags must be bound to the registry before creating / binding meters. ``` From the micrometer tag documentation: Common tags generally have to be added to the registry before any (possibly autoconfigured) meter binders. Depending on your environment, there are different ways to achieve this. ``` That's what prompted this whole set of changes - to gather the common tags (some of which are run-time dependent (i.e. port number)) and then apply them to the registry and then perform the registration of the producers - sealing the common tags with the init() call. -- 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]
