You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by wu...@apache.org on 2018/04/10 08:12:30 UTC
[incubator-servicecomb-java-chassis] branch master updated: SCB-422
prometheus switch to new mechanism
This is an automated email from the ASF dual-hosted git repository.
wujimin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 4f8f810 SCB-422 prometheus switch to new mechanism
4f8f810 is described below
commit 4f8f8100d076a48312d8e0ff82834bb8e0d1aa1a
Author: wujimin <wu...@huawei.com>
AuthorDate: Tue Apr 3 18:26:19 2018 +0800
SCB-422 prometheus switch to new mechanism
---
.../demo/springmvc/client/SpringmvcClient.java | 14 +--
.../metrics-integration/metrics-prometheus/pom.xml | 8 +-
.../metrics/prometheus/MetricsCollector.java | 76 -------------
.../metrics/prometheus/MetricsHttpPublisher.java | 83 --------------
.../metrics/prometheus/PrometheusPublisher.java | 126 +++++++++++++++++++++
...rvicecomb.foundation.metrics.MetricsInitializer | 18 +++
.../prometheus/TestMetricsHttpPublisher.java | 56 ---------
.../prometheus/TestPrometheusPublisher.java | 107 +++++++++++++++++
8 files changed, 263 insertions(+), 225 deletions(-)
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 fa53abc..da0765d 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
@@ -109,13 +109,13 @@ public class SpringmvcClient {
try {
String content = restTemplate.getForObject("cse://springmvc/codeFirstSpringmvc/prometheusForTest", String.class);
- TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_addDate"));
- TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_sayHello"));
- TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_fallbackFromCache"));
- TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_isTrue"));
- TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_add"));
- TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_sayHi2"));
- TestMgr.check(true, content.contains("servicecomb_invocation_springmvc_codeFirst_saySomething"));
+ TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.addDate"));
+ TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.sayHello"));
+ TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.fallbackFromCache"));
+ TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.isTrue"));
+ TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.add"));
+ TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.sayHi2"));
+ TestMgr.check(true, content.contains("servicecomb_invocation{operation=\"springmvc.codeFirst.saySomething"));
String[] metricLines = content.split("\n");
if (metricLines.length > 0) {
diff --git a/metrics/metrics-integration/metrics-prometheus/pom.xml b/metrics/metrics-integration/metrics-prometheus/pom.xml
index dcaeb64..3def8e7 100644
--- a/metrics/metrics-integration/metrics-prometheus/pom.xml
+++ b/metrics/metrics-integration/metrics-prometheus/pom.xml
@@ -16,8 +16,7 @@
~ limitations under the License.
-->
-<project xmlns="http://maven.apache.org/POM/4.0.0"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+<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">
<parent>
<artifactId>metrics-integration</artifactId>
@@ -43,7 +42,10 @@
<groupId>org.apache.servicecomb</groupId>
<artifactId>metrics-core</artifactId>
</dependency>
-
+ <dependency>
+ <groupId>org.apache.servicecomb</groupId>
+ <artifactId>foundation-test-scaffolding</artifactId>
+ </dependency>
</dependencies>
</project>
\ No newline at end of file
diff --git a/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/MetricsCollector.java b/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/MetricsCollector.java
deleted file mode 100644
index a99cce4..0000000
--- a/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/MetricsCollector.java
+++ /dev/null
@@ -1,76 +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.prometheus;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Map;
-import java.util.Map.Entry;
-
-import org.apache.servicecomb.foundation.metrics.MetricsConst;
-import org.apache.servicecomb.metrics.core.MonitorManager;
-
-import io.prometheus.client.Collector;
-import io.prometheus.client.Collector.MetricFamilySamples.Sample;
-
-public class MetricsCollector extends Collector implements Collector.Describable {
-
- @Override
- public List<MetricFamilySamples> collect() {
- return load();
- }
-
- @Override
- public List<MetricFamilySamples> describe() {
- return load();
- }
-
- private List<MetricFamilySamples> load() {
- Map<String, Double> metrics = MonitorManager.getInstance().measure();
- List<MetricFamilySamples> familySamples = new ArrayList<>();
-
- List<Sample> samples = new ArrayList<>();
- for (Entry<String, Double> metric : metrics.entrySet()) {
- List<String> tagNames = new ArrayList<>();
- List<String> tagValues = new ArrayList<>();
- String name = metric.getKey();
- if (metric.getKey().contains("(")) {
- String[] nameAndTag = metric.getKey().split("\\(");
- name = nameAndTag[0];
- String[] tagAnValues = nameAndTag[1].split("[=,)]");
- for (int i = 0; i < tagAnValues.length; i += 2) {
- //we need put operation name in metrics name,not a label
- if (MetricsConst.TAG_OPERATION.equals(tagAnValues[i])) {
- name = name + "." + tagAnValues[i + 1];
- } else if (!"type".equals(tagAnValues[i])) {
- tagNames.add(tagAnValues[i]);
- tagValues.add(tagAnValues[i + 1]);
- }
- }
- }
- samples.add(new Sample(formatMetricName(name), tagNames, tagValues, metric.getValue()));
- }
- familySamples.add(new MetricFamilySamples("ServiceComb Metrics", Type.UNTYPED, "ServiceComb Metrics", samples));
- return familySamples;
- }
-
- //convert name for match prometheus
- private String formatMetricName(String name) {
- return name.replace(".", "_");
- }
-}
diff --git a/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/MetricsHttpPublisher.java b/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/MetricsHttpPublisher.java
deleted file mode 100644
index d2ea6de..0000000
--- a/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/MetricsHttpPublisher.java
+++ /dev/null
@@ -1,83 +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.prometheus;
-
-import java.net.InetSocketAddress;
-
-import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.springframework.context.ApplicationEvent;
-import org.springframework.context.ApplicationListener;
-import org.springframework.context.event.ContextClosedEvent;
-import org.springframework.stereotype.Component;
-
-import com.netflix.config.DynamicPropertyFactory;
-
-import io.prometheus.client.CollectorRegistry;
-import io.prometheus.client.exporter.HTTPServer;
-
-@Component
-public class MetricsHttpPublisher implements ApplicationListener<ApplicationEvent> {
- private static final Logger LOGGER = LoggerFactory.getLogger(MetricsHttpPublisher.class);
-
- private static final String METRICS_PROMETHEUS_ADDRESS = "servicecomb.metrics.prometheus.address";
-
- private HTTPServer httpServer;
-
- public MetricsHttpPublisher() {
- //prometheus default port allocation is here : https://github.com/prometheus/prometheus/wiki/Default-port-allocations
- this.init(
- DynamicPropertyFactory.getInstance().getStringProperty(METRICS_PROMETHEUS_ADDRESS, "0.0.0.0:9696").get());
- }
-
- public MetricsHttpPublisher(String address) {
- this.init(address);
- }
-
- private void init(String address) {
- try {
- InetSocketAddress socketAddress = getSocketAddress(address);
- MetricsCollector metricsCollector = new MetricsCollector();
- metricsCollector.register();
- this.httpServer = new HTTPServer(socketAddress, CollectorRegistry.defaultRegistry, true);
- LOGGER.info("Prometheus httpServer listened : {}.", address);
- } catch (Exception e) {
- throw new ServiceCombException("create http publish server failed,may bad address : " + address, e);
- }
- }
-
- private InetSocketAddress getSocketAddress(String address) {
- String[] hostAndPort = address.split(":");
- if (hostAndPort.length == 2) {
- return new InetSocketAddress(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
- }
- throw new ServiceCombException("create http publish server failed,bad address : " + address);
- }
-
- @Override
- public void onApplicationEvent(ApplicationEvent event) {
- if (!ContextClosedEvent.class.isInstance(event) || httpServer == null) {
- return;
- }
-
- httpServer.stop();
- httpServer = null;
- LOGGER.info("Prometheus httpServer stopped.");
- }
-}
diff --git a/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/PrometheusPublisher.java b/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/PrometheusPublisher.java
new file mode 100644
index 0000000..dd4122c
--- /dev/null
+++ b/metrics/metrics-integration/metrics-prometheus/src/main/java/org/apache/servicecomb/metrics/prometheus/PrometheusPublisher.java
@@ -0,0 +1,126 @@
+/*
+ * 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.prometheus;
+
+import java.net.InetSocketAddress;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
+import org.apache.servicecomb.foundation.metrics.MetricsBootstrapConfig;
+import org.apache.servicecomb.foundation.metrics.MetricsInitializer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.eventbus.EventBus;
+import com.netflix.config.DynamicPropertyFactory;
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.Measurement;
+import com.netflix.spectator.api.Tag;
+
+import io.prometheus.client.Collector;
+import io.prometheus.client.Collector.MetricFamilySamples.Sample;
+import io.prometheus.client.CollectorRegistry;
+import io.prometheus.client.exporter.HTTPServer;
+
+public class PrometheusPublisher extends Collector implements Collector.Describable, MetricsInitializer {
+ private static final Logger LOGGER = LoggerFactory.getLogger(PrometheusPublisher.class);
+
+ static final String METRICS_PROMETHEUS_ADDRESS = "servicecomb.metrics.prometheus.address";
+
+ private HTTPServer httpServer;
+
+ private CompositeRegistry globalRegistry;
+
+ @Override
+ public void init(CompositeRegistry globalRegistry, EventBus eventBus, MetricsBootstrapConfig config) {
+ this.globalRegistry = globalRegistry;
+
+ //prometheus default port allocation is here : https://github.com/prometheus/prometheus/wiki/Default-port-allocations
+ String address =
+ DynamicPropertyFactory.getInstance().getStringProperty(METRICS_PROMETHEUS_ADDRESS, "0.0.0.0:9696").get();
+
+ try {
+ InetSocketAddress socketAddress = getSocketAddress(address);
+ register();
+ this.httpServer = new HTTPServer(socketAddress, CollectorRegistry.defaultRegistry, true);
+
+ LOGGER.info("Prometheus httpServer listened : {}.", address);
+ } catch (Exception e) {
+ throw new ServiceCombException("create http publish server failed,may bad address : " + address, e);
+ }
+ }
+
+ private InetSocketAddress getSocketAddress(String address) {
+ String[] hostAndPort = address.split(":");
+ if (hostAndPort.length == 2) {
+ return new InetSocketAddress(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
+ }
+ throw new ServiceCombException("create http publish server failed,bad address : " + address);
+ }
+
+ @Override
+ public List<MetricFamilySamples> describe() {
+ List<MetricFamilySamples> familySamples = new ArrayList<>();
+ if (globalRegistry == null) {
+ return familySamples;
+ }
+
+ List<Sample> samples = new ArrayList<>();
+ globalRegistry
+ .iterator()
+ .forEachRemaining(meter -> {
+ meter.measure().forEach(measurement -> {
+ Sample sample = convertMeasurementToSample(measurement);
+ samples.add(sample);
+ });
+ });
+ familySamples.add(new MetricFamilySamples("ServiceComb Metrics", Type.UNTYPED, "ServiceComb Metrics", samples));
+
+ return familySamples;
+ }
+
+ protected Sample convertMeasurementToSample(Measurement measurement) {
+ String prometheusName = measurement.id().name().replace(".", "_");
+ List<String> labelNames = new ArrayList<>();
+ List<String> labelValues = new ArrayList<>();
+
+ for (Tag tag : measurement.id().tags()) {
+ labelNames.add(tag.key());
+ labelValues.add(tag.value());
+ }
+
+ return new Sample(prometheusName, labelNames, labelValues, measurement.value());
+ }
+
+ @Override
+ public List<MetricFamilySamples> collect() {
+ return describe();
+ }
+
+ @Override
+ public void uninit() {
+ if (httpServer == null) {
+ return;
+ }
+
+ httpServer.stop();
+ httpServer = null;
+ LOGGER.info("Prometheus httpServer stopped.");
+ }
+}
diff --git a/metrics/metrics-integration/metrics-prometheus/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer b/metrics/metrics-integration/metrics-prometheus/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer
new file mode 100644
index 0000000..b4c9faa
--- /dev/null
+++ b/metrics/metrics-integration/metrics-prometheus/src/main/resources/META-INF/services/org.apache.servicecomb.foundation.metrics.MetricsInitializer
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.servicecomb.metrics.prometheus.PrometheusPublisher
\ No newline at end of file
diff --git a/metrics/metrics-integration/metrics-prometheus/src/test/java/org/apache/servicecomb/metrics/prometheus/TestMetricsHttpPublisher.java b/metrics/metrics-integration/metrics-prometheus/src/test/java/org/apache/servicecomb/metrics/prometheus/TestMetricsHttpPublisher.java
deleted file mode 100644
index 0f01e46..0000000
--- a/metrics/metrics-integration/metrics-prometheus/src/test/java/org/apache/servicecomb/metrics/prometheus/TestMetricsHttpPublisher.java
+++ /dev/null
@@ -1,56 +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.prometheus;
-
-import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
-import org.junit.Assert;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.ExpectedException;
-
-public class TestMetricsHttpPublisher {
-
- @Rule
- public ExpectedException thrown = ExpectedException.none();
-
- @Test
- public void testBadPublishAddress() {
- thrown.expect(ServiceCombException.class);
- new MetricsHttpPublisher("a:b:c");
- Assert.fail("testBadPublishAddress failed,this address must throw ServiceCombException : a:b:c");
- }
-
- @Test
- public void testBadPublishAddress_BadPort() {
- thrown.expect(ServiceCombException.class);
- new MetricsHttpPublisher("localhost:xxxx");
- Assert.fail("testBadPublishAddress failed,this address must throw ServiceCombException : localhost:xxxx");
- }
-
- @Test
- public void testBadPublishAddress_ToLargePort() {
- thrown.expect(ServiceCombException.class);
- new MetricsHttpPublisher("localhost:9999999");
- Assert.fail("testBadPublishAddress failed,this address must throw ServiceCombException : localhost:9999999");
- }
-
- @Test
- public void testRightPublishAddress() {
- new MetricsHttpPublisher("localhost:43234");
- }
-}
diff --git a/metrics/metrics-integration/metrics-prometheus/src/test/java/org/apache/servicecomb/metrics/prometheus/TestPrometheusPublisher.java b/metrics/metrics-integration/metrics-prometheus/src/test/java/org/apache/servicecomb/metrics/prometheus/TestPrometheusPublisher.java
new file mode 100644
index 0000000..1bf0766
--- /dev/null
+++ b/metrics/metrics-integration/metrics-prometheus/src/test/java/org/apache/servicecomb/metrics/prometheus/TestPrometheusPublisher.java
@@ -0,0 +1,107 @@
+/*
+ * 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.prometheus;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.HttpURLConnection;
+import java.net.URL;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
+import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import com.netflix.spectator.api.CompositeRegistry;
+import com.netflix.spectator.api.Counter;
+import com.netflix.spectator.api.DefaultRegistry;
+import com.netflix.spectator.api.ManualClock;
+import com.netflix.spectator.api.Registry;
+import com.netflix.spectator.api.SpectatorUtils;
+import com.sun.net.httpserver.HttpServer;
+
+import io.prometheus.client.exporter.HTTPServer;
+
+@SuppressWarnings("restriction")
+public class TestPrometheusPublisher {
+ CompositeRegistry globalRegistry = SpectatorUtils.createCompositeRegistry(new ManualClock());
+
+ PrometheusPublisher publisher = new PrometheusPublisher();
+
+ @Rule
+ public ExpectedException thrown = ExpectedException.none();
+
+ @AfterClass
+ public static void teardown() {
+ ArchaiusUtils.resetConfig();
+ }
+
+ @Test
+ public void testBadPublishAddress() {
+ thrown.expect(ServiceCombException.class);
+
+ ArchaiusUtils.setProperty(PrometheusPublisher.METRICS_PROMETHEUS_ADDRESS, "a:b:c");
+ publisher.init(globalRegistry, null, null);
+ }
+
+ @Test
+ public void testBadPublishAddress_BadPort() {
+ thrown.expect(ServiceCombException.class);
+
+ ArchaiusUtils.setProperty(PrometheusPublisher.METRICS_PROMETHEUS_ADDRESS, "localhost:xxxx");
+ publisher.init(globalRegistry, null, null);
+ }
+
+ @Test
+ public void testBadPublishAddress_TooLargePort() {
+ thrown.expect(ServiceCombException.class);
+
+ ArchaiusUtils.setProperty(PrometheusPublisher.METRICS_PROMETHEUS_ADDRESS, "localhost:9999999");
+ publisher.init(globalRegistry, null, null);
+ }
+
+ @Test
+ public void collect() throws IllegalAccessException, IOException {
+ ArchaiusUtils.setProperty(PrometheusPublisher.METRICS_PROMETHEUS_ADDRESS, "localhost:0");
+ publisher.init(globalRegistry, null, null);
+
+ Registry registry = new DefaultRegistry(new ManualClock());
+ globalRegistry.add(registry);
+
+ Counter counter = registry.counter("count.name", "tag1", "tag1v", "tag2", "tag2v");
+ counter.increment();
+
+ HTTPServer httpServer = (HTTPServer) FieldUtils.readField(publisher, "httpServer", true);
+ com.sun.net.httpserver.HttpServer server = (HttpServer) FieldUtils.readField(httpServer, "server", true);
+
+ URL url = new URL("http://localhost:" + server.getAddress().getPort() + "/metrics");
+ HttpURLConnection conn = (HttpURLConnection) url.openConnection();
+ try (InputStream is = conn.getInputStream()) {
+ Assert.assertEquals("# HELP ServiceComb Metrics ServiceComb Metrics\n" +
+ "# TYPE ServiceComb Metrics untyped\n" +
+ "count_name{tag1=\"tag1v\",tag2=\"tag2v\",} 1.0\n", IOUtils.toString(is));
+ }
+
+ publisher.uninit();
+ }
+}
--
To stop receiving notification emails like this one, please contact
wujimin@apache.org.