WillemJiang closed pull request #574: [SCB-372] support user set metrics prometheus exporter address not only port URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/574
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-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 718746766..d2ea6de36 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 @@ 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 000000000..0f01e4650 --- /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"); + } +} ---------------------------------------------------------------- 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: us...@infra.apache.org With regards, Apache Git Services