You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@servicecomb.apache.org by GitBox <gi...@apache.org> on 2017/12/23 00:37:30 UTC
[GitHub] zhengyangyong closed pull request #453: [JAV-508] & [SCB-11] Metrics Auto Publish
zhengyangyong closed pull request #453: [JAV-508] & [SCB-11] Metrics Auto Publish
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/453
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/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 000000000..535c6de2e
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/JsonMetricsPublisher.java
@@ -0,0 +1,48 @@
+/*
+ * 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 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 000000000..9d9571f64
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsBootListener.java
@@ -0,0 +1,86 @@
+/*
+ * 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
new file mode 100644
index 000000000..29cf1d83f
--- /dev/null
+++ b/metrics/metrics-core/src/main/java/io/servicecomb/metrics/core/publish/MetricsPublisher.java
@@ -0,0 +1,22 @@
+/*
+ * 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;
+
+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 000000000..0e4000c5a
--- /dev/null
+++ b/metrics/metrics-core/src/main/resources/servicecomb_internal_metrics_contract_definition.yaml
@@ -0,0 +1,42 @@
+## ---------------------------------------------------------------------------
+## 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/TestEventAndRunner.java b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java
index 02aeb6359..190c899eb 100644
--- a/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java
+++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestEventAndRunner.java
@@ -135,4 +135,4 @@ public void test() throws InterruptedException {
Assert
.assertEquals(model.getInstanceMetric().getConsumerLatency().getAverage(), TimeUnit.MILLISECONDS.toNanos(0), 0);
}
-}
+}
\ 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
new file mode 100644
index 000000000..9700d3bf2
--- /dev/null
+++ b/metrics/metrics-core/src/test/java/io/servicecomb/metrics/core/TestPublisher.java
@@ -0,0 +1,42 @@
+/*
+ * 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;
+
+import org.junit.Assert;
+import org.junit.Test;
+
+import io.servicecomb.metrics.core.monitor.RegistryMonitor;
+import io.servicecomb.metrics.core.publish.DefaultDataSource;
+import io.servicecomb.metrics.core.publish.JsonMetricsPublisher;
+
+public class TestPublisher {
+
+ @Test
+ public void test() {
+ RegistryMonitor registryMonitor = new RegistryMonitor();
+ 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\":{}}");
+ }
+}
diff --git a/metrics/metrics-sample/metrics-performance-test/pom.xml b/metrics/metrics-sample/metrics-performance-test/pom.xml
index 7b29e7fae..7a51ea625 100644
--- a/metrics/metrics-sample/metrics-performance-test/pom.xml
+++ b/metrics/metrics-sample/metrics-performance-test/pom.xml
@@ -1,18 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- ~ 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
+ ~ 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.
+ ~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
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 829ea47b7..e6651a8f5 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.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 String fun8() {
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);
- }
}
diff --git a/metrics/metrics-sample/metrics-performance-test/src/main/resources/microservice.yaml b/metrics/metrics-sample/metrics-performance-test/src/main/resources/microservice.yaml
index 0e12fdeab..42bc4aa0d 100644
--- a/metrics/metrics-sample/metrics-performance-test/src/main/resources/microservice.yaml
+++ b/metrics/metrics-sample/metrics-performance-test/src/main/resources/microservice.yaml
@@ -1,18 +1,19 @@
-#
-# 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.
-#
+## ---------------------------------------------------------------------------
+## 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.
+## ---------------------------------------------------------------------------
# all interconnected microservices must belong to an application wth the same ID
APPLICATION_ID: metricsPerfTest
diff --git a/metrics/metrics-sample/pom.xml b/metrics/metrics-sample/pom.xml
index 45c474954..b78f37ab4 100644
--- a/metrics/metrics-sample/pom.xml
+++ b/metrics/metrics-sample/pom.xml
@@ -1,18 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- ~ 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
+ ~ 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.
+ ~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
diff --git a/metrics/pom.xml b/metrics/pom.xml
index 80e53d3a2..43a7cb48c 100644
--- a/metrics/pom.xml
+++ b/metrics/pom.xml
@@ -1,18 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
- ~ 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
+ ~ 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.
+ ~ 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.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0"
----------------------------------------------------------------
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