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/03/08 08:52:23 UTC

[incubator-servicecomb-java-chassis] branch master updated: [SCB-372] support user set metrics prometheus exporter address not only port (#574)

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


The following commit(s) were added to refs/heads/master by this push:
     new c301036  [SCB-372] support user set metrics prometheus exporter address not only port (#574)
c301036 is described below

commit c3010365254b64d344694277f777c43f5b02116c
Author: zhengyangyong <ya...@huawei.com>
AuthorDate: Thu Mar 8 16:52:21 2018 +0800

    [SCB-372] support user set metrics prometheus exporter address not only port (#574)
    
    * SCB-372 support user set metrics prometheus exporter address not only port
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
    
    * SCB-372 fix pr comments
    
    Signed-off-by: zhengyangyong <ya...@huawei.com>
---
 .../metrics/prometheus/MetricsHttpPublisher.java   | 34 +++++++++----
 .../prometheus/TestMetricsHttpPublisher.java       | 56 ++++++++++++++++++++++
 2 files changed, 81 insertions(+), 9 deletions(-)

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
index 7187467..d2ea6de 100644
--- 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
@@ -17,7 +17,6 @@
 
 package org.apache.servicecomb.metrics.prometheus;
 
-import java.io.IOException;
 import java.net.InetSocketAddress;
 
 import org.apache.servicecomb.foundation.common.exceptions.ServiceCombException;
@@ -37,21 +36,38 @@ import io.prometheus.client.exporter.HTTPServer;
 public class MetricsHttpPublisher implements ApplicationListener<ApplicationEvent> {
   private static final Logger LOGGER = LoggerFactory.getLogger(MetricsHttpPublisher.class);
 
-  private static final String METRICS_PROMETHEUS_PORT = "servicecomb.metrics.prometheus.port";
+  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
-    int publishPort = DynamicPropertyFactory.getInstance().getIntProperty(METRICS_PROMETHEUS_PORT, 9696).get();
-    MetricsCollector metricsCollector = new MetricsCollector();
-    metricsCollector.register();
+    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 {
-      this.httpServer = new HTTPServer(new InetSocketAddress(publishPort), CollectorRegistry.defaultRegistry, true);
-      LOGGER.info("Prometheus httpServer listened {}.", publishPort);
-    } catch (IOException e) {
-      throw new ServiceCombException("create http publish server failed", e);
+      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
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
new file mode 100644
index 0000000..0f01e46
--- /dev/null
+++ b/metrics/metrics-integration/metrics-prometheus/src/test/java/org/apache/servicecomb/metrics/prometheus/TestMetricsHttpPublisher.java
@@ -0,0 +1,56 @@
+/*
+ * 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");
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
ningjiang@apache.org.