You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2018/04/03 08:26:56 UTC

[incubator-servicecomb-java-chassis] 02/05: SCB-385 metrics publisher switch to new mechanism, and not depend on springmvc provider anymore.

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

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

commit c37e055606c988dc70a781a41b9bc1a640ea0ab5
Author: wujimin <wu...@huawei.com>
AuthorDate: Fri Mar 30 16:58:41 2018 +0800

    SCB-385 metrics publisher switch to new mechanism, and not depend on springmvc provider anymore.
---
 metrics/metrics-core/pom.xml                       |  8 +-
 .../metrics/core/publish/MetricsPublisher.java     | 42 -----------
 .../metrics/core/publish/MetricsRestPublisher.java | 87 ++++++++++++++++++++++
 .../metrics/core/TestMetricsPublisher.java         | 36 ---------
 .../core/publish/TestMetricsRestPublisher.java     | 61 +++++++++++++++
 5 files changed, 152 insertions(+), 82 deletions(-)

diff --git a/metrics/metrics-core/pom.xml b/metrics/metrics-core/pom.xml
index f4d1e76..360c0b6 100644
--- a/metrics/metrics-core/pom.xml
+++ b/metrics/metrics-core/pom.xml
@@ -30,16 +30,16 @@
 
   <dependencies>
     <dependency>
-      <groupId>com.netflix.servo</groupId>
-      <artifactId>servo-core</artifactId>
+      <groupId>org.apache.servicecomb</groupId>
+      <artifactId>foundation-metrics</artifactId>
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
-      <artifactId>foundation-metrics</artifactId>
+      <artifactId>java-chassis-core</artifactId>
     </dependency>
     <dependency>
       <groupId>org.apache.servicecomb</groupId>
-      <artifactId>provider-springmvc</artifactId>
+      <artifactId>swagger-generator-jaxrs</artifactId>
     </dependency>
 
     <dependency>
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/MetricsPublisher.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/MetricsPublisher.java
deleted file mode 100644
index 56a5cb0..0000000
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/MetricsPublisher.java
+++ /dev/null
@@ -1,42 +0,0 @@
-/*
- * 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.publish;
-
-import java.util.Map;
-
-import org.apache.servicecomb.metrics.core.MonitorManager;
-import org.apache.servicecomb.provider.rest.common.RestSchema;
-import org.springframework.web.bind.annotation.CrossOrigin;
-import org.springframework.web.bind.annotation.RequestMapping;
-import org.springframework.web.bind.annotation.RequestMethod;
-
-import io.swagger.annotations.ApiResponse;
-import io.swagger.annotations.ApiResponses;
-
-@RestSchema(schemaId = "metricsEndpoint")
-@RequestMapping(path = "/metrics")
-public class MetricsPublisher {
-  @ApiResponses({
-      @ApiResponse(code = 400, response = String.class, message = "illegal request content"),
-  })
-  @RequestMapping(path = "/", method = RequestMethod.GET)
-  @CrossOrigin
-  public Map<String, Double> measure() {
-    return MonitorManager.getInstance().measure();
-  }
-}
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/MetricsRestPublisher.java b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/MetricsRestPublisher.java
new file mode 100644
index 0000000..2669b37
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/MetricsRestPublisher.java
@@ -0,0 +1,87 @@
+/*
+ * 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.publish;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.stream.Collectors;
+import java.util.stream.StreamSupport;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
+import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
+import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.Id;
+
+import io.swagger.annotations.ApiResponse;
+import io.swagger.annotations.ApiResponses;
+
+@Path("/metrics")
+public class MetricsRestPublisher implements MetricsInitializer {
+  private CompositeRegistry globalRegistry;
+
+  @Override
+  public void init(CompositeRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) {
+    this.globalRegistry = globalRegistry;
+  }
+
+  @ApiResponses({
+      @ApiResponse(code = 400, response = String.class, message = "illegal request content"),
+  })
+  @GET
+  @Path("/")
+  public Map<String, Double> measure() {
+    Map<String, Double> measurements = new LinkedHashMap<>();
+    if (globalRegistry == null) {
+      return measurements;
+    }
+
+    StringBuilder sb = new StringBuilder();
+    globalRegistry
+        .iterator()
+        .forEachRemaining(meter -> {
+          meter.measure().forEach(measurement -> {
+            String key = idToString(measurement.id(), sb);
+            measurements.put(key, measurement.value());
+          });
+        });
+
+    return measurements;
+  }
+
+  // format id to string:
+  // idName(tag1=value1,tag2=value2)
+  protected String idToString(Id id, StringBuilder sb) {
+    sb.setLength(0);
+    sb.append(id.name()).append('(');
+    sb.append(StreamSupport
+        .stream(id
+            .tags()
+            .spliterator(), false)
+        .map(Object::toString)
+        .collect(
+            Collectors.joining(",")));
+    sb.append(')');
+
+    return sb.toString();
+  }
+}
diff --git a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestMetricsPublisher.java b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestMetricsPublisher.java
deleted file mode 100644
index a38a0a9..0000000
--- a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestMetricsPublisher.java
+++ /dev/null
@@ -1,36 +0,0 @@
-/*
- * 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 java.util.Map;
-
-import org.apache.servicecomb.metrics.core.publish.MetricsPublisher;
-import org.junit.Assert;
-import org.junit.Test;
-
-public class TestMetricsPublisher {
-  @Test
-  public void testMeasure() {
-    System.getProperties().setProperty(MetricsConfig.METRICS_WINDOW_TIME, "2000");
-
-    MetricsPublisher publisher = new MetricsPublisher();
-    Map<String, Double> metrics = publisher.measure();
-    //10 jvm metrics get
-    Assert.assertEquals(10, metrics.size());
-  }
-}
diff --git a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestMetricsRestPublisher.java b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestMetricsRestPublisher.java
new file mode 100644
index 0000000..4278eae
--- /dev/null
+++ b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestMetricsRestPublisher.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.metrics.core.publish;
+
+import java.util.Map;
+
+import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
+import org.junit.Assert;
+import org.junit.Test;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.spectator.api.Clock;
+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;
+
+public class TestMetricsRestPublisher {
+  MetricsRestPublisher publisher = new MetricsRestPublisher();
+
+  @Test
+  public void measure_globalRegistryNull() {
+    Map<String, Double> result = publisher.measure();
+
+    Assert.assertEquals(0, result.size());
+  }
+
+  @Test
+  public void measure_normal() {
+    Clock clock = new ManualClock();
+    CompositeRegistry globalRegistry = SpectatorUtils.createCompositeRegistry(clock);
+    Registry registry = new DefaultRegistry(clock);
+    registry.timer(registry.createId("name", "t1", "v1", "t2", "v2"));
+    globalRegistry.add(registry);
+
+    EventBus eventBus = new EventBus();
+
+    publisher.init(globalRegistry, eventBus, new MetricsBootstrapConfig());
+    Map<String, Double> result = publisher.measure();
+
+
+    Assert.assertEquals(2, result.size());
+    Assert.assertEquals(0, result.get("name(statistic=count,t1=v1,t2=v2)"), 0);
+    Assert.assertEquals(0, result.get("name(statistic=totalTime,t1=v1,t2=v2)"), 0);
+  }
+}

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