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

Commit

Permalink
Publish results to the metrics topic too.
Browse files Browse the repository at this point in the history
  • Loading branch information
pilhuhn authored and jmazzitelli committed Feb 17, 2015
1 parent 05362c1 commit 631fe39
Show file tree
Hide file tree
Showing 5 changed files with 151 additions and 20 deletions.
22 changes: 17 additions & 5 deletions modules/pinger/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-client</artifactId>
<version>3.0.10.Final</version>
<version>3.0.10.Final</version> <!-- TODO move to parent -->
<scope>provided</scope>
</dependency>

Expand All @@ -67,32 +67,44 @@
<dependency>
<groupId>javax.jms</groupId>
<artifactId>jms-api</artifactId>
<version>1.1-rev-1</version>
<version>1.1-rev-1</version> <!-- TODO move to parent -->
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.3.1</version>
<version>4.3.1</version> <!-- TODO move to parent -->
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
<version>2.2.4</version> <!-- TODO move to parent -->
</dependency>
<dependency>
<groupId>org.hawkular.bus</groupId>
<artifactId>hawkular-bus-common</artifactId>
<version>1.0.0.Alpha1-SNAPSHOT</version>
</dependency>

<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging-processor</artifactId>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@ public interface Log {
@Message(id = 5000, value = "No code in message headers found")
void wNoCode();

@LogMessage(level = Logger.Level.INFO)
@LogMessage
@Message(id = 5001, value = "Pinging the remote threw an exception: %s")
void wPingExeption(String message);

@LogMessage
@Message(id = 5002, value = "post status : %s")
void metricPostStatus(String s);

@LogMessage(level = Logger.Level.WARN)
@Message(id = 5003, value = "No connection to topic %s possible")
void wNoTopicConnection(String topicName);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2015 Red Hat, Inc. and/or its affiliates
* and other contributors as indicated by the @author tags.
*
* Licensed 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.hawkular.component.pinger;

import org.hawkular.bus.common.BasicMessage;
import org.hawkular.bus.common.ConnectionContextFactory;
import org.hawkular.bus.common.Endpoint;
import org.hawkular.bus.common.MessageProcessor;
import org.hawkular.bus.common.ObjectMessage;
import org.hawkular.bus.common.producer.ProducerConnectionContext;
import org.hawkular.metrics.client.common.SingleMetric;

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.util.List;

/**
* Publish metrics data
*
* @author Heiko W. Rupp
*/
@Stateless
public class MetricPublisher {

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

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


/**
* Send a list of metric data for a tenant to the Hwkular-metrics service via REST
* @param tenantId Name of the tenant
* @param metrics List of metrics
*/
@Asynchronous
public void sendToMetricsViaRest(String tenantId, List<SingleMetric> metrics) {
// Send it to metrics via rest
Client client = ClientBuilder.newClient();
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);

Log.LOG.metricPostStatus( response.getStatus() + " : " + response.getStatusInfo());
}

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

ConnectionContextFactory factory = null;
try {

Endpoint endpoint = new Endpoint(Endpoint.Type.TOPIC,topic.getTopicName());
factory = new ConnectionContextFactory(connectionFactory);
ProducerConnectionContext pc = factory.createProducerConnectionContext(endpoint);
BasicMessage msg = new ObjectMessage(metrics);
MessageProcessor processor = new MessageProcessor();
processor.send(pc, msg);
}
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");
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,7 @@
import javax.ejb.Startup;
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.util.ArrayList;
import java.util.HashSet;
Expand All @@ -54,6 +52,8 @@ public class PingManager {

Set<PingDestination> destinations = new HashSet<>();

@EJB
MetricPublisher metricPublisher;

@PostConstruct
public void startUp() {
Expand Down Expand Up @@ -83,9 +83,11 @@ public void startUp() {
}



/**
* This method does the actual work
*/
@Lock(LockType.READ)
@Schedule(minute = "*", hour = "*")
@Schedule(minute = "*", hour = "*", persistent = false)
public void scheduleWork() {

List<PingStatus> results = new ArrayList<>(destinations.size());
Expand All @@ -100,6 +102,7 @@ public void scheduleWork() {

private void reportResults(List<PingStatus> results) {


List<SingleMetric> metrics = new ArrayList<>(results.size());
for (PingStatus status : results){
SingleMetric m = new SingleMetric(status.destination.name() + ".duration",
Expand All @@ -113,16 +116,10 @@ private void reportResults(List<PingStatus> results) {

}

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

Client client = ClientBuilder.newClient();
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);

System.err.println("post status " + response.getStatus() + " : " + response.getStatusInfo());


}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@
*
* @author Heiko W. Rupp
*
* TODO should this be a MDB, so that we can have many requests in the pipe?
*/
@Stateless
public class Pinger {

// We could mark the method as follows and return an AsyncResult.
// But how do we then publish the data? From here as individual items?
// @Asynchronous
// public Future<PingStatus> ping(PingDestination destination) {
@Timeout()
public PingStatus ping(PingDestination destination) {

Expand Down Expand Up @@ -67,6 +70,7 @@ public PingStatus ping(PingDestination destination) {
}

return status;
//return new AsyncResult<>(status);

}
}

0 comments on commit 631fe39

Please sign in to comment.