You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by ni...@apache.org on 2018/01/02 10:44:22 UTC

[incubator-servicecomb-java-chassis] 04/14: JAV-508 & SCB-11 minor refactor

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

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

commit 251c57e9ddeef926d19431853ae27fef60cb8743
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Tue Dec 26 11:37:25 2017 +0800

    JAV-508 & SCB-11 minor refactor
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 metrics/metrics-core/pom.xml                       |  8 ++
 ...Publisher.java => DefaultMetricsPublisher.java} | 36 ++++-----
 .../metrics/core/publish/MetricsBootListener.java  | 86 ----------------------
 .../metrics/core/publish/MetricsPublisher.java     |  6 +-
 ...ecomb_internal_metrics_contract_definition.yaml | 42 -----------
 .../io/servicecomb/metrics/core/TestPublisher.java | 30 +++++---
 6 files changed, 50 insertions(+), 158 deletions(-)

diff --git a/metrics/metrics-core/pom.xml b/metrics/metrics-core/pom.xml
index 798477c..9a61c21 100644
--- a/metrics/metrics-core/pom.xml
+++ b/metrics/metrics-core/pom.xml
@@ -31,6 +31,14 @@
   <dependencies>
     <dependency>
       <groupId>io.servicecomb</groupId>
+      <artifactId>provider-rest-common</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>org.springframework</groupId>
+      <artifactId>spring-web</artifactId>
+    </dependency>
+    <dependency>
+      <groupId>io.servicecomb</groupId>
       <artifactId>java-chassis-core</artifactId>
     </dependency>
     <dependency>
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/JsonMetricsPublisher.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultMetricsPublisher.java
similarity index 51%
rename from metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/JsonMetricsPublisher.java
rename to metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultMetricsPublisher.java
index 535c6de..ebc8f89 100644
--- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/JsonMetricsPublisher.java
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/DefaultMetricsPublisher.java
@@ -17,32 +17,32 @@
 
 package io.servicecomb.metrics.core.publish;
 
-import org.springframework.stereotype.Component;
+import org.springframework.web.bind.annotation.PathVariable;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
+import io.servicecomb.metrics.core.metric.RegistryMetric;
+import io.servicecomb.provider.rest.common.RestSchema;
 
-import io.servicecomb.foundation.common.exceptions.ServiceCombException;
-import io.servicecomb.foundation.common.utils.JsonUtils;
-
-@Component
-public class JsonMetricsPublisher implements MetricsPublisher {
+@RestSchema(schemaId = "metricsEndpoint")
+@RequestMapping(path = "/metrics")
+public class DefaultMetricsPublisher implements MetricsPublisher {
 
   private final DataSource dataSource;
 
-  public JsonMetricsPublisher(DataSource dataSource) {
+  public DefaultMetricsPublisher(DataSource dataSource) {
     this.dataSource = dataSource;
   }
 
+  @RequestMapping(path = "/", method = RequestMethod.GET)
+  @Override
+  public RegistryMetric metrics() {
+    return dataSource.getRegistryMetric();
+  }
+
+  @RequestMapping(path = "/{windowTimeIndex}", method = RequestMethod.GET)
   @Override
-  public String metrics(int pollerIndex) {
-    if (pollerIndex >= 0 && pollerIndex < dataSource.getAppliedPollingIntervals().size()) {
-      try {
-        return JsonUtils.writeValueAsString(dataSource.getRegistryMetric(pollerIndex));
-      } catch (JsonProcessingException e) {
-        throw new ServiceCombException("serialize metrics failed", e);
-      }
-    } else {
-      return "{}";
-    }
+  public RegistryMetric metricsWithWindowTimeIndex(@PathVariable(name = "windowTimeIndex") int windowTimeIndex) {
+    return dataSource.getRegistryMetric(windowTimeIndex);
   }
 }
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsBootListener.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsBootListener.java
deleted file mode 100644
index 9d9571f..0000000
--- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsBootListener.java
+++ /dev/null
@@ -1,86 +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 io.servicecomb.metrics.core.publish;
-
-import java.io.IOException;
-
-import org.apache.commons.io.IOUtils;
-import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.core.io.Resource;
-import org.springframework.stereotype.Component;
-
-import com.netflix.config.DynamicPropertyFactory;
-
-import io.servicecomb.core.BootListener;
-import io.servicecomb.core.definition.SchemaMeta;
-import io.servicecomb.core.definition.loader.SchemaLoader;
-import io.servicecomb.core.definition.schema.ProducerSchemaFactory;
-import io.servicecomb.foundation.common.config.PaaSResourceUtils;
-import io.servicecomb.foundation.common.exceptions.ServiceCombException;
-import io.servicecomb.serviceregistry.RegistryUtils;
-import io.servicecomb.serviceregistry.api.registry.Microservice;
-
-@Component
-public class MetricsBootListener implements BootListener {
-
-  private static final String PUBLISH_CLASS_NAME = "servicecomb.metrics.publish.class";
-
-  private static final String METRICS_CONTRACT_DEFINITION_FILE = "servicecomb_internal_metrics_contract_definition.yaml";
-
-  private final ProducerSchemaFactory schemaFactory;
-
-  private final SchemaLoader schemaLoader;
-
-  private final MetricsPublisher metricsPublisher;
-
-  @Autowired
-  public MetricsBootListener(ProducerSchemaFactory schemaFactory, SchemaLoader schemaLoader,
-      MetricsPublisher metricsPublisher) {
-    this.schemaFactory = schemaFactory;
-    this.schemaLoader = schemaLoader;
-    this.metricsPublisher = metricsPublisher;
-  }
-
-  @Override
-  public void onBootEvent(BootEvent event) {
-    //inject metrics provider before ProducerProviderManager init
-    if (EventType.BEFORE_PRODUCER_PROVIDER.equals(event.getEventType())) {
-
-      String publisherClassName = DynamicPropertyFactory.getInstance()
-          .getStringProperty(PUBLISH_CLASS_NAME, JsonMetricsPublisher.class.getName()).get();
-
-      Resource[] resources = PaaSResourceUtils.getResources(METRICS_CONTRACT_DEFINITION_FILE);
-      if (resources.length != 0) {
-        Microservice microservice = RegistryUtils.getMicroservice();
-        try {
-          String swaggerContent = IOUtils.toString(resources[0].getURL());
-          SchemaMeta meta = schemaLoader
-              .registerSchema(microservice.getServiceName(), "metricsEndpoint", swaggerContent);
-          schemaFactory
-              .getOrCreateProducerSchema(microservice.getServiceName(), meta.getSchemaId(),
-                  Class.forName(publisherClassName),
-                  metricsPublisher);
-        } catch (ClassNotFoundException e) {
-          throw new ServiceCombException("unable found publish class", e);
-        } catch (IOException e) {
-          throw new ServiceCombException("unable load metrics contract definition file", e);
-        }
-      }
-    }
-  }
-}
diff --git a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsPublisher.java b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsPublisher.java
index 29cf1d8..9f3873f 100644
--- a/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsPublisher.java
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsPublisher.java
@@ -17,6 +17,10 @@
 
 package io.servicecomb.metrics.core.publish;
 
+import io.servicecomb.metrics.core.metric.RegistryMetric;
+
 public interface MetricsPublisher {
-  String metrics(int pollerIndex);
+  RegistryMetric metrics();
+
+  RegistryMetric metricsWithWindowTimeIndex(int windowTimeIndex);
 }
diff --git a/metrics/metrics-core/src/main/resources/servicecomb_internal_metrics_contract_definition.yaml b/metrics/metrics-core/src/main/resources/servicecomb_internal_metrics_contract_definition.yaml
deleted file mode 100644
index 0e4000c..0000000
--- a/metrics/metrics-core/src/main/resources/servicecomb_internal_metrics_contract_definition.yaml
+++ /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.
-## ---------------------------------------------------------------------------
-
-swagger: "2.0"
-info:
-  version: "1.0.0"
-  title: "MetricsPublisher"
-  x-java-interface: "io.servicecomb.metrics.core.publish.MetricsPublisher"
-basePath: "/metrics"
-consumes:
-- "application/json"
-produces:
-- "application/json"
-paths:
-  /{pollerIndex}:
-    get:
-      operationId: "metrics"
-      parameters:
-      - name: "pollerIndex"
-        in: "path"
-        required: true
-        type: "integer"
-        format: "int32"
-      responses:
-        200:
-          description: "response of 200"
-          schema:
-            type: "string"
\ No newline at end of file
diff --git a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java
index 9700d3b..7096937 100644
--- a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java
+++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java
@@ -17,26 +17,34 @@
 
 package io.servicecomb.metrics.core;
 
+import java.io.IOException;
+import java.util.Map;
+
 import org.junit.Assert;
 import org.junit.Test;
 
+import io.servicecomb.metrics.core.metric.RegistryMetric;
+import io.servicecomb.metrics.core.monitor.DefaultSystemMonitor;
 import io.servicecomb.metrics.core.monitor.RegistryMonitor;
+import io.servicecomb.metrics.core.monitor.SystemMonitor;
 import io.servicecomb.metrics.core.publish.DefaultDataSource;
-import io.servicecomb.metrics.core.publish.JsonMetricsPublisher;
+import io.servicecomb.metrics.core.publish.DefaultMetricsPublisher;
 
 public class TestPublisher {
 
   @Test
-  public void test() {
-    RegistryMonitor registryMonitor = new RegistryMonitor();
+  public void test() throws IOException {
+    SystemMonitor systemMonitor = new DefaultSystemMonitor();
+    RegistryMonitor registryMonitor = new RegistryMonitor(systemMonitor);
     DefaultDataSource dataSource = new DefaultDataSource(registryMonitor);
-    JsonMetricsPublisher publisher = new JsonMetricsPublisher(dataSource);
-    String content = publisher.metrics(0);
-    Assert
-        .assertEquals(content,
-            "{\"instanceMetric\":{\"waitInQueue\":0,\"lifeTimeInQueue\":{\"total\":0,\"count\":0,\"average\":0.0,"+
-                "\"min\":0,\"max\":0},\"executionTime\":{\"total\":0,\"count\":0,\"average\":0.0,\"min\":0,\"max\":0},"+
-                "\"consumerLatency\":{\"total\":0,\"count\":0,\"average\":0.0,\"min\":0,\"max\":0},\"producerLatency"+
-                "\":{\"total\":0,\"count\":0,\"average\":0.0,\"min\":0,\"max\":0}},\"invocationMetrics\":{}}");
+    DefaultMetricsPublisher publisher = new DefaultMetricsPublisher(dataSource);
+
+    RegistryMetric registryMetric = publisher.metrics();
+    Map<String, Number> metricsMap = registryMetric.toMap();
+    Assert.assertEquals(metricsMap.size(), 30);
+
+    registryMetric = publisher.metricsWithWindowTimeIndex(0);
+    metricsMap = registryMetric.toMap();
+    Assert.assertEquals(metricsMap.size(), 30);
   }
 }

-- 
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.