Skip to content

Commit

Permalink
Merge pull request #209 from tsegismont/jira/HWKMETRICS-84
Browse files Browse the repository at this point in the history
HWKMETRICS-84 Update ptrans to use tenant as a request header
  • Loading branch information
pilhuhn committed May 12, 2015
2 parents cbcf7fc + 56e4c3c commit b9d6249
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 41 deletions.
3 changes: 2 additions & 1 deletion clients/ptranslator/ptrans.conf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ statsd.port=8125
collectd.port=25826

# REST endpoint
rest.url=http://localhost:8080/hawkular/metrics/gauges/data?tenantId=test
rest.url=http://localhost:8080/hawkular/metrics/gauges/data
tenant=default
# Close connection to rest-server after this many requests
rest.close-after=200

Expand Down
3 changes: 2 additions & 1 deletion clients/ptranslator/src/assembly/dist/assets/ptrans.conf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ statsd.port=8125
collectd.port=25826

# REST endpoint
rest.url=http://localhost:8080/hawkular/metrics/gauges/data?tenantId=test
rest.url=http://localhost:8080/hawkular/metrics/gauges/data
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 @@ -28,6 +28,7 @@
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.SPOOL_SIZE;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.STATSD_PORT;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.TCP_PORT;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.TENANT;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.UDP_PORT;

import java.net.URI;
Expand Down Expand Up @@ -59,6 +60,7 @@ public class Configuration {
private final int minimumBatchSize;
private final int maximumBatchDelay;
private final URI restUrl;
private final String tenant;
private final int restCloseAfterRequests;
private final int spoolSize;
private final Set<String> validationMessages;
Expand All @@ -75,6 +77,7 @@ private Configuration(
int minimumBatchSize,
int maximumBatchDelay,
URI restUrl,
String tenant,
int restCloseAfterRequests,
int spoolSize,
Set<String> validationMessages
Expand All @@ -90,6 +93,7 @@ private Configuration(
this.minimumBatchSize = minimumBatchSize;
this.maximumBatchDelay = maximumBatchDelay;
this.restUrl = restUrl;
this.tenant = tenant;
this.restCloseAfterRequests = restCloseAfterRequests;
this.spoolSize = spoolSize;
this.validationMessages = Collections.unmodifiableSet(validationMessages);
Expand All @@ -101,14 +105,15 @@ public static Configuration from(Properties properties) {
int udpPort = getIntProperty(properties, UDP_PORT, 5140);
int tcpPort = getIntProperty(properties, TCP_PORT, 5140);
int gangliaPort = getIntProperty(properties, GANGLIA_PORT, 8649);
String gangliaGroup = properties.getProperty(GANGLIA_GROUP.getExternalForm(), "239.2.11.71");
String multicastIfOverride = properties.getProperty(GANGLIA_MULTICAST_INTERFACE.getExternalForm());
String gangliaGroup = properties.getProperty(GANGLIA_GROUP.toString(), "239.2.11.71");
String multicastIfOverride = properties.getProperty(GANGLIA_MULTICAST_INTERFACE.toString());
int statsDport = getIntProperty(properties, STATSD_PORT, 8125);
int collectdPort = getIntProperty(properties, COLLECTD_PORT, 25826);
int minimumBatchSize = getIntProperty(properties, BATCH_SIZE, 50);
int maximumBatchDelay = getIntProperty(properties, BATCH_DELAY, 1);
URI restUrl = URI.create(properties.getProperty(REST_URL.getExternalForm(),
"http://localhost:8080/hawkular/metrics/"));
URI restUrl = URI.create(properties.getProperty(REST_URL.toString(),
"http://localhost:8080/hawkular/metrics/gauges/data"));
String tenant = properties.getProperty(TENANT.toString(), "default");
int restCloseAfterRequests = getIntProperty(properties, REST_CLOSE_AFTER_REQUESTS, 200);
int spoolSize = getIntProperty(properties, SPOOL_SIZE, 10000);
return new Configuration(
Expand All @@ -123,16 +128,17 @@ public static Configuration from(Properties properties) {
minimumBatchSize,
maximumBatchDelay,
restUrl,
tenant,
restCloseAfterRequests,
spoolSize,
validationMessages
);
}

private static Set<Service> getServices(Properties properties, Set<String> validationMessages) {
String servicesProperty = properties.getProperty(SERVICES.getExternalForm());
String servicesProperty = properties.getProperty(SERVICES.toString());
if (servicesProperty == null) {
validationMessages.add(String.format(Locale.ROOT, "Property %s not found", SERVICES.getExternalForm()));
validationMessages.add(String.format(Locale.ROOT, "Property %s not found", SERVICES.toString()));
return Collections.emptySet();
}
Set<Service> services = EnumSet.noneOf(Service.class);
Expand All @@ -156,7 +162,7 @@ private static Set<Service> getServices(Properties properties, Set<String> valid
}

private static int getIntProperty(Properties properties, ConfigurationKey key, int defaultValue) {
String property = properties.getProperty(key.getExternalForm());
String property = properties.getProperty(key.toString());
if (property == null) {
return defaultValue;
}
Expand Down Expand Up @@ -221,6 +227,10 @@ public URI getRestUrl() {
return restUrl;
}

public String getTenant() {
return tenant;
}

public int getRestCloseAfterRequests() {
return restCloseAfterRequests;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public enum ConfigurationKey {
BATCH_DELAY("batch.delay"),
/** REST endpoint **/
REST_URL("rest.url"),
/** Tenant **/
TENANT("tenant"),
/** Close connection to rest-server after this many requests **/
REST_CLOSE_AFTER_REQUESTS("rest.close-after"),
/** Maximum number of metrics to spool if the server is not reachable **/
Expand All @@ -60,7 +62,8 @@ public enum ConfigurationKey {
/**
* @return string representation of this configuration key
*/
public String getExternalForm() {
@Override
public String toString() {
return externalForm;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
*/
package org.hawkular.metrics.clients.ptrans.backend;

import static io.netty.handler.codec.http.HttpHeaders.Names.CONTENT_TYPE;
import static io.netty.handler.codec.http.HttpMethod.POST;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
import io.netty.bootstrap.Bootstrap;
Expand Down Expand Up @@ -66,10 +67,12 @@
public class RestForwardingHandler extends ChannelInboundHandlerAdapter {
private static final Logger LOG = LoggerFactory.getLogger(RestForwardingHandler.class);

private static final String TENANT_HEADER_NAME = "tenantId";

private final String restHost;
private final int restPort;
private final String restPrefix;
private final String restParams;
private final String restUri;
private final String tenant;
private final int restCloseAfterRequests;

BoundMetricFifo fifo;
Expand All @@ -91,8 +94,9 @@ public RestForwardingHandler(Configuration configuration) {
URI restUrl = configuration.getRestUrl();
restHost = restUrl.getHost();
restPort = restUrl.getPort();
restPrefix = restUrl.getPath();
restParams = restUrl.getQuery();
restUri = restUrl.getPath();

tenant = configuration.getTenant();

restCloseAfterRequests = configuration.getRestCloseAfterRequests();

Expand Down Expand Up @@ -157,10 +161,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, restPrefix + "?" + restParams, content);
FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, POST, restUri, content);
HttpHeaders.setHeader(request, CONTENT_TYPE, "application/json;charset=utf-8");
HttpHeaders.setContentLength(request, content.readableBytes());
HttpHeaders.setKeepAlive(request, true);
HttpHeaders.setHeader(request, HttpHeaders.Names.CONTENT_TYPE, "application/json;charset=utf-8");
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
ch.attr(listKey).set(metricsToSend);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void shouldExitWithErrorIfServicesPropertyIsMissing() throws Exception {
try (InputStream in = new FileInputStream(ptransConfFile)) {
properties.load(in);
}
properties.remove(SERVICES.getExternalForm());
properties.remove(SERVICES.toString());
try (OutputStream out = new FileOutputStream(ptransConfFile)) {
properties.store(out, "");
}
Expand All @@ -71,7 +71,7 @@ public void shouldExitWithErrorIfServicesListIsEmpty() throws Exception {
try (InputStream in = new FileInputStream(ptransConfFile)) {
properties.load(in);
}
properties.setProperty(SERVICES.getExternalForm(), " , , ,,,, ,,,, ,");
properties.setProperty(SERVICES.toString(), " , , ,,,, ,,,, ,");
try (OutputStream out = new FileOutputStream(ptransConfFile)) {
properties.store(out, "");
}
Expand All @@ -95,7 +95,7 @@ public void shouldExitWithErrorIfServicesListContainsUnknownService() throws Exc
try (InputStream in = new FileInputStream(ptransConfFile)) {
properties.load(in);
}
properties.setProperty(SERVICES.getExternalForm(), "marseille, collectd");
properties.setProperty(SERVICES.toString(), "marseille, collectd");
try (OutputStream out = new FileOutputStream(ptransConfFile)) {
properties.store(out, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public void shouldBindPortsForEnabledServicesOnly() throws Exception {
try (InputStream in = new FileInputStream(ptransConfFile)) {
properties.load(in);
}
properties.setProperty(SERVICES.getExternalForm(), Service.COLLECTD.getExternalForm());
properties.setProperty(SERVICES.toString(), Service.COLLECTD.getExternalForm());
try (OutputStream out = new FileOutputStream(ptransConfFile)) {
properties.store(out, "");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void constructorShouldThrowExceptionWhenProvidedConfigurationIsInvalid()
@Test
public void constructorShouldNotThrowExceptionWhenProvidedConfigurationIsValid() {
Properties properties = new Properties();
properties.setProperty(SERVICES.getExternalForm(), COLLECTD.getExternalForm());
properties.setProperty(SERVICES.toString(), COLLECTD.getExternalForm());
new PTrans(Configuration.from(properties));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,14 @@ public void setUp() throws Exception {
when(eventLoop.parent()).thenReturn(eventLoopGroup);

Properties properties = new Properties();
String addGuageDataUrl = "http://" + BASE_URI + "/gauges/data?tenantId=" + TENANT;
properties.setProperty(ConfigurationKey.REST_URL.getExternalForm(), addGuageDataUrl);
String addGaugeDataUrl = "http://" + BASE_URI + "/gauges/data";
properties.setProperty(ConfigurationKey.REST_URL.toString(), addGaugeDataUrl);
properties.setProperty(ConfigurationKey.TENANT.toString(), TENANT);
Configuration configuration = Configuration.from(properties);

restForwardingHandler = new RestForwardingHandler(configuration);

findGaugeDataUrl = "http://" + BASE_URI + "/gauges/" + METRIC_NAME + "/data?tenantId=" + TENANT;
findGaugeDataUrl = "http://" + BASE_URI + "/gauges/" + METRIC_NAME + "/data";
}

@Test
Expand All @@ -101,6 +102,7 @@ public void shouldForwardMetrics() throws Exception {

private JsonNode findGaugeDataOnServer() throws IOException {
HttpURLConnection urlConnection = (HttpURLConnection) new URL(findGaugeDataUrl).openConnection();
urlConnection.setRequestProperty("tenantId", TENANT);
urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@
import static java.util.stream.Collectors.counting;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.BATCH_DELAY;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.BATCH_SIZE;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.REST_URL;
import static org.hawkular.metrics.clients.ptrans.ConfigurationKey.SERVICES;

import static org.hawkular.metrics.clients.ptrans.util.ProcessUtil.kill;
import static org.hawkular.metrics.clients.ptrans.util.TenantUtil.getRandomTenantId;
import static org.junit.Assert.assertEquals;
Expand All @@ -49,8 +46,7 @@
import java.util.Properties;
import java.util.stream.Stream;

import jnr.constants.platform.Signal;

import org.hawkular.metrics.clients.ptrans.ConfigurationKey;
import org.hawkular.metrics.clients.ptrans.ExecutableITestBase;
import org.hawkular.metrics.clients.ptrans.PrintOutputOnFailureWatcher;
import org.hawkular.metrics.clients.ptrans.Service;
Expand All @@ -66,6 +62,8 @@
import com.google.common.collect.ImmutableList;
import com.google.common.io.Resources;

import jnr.constants.platform.Signal;

/**
* @author Thomas Segismont
*/
Expand All @@ -75,9 +73,10 @@ public class CollectdITest extends ExecutableITestBase {
"hawkular-metrics.base-uri",
"127.0.0.1:8080/hawkular/metrics"
);
private static final String HAWKULAR_TENANT_HEADER = "tenantId";

private String tenant;
private String findGuageMetricsUrl;
private String findGaugeMetricsUrl;
private File collectdConfFile;
private File collectdOut;
private File collectdErr;
Expand All @@ -94,7 +93,7 @@ public class CollectdITest extends ExecutableITestBase {
@Before
public void setUp() throws Exception {
tenant = getRandomTenantId();
findGuageMetricsUrl = "http://" + BASE_URI + "/metrics?type=gauge&tenantId=" + tenant;
findGaugeMetricsUrl = "http://" + BASE_URI + "/metrics?type=gauge";
assumeCollectdIsPresent();
configureCollectd();
assertCollectdConfIsValid();
Expand Down Expand Up @@ -141,11 +140,12 @@ public void configurePTrans() throws Exception {
try (InputStream in = new FileInputStream(ptransConfFile)) {
properties.load(in);
}
properties.setProperty(SERVICES.getExternalForm(), Service.COLLECTD.getExternalForm());
properties.setProperty(BATCH_DELAY.getExternalForm(), String.valueOf(1));
properties.setProperty(BATCH_SIZE.getExternalForm(), String.valueOf(1));
String restUrl = "http://" + BASE_URI + "/gauges/data?tenantId=" + tenant;
properties.setProperty(REST_URL.getExternalForm(), restUrl);
properties.setProperty(ConfigurationKey.SERVICES.toString(), Service.COLLECTD.getExternalForm());
properties.setProperty(ConfigurationKey.BATCH_DELAY.toString(), String.valueOf(1));
properties.setProperty(ConfigurationKey.BATCH_SIZE.toString(), String.valueOf(1));
String restUrl = "http://" + BASE_URI + "/gauges/data";
properties.setProperty(ConfigurationKey.REST_URL.toString(), restUrl);
properties.setProperty(ConfigurationKey.TENANT.toString(), tenant);
try (OutputStream out = new FileOutputStream(ptransConfFile)) {
properties.store(out, "");
}
Expand Down Expand Up @@ -242,12 +242,13 @@ private Point collectdLogToPoint(String line) {
private List<Point> getServerData() throws Exception {
ObjectMapper objectMapper = new ObjectMapper();

HttpURLConnection urlConnection = (HttpURLConnection) new URL(findGuageMetricsUrl).openConnection();
HttpURLConnection urlConnection = (HttpURLConnection) new URL(findGaugeMetricsUrl).openConnection();
urlConnection.setRequestProperty(HAWKULAR_TENANT_HEADER, tenant);
urlConnection.connect();
int responseCode = urlConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
String msg = "Could not get metrics list from server: %s, %d";
fail(String.format(Locale.ROOT, msg, findGuageMetricsUrl, responseCode));
fail(String.format(Locale.ROOT, msg, findGaugeMetricsUrl, responseCode));
}
List<String> metricNames;
try (InputStream inputStream = urlConnection.getInputStream()) {
Expand All @@ -264,6 +265,7 @@ private List<Point> getServerData() throws Exception {
String type = split[split.length - 1];

urlConnection = (HttpURLConnection) new URL(findGaugeDataUrl(metricName)).openConnection();
urlConnection.setRequestProperty(HAWKULAR_TENANT_HEADER, tenant);
urlConnection.connect();
responseCode = urlConnection.getResponseCode();
if (responseCode != HttpURLConnection.HTTP_OK) {
Expand All @@ -284,7 +286,7 @@ private List<Point> getServerData() throws Exception {
}

private String findGaugeDataUrl(String metricName) {
return "http://" + BASE_URI + "/gauges/" + metricName + "/data?tenantId=" + tenant;
return "http://" + BASE_URI + "/gauges/" + metricName + "/data";
}

@After
Expand Down
3 changes: 2 additions & 1 deletion clients/ptranslator/src/test/resources/ptrans.conf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ statsd.port=8125
collectd.port=25826

# REST endpoint
rest.url=http://localhost:8080/hawkular/metrics/gauges/data?tenantId=test
rest.url=http://localhost:8080/hawkular/metrics/gauges/data
tenant=default
# Close connection to rest-server after this many requests
rest.close-after=200

Expand Down

0 comments on commit b9d6249

Please sign in to comment.