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/09/07 02:45:06 UTC

[incubator-servicecomb-java-chassis] 01/03: [SCB-894][WIP] create vertx metrics factory

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 8f2bf95b25bc78e03f8195be30b5f81b2f8fc75d
Author: wujimin <wu...@huawei.com>
AuthorDate: Wed Sep 5 10:33:25 2018 +0800

    [SCB-894][WIP] create vertx metrics factory
---
 .../vertx/metrics/DefaultVertxMetrics.java         | 91 ++++++++++++++++++++++
 .../vertx/metrics/DefaultVertxMetricsFactory.java  | 61 +++++++++++++++
 .../foundation/vertx/metrics/MetricsOptionsEx.java | 47 +++++++++++
 .../metrics/TestDefaultVertxMetricsFactory.java    | 51 ++++++++++++
 .../vertx/metrics/TestMetricsOptionsEx.java        | 47 +++++++++++
 5 files changed, 297 insertions(+)

diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/DefaultVertxMetrics.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/DefaultVertxMetrics.java
new file mode 100644
index 0000000..e0dfcee
--- /dev/null
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/DefaultVertxMetrics.java
@@ -0,0 +1,91 @@
+/*
+ * 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.vertx.metrics;
+
+import java.util.Map;
+
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.foundation.vertx.metrics.metric.DefaultClientEndpointMetricManager;
+import org.apache.servicecomb.foundation.vertx.metrics.metric.DefaultEndpointMetric;
+
+import io.vertx.core.Vertx;
+import io.vertx.core.VertxOptions;
+import io.vertx.core.http.HttpClient;
+import io.vertx.core.http.HttpClientOptions;
+import io.vertx.core.http.HttpServer;
+import io.vertx.core.http.HttpServerOptions;
+import io.vertx.core.metrics.impl.DummyVertxMetrics;
+import io.vertx.core.net.SocketAddress;
+import io.vertx.core.spi.metrics.HttpClientMetrics;
+import io.vertx.core.spi.metrics.HttpServerMetrics;
+
+public class DefaultVertxMetrics extends DummyVertxMetrics {
+  private final Vertx vertx;
+
+  private VertxOptions vertxOptions;
+
+  // to support listen multiple addresses, must use a map to manage the metric
+  private Map<SocketAddress, DefaultEndpointMetric> serverEndpointMetricMap = new ConcurrentHashMapEx<>();
+
+  private volatile DefaultClientEndpointMetricManager clientEndpointMetricManager;
+
+  public DefaultVertxMetrics(Vertx vertx, VertxOptions vertxOptions) {
+    this.vertx = vertx;
+    this.vertxOptions = vertxOptions;
+  }
+
+  public Vertx getVertx() {
+    return vertx;
+  }
+
+  public DefaultClientEndpointMetricManager getClientEndpointMetricManager() {
+    return clientEndpointMetricManager;
+  }
+
+  @Override
+  public HttpServerMetrics<?, ?, ?> createMetrics(HttpServer server, SocketAddress localAddress,
+      HttpServerOptions options) {
+    DefaultEndpointMetric endpointMetric = serverEndpointMetricMap
+        .computeIfAbsent(localAddress, DefaultEndpointMetric::new);
+    return new DefaultHttpServerMetrics(endpointMetric, server, localAddress, options);
+  }
+
+  @Override
+  public HttpClientMetrics<?, ?, ?, ?, ?> createMetrics(HttpClient client, HttpClientOptions options) {
+    if (clientEndpointMetricManager == null) {
+      synchronized (vertx) {
+        if (clientEndpointMetricManager == null) {
+          clientEndpointMetricManager = new DefaultClientEndpointMetricManager(vertx,
+              (MetricsOptionsEx) vertxOptions.getMetricsOptions());
+        }
+      }
+    }
+
+    return new DefaultHttpClientMetrics(clientEndpointMetricManager, client, options);
+  }
+
+  @Override
+  public boolean isMetricsEnabled() {
+    return true;
+  }
+
+  @Deprecated
+  @Override
+  public boolean isEnabled() {
+    return true;
+  }
+}
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/DefaultVertxMetricsFactory.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/DefaultVertxMetricsFactory.java
new file mode 100644
index 0000000..1ceb916
--- /dev/null
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/DefaultVertxMetricsFactory.java
@@ -0,0 +1,61 @@
+/*
+ * 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.vertx.metrics;
+
+import io.vertx.core.Vertx;
+import io.vertx.core.VertxOptions;
+import io.vertx.core.metrics.MetricsOptions;
+import io.vertx.core.spi.VertxMetricsFactory;
+import io.vertx.core.spi.metrics.VertxMetrics;
+
+/**
+ * <pre>
+ * only for one vertx instance
+ * DO NOT inject to vertx by SPI
+ * typical usage:
+ *     DefaultVertxMetricsFactory factory = new DefaultVertxMetricsFactory();
+ *     MetricsOptionsEx metricsOptionsEx = (MetricsOptionsEx) factory.newOptions();
+ *
+ *     VertxOptions vertxOptions = new VertxOptions();
+ *     vertxOptions.setMetricsOptions(metricsOptionsEx);
+ *
+ *     Vertx vertx = Vertx.vertx(vertxOptions);
+ * </pre>
+ */
+public class DefaultVertxMetricsFactory implements VertxMetricsFactory {
+  private DefaultVertxMetrics vertxMetrics;
+
+  public DefaultVertxMetrics getVertxMetrics() {
+    return vertxMetrics;
+  }
+
+  @Override
+  public synchronized VertxMetrics metrics(Vertx vertx, VertxOptions options) {
+    if (vertxMetrics == null) {
+      vertxMetrics = new DefaultVertxMetrics(vertx, options);
+    }
+    return vertxMetrics;
+  }
+
+  @Override
+  public MetricsOptions newOptions() {
+    MetricsOptionsEx metricsOptions = new MetricsOptionsEx();
+    metricsOptions.setFactory(this);
+    metricsOptions.setEnabled(true);
+    return metricsOptions;
+  }
+}
diff --git a/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/MetricsOptionsEx.java b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/MetricsOptionsEx.java
new file mode 100644
index 0000000..702ca64
--- /dev/null
+++ b/foundations/foundation-vertx/src/main/java/org/apache/servicecomb/foundation/vertx/metrics/MetricsOptionsEx.java
@@ -0,0 +1,47 @@
+/*
+ * 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.vertx.metrics;
+
+import java.util.concurrent.TimeUnit;
+
+import io.vertx.core.metrics.MetricsOptions;
+
+public class MetricsOptionsEx extends MetricsOptions {
+  private long checkClientEndpointMetricIntervalInMilliseconds = TimeUnit.MINUTES.toMillis(1);
+
+  private long checkClientEndpointMetricExpiredInNano = TimeUnit.MINUTES.toNanos(30);
+
+  public long getCheckClientEndpointMetricIntervalInMilliseconds() {
+    return checkClientEndpointMetricIntervalInMilliseconds;
+  }
+
+  public void setCheckClientEndpointMetricIntervalInMinute(long minute) {
+    this.checkClientEndpointMetricIntervalInMilliseconds = TimeUnit.MINUTES.toMillis(minute);
+  }
+
+  public long getCheckClientEndpointMetricExpiredInNano() {
+    return checkClientEndpointMetricExpiredInNano;
+  }
+
+  public void setCheckClientEndpointMetricExpiredInNano(long nanoTime) {
+    this.checkClientEndpointMetricExpiredInNano = nanoTime;
+  }
+
+  public void setCheckClientEndpointMetricExpiredInMinute(long minute) {
+    this.checkClientEndpointMetricExpiredInNano = TimeUnit.MINUTES.toNanos(minute);
+  }
+}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestDefaultVertxMetricsFactory.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestDefaultVertxMetricsFactory.java
new file mode 100644
index 0000000..a96f23c
--- /dev/null
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestDefaultVertxMetricsFactory.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.foundation.vertx.metrics;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.vertx.core.Vertx;
+import io.vertx.core.VertxOptions;
+import io.vertx.core.metrics.MetricsOptions;
+import io.vertx.core.spi.metrics.VertxMetrics;
+import mockit.Mocked;
+
+public class TestDefaultVertxMetricsFactory {
+  @Mocked
+  Vertx vertx;
+
+  VertxOptions options = new VertxOptions();
+
+  DefaultVertxMetricsFactory factory = new DefaultVertxMetricsFactory();
+
+  @SuppressWarnings("deprecation")
+  @Test
+  public void metrics() {
+    MetricsOptions metricsOptions = factory.newOptions();
+    options.setMetricsOptions(metricsOptions);
+    VertxMetrics vertxMetrics = factory.metrics(vertx, options);
+
+    Assert.assertSame(factory, metricsOptions.getFactory());
+    Assert.assertTrue(metricsOptions.isEnabled());
+
+    Assert.assertSame(factory.getVertxMetrics(), vertxMetrics);
+    Assert.assertSame(vertx, ((DefaultVertxMetrics) vertxMetrics).getVertx());
+    Assert.assertTrue(vertxMetrics.isMetricsEnabled());
+    Assert.assertTrue(vertxMetrics.isEnabled());
+  }
+}
diff --git a/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestMetricsOptionsEx.java b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestMetricsOptionsEx.java
new file mode 100644
index 0000000..cced353
--- /dev/null
+++ b/foundations/foundation-vertx/src/test/java/org/apache/servicecomb/foundation/vertx/metrics/TestMetricsOptionsEx.java
@@ -0,0 +1,47 @@
+/*
+ * 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.vertx.metrics;
+
+import java.util.concurrent.TimeUnit;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestMetricsOptionsEx {
+  MetricsOptionsEx metricsOptionsEx = new MetricsOptionsEx();
+
+  @Test
+  public void interval() {
+    Assert.assertEquals(TimeUnit.MINUTES.toMillis(1),
+        metricsOptionsEx.getCheckClientEndpointMetricIntervalInMilliseconds());
+
+    metricsOptionsEx.setCheckClientEndpointMetricIntervalInMinute(2);
+    Assert.assertEquals(TimeUnit.MINUTES.toMillis(2),
+        metricsOptionsEx.getCheckClientEndpointMetricIntervalInMilliseconds());
+  }
+
+  @Test
+  public void expired() {
+    Assert.assertEquals(TimeUnit.MINUTES.toNanos(30), metricsOptionsEx.getCheckClientEndpointMetricExpiredInNano());
+
+    metricsOptionsEx.setCheckClientEndpointMetricExpiredInNano(10);
+    Assert.assertEquals(10, metricsOptionsEx.getCheckClientEndpointMetricExpiredInNano());
+
+    metricsOptionsEx.setCheckClientEndpointMetricExpiredInMinute(60);
+    Assert.assertEquals(TimeUnit.MINUTES.toNanos(60), metricsOptionsEx.getCheckClientEndpointMetricExpiredInNano());
+  }
+}