Skip to content
This repository has been archived by the owner on Nov 9, 2017. It is now read-only.

Commit

Permalink
Merge pull request #121 from metlos/pull/113
Browse files Browse the repository at this point in the history
[HAWKULAR-173][HWKINVENT-43] reworked pinger + fixes to it.
  • Loading branch information
mtho11 committed May 22, 2015
2 parents b26552c + e4dc639 commit 10bc739
Show file tree
Hide file tree
Showing 16 changed files with 927 additions and 325 deletions.
29 changes: 29 additions & 0 deletions modules/pinger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@

<dependencies>

<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
Expand All @@ -46,11 +52,19 @@
<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>inventory-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>inventory-bus-api</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.hawkular.inventory</groupId>
<artifactId>hawkular-inventory-cdi</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
Expand All @@ -69,6 +83,13 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms-api</artifactId>
Expand All @@ -92,6 +113,13 @@
<artifactId>gson</artifactId>
<version>${version.com.google.code.gson}</version> <!-- TODO move to parent -->
</dependency>

<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>${version.com.google.guava}</version>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
Expand All @@ -100,6 +128,7 @@
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public interface Log extends BasicLogger {
void wPingExeption(String message);

@LogMessage(level = Logger.Level.WARN)
@Message(id = 5002, value = "Post to metrics faild with post status : %s")
@Message(id = 5002, value = "Post to metrics failed with post status : %s")
void metricPostStatus(String s);

@LogMessage(level = Logger.Level.WARN)
Expand All @@ -53,4 +53,5 @@ public interface Log extends BasicLogger {
@Message(id = 5004, value = "Could not contact inventory - there will be no resources to start pinging. " +
"Code %d, message= %s")
void wNoInventoryFound(int status, String reasonPhrase);

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.hawkular.component.pinger;

import com.google.gson.Gson;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
Expand All @@ -34,7 +35,9 @@
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.jms.ConnectionFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -47,72 +50,110 @@
@Stateless
public class MetricPublisher {

@javax.annotation.Resource( lookup = "java:/topic/HawkularMetricData")
private static void addDataItem(List<Map<String, Object>> mMetrics, String resourceId, long timestamp,
Number value, String name) {
Map<String, Number> dataMap = new HashMap<>(4);
dataMap.put("timestamp", timestamp);
dataMap.put("value", value);
List<Map<String, Number>> data = new ArrayList<>(1);
data.add(dataMap);
Map<String, Object> outer = new HashMap<>(4);
outer.put("id", resourceId + ".status." + name);
outer.put("data", data);
mMetrics.add(outer);
}

@javax.annotation.Resource(lookup = "java:/topic/HawkularMetricData")
javax.jms.Topic topic;

@javax.annotation.Resource (lookup = "java:/HawkularBusConnectionFactory")
@javax.annotation.Resource(lookup = "java:/HawkularBusConnectionFactory")
ConnectionFactory connectionFactory;

private final PingerConfiguration configuration = PingerConfiguration.defaults();

/**
* Send a list of metric data for a tenant to the Hawkular-metrics service via REST
* @param tenantId Name of the tenant
* @param metrics List of metrics
* Serializes the given {@link PingStatus} and then submits it to Hawkular-metrics service via REST
*
* @param status
* the {@link PingStatus} to publish
*/
@Asynchronous
public void sendToMetricsViaRest(String tenantId, List<Map<String, Object>> metrics) {
public void sendToMetricsViaRest(PingStatus status) {

List<Map<String, Object>> mMetrics = new ArrayList<>();

final PingDestination dest = status.getDestination();
final String resourceId = dest.getResourceId();
final long timestamp = status.getTimestamp();
addDataItem(mMetrics, resourceId, timestamp, status.getDuration(), "duration");
addDataItem(mMetrics, resourceId, timestamp, status.getCode(), "code");

// Send it to metrics via rest
String payload = new Gson().toJson(metrics);
String payload = new Gson().toJson(mMetrics);
HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost(configuration.getMetricsBaseUri() +"/" + tenantId +
"/metrics/numeric/data");
request.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));

// TODO replace for metrics 0.3.3
HttpPost request = new HttpPost(configuration.getMetricsBaseUri() + "/" + status.getDestination().getTenantId()
+ "/metrics/numeric/data");
// HttpPost request = new HttpPost(configuration.getMetricsBaseUri() + "/metrics/numeric/data");
// request.addHeader("tenantId", status.getDestination().getTenantId());
// TODO end replace for metrics 0.3.3

request.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));

try {
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode()>399) {
if (response.getStatusLine().getStatusCode() > 399) {
Log.LOG.metricPostStatus(response.getStatusLine().toString());
}
} catch (IOException e) {
e.printStackTrace(); // TODO: Customise this generated block
e.printStackTrace(); // TODO: Customise this generated block
}
}

/**
* Put a list of metric data on the Metrics topic.
* @param tenantId Id of the tenant
* @param metrics Metrics to publish
* Serializes the given {@link PingStatus} and then submits it on the Metrics topic of the bus.
*
* @param status
* the {@link PingStatus} to publish
*/
@Asynchronous
public void publishToTopic(String tenantId, List<SingleMetric> metrics) {
public void publishToTopic(PingStatus status) {
if (topic != null) {

try ( ConnectionContextFactory factory = new ConnectionContextFactory(connectionFactory)) {
try (ConnectionContextFactory factory = new ConnectionContextFactory(connectionFactory)) {

List<SingleMetric> singleMetrics = new ArrayList<>();

final PingDestination dest = status.getDestination();
final String resourceId = dest.getResourceId();
final long timestamp = status.getTimestamp();

Map<String,Object> outer = new HashMap<>(1);
SingleMetric singleMetric = new SingleMetric(resourceId + ".status.duration", timestamp,
(double) status.getDuration());
singleMetrics.add(singleMetric);
singleMetric = new SingleMetric(resourceId + ".status.code", timestamp, (double) status.getCode());
singleMetrics.add(singleMetric);

Map<String,Object> data = new HashMap<>(2);
data.put("tenantId",tenantId);
data.put("data",metrics);
Map<String, Object> outer = new HashMap<>(1);

outer.put("metricData",data);
Map<String, Object> data = new HashMap<>(2);
data.put("tenantId", status.getDestination().getTenantId());
data.put("data", singleMetrics);

Endpoint endpoint = new Endpoint(Endpoint.Type.TOPIC,topic.getTopicName());
outer.put("metricData", data);

Endpoint endpoint = new Endpoint(Endpoint.Type.TOPIC, topic.getTopicName());
ProducerConnectionContext pc = factory.createProducerConnectionContext(endpoint);
BasicMessage msg = new ObjectMessage(outer);
MessageProcessor processor = new MessageProcessor();
processor.send(pc, msg);
}
catch (Exception e) {
} catch (Exception e) {
e.printStackTrace();
}
}
else {
} else {
Log.LOG.wNoTopicConnection("HawkularMetricData");
}
}


}

This file was deleted.

0 comments on commit 10bc739

Please sign in to comment.