servicecomb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] wujimin closed pull request #628: [SCB-385] Metrics publisher switch to new mechanism
Date Tue, 03 Apr 2018 08:26:53 GMT
wujimin closed pull request #628: [SCB-385] Metrics publisher switch to new mechanism
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/628
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/SpringmvcClient.java
b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/SpringmvcClient.java
index 42d543752..fa53abc16 100644
--- a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/SpringmvcClient.java
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/SpringmvcClient.java
@@ -96,20 +96,14 @@ public static void run() {
     }
 
     //0.5.0 later version metrics integration test
-    try {
-      Thread.sleep(1000);
-      @SuppressWarnings("unchecked")
-      Map<String, Double> metrics = restTemplate.getForObject(prefix + "/metrics",
Map.class);
-
-      TestMgr
-          .check(true, metrics.get("jvm(name=heapUsed,statistic=gauge)") != 0);
-      TestMgr.check(true, metrics.size() > 0);
-      TestMgr.check(true,
-          metrics.get(
-              "servicecomb.invocation(operation=springmvc.codeFirst.saySomething,role=producer,stage=total,statistic=count,status=200)")
>= 0);
-    } catch (Exception e) {
-      TestMgr.check("true", "false");
-    }
+    @SuppressWarnings("unchecked")
+    Map<String, Double> metrics = restTemplate.getForObject(prefix + "/metrics", Map.class);
+
+//    TestMgr.check(true, metrics.get("jvm(name=heapUsed,statistic=gauge)") != 0);
+    TestMgr.check(true, metrics.size() > 0);
+    TestMgr.check(true,
+        metrics.get(
+            "servicecomb.invocation(operation=springmvc.codeFirst.saySomething,role=PRODUCER,stage=total,statistic=count,status=200,transport=highway)")
>= 0);
 
     //prometheus integration test
     try {
diff --git a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
index be64c9e6c..fbe42c314 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
+++ b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
@@ -82,6 +82,3 @@ ssl.keyStoreValue: Changeme_123
 ssl.crl: revoke.crl
 ssl.sslCustomClass: org.apache.servicecomb.demo.DemoSSLCustom
 
-servicecomb:
-  metrics:
-    window_time: 1000
diff --git a/metrics/metrics-core/pom.xml b/metrics/metrics-core/pom.xml
index f4d1e761b..360c0b6d7 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/MetricsBootListener.java
b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/MetricsBootListener.java
index 48c2837b3..289c8796f 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/MetricsBootListener.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/MetricsBootListener.java
@@ -16,9 +16,18 @@
  */
 package org.apache.servicecomb.metrics.core;
 
+import javax.inject.Inject;
+
 import org.apache.servicecomb.core.BootListener;
+import org.apache.servicecomb.core.definition.schema.ProducerSchemaFactory;
 import org.apache.servicecomb.foundation.common.event.EventManager;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
 import org.apache.servicecomb.foundation.metrics.MetricsBootstrap;
+import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
+import org.apache.servicecomb.metrics.core.publish.HealthCheckerRestPublisher;
+import org.apache.servicecomb.metrics.core.publish.MetricsRestPublisher;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
 import org.springframework.stereotype.Component;
 
 import com.netflix.spectator.api.Spectator;
@@ -27,12 +36,36 @@
 public class MetricsBootListener implements BootListener {
   private MetricsBootstrap metricsBootstrap = new MetricsBootstrap();
 
+  @Inject
+  private ProducerSchemaFactory producerSchemaFactory;
+
   @Override
   public void onBootEvent(BootEvent event) {
-    if (!EventType.AFTER_REGISTRY.equals(event.getEventType())) {
-      return;
+    switch (event.getEventType()) {
+      case BEFORE_PRODUCER_PROVIDER:
+        registerSchemas();
+        break;
+      case AFTER_REGISTRY:
+        metricsBootstrap.start(Spectator.globalRegistry(), EventManager.getEventBus());
+        break;
+      default:
+        break;
     }
+  }
+
+  private void registerSchemas() {
+    Microservice microservice = RegistryUtils.getMicroservice();
+
+    producerSchemaFactory.getOrCreateProducerSchema(microservice.getServiceName(),
+        "healthEndpoint",
+        HealthCheckerRestPublisher.class,
+        new HealthCheckerRestPublisher());
 
-    metricsBootstrap.start(Spectator.globalRegistry(), EventManager.getEventBus());
+    MetricsRestPublisher metricsRestPublisher =
+        SPIServiceUtils.getTargetService(MetricsInitializer.class, MetricsRestPublisher.class);
+    producerSchemaFactory.getOrCreateProducerSchema(microservice.getServiceName(),
+        "metricsEndpoint",
+        metricsRestPublisher.getClass(),
+        metricsRestPublisher);
   }
 }
diff --git a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/HealthCheckerPublisher.java
b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/HealthCheckerRestPublisher.java
similarity index 73%
rename from metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/HealthCheckerPublisher.java
rename to metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/HealthCheckerRestPublisher.java
index fba4f44e2..ccc76438e 100644
--- a/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/HealthCheckerPublisher.java
+++ b/metrics/metrics-core/src/main/java/org/apache/servicecomb/metrics/core/publish/HealthCheckerRestPublisher.java
@@ -19,18 +19,16 @@
 
 import java.util.Map;
 
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+
 import org.apache.servicecomb.foundation.metrics.health.HealthCheckResult;
 import org.apache.servicecomb.foundation.metrics.health.HealthCheckerManager;
-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;
 
-@RestSchema(schemaId = "healthEndpoint")
-@RequestMapping(path = "/health")
-public class HealthCheckerPublisher {
-  @RequestMapping(path = "/", method = RequestMethod.GET)
-  @CrossOrigin
+@Path("/health")
+public class HealthCheckerRestPublisher {
+  @Path("/")
+  @GET
   public boolean checkHealth() {
     Map<String, HealthCheckResult> results = HealthCheckerManager.getInstance().check();
     for (HealthCheckResult result : results.values()) {
@@ -41,8 +39,8 @@ public boolean checkHealth() {
     return true;
   }
 
-  @RequestMapping(path = "/details", method = RequestMethod.GET)
-  @CrossOrigin
+  @Path("/details")
+  @GET
   public Map<String, HealthCheckResult> checkHealthDetails() {
     return HealthCheckerManager.getInstance().check();
   }
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 56a5cb017..000000000
--- 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 000000000..2669b375f
--- /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/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
index 293334974..fb6c4662b 100644
--- 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
@@ -16,4 +16,5 @@
 #
 
 org.apache.servicecomb.metrics.core.DefaultMetricsInitializer
-org.apache.servicecomb.metrics.core.publish.DefaultLogPublisher
\ No newline at end of file
+org.apache.servicecomb.metrics.core.publish.DefaultLogPublisher
+org.apache.servicecomb.metrics.core.publish.MetricsRestPublisher
\ No newline at end of file
diff --git a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestMetricsBootListener.java
b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestMetricsBootListener.java
new file mode 100644
index 000000000..ce2d4627a
--- /dev/null
+++ b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestMetricsBootListener.java
@@ -0,0 +1,85 @@
+/*
+ * 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.ArrayList;
+import java.util.List;
+
+import org.apache.servicecomb.core.BootListener.BootEvent;
+import org.apache.servicecomb.core.BootListener.EventType;
+import org.apache.servicecomb.core.definition.SchemaMeta;
+import org.apache.servicecomb.core.definition.schema.ProducerSchemaFactory;
+import org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
+import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
+import org.apache.servicecomb.metrics.core.publish.HealthCheckerRestPublisher;
+import org.apache.servicecomb.metrics.core.publish.MetricsRestPublisher;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
+import org.apache.servicecomb.serviceregistry.api.registry.Microservice;
+import org.junit.Assert;
+import org.junit.Test;
+
+import mockit.Deencapsulation;
+import mockit.Expectations;
+import mockit.Mock;
+import mockit.MockUp;
+
+public class TestMetricsBootListener {
+  MetricsBootListener listener = new MetricsBootListener();
+
+  @Test
+  public void registerSchemas() {
+    List<Object[]> argsList = new ArrayList<>();
+
+    ProducerSchemaFactory producerSchemaFactory = new MockUp<ProducerSchemaFactory>()
{
+      @Mock
+      SchemaMeta getOrCreateProducerSchema(String microserviceName, String schemaId,
+          Class<?> producerClass,
+          Object producerInstance) {
+        argsList.add(new Object[] {microserviceName, schemaId, producerClass, producerInstance});
+        return null;
+      }
+    }.getMockInstance();
+    Deencapsulation.setField(listener, "producerSchemaFactory", producerSchemaFactory);
+
+    Microservice microservice = new Microservice();
+    microservice.setServiceName("name");
+    new Expectations(RegistryUtils.class) {
+      {
+        RegistryUtils.getMicroservice();
+        result = microservice;
+      }
+    };
+
+    BootEvent event = new BootEvent();
+    event.setEventType(EventType.BEFORE_PRODUCER_PROVIDER);
+    listener.onBootEvent(event);
+
+    Object[] args = argsList.get(0);
+    Assert.assertEquals("name", args[0]);
+    Assert.assertEquals("healthEndpoint", args[1]);
+    Assert.assertEquals(HealthCheckerRestPublisher.class, args[2]);
+    Assert.assertEquals(HealthCheckerRestPublisher.class, args[3].getClass());
+
+    MetricsRestPublisher metricsRestPublisher =
+        SPIServiceUtils.getTargetService(MetricsInitializer.class, MetricsRestPublisher.class);
+    args = argsList.get(1);
+    Assert.assertEquals("name", args[0]);
+    Assert.assertEquals("metricsEndpoint", args[1]);
+    Assert.assertEquals(MetricsRestPublisher.class, args[2]);
+    Assert.assertSame(metricsRestPublisher, args[3]);
+  }
+}
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 a38a0a97a..000000000
--- 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/TestHealthCheckerPublisher.java
b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestHealthCheckerPublisher.java
similarity index 90%
rename from metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestHealthCheckerPublisher.java
rename to metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestHealthCheckerPublisher.java
index d0c182e2d..bf8a14619 100644
--- a/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/TestHealthCheckerPublisher.java
+++ b/metrics/metrics-core/src/test/java/org/apache/servicecomb/metrics/core/publish/TestHealthCheckerPublisher.java
@@ -15,14 +15,14 @@
  * limitations under the License.
  */
 
-package org.apache.servicecomb.metrics.core;
+package org.apache.servicecomb.metrics.core.publish;
 
 import java.util.Map;
 
 import org.apache.servicecomb.foundation.metrics.health.HealthCheckResult;
 import org.apache.servicecomb.foundation.metrics.health.HealthChecker;
 import org.apache.servicecomb.foundation.metrics.health.HealthCheckerManager;
-import org.apache.servicecomb.metrics.core.publish.HealthCheckerPublisher;
+import org.apache.servicecomb.metrics.core.publish.HealthCheckerRestPublisher;
 import org.junit.Assert;
 import org.junit.Before;
 import org.junit.Test;
@@ -62,7 +62,7 @@ public void reset() {
   @Test
   public void checkHealthGood() {
     HealthCheckerManager.getInstance().register(good);
-    HealthCheckerPublisher publisher = new HealthCheckerPublisher();
+    HealthCheckerRestPublisher publisher = new HealthCheckerRestPublisher();
     Assert.assertEquals(true, publisher.checkHealth());
   }
 
@@ -70,7 +70,7 @@ public void checkHealthGood() {
   public void checkHealthBad() {
     HealthCheckerManager.getInstance().register(good);
     HealthCheckerManager.getInstance().register(bad);
-    HealthCheckerPublisher publisher = new HealthCheckerPublisher();
+    HealthCheckerRestPublisher publisher = new HealthCheckerRestPublisher();
     Assert.assertEquals(false, publisher.checkHealth());
   }
 
@@ -78,7 +78,7 @@ public void checkHealthBad() {
   public void checkHealthDetails() {
     HealthCheckerManager.getInstance().register(good);
     HealthCheckerManager.getInstance().register(bad);
-    HealthCheckerPublisher publisher = new HealthCheckerPublisher();
+    HealthCheckerRestPublisher publisher = new HealthCheckerRestPublisher();
     Map<String, HealthCheckResult> content = publisher.checkHealthDetails();
     Assert.assertEquals(true, content.get("test").isHealthy());
     Assert.assertEquals("info", content.get("test").getInformation());
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 000000000..4278eae93
--- /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);
+  }
+}
diff --git a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
index e30bf6fbd..a3c25f923 100644
--- a/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
+++ b/service-registry/src/main/java/org/apache/servicecomb/serviceregistry/registry/AbstractServiceRegistry.java
@@ -195,16 +195,11 @@ private boolean needSetFrameworkVersion() {
     if (serviceCenterInfo == null) {
       LOGGER.warn("Server startup when service center not started and cannot retrieve version
info, assume latest.");
       return true;
-    } else {
-      Version scVersion = VersionUtils.getOrCreate(serviceCenterInfo.getVersion());
-      Version frameworkVersion = VersionUtils.getOrCreate(Const.SERVICECENTER_FRAMEWORK_VERSION);
-
-      if (scVersion.compareTo(frameworkVersion) >= 0) {
-        return true;
-      } else {
-        return false;
-      }
     }
+
+    Version scVersion = VersionUtils.getOrCreate(serviceCenterInfo.getVersion());
+    Version frameworkVersion = VersionUtils.getOrCreate(Const.SERVICECENTER_FRAMEWORK_VERSION);
+    return scVersion.compareTo(frameworkVersion) >= 0;
   }
 
   private void loadStaticConfiguration() {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message