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

Commit

Permalink
Update pinger for metrics sending. Needs a bit more work
Browse files Browse the repository at this point in the history
  • Loading branch information
pilhuhn committed Feb 24, 2015
1 parent 6ac34af commit e44f90a
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 37 deletions.
5 changes: 5 additions & 0 deletions modules/pinger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<artifactId>clients-common</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.hawkular.metrics</groupId>
<artifactId>hawkular-metrics-api</artifactId>
<version>0.3.0-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.jboss.spec.javax.ejb</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@
*/
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;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.hawkular.bus.common.BasicMessage;
import org.hawkular.bus.common.ConnectionContextFactory;
import org.hawkular.bus.common.Endpoint;
Expand All @@ -27,13 +34,7 @@
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import javax.jms.ConnectionFactory;
import javax.jms.JMSException;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -54,42 +55,57 @@ public class MetricPublisher {


/**
* Send a list of metric data for a tenant to the Hwkular-metrics service via REST
* 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
*/
@Asynchronous
public void sendToMetricsViaRest(String tenantId, List<SingleMetric> metrics) {
public void sendToMetricsViaRest(String tenantId, List<Map<String, Object>> metrics) {
// Send it to metrics via rest
/*
Client client = ClientBuilder.newClient();
WebTarget target = client.target("http://localhost:8080/hawkular/metrics/" + tenantId +
WebTarget target = client.target("http://localhost:8080/hawkular-metrics/" + tenantId +
"/metrics/numeric/data");
Entity<List<SingleMetric>> payload = Entity.entity(metrics, MediaType.APPLICATION_JSON_TYPE);
Response response = target.request().post(payload);
Entity<List<NumericMetric>> payload = Entity.entity(metrics, MediaType.APPLICATION_JSON_TYPE);
Invocation invocation = target.request().buildPost(payload);
Response response = invocation.invoke();
Log.LOG.metricPostStatus( response.getStatus() + " : " + response.getStatusInfo());
*/

String payload = new Gson().toJson(metrics);
HttpClient client = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("http://localhost:8080/hawkular-metrics/" + tenantId +
"/metrics/numeric/data");
request.setEntity(new StringEntity(payload, ContentType.APPLICATION_JSON));


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

/**
* Put a list of metric data on the Metrics topic.
* @param tenantId
* @param tenantId Id of the tenant
* @param metrics Metrics to publish
*/
@Asynchronous
public void publishToTopic(String tenantId, List<SingleMetric> metrics) {
if (topic != null) {

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

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

Endpoint endpoint = new Endpoint(Endpoint.Type.TOPIC,topic.getTopicName());
factory = new ConnectionContextFactory(connectionFactory);
ProducerConnectionContext pc = factory.createProducerConnectionContext(endpoint);
BasicMessage msg = new ObjectMessage(data);
MessageProcessor processor = new MessageProcessor();
Expand All @@ -98,15 +114,6 @@ public void publishToTopic(String tenantId, List<SingleMetric> metrics) {
catch (Exception e) {
e.printStackTrace();
}
finally {
if (factory!=null) {
try {
factory.close();
} catch (JMSException e) {
e.printStackTrace(); // TODO: Customise this generated block
}
}
}
}
else {
Log.LOG.wNoTopicConnection("HawkularMetricData");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.hawkular.component.pinger;

import org.hawkular.metrics.client.common.SingleMetric;
import org.hawkular.metrics.core.MetricId;
import org.hawkular.metrics.core.NumericMetric;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
Expand All @@ -30,6 +32,7 @@
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
Expand All @@ -47,7 +50,7 @@
@Singleton
public class PingManager {

String tenantId = "rest-test";
String tenantId = "test";

@EJB
public Pinger pinger;
Expand Down Expand Up @@ -158,25 +161,52 @@ private void reportResults(List<PingStatus> results) {
return;
}

List<SingleMetric> metrics = new ArrayList<>(results.size());
List<SingleMetric> singleMetrics = new ArrayList<>(results.size());
List<NumericMetric> nMetrics = new ArrayList<>(results.size());
List<Map<String,Object>> mMetrics = new ArrayList<>();

for (PingStatus status : results){
SingleMetric m = new SingleMetric(status.destination.name() + ".duration",
status
.getTimestamp(), (double) status.getDuration());
metrics.add(m);
m = new SingleMetric(status.destination.name() + ".code",
status
.getTimestamp(), (double) status.getCode());
metrics.add(m);
NumericMetric numericMetric = new NumericMetric(tenantId,
new MetricId(status.destination.resourceId + ".status.duration"));
numericMetric.addData(status.getTimestamp(), status.getDuration());
nMetrics.add(numericMetric);
numericMetric = new NumericMetric(tenantId,new MetricId(status.destination.resourceId + ".status" +
".code"));
numericMetric.addData(status.getTimestamp(), status.getCode());
nMetrics.add(numericMetric);

addDataItem(mMetrics, status, status.duration, "duration");
addDataItem(mMetrics, status, status.code, "code");


// for the topic to alerting
SingleMetric singleMetric = new SingleMetric(status.destination.name() + ".duration",
status.getTimestamp(), (double) status.getDuration());
singleMetrics.add(singleMetric);
singleMetric = new SingleMetric(status.destination.name() + ".code",
status.getTimestamp(), (double) status.getCode());
singleMetrics.add(singleMetric);

}

// Send them away
metricPublisher.sendToMetricsViaRest(tenantId, metrics);
metricPublisher.publishToTopic(tenantId, metrics);
metricPublisher.sendToMetricsViaRest(tenantId, mMetrics);
metricPublisher.publishToTopic(tenantId, singleMetrics);



}

private void addDataItem(List<Map<String, Object>> mMetrics, PingStatus status, Number value, String name) {
Map<String,Number> dataMap = new HashMap<>(2);
dataMap.put("timestamp", status.getTimestamp());
dataMap.put("value", value);
List<Map<String,Number>> data = new ArrayList<>(1);
data.add(dataMap);
Map<String,Object> outer = new HashMap<>(2);
outer.put("name",status.destination.resourceId + ".status." + name);
outer.put("data",data);
mMetrics.add(outer);
}

public void addDestination(PingDestination s) {
Expand Down

0 comments on commit e44f90a

Please sign in to comment.