Skip to content

Commit

Permalink
Merge 72a9e07 into 6f62b75
Browse files Browse the repository at this point in the history
  • Loading branch information
zhengyangyong committed Mar 7, 2018
2 parents 6f62b75 + 72a9e07 commit 04e5eb4
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 10 deletions.
Expand Up @@ -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;
Expand All @@ -37,20 +36,34 @@
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();
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);
this.init(
DynamicPropertyFactory.getInstance().getStringProperty(METRICS_PROMETHEUS_ADDRESS, "localhost:9696").get());
}

public MetricsHttpPublisher(String address) {
this.init(address);
}

private void init(String address) {
String[] hostAndPort = address.split(":");
if (hostAndPort.length == 2) {
try {
InetSocketAddress socketAddress = new InetSocketAddress(hostAndPort[0], Integer.parseInt(hostAndPort[1]));
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);
}
} else {
throw new ServiceCombException("create http publish server failed,bad address : " + address);
}
}

Expand Down
@@ -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("testPublishAddress failed : a:b:c");
}

@Test
public void testBadPublishAddress_BadPort() {
thrown.expect(ServiceCombException.class);
new MetricsHttpPublisher("localhost:xxxx");
Assert.fail("testPublishAddress failed : localhost:xxxx");
}

@Test
public void testBadPublishAddress_ToLargePort() {
thrown.expect(ServiceCombException.class);
new MetricsHttpPublisher("localhost:9999999");
Assert.fail("testPublishAddress failed : localhost:9999999");
}

@Test
public void testRightPublishAddress() {
new MetricsHttpPublisher("localhost:43234");
}
}

0 comments on commit 04e5eb4

Please sign in to comment.