You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@geode.apache.org by kl...@apache.org on 2019/03/26 23:56:12 UTC

[geode] branch develop updated: GEODE-6558: Add addMeterSubregistry to CacheFactory (#3352)

This is an automated email from the ASF dual-hosted git repository.

klund pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new abbba67  GEODE-6558: Add addMeterSubregistry to CacheFactory (#3352)
abbba67 is described below

commit abbba679666312696b4a9754d08e3d0f19514f49
Author: Kirk Lund <kl...@apache.org>
AuthorDate: Tue Mar 26 16:56:01 2019 -0700

    GEODE-6558: Add addMeterSubregistry to CacheFactory (#3352)
    
    Provides an API to add meter registries to the cache's composite
    registry for publishing cache metrics to external monitoring systems.
    
    Include example in javadocs for CacheFactory.addMeterRegistry.
    
    Update the Javadocs of all metrics APIs.
    
    Co-authored-by: Dale Emery <de...@pivotal.io>
    Co-authored-by: Kirk Lund <kl...@apache.org>
---
 .../geode/cache/CacheFactoryIntegrationTest.java   | 67 ++++++++++++++++++++++
 .../java/org/apache/geode/cache/CacheFactory.java  | 48 ++++++++++++++++
 .../geode/internal/cache/InternalCacheBuilder.java | 16 ++++++
 .../geode/metrics/MetricsPublishingService.java    |  3 +
 .../org/apache/geode/metrics/MetricsSession.java   |  7 +++
 .../internal/cache/InternalCacheBuilderTest.java   | 23 ++++++++
 6 files changed, 164 insertions(+)

diff --git a/geode-core/src/integrationTest/java/org/apache/geode/cache/CacheFactoryIntegrationTest.java b/geode-core/src/integrationTest/java/org/apache/geode/cache/CacheFactoryIntegrationTest.java
new file mode 100644
index 0000000..9719b37
--- /dev/null
+++ b/geode-core/src/integrationTest/java/org/apache/geode/cache/CacheFactoryIntegrationTest.java
@@ -0,0 +1,67 @@
+/*
+ * 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
+ *
+ * http://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.geode.cache;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import io.micrometer.core.instrument.MeterRegistry;
+import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
+import org.junit.After;
+import org.junit.Test;
+
+import org.apache.geode.internal.cache.InternalCache;
+
+public class CacheFactoryIntegrationTest {
+
+  private InternalCache cache;
+
+  @After
+  public void tearDown() {
+    cache.close();
+  }
+
+  @Test
+  public void addMeterSubregistryAddsSubregistryToCacheMeterRegistry() {
+    MeterRegistry theMeterRegistry = new SimpleMeterRegistry();
+
+    cache = (InternalCache) new CacheFactory()
+        .addMeterSubregistry(theMeterRegistry)
+        .create();
+
+    assertThat(getCacheMeterRegistry(cache).getRegistries())
+        .containsExactly(theMeterRegistry);
+  }
+
+  @Test
+  public void addMeterSubregistryAddsMultipleSubregistriesToCacheMeterRegistry() {
+    MeterRegistry firstMeterRegistry = new SimpleMeterRegistry();
+    MeterRegistry secondMeterRegistry = new SimpleMeterRegistry();
+    MeterRegistry thirdMeterRegistry = new SimpleMeterRegistry();
+
+    cache = (InternalCache) new CacheFactory()
+        .addMeterSubregistry(firstMeterRegistry)
+        .addMeterSubregistry(secondMeterRegistry)
+        .addMeterSubregistry(thirdMeterRegistry)
+        .create();
+
+    assertThat(getCacheMeterRegistry(cache).getRegistries())
+        .containsExactlyInAnyOrder(firstMeterRegistry, secondMeterRegistry, thirdMeterRegistry);
+  }
+
+  private CompositeMeterRegistry getCacheMeterRegistry(InternalCache cache) {
+    return (CompositeMeterRegistry) cache.getMeterRegistry();
+  }
+}
diff --git a/geode-core/src/main/java/org/apache/geode/cache/CacheFactory.java b/geode-core/src/main/java/org/apache/geode/cache/CacheFactory.java
index 02f41b1..2179a94 100644
--- a/geode-core/src/main/java/org/apache/geode/cache/CacheFactory.java
+++ b/geode-core/src/main/java/org/apache/geode/cache/CacheFactory.java
@@ -16,6 +16,9 @@ package org.apache.geode.cache;
 
 import java.util.Properties;
 
+import io.micrometer.core.instrument.MeterRegistry;
+
+import org.apache.geode.annotations.Experimental;
 import org.apache.geode.cache.client.ClientCacheFactory;
 import org.apache.geode.distributed.ConfigurationProperties;
 import org.apache.geode.distributed.DistributedSystem;
@@ -271,6 +274,51 @@ public class CacheFactory {
   }
 
   /**
+   * Adds the given meter registry to the cache's composite registry for publishing cache metrics
+   * to external monitoring systems.
+   *
+   * <p>
+   * Example adding a meter sub-registry:
+   *
+   * <pre>
+   * MeterRegistry prometheusRegistry = new PrometheusMeterRegistry(...);
+   *
+   * Cache cache = new CacheFactory()
+   *     .addMeterSubregistry(prometheusRegistry)
+   *     .create();
+   * </pre>
+   *
+   * <p>
+   * Example adding multiple meter sub-registries:
+   *
+   * <pre>
+   * MeterRegistry prometheusRegistry = new PrometheusMeterRegistry(...);
+   * MeterRegistry influxRegistry = new InfluxMeterRegistry(...);
+   * MeterRegistry newRelicRegistry = new NewRelicMeterRegistry(...);
+   *
+   * Cache cache = new CacheFactory()
+   *     .addMeterSubregistry(prometheusRegistry)
+   *     .addMeterSubregistry(influxRegistry)
+   *     .addMeterSubregistry(newRelicRegistry)
+   *     .create();
+   * </pre>
+   *
+   * <p>
+   * Experimental: Micrometer metrics is a new addition to Geode and the API may change.
+   *
+   * @param subregistry the registry to add
+   * @return this CacheFactory
+   *
+   * @see <a href="https://micrometer.io/docs">Micrometer Documentation</a>
+   * @see <a href="https://micrometer.io/docs/concepts">Micrometer Concepts</a>
+   */
+  @Experimental("Micrometer metrics is a new addition to Geode and the API may change")
+  public CacheFactory addMeterSubregistry(MeterRegistry subregistry) {
+    internalCacheBuilder.addMeterSubregistry(subregistry);
+    return this;
+  }
+
+  /**
    * Creates a new cache that uses the specified {@code system}.
    * <p>
    * The {@code system} can specify a
diff --git a/geode-core/src/main/java/org/apache/geode/internal/cache/InternalCacheBuilder.java b/geode-core/src/main/java/org/apache/geode/internal/cache/InternalCacheBuilder.java
index a567597..20e549d 100644
--- a/geode-core/src/main/java/org/apache/geode/internal/cache/InternalCacheBuilder.java
+++ b/geode-core/src/main/java/org/apache/geode/internal/cache/InternalCacheBuilder.java
@@ -18,8 +18,10 @@ import static java.util.Objects.requireNonNull;
 import static org.apache.geode.distributed.internal.DistributionConfig.GEMFIRE_PREFIX;
 import static org.apache.geode.distributed.internal.InternalDistributedSystem.ALLOW_MULTIPLE_SYSTEMS;
 
+import java.util.HashSet;
 import java.util.Optional;
 import java.util.Properties;
+import java.util.Set;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 
@@ -62,6 +64,8 @@ public class InternalCacheBuilder {
   private static final boolean IS_EXISTING_OK_DEFAULT = true;
   private static final boolean IS_CLIENT_DEFAULT = false;
 
+  private final Set<MeterRegistry> meterSubregistries = new HashSet<>();
+
   private final Properties configProperties;
   private final CacheConfig cacheConfig;
   private final CompositeMeterRegistryFactory compositeMeterRegistryFactory;
@@ -197,6 +201,10 @@ public class InternalCacheBuilder {
             CompositeMeterRegistry compositeMeterRegistry = compositeMeterRegistryFactory
                 .create(systemId, memberName, hostName);
 
+            for (MeterRegistry meterSubregistry : meterSubregistries) {
+              compositeMeterRegistry.add(meterSubregistry);
+            }
+
             metricsSessionInitializer.accept(compositeMeterRegistry);
 
             cache =
@@ -334,6 +342,14 @@ public class InternalCacheBuilder {
     return this;
   }
 
+  /**
+   * @see CacheFactory#addMeterSubregistry(MeterRegistry)
+   */
+  public InternalCacheBuilder addMeterSubregistry(MeterRegistry subregistry) {
+    meterSubregistries.add(subregistry);
+    return this;
+  }
+
   private Optional<InternalDistributedSystem> findInternalDistributedSystem() {
     InternalDistributedSystem internalDistributedSystem = null;
     if (configProperties.isEmpty() && !ALLOW_MULTIPLE_SYSTEMS) {
diff --git a/geode-core/src/main/java/org/apache/geode/metrics/MetricsPublishingService.java b/geode-core/src/main/java/org/apache/geode/metrics/MetricsPublishingService.java
index 0baf765..2c9e000 100644
--- a/geode-core/src/main/java/org/apache/geode/metrics/MetricsPublishingService.java
+++ b/geode-core/src/main/java/org/apache/geode/metrics/MetricsPublishingService.java
@@ -67,6 +67,9 @@ import org.apache.geode.annotations.Experimental;
  * <p>
  * {@code com.application.MyMetricsPublishingService}
  *
+ * <p>
+ * Experimental: Micrometer metrics is a new addition to Geode and the API may change.
+ *
  * @see <a href="https://micrometer.io/docs">Micrometer Documentation</a>
  * @see <a href="https://micrometer.io/docs/concepts">Micrometer Concepts</a>
  */
diff --git a/geode-core/src/main/java/org/apache/geode/metrics/MetricsSession.java b/geode-core/src/main/java/org/apache/geode/metrics/MetricsSession.java
index dcad5f4..6141075 100644
--- a/geode-core/src/main/java/org/apache/geode/metrics/MetricsSession.java
+++ b/geode-core/src/main/java/org/apache/geode/metrics/MetricsSession.java
@@ -21,9 +21,16 @@ import org.apache.geode.annotations.Experimental;
 /**
  * Provides the ability to add and remove a meter registry for publishing cache metrics to external
  * monitoring systems.
+ *
+ * <p>
+ * Experimental: Micrometer metrics is a new addition to Geode and the API may change.
+ *
+ * @see <a href="https://micrometer.io/docs">Micrometer Documentation</a>
+ * @see <a href="https://micrometer.io/docs/concepts">Micrometer Concepts</a>
  */
 @Experimental("Micrometer metrics is a new addition to Geode and the API may change")
 public interface MetricsSession {
+
   /**
    * Adds the given registry to the cache's composite registry.
    *
diff --git a/geode-core/src/test/java/org/apache/geode/internal/cache/InternalCacheBuilderTest.java b/geode-core/src/test/java/org/apache/geode/internal/cache/InternalCacheBuilderTest.java
index e30a1a7..97fc693 100644
--- a/geode-core/src/test/java/org/apache/geode/internal/cache/InternalCacheBuilderTest.java
+++ b/geode-core/src/test/java/org/apache/geode/internal/cache/InternalCacheBuilderTest.java
@@ -35,7 +35,9 @@ import java.util.Properties;
 import java.util.function.Consumer;
 import java.util.function.Supplier;
 
+import io.micrometer.core.instrument.MeterRegistry;
 import io.micrometer.core.instrument.composite.CompositeMeterRegistry;
+import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
 import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
 import org.junit.Before;
 import org.junit.Test;
@@ -621,6 +623,27 @@ public class InternalCacheBuilderTest {
     verify(givenSystem).setCache(same(singletonCache));
   }
 
+  @Test
+  public void addMeterRegistry_createsCache_withGivenMeterRegistry() {
+    MeterRegistry theMeterRegistry = new SimpleMeterRegistry();
+
+    CompositeMeterRegistry theCompositeMeterRegistry = new CompositeMeterRegistry();
+    when(compositeMeterRegistryFactory.create(anyInt(), any(), any()))
+        .thenReturn(theCompositeMeterRegistry);
+
+    InternalCacheBuilder internalCacheBuilder = new InternalCacheBuilder(
+        new Properties(), new CacheConfig(),
+        compositeMeterRegistryFactory, metricsSessionInitializer,
+        nullSingletonSystemSupplier, constructorOf(constructedSystem()),
+        nullSingletonCacheSupplier, constructorOf(constructedCache()));
+
+    internalCacheBuilder
+        .addMeterSubregistry(theMeterRegistry)
+        .create();
+
+    assertThat(theCompositeMeterRegistry.getRegistries()).containsExactly(theMeterRegistry);
+  }
+
   private InternalDistributedSystem constructedSystem() {
     return systemWith("constructedSystem", ANY_SYSTEM_ID, ANY_MEMBER_NAME, ANY_HOST_NAME);
   }