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


##########
core/src/main/java/org/apache/accumulo/core/spi/metrics/MeterRegistryFactory.java:
##########
@@ -0,0 +1,75 @@
+/*
+ * 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.spi.metrics;
+
+import java.util.Map;
+
+import org.apache.accumulo.core.spi.common.ServiceEnvironment;
+
+import io.micrometer.core.instrument.MeterRegistry;
+
+/**
+ * The Micrometer metrics allows for different monitoring systems. and can be 
enabled within
+ * Accumulo with properties and are initialized by implementing this interface 
and providing the
+ * factory implementation clas name as a property. Metrics are specified with 
the following
+ * properties:
+ * <p>
+ * Property.GENERAL_MICROMETER_ENABLED = true
+ * <p>
+ * Property.GENERAL_MICROMETER_FACTORY = [implementation].class.getName()
+ * <p>
+ * Properties can be passed in the Accumulo properties files using the prefix
+ * {@code general.custom.metrics.opts} for example. to set the polling rate 
used in the
+ * {@link org.apache.accumulo.core.spi.metrics.LoggingMeterRegistryFactory} to 
10 seconds use
+ *
+ * <pre>
+ *     general.custom.metrics.opts.logging.step = 10s
+ * </pre>
+ *
+ * Classes that implement custom properties will need to take care to 
implement unique names for
+ * each factory, if multiple factories are used.
+ *
+ * @since 2.1.3
+ */
+public interface MeterRegistryFactory {
+  // full form in property file is "general.custom.metrics.opts"
+  String METRICS_PROP_SUBSTRING = "metrics.opts.";
+
+  interface InitParameters {
+    /**
+     *
+     * @return The configured options. For example properties
+     *         {@code general.custom.metrics.opts.prop1=abc} and
+     *         {@code general.custom.metrics.opts.prop9=123} were set, then 
this map would contain
+     *         {@code prop1=abc} and {@code prop9=123}.
+     */
+    Map<String,String> getOptions();
+
+    ServiceEnvironment getServiceEnv();
+  }
+
+  default void init(InitParameters params) {}
+
+  /**
+   * Called on metrics initialization.
+   *
+   * @return a Micrometer registry that will be added to the metrics 
configuration.
+   */
+  MeterRegistry create();

Review Comment:
   I wonder if the interface declaration should be:
   ```
   public interface MeterRegistryFactory<T extends MeterRegistry>
   ```
   
   and then this method could be
   ```
   T create();
   ```



##########
core/src/main/java/org/apache/accumulo/core/spi/metrics/LoggingMeterRegistryFactory.java:
##########
@@ -0,0 +1,88 @@
+/*
+ * 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.spi.metrics;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.function.Consumer;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.logging.LoggingMeterRegistry;
+import io.micrometer.core.instrument.logging.LoggingRegistryConfig;
+
+/**
+ * Example implementation of enabling a metrics provider by implementing the
+ * {@link org.apache.accumulo.core.spi.metrics.MeterRegistryFactory} 
interface. When enabled though
+ * properties by enabling {@code Property.GENERAL_MICROMETER_ENABLED} and 
providing this class for
+ * the {@code Property.GENERAL_MICROMETER_FACTORY}
+ * <p>
+ * The metrics will appear in the normal service logs with a named logger of
+ * {@code org.apache.accumulo.METRICS} at the INFO level. The metrics output 
can be directed to a
+ * file using standard logging configuration properties by configuring the 
log4j2-service.properties
+ * file.
+ * <p>
+ * The update frequency can be adjusted by setting {@code 
general.custom.metrics.opts.logging.step}
+ * in the Accumulo configuration. The default is 60 sec.
+ *
+ */
+public class LoggingMeterRegistryFactory implements MeterRegistryFactory {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(LoggingMeterRegistryFactory.class);
+
+  // named logger that can be configured using standard logging properties.
+  private static final Logger METRICS = 
LoggerFactory.getLogger("org.apache.accumulo.METRICS");
+
+  private final Consumer<String> metricConsumer = METRICS::info;
+  private final Map<String,String> metricsProps = new HashMap<>();
+  // defines the metrics update period, default is 60 seconds.
+  private final LoggingRegistryConfig lconf = c -> {
+    if (c.equals("logging.step")) {
+      return metricsProps.getOrDefault("logging.step", "60s");
+    }
+    return null;
+  };
+
+  private final AtomicBoolean initCalled = new AtomicBoolean(false);

Review Comment:
   I think you could probably get rid of the `synchronized` modifier on `init` 
and `create` and get rid of the `initCalled` variable and replace it with an 
`AtomicReference<Map<String,String>>` for metricsProps initialized to an empty 
reference. For example:
   
   ```
     private final AtomicReference<Map<String,String>> metricsProps = new 
AtomicReference<>();
   
     @Override
     public void init(final InitParameters params) {
       Objects.requireNonNull(params, "InitParams not provided");
       if (!metricsProps.compareAndSet(null, new HashMap<>(params.getOptions))) 
{
         LOG.error("Already initialized");
       }
       LOG.info("initialized with parameters: {}", metricsProps.get());
     }
   
     @Override
     public synchronized MeterRegistry create() {
       LOG.info("starting metrics registration.");
       if (metricsProps.get() == null) {
         throw new IllegalStateException("init() not called");
       }
       return 
LoggingMeterRegistry.builder(lconf).loggingSink(metricConsumer).build();
     }
   ```



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