dlmarion commented on code in PR #4459:
URL: https://github.com/apache/accumulo/pull/4459#discussion_r1572290061


##########
server/base/src/main/java/org/apache/accumulo/server/metrics/MeterRegistryEnvImpl.java:
##########
@@ -0,0 +1,55 @@
+/*
+ * 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.spi.metrics.MeterRegistryFactory.METRICS_PROP_SUBSTRING;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.accumulo.core.spi.common.ServiceEnvironment;
+import org.apache.accumulo.core.spi.metrics.MeterRegistryFactory;
+import org.apache.accumulo.server.ServerContext;
+import org.apache.accumulo.server.ServiceEnvironmentImpl;
+
+public class MeterRegistryEnvImpl implements 
MeterRegistryFactory.InitParameters {
+
+  private final ServerContext context;
+
+  public MeterRegistryEnvImpl(final ServerContext context) {
+    this.context = context;
+  }
+
+  @Override
+  public Map<String,String> getOptions() {
+    Map<String,String> options = new HashMap<>();
+    options.forEach((k, v) -> {

Review Comment:
   Did you intend to iterate over something else, the configuration?  
MeterRegistryEnvImplTest exists, but doesn't test anything. Suggest unit 
testing this class.



##########
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:
   It appears there is a common pattern being used by the server processes:
   
   ```
   addServiceTags();
   addMetricsProducers();
   init()
   ```
   
   I'm wondering if this could be collapsed to one method that initializes the 
MetricsInfo object and makes it ready for use.



##########
server/base/src/test/java/org/apache/accumulo/server/metrics/MetricsInfoImplTest.java:
##########
@@ -0,0 +1,101 @@
+/*
+ * 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.easymock.EasyMock.anyObject;
+import static org.easymock.EasyMock.expect;
+import static org.easymock.EasyMock.mock;
+import static org.easymock.EasyMock.replay;
+import static org.easymock.EasyMock.verify;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import java.util.Map;
+
+import org.apache.accumulo.core.conf.AccumuloConfiguration;
+import org.apache.accumulo.core.spi.common.ServiceEnvironment;
+import org.apache.accumulo.core.spi.metrics.MeterRegistryFactory;
+import org.apache.accumulo.server.ServerContext;
+import org.junit.jupiter.api.Test;
+
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+
+public class MetricsInfoImplTest {
+  @Test
+  public void factoryTest() throws Exception {
+
+    ServerContext context = mock(ServerContext.class);
+    AccumuloConfiguration conf = mock(AccumuloConfiguration.class);
+    expect(context.getConfiguration()).andReturn(conf).anyTimes();
+    
expect(conf.getAllPropertiesWithPrefixStripped(anyObject())).andReturn(Map.of()).anyTimes();
+    expect(conf.newDeriver(anyObject())).andReturn(Map::of).anyTimes();
+    replay(context, conf);
+    
assertNotNull(MetricsInfoImpl.getRegistryFromFactory(SPIFactory.class.getName(),
 context));
+
+    assertNotNull(
+        
MetricsInfoImpl.getRegistryFromFactory(DeprecatedFactory.class.getName(), 
context));
+
+    assertThrows(ClassNotFoundException.class,
+        () -> MetricsInfoImpl.getRegistryFromFactory(String.class.getName(), 
context));
+
+    verify(context, conf);
+  }
+
+  MeterRegistryFactory.InitParameters emptyParameters = new 
MeterRegistryFactory.InitParameters() {
+
+    private final ServiceEnvironment senv = null; // new 
ServiceEnvironmentImpl(context);

Review Comment:
   remove commented code?



##########
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);

Review Comment:
   If you move this before `printMetricsConfig`, then you can use this variable 
inside of `printMetricsConfig`. As it stands now, it's getting the same 
property twice



##########
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() {
+    lock.lock();
+    try {
+      if (composite != null) {
+        LOG.warn("metrics registry has already been initialized");
+        return;
+      }
+      composite = new CompositeMeterRegistry();
+      composite.config().commonTags(commonTags.values());
+
+      LOG.info("Metrics initialization. common tags: {}", commonTags);
+
+      boolean jvmMetricsEnabled =
+          
context.getConfiguration().getBoolean(Property.GENERAL_MICROMETER_JVM_METRICS_ENABLED);
+
+      if (jvmMetricsEnabled) {
+        LOG.info("enabling detailed jvm, classloader, jvm gc and process 
metrics");
+        new ClassLoaderMetrics().bindTo(composite);
+        new JvmMemoryMetrics().bindTo(composite);
+        jvmGcMetrics = new JvmGcMetrics();
+        jvmGcMetrics.bindTo(composite);
+        new ProcessorMetrics().bindTo(composite);
+        new JvmThreadMetrics().bindTo(composite);
+      }
+
+      MeterFilter replicationFilter = new MeterFilter() {
+        @Override
+        public DistributionStatisticConfig configure(Meter.Id id,
+            @NonNull DistributionStatisticConfig config) {
+          if (id.getName().equals("replicationQueue")) {
+            return DistributionStatisticConfig.builder().percentiles(0.5, 
0.75, 0.9, 0.95, 0.99)
+                .expiry(Duration.ofMinutes(10)).build().merge(config);
+          }
+          return config;
+        }
+      };
+
+      if (metricsEnabled()) {
+        // user specified registries
+        String userRegistryFactories =
+            
context.getConfiguration().get(Property.GENERAL_MICROMETER_FACTORY);
+
+        for (String factoryName : getTrimmedStrings(userRegistryFactories)) {
+          try {
+            MeterRegistry registry = getRegistryFromFactory(factoryName, 
context);
+            registry.config().commonTags(commonTags.values());
+            registry.config().meterFilter(replicationFilter);
+            addRegistry(registry);
+          } catch (ClassNotFoundException | NoSuchMethodException | 
InvocationTargetException
+              | InstantiationException | IllegalAccessException ex) {
+            LOG.warn("Could not load registry {}", factoryName, ex);
+          }
+        }
+      }
+
+      Metrics.globalRegistry.add(composite);
+      pendingRegistries.forEach(registry -> composite.add(registry));

Review Comment:
   is there a reason to add the registries to the composite *after* setting the 
composite on the global registry? I'm wondering if the composite should be 
constructed first and then added to the global registry.



##########
server/compaction-coordinator/src/main/java/org/apache/accumulo/coordinator/CompactionCoordinator.java:
##########
@@ -265,14 +264,11 @@ public void run() {
       throw new IllegalStateException("Exception getting Coordinator lock", e);
     }
 
-    try {
-      MetricsUtil.initializeMetrics(getContext().getConfiguration(), 
this.applicationName,
-          clientAddress, getContext().getInstanceName());
-    } catch (ClassNotFoundException | InstantiationException | 
IllegalAccessException
-        | IllegalArgumentException | InvocationTargetException | 
NoSuchMethodException
-        | SecurityException e1) {
-      LOG.error("Error initializing metrics, metrics will not be emitted.", 
e1);
-    }
+    MetricsInfo metricsInfo = getContext().getMetricsInfo();
+    metricsInfo.addServiceTags(getApplicationName(), clientAddress);
+
+    metricsInfo.addMetricsProducers();

Review Comment:
   I don't think this does anything if you don't pass a parameter. Might want 
to add a check to `addMetricsProducers` that at least one parameter is passed.



##########
core/src/main/java/org/apache/accumulo/core/metrics/MetricsInfo.java:
##########
@@ -0,0 +1,114 @@
+/*
+ * 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.core.metrics;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+
+import org.apache.accumulo.core.util.HostAndPort;
+
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.Tag;
+
+public interface MetricsInfo {
+
+  /**
+   * Convenience method to create tag name / value pair for the instance name
+   *
+   * @param instanceName the instance name
+   */
+  static Tag instanceNameTag(final String instanceName) {
+    Objects.requireNonNull(instanceName,
+        "cannot create the tag without providing the instance name");
+    return Tag.of("instance.name", instanceName);
+  }
+
+  /**
+   * Convenience method to create tag name / value pair for the process name
+   *
+   * @param processName the process name
+   */
+  static Tag processTag(final String processName) {
+    Objects.requireNonNull(processName, "cannot create the tag without 
providing the process name");
+    return Tag.of("process.name", processName);
+  }
+
+  /**
+   * Convenience method to create tag name / value pairs for the host and port 
from address
+   * host:port pair.
+   *
+   * @param hostAndPort the host:port pair
+   */
+  static List<Tag> addressTags(final HostAndPort hostAndPort) {
+    Objects.requireNonNull(hostAndPort, "cannot create the tag without 
providing the hostAndPort");
+    List<Tag> tags = new ArrayList<>(2);
+    tags.add(Tag.of("host", hostAndPort.getHost()));
+    int port = hostAndPort.getPort();
+    if (port != 0) {
+      tags.add(Tag.of("port", Integer.toString(hostAndPort.getPort())));
+    }
+    return Collections.unmodifiableList(tags);
+  }
+
+  boolean metricsEnabled();

Review Comment:
   ```suggestion
     boolean isMetricsEnabled();
   ```



##########
server/tserver/src/main/java/org/apache/accumulo/tserver/TabletServerResourceManager.java:
##########
@@ -251,6 +251,7 @@ public Config getConfig() {
   public TabletServerResourceManager(ServerContext context, 
TabletHostingServer tserver) {
     this.context = context;
     final AccumuloConfiguration acuConf = context.getConfiguration();
+    final boolean enableMetrics = 
acuConf.getBoolean(Property.GENERAL_MICROMETER_ENABLED);

Review Comment:
   Can you use context.getMetricsInfo().metricsEnabled() ?



-- 
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]

Reply via email to