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

[incubator-servicecomb-java-chassis] 01/14: JAV-508 & SCB-11 Metrics Auto Publish

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 9328aa5fb31d9c67737503913b988c50786657c6
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Wed Dec 20 14:29:46 2017 +0800

    JAV-508 & SCB-11 Metrics Auto Publish
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 .../metrics/core/publish/JsonMetricsPublisher.java | 47 ++++++++++++
 .../metrics/core/publish/MetricsBootListener.java  | 85 ++++++++++++++++++++++
 .../metrics/core/publish/MetricsPublisher.java     | 21 ++++++
 ...ecomb_internal_metrics_contract_definition.yaml | 25 +++++++
 .../metrics/sample/perf/PerfService.java           | 10 ---
 5 files changed, 178 insertions(+), 10 deletions(-)

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/JsonMetricsPublisher.java
new file mode 100644
index 0000000..a4caf70
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/JsonMetricsPublisher.java
@@ -0,0 +1,47 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd
+ *
+ * Licensed 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 org.springframework.stereotype.Component;
+
+import com.fasterxml.jackson.core.JsonProcessingException;
+
+import io.servicecomb.foundation.common.exceptions.ServiceCombException;
+import io.servicecomb.foundation.common.utils.JsonUtils;
+
+@Component
+public class JsonMetricsPublisher implements MetricsPublisher {
+
+  private final DataSource dataSource;
+
+  public JsonMetricsPublisher(DataSource dataSource) {
+    this.dataSource = dataSource;
+  }
+
+  @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 "{}";
+    }
+  }
+}
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
new file mode 100644
index 0000000..a307788
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsBootListener.java
@@ -0,0 +1,85 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd
+ *
+ * Licensed 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
new file mode 100644
index 0000000..c9abf17
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsPublisher.java
@@ -0,0 +1,21 @@
+/*
+ * Copyright 2017 Huawei Technologies Co., Ltd
+ *
+ * Licensed 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;
+
+public interface MetricsPublisher {
+  String metrics(int pollerIndex);
+}
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
new file mode 100644
index 0000000..a97e75c
--- /dev/null
+++ b/metrics/metrics-core/src/main/resources/servicecomb_internal_metrics_contract_definition.yaml
@@ -0,0 +1,25 @@
+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-sample/metrics-performance-test/src/main/java/io/servicecomb/metrics/sample/perf/PerfService.java b/metrics/metrics-sample/metrics-performance-test/src/main/java/io/servicecomb/metrics/sample/perf/PerfService.java
index 829ea47..e6651a8 100644
--- a/metrics/metrics-sample/metrics-performance-test/src/main/java/io/servicecomb/metrics/sample/perf/PerfService.java
+++ b/metrics/metrics-sample/metrics-performance-test/src/main/java/io/servicecomb/metrics/sample/perf/PerfService.java
@@ -23,10 +23,6 @@ import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.GetMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
 
-import com.fasterxml.jackson.core.JsonProcessingException;
-
-import io.servicecomb.foundation.common.utils.JsonUtils;
-import io.servicecomb.metrics.core.metric.RegistryMetric;
 import io.servicecomb.metrics.core.publish.DataSource;
 import io.servicecomb.provider.rest.common.RestSchema;
 
@@ -90,10 +86,4 @@ public class PerfService {
   public String fun9() {
     return UUID.randomUUID().toString();
   }
-
-  @GetMapping(path = "/metrics")
-  public String metrics() throws JsonProcessingException {
-    RegistryMetric metric = this.dataSource.getRegistryMetric(0);
-    return JsonUtils.writeValueAsString(metric);
-  }
 }

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