Skip to content

Commit

Permalink
HWKMETRICS-116 Allow ptrans to work behind an HTTP proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
tsegismont committed Jun 1, 2015
1 parent 09d7454 commit d9508da
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 8 deletions.
1 change: 1 addition & 0 deletions clients/ptranslator/ptrans.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ collectd.port=25826

# REST endpoint
rest.url=http://localhost:8080/hawkular/metrics/gauges/data
#http.proxy=http://proxyhost:8000
tenant=default
# Close connection to rest-server after this many requests
rest.close-after=200
Expand Down
1 change: 1 addition & 0 deletions clients/ptranslator/src/assembly/dist/assets/ptrans.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ collectd.port=25826

# REST endpoint
rest.url=http://localhost:8080/hawkular/metrics/gauges/data
#http.proxy=http://proxyhost:8000
tenant=default
# Close connection to rest-server after this many requests
rest.close-after=200
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.GANGLIA_GROUP;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.GANGLIA_MULTICAST_INTERFACE;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.GANGLIA_PORT;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.HTTP_PROXY;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.REST_CLOSE_AFTER_REQUESTS;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.REST_URL;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.SERVICES;
Expand Down Expand Up @@ -60,6 +61,7 @@ public class Configuration {
private final int minimumBatchSize;
private final int maximumBatchDelay;
private final URI restUrl;
private final URI httpProxy;
private final String tenant;
private final int restCloseAfterRequests;
private final int spoolSize;
Expand All @@ -77,6 +79,7 @@ private Configuration(
int minimumBatchSize,
int maximumBatchDelay,
URI restUrl,
URI httpProxy,
String tenant,
int restCloseAfterRequests,
int spoolSize,
Expand All @@ -93,6 +96,7 @@ private Configuration(
this.minimumBatchSize = minimumBatchSize;
this.maximumBatchDelay = maximumBatchDelay;
this.restUrl = restUrl;
this.httpProxy = httpProxy;
this.tenant = tenant;
this.restCloseAfterRequests = restCloseAfterRequests;
this.spoolSize = spoolSize;
Expand All @@ -113,6 +117,11 @@ public static Configuration from(Properties properties) {
int maximumBatchDelay = getIntProperty(properties, BATCH_DELAY, 1);
URI restUrl = URI.create(properties.getProperty(REST_URL.toString(),
"http://localhost:8080/hawkular/metrics/gauges/data"));
String proxyString = properties.getProperty(HTTP_PROXY.toString());
URI httpProxy = null;
if (proxyString != null && !proxyString.trim().isEmpty()) {
httpProxy = URI.create(proxyString);
}
String tenant = properties.getProperty(TENANT.toString(), "default");
int restCloseAfterRequests = getIntProperty(properties, REST_CLOSE_AFTER_REQUESTS, 200);
int spoolSize = getIntProperty(properties, SPOOL_SIZE, 10000);
Expand All @@ -128,6 +137,7 @@ public static Configuration from(Properties properties) {
minimumBatchSize,
maximumBatchDelay,
restUrl,
httpProxy,
tenant,
restCloseAfterRequests,
spoolSize,
Expand Down Expand Up @@ -227,6 +237,10 @@ public URI getRestUrl() {
return restUrl;
}

public URI getHttpProxy() {
return httpProxy;
}

public String getTenant() {
return tenant;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public enum ConfigurationKey {
BATCH_DELAY("batch.delay"),
/** REST endpoint **/
REST_URL("rest.url"),
/**
* If present the HTTP proxy to use
**/
HTTP_PROXY("http.proxy"),
/** Tenant **/
TENANT("tenant"),
/** Close connection to rest-server after this many requests **/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,10 @@ public class RestForwardingHandler extends ChannelInboundHandlerAdapter {

public static final String TENANT_HEADER_NAME = "Hawkular-Tenant";

private final String restHost;
private final int restPort;
private final String restUri;
private final String host;
private final int port;
private final String postUri;
private final String hostHeader;
private final String tenant;
private final int restCloseAfterRequests;

Expand All @@ -93,9 +94,17 @@ public RestForwardingHandler(Configuration configuration) {
LOG.debug("RestForwardingHandler init");

URI restUrl = configuration.getRestUrl();
restHost = restUrl.getHost();
restPort = restUrl.getPort();
restUri = restUrl.getPath();
URI httpProxy = configuration.getHttpProxy();
if (httpProxy == null) {
host = restUrl.getHost();
port = restUrl.getPort();
postUri = restUrl.getPath();
} else {
host = httpProxy.getHost();
port = httpProxy.getPort();
postUri = restUrl.toString();
}
hostHeader = restUrl.getHost();

tenant = configuration.getTenant();

Expand Down Expand Up @@ -162,10 +171,11 @@ private void sendToChannel(final Channel ch) {

String payload = Batcher.metricListToJson(metricsToSend);
ByteBuf content = Unpooled.copiedBuffer(payload, CharsetUtil.UTF_8);
FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, restUri, content);
FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, postUri, content);
HttpHeaders.setHeader(request, CONTENT_TYPE, "application/json;charset=utf-8");
HttpHeaders.setContentLength(request, content.readableBytes());
HttpHeaders.setKeepAlive(request, true);
HttpHeaders.setHost(request, hostHeader);
HttpHeaders.setHeader(request, TENANT_HEADER_NAME, tenant);
// We need to send the list of metrics we are sending down the pipeline, so the status watcher
// can later clean them out of the fifo
Expand Down Expand Up @@ -209,7 +219,7 @@ ChannelFuture connectRestServer(EventLoopGroup group) throws Exception {
}

Bootstrap clientBootstrap = new Bootstrap();
clientBootstrap.group(group).channel(NioSocketChannel.class).remoteAddress(restHost, restPort)
clientBootstrap.group(group).channel(NioSocketChannel.class).remoteAddress(host, port)
.handler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
Expand Down
1 change: 1 addition & 0 deletions clients/ptranslator/src/test/resources/ptrans.conf
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ collectd.port=25826

# REST endpoint
rest.url=http://localhost:8080/hawkular/metrics/gauges/data
#http.proxy=http://proxyhost:8000
tenant=default
# Close connection to rest-server after this many requests
rest.close-after=200
Expand Down

0 comments on commit d9508da

Please sign in to comment.