You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/03/27 12:15:35 UTC

[incubator-servicecomb-java-chassis] branch master updated (c9b0704 -> ae28a9f)

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

liubao pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git.


    from c9b0704  SCB-434 add @GET sample code and update readme
     new 18ca4ed  SCB-383 define metrics init and poll mechanism
     new 63c14b9  SCB-383 upgrade spectator to 0.63.0, that fix servo meter count/gauge bug
     new 34ab156  SCB-383 add support of create spectator GlobalRegistr for UT.
     new ae28a9f  SCB-383 subscribe invocation life event and statistics

The 4 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 .../core/event/InvocationStartEvent.java           |   1 -
 .../foundation/common/event/EventManager.java      |   4 +
 .../foundation/metrics/MetricsBootstrap.java       |  82 ++++++++++++
 .../foundation/metrics/MetricsBootstrapConfig.java |  18 ++-
 .../foundation/metrics/MetricsInitializer.java     |  16 ++-
 .../foundation/metrics/PolledEvent.java            |  19 +--
 .../foundation/metrics/TestMetricsBootstrap.java   |  95 ++++++++++++++
 foundations/foundation-test-scaffolding/pom.xml    |   4 +
 .../com/netflix/spectator/api/SpectatorUtils.java} |   9 +-
 java-chassis-dependencies/pom.xml                  |   2 +-
 java-chassis-distribution/src/release/LICENSE      |   2 +-
 metrics/metrics-core/pom.xml                       |   8 +-
 .../metrics/core/DefaultMetricsInitializer.java    |  82 ++++++++++++
 .../metrics/core}/MetricsBootListener.java         |  22 ++--
 .../metrics/core/meter/ConsumerMeters.java         |  27 ++--
 .../metrics/core/meter/ProducerMeters.java         |  27 ++--
 .../meter/invocation/AbstractInvocationMeter.java  |  33 ++---
 .../meter/invocation/AbstractInvocationMeters.java |  79 ++++++++++++
 .../meter/invocation/ConsumerInvocationMeter.java  |  13 +-
 .../meter/invocation/ConsumerInvocationMeters.java |  19 ++-
 .../meter/invocation/MeterInvocationConst.java     |  27 ++--
 .../meter/invocation/ProducerInvocationMeter.java  |  51 ++++++++
 .../meter/invocation/ProducerInvocationMeters.java |  19 ++-
 ...rvicecomb.foundation.metrics.MetricsInitializer |   4 +-
 .../core/TestDefaultMetricsInitializer.java        | 138 +++++++++++++++++++++
 25 files changed, 661 insertions(+), 140 deletions(-)
 create mode 100644 foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsBootstrap.java
 copy metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/MetricsConfig.java => foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsBootstrapConfig.java (64%)
 copy samples/bmi/calculator/src/main/java/org/apache/servicecomb/samples/bmi/CalculatorEndpoint.java => foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsInitializer.java (70%)
 copy demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/MicroserviceArray.java => foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/PolledEvent.java (70%)
 create mode 100644 foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/TestMetricsBootstrap.java
 copy foundations/{foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/tcp/TcpConst.java => foundation-test-scaffolding/src/main/java/com/netflix/spectator/api/SpectatorUtils.java} (81%)
 create mode 100644 metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultMetricsInitializer.java
 copy {samples/metrics-write-file-sample/metrics-write-file/src/main/java/org/apache/servicecomb/samples/mwf => metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core}/MetricsBootListener.java (69%)
 copy common/common-javassist/src/main/java/org/apache/servicecomb/common/javassist/ParameterConfig.java => metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/ConsumerMeters.java (60%)
 copy common/common-javassist/src/main/java/org/apache/servicecomb/common/javassist/ParameterConfig.java => metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/ProducerMeters.java (60%)
 copy core/src/main/java/org/apache/servicecomb/core/event/InvocationFinishEvent.java => metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/AbstractInvocationMeter.java (55%)
 create mode 100644 metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/AbstractInvocationMeters.java
 copy handlers/handler-bizkeeper/src/main/java/org/apache/servicecomb/bizkeeper/FallbackPolicy.java => metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ConsumerInvocationMeter.java (71%)
 copy demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/MockedFallbackExample.java => metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ConsumerInvocationMeters.java (66%)
 copy demo/demo-schema/src/main/java/org/apache/servicecomb/demo/compute/Compute.java => metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/MeterInvocationConst.java (57%)
 create mode 100644 metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ProducerInvocationMeter.java
 copy demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/MockedFallbackExample.java => metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ProducerInvocationMeters.java (66%)
 copy common/common-rest/src/test/resources/config/test.properties => metrics/metrics-core/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer (92%)
 create mode 100644 metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultMetricsInitializer.java

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.

[incubator-servicecomb-java-chassis] 02/04: SCB-383 upgrade spectator to 0.63.0, that fix servo meter count/gauge bug

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 63c14b9997fc5ba29e78f5cf655a449cd1bb29a4
Author: wujimin <wu...@huawei.com>
AuthorDate: Fri Mar 16 09:33:43 2018 +0800

    SCB-383 upgrade spectator to 0.63.0, that fix servo meter count/gauge bug
---
 java-chassis-dependencies/pom.xml             | 2 +-
 java-chassis-distribution/src/release/LICENSE | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/java-chassis-dependencies/pom.xml b/java-chassis-dependencies/pom.xml
index e9ae9ec..b7b6227 100644
--- a/java-chassis-dependencies/pom.xml
+++ b/java-chassis-dependencies/pom.xml
@@ -360,7 +360,7 @@
       <dependency>
         <groupId>com.netflix.spectator</groupId>
         <artifactId>spectator-reg-servo</artifactId>
-        <version>0.62.0</version>
+        <version>0.63.0</version>
       </dependency>
       <dependency>
         <groupId>com.google.inject</groupId>
diff --git a/java-chassis-distribution/src/release/LICENSE b/java-chassis-distribution/src/release/LICENSE
index 8fb6c36..8243952 100644
--- a/java-chassis-distribution/src/release/LICENSE
+++ b/java-chassis-distribution/src/release/LICENSE
@@ -432,7 +432,7 @@ rxnetty-contexts (https://github.com/ReactiveX/RxNetty) io.reactivex:rxnetty-con
 rxnetty-servo (https://github.com/ReactiveX/RxNetty) io.reactivex:rxnetty-servo:jar:0.4.9
 servo-core (https://github.com/Netflix/servo) com.netflix.servo:servo-core:jar:0.10.1
 servo-internal (https://github.com/Netflix/servo) com.netflix.servo:servo-internal:jar:0.10.1
-spectator-reg-servo (https://github.com/Netflix/spectator) com.netflix.spectator:spectator-reg-servo:0.62.0
+spectator-reg-servo (https://github.com/Netflix/spectator) com.netflix.spectator:spectator-reg-servo:0.63.0
 spectator-api (https://github.com/Netflix/spectator) com.netflix.spectator:spectator-api:0.62.0
 spring-cloud-starter (https://projects.spring.io/spring-cloud) org.springframework.cloud:spring-cloud-starter:jar:1.1.8.RELEASE
 spring-cloud-starter-archaius (https://projects.spring.io/spring-cloud) org.springframework.cloud:spring-cloud-starter-archaius:jar:1.2.6.RELEASE

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.

[incubator-servicecomb-java-chassis] 04/04: SCB-383 subscribe invocation life event and statistics

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit ae28a9faff24e773520f3452dfd80b86dfbe7aa4
Author: wujimin <wu...@huawei.com>
AuthorDate: Sat Mar 17 10:54:16 2018 +0800

    SCB-383 subscribe invocation life event and statistics
---
 .../core/event/InvocationStartEvent.java           |   1 -
 .../foundation/common/event/EventManager.java      |   4 +
 metrics/metrics-core/pom.xml                       |   8 +-
 .../metrics/core/DefaultMetricsInitializer.java    |  82 ++++++++++++
 .../metrics/core/MetricsBootListener.java          |  42 +++----
 .../metrics/core/meter/ConsumerMeters.java         |  20 +--
 .../metrics/core/meter/ProducerMeters.java         |  20 +--
 .../meter/invocation/AbstractInvocationMeter.java  |  23 ++--
 .../meter/invocation/AbstractInvocationMeters.java |  79 ++++++++++++
 .../meter/invocation/ConsumerInvocationMeter.java  |  17 ++-
 .../meter/invocation/ConsumerInvocationMeters.java |  18 +--
 .../meter/invocation/MeterInvocationConst.java     |  32 +++--
 .../meter/invocation/ProducerInvocationMeter.java  |  51 ++++++++
 .../meter/invocation/ProducerInvocationMeters.java |  18 +--
 ...rvicecomb.foundation.metrics.MetricsInitializer |  18 +++
 .../core/TestDefaultMetricsInitializer.java        | 138 +++++++++++++++++++++
 16 files changed, 478 insertions(+), 93 deletions(-)

diff --git a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java b/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
index 5b93e66..e83c447 100644
--- a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
+++ b/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
@@ -22,7 +22,6 @@ public class InvocationStartEvent {
   private Invocation invocation;
 
   public InvocationStartEvent(Invocation invocation) {
-    super();
     this.invocation = invocation;
   }
 
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java
index d50e767..9672a92 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java
+++ b/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java
@@ -27,6 +27,10 @@ import com.google.common.eventbus.SimpleEventBus;
 public class EventManager {
   public static EventBus eventBus = new SimpleEventBus();
 
+  public static EventBus getEventBus() {
+    return eventBus;
+  }
+
   /**
    * Registering listener.
    */
diff --git a/metrics/metrics-core/pom.xml b/metrics/metrics-core/pom.xml
index 8e4d3a4..f4d1e76 100644
--- a/metrics/metrics-core/pom.xml
+++ b/metrics/metrics-core/pom.xml
@@ -16,8 +16,7 @@
   ~ limitations under the License.
   -->
 
-<project xmlns="http://maven.apache.org/POM/4.0.0"
-  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
   <parent>
     <artifactId>metrics</artifactId>
@@ -48,6 +47,11 @@
       <artifactId>slf4j-log4j12</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>foundation-test-scaffolding</artifactId>
+    </dependency>
+
   </dependencies>
 
 </project>
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultMetricsInitializer.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultMetricsInitializer.java
new file mode 100644
index 0000000..12e090e
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/DefaultMetricsInitializer.java
@@ -0,0 +1,82 @@
+/*
+ * 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.servicecomb.metrics.core;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.event.InvocationStartEvent;
+import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
+import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
+import org.apache.servicecomb.metrics.core.meter.ConsumerMeters;
+import org.apache.servicecomb.metrics.core.meter.ProducerMeters;
+import org.apache.servicecomb.metrics.core.meter.invocation.AbstractInvocationMeters;
+
+import com.google.common.eventbus.AllowConcurrentEvents;
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.Registry;
+import com.netflix.spectator.servo.ServoRegistry;
+
+public class DefaultMetricsInitializer implements MetricsInitializer {
+  public static final String METRICS_WINDOW_TIME = "servicecomb.metrics.window_time";
+
+  public static final int DEFAULT_METRICS_WINDOW_TIME = 5000;
+
+  private Registry registry;
+
+  private ConsumerMeters consumerMeters;
+
+  private ProducerMeters producerMeters;
+
+  @Override
+  public void init(CompositeRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) {
+    registry = createRegistry(config);
+
+    this.consumerMeters = new ConsumerMeters(registry);
+    this.producerMeters = new ProducerMeters(registry);
+
+    globalRegistry.add(registry);
+    eventBus.register(this);
+  }
+
+  protected Registry createRegistry(MetricsBootstrapConfig config) {
+    System.getProperties().setProperty("servo.pollers", String.valueOf(config.getMsPollInterval()));
+    return new ServoRegistry();
+  }
+
+  protected AbstractInvocationMeters findInvocationMeters(Invocation invocation) {
+    if (invocation.isConsumer()) {
+      return consumerMeters.getInvocationMeters();
+    }
+    return producerMeters.getInvocationMeters();
+  }
+
+  @Subscribe
+  @AllowConcurrentEvents
+  public void onInvocationStart(InvocationStartEvent event) {
+    AbstractInvocationMeters invocationMeters = findInvocationMeters(event.getInvocation());
+    invocationMeters.onInvocationStart(event);
+  }
+
+  @Subscribe
+  @AllowConcurrentEvents
+  public void onInvocationFinish(InvocationFinishEvent event) {
+    AbstractInvocationMeters invocationMeters = findInvocationMeters(event.getInvocation());
+    invocationMeters.onInvocationFinish(event);
+  }
+}
diff --git a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/MetricsBootListener.java
similarity index 54%
copy from foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java
copy to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/MetricsBootListener.java
index d50e767..48c2837 100644
--- a/foundations/foundation-common/src/main/java/org/apache/servicecomb/foundation/common/event/EventManager.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/MetricsBootListener.java
@@ -14,37 +14,25 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+package org.apache.servicecomb.metrics.core;
 
-package org.apache.servicecomb.foundation.common.event;
+import org.apache.servicecomb.core.BootListener;
+import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.apache.servicecomb.foundation.metrics.MetricsBootstrap;
+import org.springframework.stereotype.Component;
 
-import com.google.common.eventbus.EventBus;
-import com.google.common.eventbus.SimpleEventBus;
+import com.netflix.spectator.api.Spectator;
 
-/**
- * EventManager for chassis events
- *
- */
-public class EventManager {
-  public static EventBus eventBus = new SimpleEventBus();
+@Component
+public class MetricsBootListener implements BootListener {
+  private MetricsBootstrap metricsBootstrap = new MetricsBootstrap();
 
-  /**
-   * Registering listener.
-   */
-  public static void register(Object listener) {
-    eventBus.register(listener);
-  }
-
-  /**
-   * post event.
-   */
-  public static void post(Object event) {
-    eventBus.post(event);
-  }
+  @Override
+  public void onBootEvent(BootEvent event) {
+    if (!EventType.AFTER_REGISTRY.equals(event.getEventType())) {
+      return;
+    }
 
-  /**
-   * Unregistering listener.
-   */
-  public static void unregister(Object listener) {
-    eventBus.unregister(listener);
+    metricsBootstrap.start(Spectator.globalRegistry(), EventManager.getEventBus());
   }
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/ConsumerMeters.java
similarity index 59%
copy from core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
copy to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/ConsumerMeters.java
index 5b93e66..b249fdc 100644
--- a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/ConsumerMeters.java
@@ -14,19 +14,21 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.servicecomb.core.event;
+package org.apache.servicecomb.metrics.core.meter;
 
-import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.metrics.core.meter.invocation.AbstractInvocationMeters;
+import org.apache.servicecomb.metrics.core.meter.invocation.ConsumerInvocationMeters;
 
-public class InvocationStartEvent {
-  private Invocation invocation;
+import com.netflix.spectator.api.Registry;
 
-  public InvocationStartEvent(Invocation invocation) {
-    super();
-    this.invocation = invocation;
+public class ConsumerMeters {
+  private AbstractInvocationMeters invocationMeters;
+
+  public ConsumerMeters(Registry registry) {
+    invocationMeters = new ConsumerInvocationMeters(registry);
   }
 
-  public Invocation getInvocation() {
-    return invocation;
+  public AbstractInvocationMeters getInvocationMeters() {
+    return invocationMeters;
   }
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/ProducerMeters.java
similarity index 59%
copy from core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
copy to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/ProducerMeters.java
index 5b93e66..e867f8e 100644
--- a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/ProducerMeters.java
@@ -14,19 +14,21 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.servicecomb.core.event;
+package org.apache.servicecomb.metrics.core.meter;
 
-import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.metrics.core.meter.invocation.AbstractInvocationMeters;
+import org.apache.servicecomb.metrics.core.meter.invocation.ProducerInvocationMeters;
 
-public class InvocationStartEvent {
-  private Invocation invocation;
+import com.netflix.spectator.api.Registry;
 
-  public InvocationStartEvent(Invocation invocation) {
-    super();
-    this.invocation = invocation;
+public class ProducerMeters {
+  private AbstractInvocationMeters invocationMeters;
+
+  public ProducerMeters(Registry registry) {
+    invocationMeters = new ProducerInvocationMeters(registry);
   }
 
-  public Invocation getInvocation() {
-    return invocation;
+  public AbstractInvocationMeters getInvocationMeters() {
+    return invocationMeters;
   }
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/AbstractInvocationMeter.java
similarity index 51%
copy from core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
copy to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/AbstractInvocationMeter.java
index 5b93e66..d2ae510 100644
--- a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/AbstractInvocationMeter.java
@@ -14,19 +14,26 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.servicecomb.core.event;
+package org.apache.servicecomb.metrics.core.meter.invocation;
+
+import java.util.concurrent.TimeUnit;
 
 import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.swagger.invocation.Response;
+
+import com.netflix.spectator.api.Id;
+import com.netflix.spectator.api.Registry;
+import com.netflix.spectator.api.Timer;
 
-public class InvocationStartEvent {
-  private Invocation invocation;
+public abstract class AbstractInvocationMeter {
+  private Timer totalTimer;
 
-  public InvocationStartEvent(Invocation invocation) {
-    super();
-    this.invocation = invocation;
+  public AbstractInvocationMeter(Registry registry, Id id, Invocation invocation, Response response) {
+    totalTimer = registry.timer(id.withTag(MeterInvocationConst.TAG_STAGE, MeterInvocationConst.STAGE_TOTAL));
   }
 
-  public Invocation getInvocation() {
-    return invocation;
+  public void onInvocationFinish(InvocationFinishEvent event) {
+    totalTimer.record(event.getNanoCurrent() - event.getInvocation().getStartTime(), TimeUnit.NANOSECONDS);
   }
 }
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/AbstractInvocationMeters.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/AbstractInvocationMeters.java
new file mode 100644
index 0000000..43df838
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/AbstractInvocationMeters.java
@@ -0,0 +1,79 @@
+/*
+ * 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.servicecomb.metrics.core.meter.invocation;
+
+import java.util.Map;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.core.event.InvocationStartEvent;
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.swagger.invocation.Response;
+
+import com.netflix.spectator.api.Id;
+import com.netflix.spectator.api.Registry;
+
+public abstract class AbstractInvocationMeters {
+  protected Registry registry;
+
+  private Map<String, AbstractInvocationMeter> metersMap = new ConcurrentHashMapEx<>();
+
+  // not care for concurrency, just for make build key faster 
+  private int maxKeyLen = 64;
+
+  public AbstractInvocationMeters(Registry registry) {
+    this.registry = registry;
+  }
+
+  protected AbstractInvocationMeter getOrCreateMeters(Invocation invocation, Response response) {
+    // build string key is faster then use Id to locate timer directly
+    StringBuilder keyBuilder = new StringBuilder(maxKeyLen);
+    keyBuilder
+        .append(invocation.getInvocationType().name())
+        .append(invocation.getRealTransportName())
+        .append(invocation.getMicroserviceQualifiedName())
+        .append(response.getStatusCode());
+    if (keyBuilder.length() > maxKeyLen) {
+      maxKeyLen = keyBuilder.length();
+    }
+
+    return metersMap.computeIfAbsent(keyBuilder.toString(), k -> {
+      Id id = registry.createId(MeterInvocationConst.INVOCATION_NAME,
+          MeterInvocationConst.TAG_ROLE,
+          invocation.getInvocationType().name(),
+          MeterInvocationConst.TAG_TRANSPORT,
+          invocation.getRealTransportName(),
+          MeterInvocationConst.TAG_OPERATION,
+          invocation.getMicroserviceQualifiedName(),
+          MeterInvocationConst.TAG_STATUS,
+          String.valueOf(response.getStatusCode()));
+
+      return createMeter(id, invocation, response);
+    });
+  }
+
+  protected abstract AbstractInvocationMeter createMeter(Id id, Invocation invocation,
+      Response response);
+
+  public void onInvocationStart(InvocationStartEvent event) {
+  }
+
+  public void onInvocationFinish(InvocationFinishEvent event) {
+    AbstractInvocationMeter meters = getOrCreateMeters(event.getInvocation(), event.getResponse());
+    meters.onInvocationFinish(event);
+  }
+}
diff --git a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ConsumerInvocationMeter.java
similarity index 67%
copy from core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
copy to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ConsumerInvocationMeter.java
index 5b93e66..0f473a0 100644
--- a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ConsumerInvocationMeter.java
@@ -14,19 +14,16 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.servicecomb.core.event;
+package org.apache.servicecomb.metrics.core.meter.invocation;
 
 import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.swagger.invocation.Response;
 
-public class InvocationStartEvent {
-  private Invocation invocation;
+import com.netflix.spectator.api.Id;
+import com.netflix.spectator.api.Registry;
 
-  public InvocationStartEvent(Invocation invocation) {
-    super();
-    this.invocation = invocation;
-  }
-
-  public Invocation getInvocation() {
-    return invocation;
+public class ConsumerInvocationMeter extends AbstractInvocationMeter {
+  public ConsumerInvocationMeter(Registry registry, Id id, Invocation invocation, Response response) {
+    super(registry, id, invocation, response);
   }
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ConsumerInvocationMeters.java
similarity index 61%
copy from core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
copy to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ConsumerInvocationMeters.java
index 5b93e66..dd24ecb 100644
--- a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ConsumerInvocationMeters.java
@@ -14,19 +14,21 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.servicecomb.core.event;
+package org.apache.servicecomb.metrics.core.meter.invocation;
 
 import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.swagger.invocation.Response;
 
-public class InvocationStartEvent {
-  private Invocation invocation;
+import com.netflix.spectator.api.Id;
+import com.netflix.spectator.api.Registry;
 
-  public InvocationStartEvent(Invocation invocation) {
-    super();
-    this.invocation = invocation;
+public class ConsumerInvocationMeters extends AbstractInvocationMeters {
+  public ConsumerInvocationMeters(Registry registry) {
+    super(registry);
   }
 
-  public Invocation getInvocation() {
-    return invocation;
+  @Override
+  protected AbstractInvocationMeter createMeter(Id id, Invocation invocation, Response response) {
+    return new ConsumerInvocationMeter(registry, id, invocation, response);
   }
 }
diff --git a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/MeterInvocationConst.java
similarity index 56%
copy from core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
copy to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/MeterInvocationConst.java
index 5b93e66..3d208c3 100644
--- a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/MeterInvocationConst.java
@@ -14,19 +14,29 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.servicecomb.core.event;
+package org.apache.servicecomb.metrics.core.meter.invocation;
 
-import org.apache.servicecomb.core.Invocation;
+import com.netflix.spectator.api.Statistic;
 
-public class InvocationStartEvent {
-  private Invocation invocation;
+public interface MeterInvocationConst {
+  final String INVOCATION_NAME = "servicecomb.invocation";
 
-  public InvocationStartEvent(Invocation invocation) {
-    super();
-    this.invocation = invocation;
-  }
+  // consumer or producer
+  final String TAG_ROLE = "role";
 
-  public Invocation getInvocation() {
-    return invocation;
-  }
+  final String TAG_OPERATION = "operation";
+
+  final String TAG_TRANSPORT = "transport";
+
+  final String TAG_STAGE = "stage";
+
+  final String TAG_STATUS = "status";
+
+  final String TAG_STATISTIC = Statistic.count.key();
+
+  final String STAGE_TOTAL = "total";
+
+  final String STAGE_EXECUTOR_QUEUE = "queue";
+
+  final String STAGE_EXECUTION = "execution";
 }
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ProducerInvocationMeter.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ProducerInvocationMeter.java
new file mode 100644
index 0000000..1106265
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ProducerInvocationMeter.java
@@ -0,0 +1,51 @@
+/*
+ * 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.servicecomb.metrics.core.meter.invocation;
+
+import java.util.concurrent.TimeUnit;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.swagger.invocation.Response;
+
+import com.netflix.spectator.api.Id;
+import com.netflix.spectator.api.Registry;
+import com.netflix.spectator.api.Timer;
+
+public class ProducerInvocationMeter extends AbstractInvocationMeter {
+  private Timer executorQueueTimer;
+
+  private Timer executionTimer;
+
+  public ProducerInvocationMeter(Registry registry, Id id, Invocation invocation, Response response) {
+    super(registry, id, invocation, response);
+
+    executorQueueTimer =
+        registry.timer(id.withTag(MeterInvocationConst.TAG_STAGE, MeterInvocationConst.STAGE_EXECUTOR_QUEUE));
+    executionTimer =
+        registry.timer(id.withTag(MeterInvocationConst.TAG_STAGE, MeterInvocationConst.STAGE_EXECUTION));
+  }
+
+  @Override
+  public void onInvocationFinish(InvocationFinishEvent event) {
+    super.onInvocationFinish(event);
+
+    Invocation invocation = event.getInvocation();
+    executorQueueTimer.record(invocation.getStartExecutionTime() - invocation.getStartTime(), TimeUnit.NANOSECONDS);
+    executionTimer.record(event.getNanoCurrent() - invocation.getStartExecutionTime(), TimeUnit.NANOSECONDS);
+  }
+}
diff --git a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ProducerInvocationMeters.java
similarity index 61%
copy from core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
copy to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ProducerInvocationMeters.java
index 5b93e66..56031d0 100644
--- a/core/src/main/java/org/apache/servicecomb/core/event/InvocationStartEvent.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/meter/invocation/ProducerInvocationMeters.java
@@ -14,19 +14,21 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.servicecomb.core.event;
+package org.apache.servicecomb.metrics.core.meter.invocation;
 
 import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.swagger.invocation.Response;
 
-public class InvocationStartEvent {
-  private Invocation invocation;
+import com.netflix.spectator.api.Id;
+import com.netflix.spectator.api.Registry;
 
-  public InvocationStartEvent(Invocation invocation) {
-    super();
-    this.invocation = invocation;
+public class ProducerInvocationMeters extends AbstractInvocationMeters {
+  public ProducerInvocationMeters(Registry registry) {
+    super(registry);
   }
 
-  public Invocation getInvocation() {
-    return invocation;
+  @Override
+  protected AbstractInvocationMeter createMeter(Id id, Invocation invocation, Response response) {
+    return new ProducerInvocationMeter(registry, id, invocation, response);
   }
 }
diff --git a/metrics/metrics-core/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer b/metrics/metrics-core/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer
new file mode 100644
index 0000000..2bf2069
--- /dev/null
+++ b/metrics/metrics-core/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicecomb.metrics.core.DefaultMetricsInitializer
diff --git a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultMetricsInitializer.java b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultMetricsInitializer.java
new file mode 100644
index 0000000..2c48866
--- /dev/null
+++ b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestDefaultMetricsInitializer.java
@@ -0,0 +1,138 @@
+/*
+ * 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.servicecomb.metrics.core;
+
+import org.apache.servicecomb.core.Const;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.event.InvocationFinishEvent;
+import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
+import org.apache.servicecomb.foundation.metrics.publish.spectator.MeasurementGroupConfig;
+import org.apache.servicecomb.foundation.metrics.publish.spectator.MeasurementTree;
+import org.apache.servicecomb.metrics.core.meter.invocation.MeterInvocationConst;
+import org.apache.servicecomb.swagger.invocation.InvocationType;
+import org.apache.servicecomb.swagger.invocation.Response;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.DefaultRegistry;
+import com.netflix.spectator.api.ManualClock;
+import com.netflix.spectator.api.Registry;
+import com.netflix.spectator.api.SpectatorUtils;
+
+import mockit.Expectations;
+import mockit.Mocked;
+
+public class TestDefaultMetricsInitializer {
+  EventBus eventBus = new EventBus();
+
+  ManualClock clock = new ManualClock();
+
+  CompositeRegistry globalRegistry = SpectatorUtils.createCompositeRegistry(new ManualClock());
+
+  DefaultMetricsInitializer metricsInitializer = new DefaultMetricsInitializer() {
+    protected Registry createRegistry(MetricsBootstrapConfig config) {
+      return new DefaultRegistry(new ManualClock());
+    };
+  };
+
+  MetricsBootstrapConfig config = new MetricsBootstrapConfig();
+
+  @Mocked
+  private Invocation invocation;
+
+  @Mocked
+  private Response response;
+
+
+  @Before
+  public void setup() {
+    metricsInitializer.init(globalRegistry, eventBus, config);
+  }
+
+  @Test
+  public void consumerInvocation(@Mocked InvocationFinishEvent event) {
+    new Expectations() {
+      {
+        invocation.isConsumer();
+        result = true;
+        invocation.getInvocationType();
+        result = InvocationType.CONSUMER;
+        invocation.getRealTransportName();
+        result = Const.RESTFUL;
+        invocation.getMicroserviceQualifiedName();
+        result = "m.s.o";
+        invocation.getStartTime();
+        result = 1;
+        event.getInvocation();
+        result = invocation;
+        event.getNanoCurrent();
+        result = 10;
+      }
+    };
+
+    eventBus.post(event);
+    eventBus.post(event);
+
+    MeasurementTree tree = new MeasurementTree();
+    tree.from(globalRegistry.iterator(), new MeasurementGroupConfig(MeterInvocationConst.INVOCATION_NAME));
+    Assert.assertEquals(
+        "[Measurement(servicecomb.invocation:operation=m.s.o:role=CONSUMER:stage=total:statistic=count:status=0:transport=rest,0,2.0), "
+            + "Measurement(servicecomb.invocation:operation=m.s.o:role=CONSUMER:stage=total:statistic=totalTime:status=0:transport=rest,0,18.0)]",
+        tree.findChild(MeterInvocationConst.INVOCATION_NAME).getMeasurements().toString());
+  }
+
+  @Test
+  public void producerInvocation(@Mocked InvocationFinishEvent event) {
+    new Expectations() {
+      {
+        invocation.isConsumer();
+        result = false;
+        invocation.getInvocationType();
+        result = InvocationType.PRODUCER;
+        invocation.getRealTransportName();
+        result = Const.RESTFUL;
+        invocation.getMicroserviceQualifiedName();
+        result = "m.s.o";
+        invocation.getStartTime();
+        result = 1;
+        invocation.getStartExecutionTime();
+        result = 3;
+        event.getNanoCurrent();
+        result = 10;
+        event.getInvocation();
+        result = invocation;
+      }
+    };
+
+    eventBus.post(event);
+    eventBus.post(event);
+
+    MeasurementTree tree = new MeasurementTree();
+    tree.from(globalRegistry.iterator(), new MeasurementGroupConfig(MeterInvocationConst.INVOCATION_NAME));
+    Assert.assertEquals(
+        "[Measurement(servicecomb.invocation:operation=m.s.o:role=PRODUCER:stage=execution:statistic=count:status=0:transport=rest,0,2.0), "
+            + "Measurement(servicecomb.invocation:operation=m.s.o:role=PRODUCER:stage=execution:statistic=totalTime:status=0:transport=rest,0,14.0), "
+            + "Measurement(servicecomb.invocation:operation=m.s.o:role=PRODUCER:stage=total:statistic=count:status=0:transport=rest,0,2.0), "
+            + "Measurement(servicecomb.invocation:operation=m.s.o:role=PRODUCER:stage=total:statistic=totalTime:status=0:transport=rest,0,18.0), "
+            + "Measurement(servicecomb.invocation:operation=m.s.o:role=PRODUCER:stage=queue:statistic=count:status=0:transport=rest,0,2.0), "
+            + "Measurement(servicecomb.invocation:operation=m.s.o:role=PRODUCER:stage=queue:statistic=totalTime:status=0:transport=rest,0,4.0)]",
+        tree.findChild(MeterInvocationConst.INVOCATION_NAME).getMeasurements().toString());
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.

[incubator-servicecomb-java-chassis] 01/04: SCB-383 define metrics init and poll mechanism

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 18ca4ed45fd47c69aabdaec9820584a7ec6b23d7
Author: wujimin <wu...@huawei.com>
AuthorDate: Mon Mar 12 16:26:06 2018 +0800

    SCB-383 define metrics init and poll mechanism
---
 .../foundation/metrics/MetricsBootstrap.java       | 82 +++++++++++++++++++
 .../foundation/metrics/MetricsBootstrapConfig.java | 36 ++++++++
 .../foundation/metrics/MetricsInitializer.java     | 27 ++++++
 .../foundation/metrics/PolledEvent.java            | 37 +++++++++
 .../foundation/metrics/TestMetricsBootstrap.java   | 95 ++++++++++++++++++++++
 5 files changed, 277 insertions(+)

diff --git a/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsBootstrap.java b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsBootstrap.java
new file mode 100644
index 0000000..2335264
--- /dev/null
+++ b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsBootstrap.java
@@ -0,0 +1,82 @@
+/*
+ * 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.servicecomb.foundation.metrics;
+
+import java.util.List;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.Lists;
+import com.google.common.eventbus.EventBus;
+import com.google.common.util.concurrent.ThreadFactoryBuilder;
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.Meter;
+
+public class MetricsBootstrap {
+  private static final Logger LOGGER = LoggerFactory.getLogger(MetricsBootstrap.class);
+
+  private CompositeRegistry globalRegistry;
+
+  private EventBus eventBus;
+
+  private MetricsBootstrapConfig config = new MetricsBootstrapConfig();
+
+  private ScheduledExecutorService executorService;
+
+  public void start(CompositeRegistry globalRegistry, EventBus eventBus) {
+    this.globalRegistry = globalRegistry;
+    this.eventBus = eventBus;
+    this.executorService = Executors.newScheduledThreadPool(1,
+        new ThreadFactoryBuilder()
+            .setNameFormat("spectator-poller-%d")
+            .build());
+
+    loadMetricsInitializers();
+    startPoll();
+  }
+
+  public void shutdown() {
+    executorService.shutdown();
+  }
+
+  protected void loadMetricsInitializers() {
+    SPIServiceUtils.getSortedService(MetricsInitializer.class).forEach(initializer -> {
+      LOGGER.info("Found MetricsInitializer: {}", initializer.getClass().getName());
+
+      initializer.init(globalRegistry, eventBus, config);
+    });
+  }
+
+  protected void startPoll() {
+    executorService.scheduleAtFixedRate(this::pollMeters,
+        0,
+        config.getMsPollInterval(),
+        TimeUnit.MILLISECONDS);
+  }
+
+  protected void pollMeters() {
+    List<Meter> meters = Lists.newArrayList(globalRegistry.iterator());
+    PolledEvent event = new PolledEvent(meters);
+
+    eventBus.post(event);
+  }
+}
diff --git a/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsBootstrapConfig.java b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsBootstrapConfig.java
new file mode 100644
index 0000000..41baa95
--- /dev/null
+++ b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsBootstrapConfig.java
@@ -0,0 +1,36 @@
+/*
+ * 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.servicecomb.foundation.metrics;
+
+import com.netflix.config.DynamicPropertyFactory;
+
+public class MetricsBootstrapConfig {
+  public static final String METRICS_WINDOW_TIME = "servicecomb.metrics.window_time";
+
+  public static final int DEFAULT_METRICS_WINDOW_TIME = 5000;
+
+  private long msPollInterval;
+
+  public MetricsBootstrapConfig() {
+    msPollInterval =
+        DynamicPropertyFactory.getInstance().getIntProperty(METRICS_WINDOW_TIME, DEFAULT_METRICS_WINDOW_TIME).get();;
+  }
+
+  public long getMsPollInterval() {
+    return msPollInterval;
+  }
+}
diff --git a/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsInitializer.java b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsInitializer.java
new file mode 100644
index 0000000..5e1aaef
--- /dev/null
+++ b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/MetricsInitializer.java
@@ -0,0 +1,27 @@
+/*
+ * 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.servicecomb.foundation.metrics;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.spectator.api.CompositeRegistry;
+
+public interface MetricsInitializer {
+  /** 
+   * if create new registry, must add to globalRegistry
+   */
+  void init(CompositeRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config);
+}
diff --git a/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/PolledEvent.java b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/PolledEvent.java
new file mode 100644
index 0000000..b2aa070
--- /dev/null
+++ b/foundations/foundation-metrics/src/main/java/org/apache/servicecomb/foundation/metrics/PolledEvent.java
@@ -0,0 +1,37 @@
+/*
+ * 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.servicecomb.foundation.metrics;
+
+import java.util.List;
+
+import com.netflix.spectator.api.Meter;
+
+public class PolledEvent {
+  private List<Meter> meters;
+
+  public PolledEvent(List<Meter> meters) {
+    this.meters = meters;
+  }
+
+  public List<Meter> getMeters() {
+    return meters;
+  }
+
+  public void setMeters(List<Meter> meters) {
+    this.meters = meters;
+  }
+}
diff --git a/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/TestMetricsBootstrap.java b/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/TestMetricsBootstrap.java
new file mode 100644
index 0000000..b8280f4
--- /dev/null
+++ b/foundations/foundation-metrics/src/test/java/org/apache/servicecomb/foundation/metrics/TestMetricsBootstrap.java
@@ -0,0 +1,95 @@
+/*
+ * 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.servicecomb.foundation.metrics;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.hamcrest.Matchers;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.google.common.eventbus.EventBus;
+import com.google.common.eventbus.Subscribe;
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.Meter;
+
+import mockit.Expectations;
+import mockit.Mocked;
+
+public class TestMetricsBootstrap {
+  MetricsBootstrap bootstrap = new MetricsBootstrap();
+
+  @Mocked
+  CompositeRegistry globalRegistry;
+
+  EventBus eventBus = new EventBus();
+
+  @After
+  public void teardown() {
+    bootstrap.shutdown();
+  }
+
+  @Test
+  public void loadMetricsInitializers() {
+    List<MetricsInitializer> initList = new ArrayList<>();
+    MetricsInitializer metricsInitializer = new MetricsInitializer() {
+      @Override
+      public void init(CompositeRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) {
+        initList.add(this);
+      }
+    };
+    new Expectations(SPIServiceUtils.class) {
+      {
+        SPIServiceUtils.getSortedService(MetricsInitializer.class);
+        result = Arrays.asList(metricsInitializer, metricsInitializer);
+      }
+    };
+
+    bootstrap.start(globalRegistry, eventBus);
+
+    Assert.assertThat(initList, Matchers.contains(metricsInitializer, metricsInitializer));
+  }
+
+  @Test
+  public void pollMeters() {
+    bootstrap.start(globalRegistry, eventBus);
+
+    List<Meter> meters = new ArrayList<>();
+    new Expectations() {
+      {
+        globalRegistry.iterator();
+        result = meters.iterator();
+      }
+    };
+
+    PolledEvent result = new PolledEvent(null);
+    eventBus.register(new Object() {
+      @Subscribe
+      public void onEvent(PolledEvent event) {
+        result.setMeters(event.getMeters());
+      }
+    });
+
+    bootstrap.pollMeters();
+
+    Assert.assertEquals(meters, result.getMeters());
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.

[incubator-servicecomb-java-chassis] 03/04: SCB-383 add support of create spectator GlobalRegistr for UT.

Posted by li...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git

commit 34ab156eaa0a3a1dddabb45eebe226ab123b90ec
Author: wujimin <wu...@huawei.com>
AuthorDate: Mon Mar 26 21:35:06 2018 +0800

    SCB-383 add support of create spectator GlobalRegistr for UT.
---
 foundations/foundation-test-scaffolding/pom.xml    |  4 ++++
 .../com/netflix/spectator/api/SpectatorUtils.java  | 23 ++++++++++++++++++++++
 2 files changed, 27 insertions(+)

diff --git a/foundations/foundation-test-scaffolding/pom.xml b/foundations/foundation-test-scaffolding/pom.xml
index cf5592f..abfe024 100644
--- a/foundations/foundation-test-scaffolding/pom.xml
+++ b/foundations/foundation-test-scaffolding/pom.xml
@@ -44,5 +44,9 @@
       <groupId>io.vertx</groupId>
       <artifactId>vertx-web</artifactId>
     </dependency>
+    <dependency>
+      <groupId>com.netflix.spectator</groupId>
+      <artifactId>spectator-reg-servo</artifactId>
+    </dependency>
   </dependencies>
 </project>
diff --git a/foundations/foundation-test-scaffolding/src/main/java/com/netflix/spectator/api/SpectatorUtils.java b/foundations/foundation-test-scaffolding/src/main/java/com/netflix/spectator/api/SpectatorUtils.java
new file mode 100644
index 0000000..7a2f785
--- /dev/null
+++ b/foundations/foundation-test-scaffolding/src/main/java/com/netflix/spectator/api/SpectatorUtils.java
@@ -0,0 +1,23 @@
+/*
+ * 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 com.netflix.spectator.api;
+
+public class SpectatorUtils {
+  public static CompositeRegistry createCompositeRegistry(Clock clock) {
+    return new CompositeRegistry(clock);
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
liubao@apache.org.