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:36 UTC

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

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.