You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2018/01/09 08:57:32 UTC
[incubator-servicecomb-java-chassis] 01/09: SCB-118 add prometheus
integration impl
This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
commit 5c97791758e9d487d7ac0ebae95ee36f8dd01944
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Tue Jan 2 12:57:06 2018 +0800
SCB-118 add prometheus integration impl
Signed-off-by: zhengyangyong <ya...@huawei.com>
---
java-chassis-dependencies/pom.xml | 16 +++++
.../metrics/common/ConsumerInvocationMetric.java | 4 +-
.../servicecomb/metrics/common/InstanceMetric.java | 11 ++++
.../metrics/common/ProducerInvocationMetric.java | 8 +--
.../servicecomb/metrics/common/RegistryMetric.java | 6 +-
.../metrics-prometheus}/pom.xml | 30 ++++++---
.../metrics/prometheus/MetricsCollector.java | 77 ++++++++++++++++++++++
.../metrics/prometheus/MetricsPublisher.java | 52 +++++++++++++++
metrics/{ => metrics-integration}/pom.xml | 16 ++---
metrics/pom.xml | 1 +
10 files changed, 192 insertions(+), 29 deletions(-)
diff --git a/java-chassis-dependencies/pom.xml b/java-chassis-dependencies/pom.xml
index 7c7f351..a111a3f 100644
--- a/java-chassis-dependencies/pom.xml
+++ b/java-chassis-dependencies/pom.xml
@@ -699,6 +699,17 @@
</dependency>
<dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient</artifactId>
+ <version>0.1.0</version>
+ </dependency>
+ <dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient_httpserver</artifactId>
+ <version>0.1.0</version>
+ </dependency>
+
+ <dependency>
<groupId>io.servicecomb</groupId>
<artifactId>foundation-common</artifactId>
<version>0.6.0-SNAPSHOT</version>
@@ -954,6 +965,11 @@
<artifactId>metrics-common</artifactId>
<version>0.6.0-SNAPSHOT</version>
</dependency>
+ <dependency>
+ <groupId>io.servicecomb</groupId>
+ <artifactId>metrics-prometheus</artifactId>
+ <version>0.6.0-SNAPSHOT</version>
+ </dependency>
</dependencies>
</dependencyManagement>
diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/ConsumerInvocationMetric.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/ConsumerInvocationMetric.java
index d4aee4a..bbd5f07 100644
--- a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/ConsumerInvocationMetric.java
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/ConsumerInvocationMetric.java
@@ -46,8 +46,8 @@ public class ConsumerInvocationMetric extends InvocationMetric {
public ConsumerInvocationMetric merge(ConsumerInvocationMetric metric) {
return new ConsumerInvocationMetric(this.getOperationName(), this.getPrefix(),
- metric.getConsumerLatency().merge(consumerLatency),
- metric.getConsumerCall().merge(consumerCall));
+ consumerLatency.merge(metric.getConsumerLatency()),
+ consumerCall.merge(metric.getConsumerCall()));
}
public Map<String, Number> toMap() {
diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/InstanceMetric.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/InstanceMetric.java
index b8dd03e..c7dd0c2 100644
--- a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/InstanceMetric.java
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/InstanceMetric.java
@@ -17,6 +17,9 @@
package io.servicecomb.metrics.common;
+import java.util.HashMap;
+import java.util.Map;
+
import com.fasterxml.jackson.annotation.JsonProperty;
public class InstanceMetric {
@@ -45,4 +48,12 @@ public class InstanceMetric {
this.consumerMetric = consumerMetric;
this.producerMetric = producerMetric;
}
+
+ public Map<String, Number> toMap() {
+ Map<String, Number> metrics = new HashMap<>();
+ metrics.putAll(systemMetric.toMap());
+ metrics.putAll(consumerMetric.toMap());
+ metrics.putAll(producerMetric.toMap());
+ return metrics;
+ }
}
diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/ProducerInvocationMetric.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/ProducerInvocationMetric.java
index aced1c7..511d792 100644
--- a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/ProducerInvocationMetric.java
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/ProducerInvocationMetric.java
@@ -71,10 +71,10 @@ public class ProducerInvocationMetric extends InvocationMetric {
public ProducerInvocationMetric merge(ProducerInvocationMetric metric) {
return new ProducerInvocationMetric(this.getOperationName(), this.getPrefix(),
this.getWaitInQueue() + metric.getWaitInQueue(),
- metric.getLifeTimeInQueue().merge(lifeTimeInQueue),
- metric.getExecutionTime().merge(executionTime),
- metric.getProducerLatency().merge(producerLatency),
- metric.getProducerCall().merge(producerCall));
+ lifeTimeInQueue.merge(metric.getLifeTimeInQueue()),
+ executionTime.merge(metric.getExecutionTime()),
+ producerLatency.merge(metric.getProducerLatency()),
+ producerCall.merge(metric.getProducerCall()));
}
public Map<String, Number> toMap() {
diff --git a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/RegistryMetric.java b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/RegistryMetric.java
index 1dad927..4b08a7a 100644
--- a/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/RegistryMetric.java
+++ b/metrics/metrics-common/src/main/java/io/servicecomb/metrics/common/RegistryMetric.java
@@ -57,7 +57,7 @@ public class RegistryMetric {
ConsumerInvocationMetric instanceConsumerInvocationMetric = new ConsumerInvocationMetric("instance",
MetricsConst.INSTANCE_CONSUMER_PREFIX,
- new TimerMetric(MetricsConst.INSTANCE_PRODUCER_PREFIX + ".producerLatency"),
+ new TimerMetric(MetricsConst.INSTANCE_CONSUMER_PREFIX + ".producerLatency"),
new CallMetric(MetricsConst.INSTANCE_CONSUMER_PREFIX + ".consumerCall"));
ProducerInvocationMetric instanceProducerInvocationMetric = new ProducerInvocationMetric("instance",
MetricsConst.INSTANCE_PRODUCER_PREFIX, 0,
@@ -80,9 +80,7 @@ public class RegistryMetric {
public Map<String, Number> toMap() {
Map<String, Number> metrics = new HashMap<>();
- metrics.putAll(instanceMetric.getSystemMetric().toMap());
- metrics.putAll(instanceMetric.getConsumerMetric().toMap());
- metrics.putAll(instanceMetric.getProducerMetric().toMap());
+ metrics.putAll(instanceMetric.toMap());
for (ConsumerInvocationMetric metric : consumerMetrics.values()) {
metrics.putAll(metric.toMap());
}
diff --git a/metrics/pom.xml b/metrics/metrics-integration/metrics-prometheus/pom.xml
similarity index 70%
copy from metrics/pom.xml
copy to metrics/metrics-integration/metrics-prometheus/pom.xml
index 7331a3b..70a1ff2 100644
--- a/metrics/pom.xml
+++ b/metrics/metrics-integration/metrics-prometheus/pom.xml
@@ -19,20 +19,30 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
<parent>
+ <artifactId>metrics-integration</artifactId>
<groupId>io.servicecomb</groupId>
- <artifactId>java-chassis-parent</artifactId>
<version>0.6.0-SNAPSHOT</version>
- <relativePath>../parent</relativePath>
</parent>
+ <modelVersion>4.0.0</modelVersion>
+
+ <artifactId>metrics-prometheus</artifactId>
+
+ <dependencies>
+ <dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient</artifactId>
+ </dependency>
+ <dependency>
+ <groupId>io.prometheus</groupId>
+ <artifactId>simpleclient_httpserver</artifactId>
+ </dependency>
+
+ <dependency>
+ <groupId>io.servicecomb</groupId>
+ <artifactId>metrics-core</artifactId>
+ </dependency>
- <artifactId>metrics</artifactId>
+ </dependencies>
- <modules>
- <module>metrics-common</module>
- <module>metrics-core</module>
- <module>metrics-extension</module>
- </modules>
</project>
\ No newline at end of file
diff --git a/metrics/metrics-integration/metrics-prometheus/src/main/java/io/servicecomb/metrics/prometheus/MetricsCollector.java b/metrics/metrics-integration/metrics-prometheus/src/main/java/io/servicecomb/metrics/prometheus/MetricsCollector.java
new file mode 100644
index 0000000..863736b
--- /dev/null
+++ b/metrics/metrics-integration/metrics-prometheus/src/main/java/io/servicecomb/metrics/prometheus/MetricsCollector.java
@@ -0,0 +1,77 @@
+/*
+ * 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.prometheus;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.stream.Collectors;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import io.prometheus.client.Collector;
+import io.prometheus.client.Collector.MetricFamilySamples.Sample;
+import io.servicecomb.metrics.common.ConsumerInvocationMetric;
+import io.servicecomb.metrics.common.ProducerInvocationMetric;
+import io.servicecomb.metrics.common.RegistryMetric;
+import io.servicecomb.metrics.core.publish.DataSource;
+
+@Component
+public class MetricsCollector extends Collector implements Collector.Describable {
+
+ private final DataSource dataSource;
+
+ @Autowired
+ public MetricsCollector(DataSource dataSource) {
+ this.dataSource = dataSource;
+ }
+
+ @Override
+ public List<MetricFamilySamples> collect() {
+ return load();
+ }
+
+ @Override
+ public List<MetricFamilySamples> describe() {
+ return load();
+ }
+
+ private List<MetricFamilySamples> load() {
+ RegistryMetric registryMetric = dataSource.getRegistryMetric(0);
+ List<MetricFamilySamples> familySamples = new ArrayList<>();
+ familySamples.add(getFamilySamples("Instance Level", registryMetric.getInstanceMetric().toMap()));
+ for (Entry<String, ConsumerInvocationMetric> consumerMetric : registryMetric.getConsumerMetrics().entrySet()) {
+ familySamples
+ .add(getFamilySamples(consumerMetric.getKey() + " Consumer Side", consumerMetric.getValue().toMap()));
+ }
+ for (Entry<String, ProducerInvocationMetric> producerMetric : registryMetric.getProducerMetrics().entrySet()) {
+ familySamples
+ .add(getFamilySamples(producerMetric.getKey() + " Producer Side", producerMetric.getValue().toMap()));
+ }
+ return familySamples;
+ }
+
+ private MetricFamilySamples getFamilySamples(String name, Map<String, Number> metrics) {
+ List<Sample> samples = metrics.entrySet().stream().map((entry) ->
+ new Sample(entry.getKey().replace(".", "_"),
+ new ArrayList<>(), new ArrayList<>(), entry.getValue().doubleValue())).collect(Collectors.toList());
+ return new MetricFamilySamples(name, Type.UNTYPED, name + " Metrics", samples);
+ }
+}
diff --git a/metrics/metrics-integration/metrics-prometheus/src/main/java/io/servicecomb/metrics/prometheus/MetricsPublisher.java b/metrics/metrics-integration/metrics-prometheus/src/main/java/io/servicecomb/metrics/prometheus/MetricsPublisher.java
new file mode 100644
index 0000000..dc83a56
--- /dev/null
+++ b/metrics/metrics-integration/metrics-prometheus/src/main/java/io/servicecomb/metrics/prometheus/MetricsPublisher.java
@@ -0,0 +1,52 @@
+/*
+ * 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.prometheus;
+
+import java.io.IOException;
+import java.net.InetSocketAddress;
+
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+import com.netflix.config.DynamicPropertyFactory;
+
+import io.prometheus.client.CollectorRegistry;
+import io.prometheus.client.exporter.HTTPServer;
+import io.servicecomb.foundation.common.exceptions.ServiceCombException;
+
+@Component
+public class MetricsPublisher {
+ private static final String METRICS_PROMETHEUS_PORT = "servicecomb.metrics.prometheus.port";
+
+ private final MetricsCollector metricsCollector;
+
+ private final HTTPServer httpServer;
+
+ @Autowired
+ public MetricsPublisher(MetricsCollector metricsCollector) {
+ //prometheus default port allocation is here : https://github.com/prometheus/prometheus/wiki/Default-port-allocations
+ int publishPort = DynamicPropertyFactory.getInstance().getIntProperty(METRICS_PROMETHEUS_PORT, 9696).get();
+ this.metricsCollector = metricsCollector;
+ this.metricsCollector.register();
+ try {
+ this.httpServer = new HTTPServer(new InetSocketAddress(publishPort), CollectorRegistry.defaultRegistry, true);
+ } catch (IOException e) {
+ throw new ServiceCombException("create http publish server failed", e);
+ }
+ }
+}
diff --git a/metrics/pom.xml b/metrics/metrics-integration/pom.xml
similarity index 83%
copy from metrics/pom.xml
copy to metrics/metrics-integration/pom.xml
index 7331a3b..c94879a 100644
--- a/metrics/pom.xml
+++ b/metrics/metrics-integration/pom.xml
@@ -19,20 +19,18 @@
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <packaging>pom</packaging>
<parent>
+ <artifactId>metrics</artifactId>
<groupId>io.servicecomb</groupId>
- <artifactId>java-chassis-parent</artifactId>
<version>0.6.0-SNAPSHOT</version>
- <relativePath>../parent</relativePath>
</parent>
+ <modelVersion>4.0.0</modelVersion>
- <artifactId>metrics</artifactId>
-
+ <artifactId>metrics-integration</artifactId>
+ <packaging>pom</packaging>
<modules>
- <module>metrics-common</module>
- <module>metrics-core</module>
- <module>metrics-extension</module>
+ <module>metrics-prometheus</module>
</modules>
+
+
</project>
\ No newline at end of file
diff --git a/metrics/pom.xml b/metrics/pom.xml
index 7331a3b..cd5adcc 100644
--- a/metrics/pom.xml
+++ b/metrics/pom.xml
@@ -34,5 +34,6 @@
<module>metrics-common</module>
<module>metrics-core</module>
<module>metrics-extension</module>
+ <module>metrics-integration</module>
</modules>
</project>
\ No newline at end of file
--
To stop receiving notification emails like this one, please contact
"commits@servicecomb.apache.org" <co...@servicecomb.apache.org>.