EdColeman commented on code in PR #4459: URL: https://github.com/apache/accumulo/pull/4459#discussion_r1572413421
########## 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: The pattern is common, but in `TimedProcessor` there is an example where a producer is added, but init() is not directly called. With `TServerUtils` it was necessary to allow producers to be added, but init() deferred until service start. I kept the methods separate because it is not required that they are always called back-to-back. There could be an `confgAndInit()` method that would collapse the pattern, but addProducers() would still be necessary and seemed it could be more confusing to understand when to use one or the other, and the discrete methods made it more explicit, -- 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]
