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 #357 from ppalaga/HAWKULAR-226.1
Browse files Browse the repository at this point in the history
HAWKULAR-226 Display traits from a URL
  • Loading branch information
mtho11 committed Jul 26, 2015
2 parents a6d398e + dd31630 commit d86ae0e
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 138 deletions.
4 changes: 3 additions & 1 deletion console/src/main/scripts/plugins/metrics/html/url-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ <h2 ng-show="vm.resourceList.length > 0" class="text-center hk-fade-in">Current

<div ng-class="{'hk-fade-in': vm.resourceList}">
<div class="hk-url-item" ng-repeat="res in vm.resourceList | orderBy:'properties.url':vm.reverse">
<div class="hk-url-heading">
<div class="panel panel-default hk-url-heading">
<a href="/hawkular-ui/url/availability/{{res.id}}">{{res.properties.url}}</a>
<span ng-show="res.properties['trait-collected-on']" class="pull-right">{{res.traits}}</span>
</div>

<div class="panel panel-default hk-summary">
<div class="row">
<div class="col-sm-3 hk-summary-item">
Expand Down
8 changes: 7 additions & 1 deletion console/src/main/scripts/plugins/metrics/less/metrics.less
Original file line number Diff line number Diff line change
Expand Up @@ -465,11 +465,17 @@ a:hover .fa-spinner {
margin-top: @grid-gutter-width/4*3;

.hk-url-heading {
margin-bottom: @grid-gutter-width/4;
margin-bottom: 1px;
padding: @grid-gutter-width/4 @grid-gutter-width/3;

a {
font-size: ceil(@font-size-base * 1.333333);
}

span {
color: @gray-light;
margin-top: ceil(@font-size-base * 1.333333) - @font-size-base;
}
}

.hk-summary {
Expand Down
8 changes: 8 additions & 0 deletions console/src/main/scripts/plugins/metrics/ts/urlList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,14 @@ module HawkularMetrics {
}, this);
var promises = [];
angular.forEach(aResourceList, function(res, idx) {
var traitsArray:string[] = [];
if (res.properties['trait-remote-address']) {
traitsArray.push('IP: ' + res.properties['trait-remote-address']);
}
if (res.properties['trait-powered-by']) {
traitsArray.push('Powered by: ' + res.properties['trait-powered-by']);
}
res['traits'] = traitsArray.join(' | ');
promises.push(this.HawkularMetric.GaugeMetricData(tenantId).queryMetrics({
resourceId: res.id, gaugeId: (res.id + '.status.duration'),
start: moment().subtract(1, 'hours').valueOf(), end: moment().valueOf()}, (resource) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package org.hawkular.component.pinger;



/**
* An outcome of a ping.
*
Expand Down Expand Up @@ -67,9 +68,10 @@ public static final PingStatus error(PingDestination destination, int code, long
private final boolean timedOut;

/** The value of {@code System.currentTimeMillis()} when the response was received or when the timeout or other
* error was detected. */
* error was detected */
private final long timestamp;

/** The {@link Traits} collected from the ping response */
private final Traits traits;

/**
Expand All @@ -95,6 +97,7 @@ public PingStatus(PingDestination destination, int code, long timestamp, int dur
* the timeout or other error was detected
* @param duration Ping round trip duration in milliseconds or {@value #INVALID_DURATION} if the ping timed out
* @param timedOut {@code true} if the ping timed out, {@code false} otherwise
* @param remoteAddress
*
* @see #timeout(PingDestination, long, int)
* @see #error(PingDestination, int, long)
Expand All @@ -109,27 +112,46 @@ private PingStatus(PingDestination destination, int code, long timestamp, int du
this.traits = traits;
}

/**
* @return the HTTP status code of the ping response
*/
public int getCode() {
return code;
}

/**
* @return ping round trip duration in milliseconds or {@value #INVALID_DURATION} if the ping timed out
*/
public int getDuration() {
return duration;
}

/**
* @return {@code true} if the ping timed out, {@code false} otherwise
*/
public boolean isTimedOut() {
return timedOut;
}


/**
* @return the value of {@code System.currentTimeMillis()} when the response was received or when the timeout or
* other error was detected
*/
public long getTimestamp() {
return timestamp;
}

/**
* @return the destination where the ping was sent
*/
public PingDestination getDestination() {
return destination;
}

/**
* @return the {@link Traits} collected from the ping response
*/
public Traits getTraits() {
return traits;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package org.hawkular.component.pinger;

import java.io.IOException;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
Expand All @@ -27,16 +29,23 @@
import javax.ejb.Stateless;
import javax.net.ssl.SSLContext;

import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;

/**
Expand All @@ -50,12 +59,37 @@
@Stateless
public class Pinger {

/** A key to use when storing and retrieving remote IP address from and to {@link HttpContext} */
static final String REMOTE_ADDRESS_ATTRIBUTE = Pinger.class.getPackage().getName() + ".remoteAddress";

/** A custom connnection manager used by this pinger */
private final HttpClientConnectionManager connectionManager;

public Pinger() throws Exception {
connectionManager = createConnectionManager();
}

/**
* SSL Context trusting all certificates.
* Creates a custom {@link HttpClientConnectionManager} that will be used by this pinger. The returned connection
* manager accepts all SSL certificates, and stores remote IP address into {@link HttpContext} under
* {@link #REMOTE_ADDRESS_ATTRIBUTE}.
*
* @return a new {@link HttpClientConnectionManager}
*/
private final SSLContext sslContext;
private HttpClientConnectionManager createConnectionManager() {

PlainConnectionSocketFactory plainSf = new PlainConnectionSocketFactory() {
@Override
public Socket connectSocket(int connectTimeout, Socket socket, org.apache.http.HttpHost host,
java.net.InetSocketAddress remoteAddress, java.net.InetSocketAddress localAddress,
HttpContext context) throws IOException {
InetAddress remoteInetAddress = remoteAddress.getAddress();
Log.LOG.tracef("Putting remote IP address to HttpContext %s", remoteInetAddress);
context.setAttribute(REMOTE_ADDRESS_ATTRIBUTE, remoteInetAddress);
return super.connectSocket(connectTimeout, socket, host, remoteAddress, localAddress, context);
}
};

public Pinger() throws Exception {
SSLContext tmpSslContext;

try {
Expand All @@ -72,17 +106,11 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
tmpSslContext = null;
}

sslContext = tmpSslContext;
}
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(tmpSslContext, null, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);

private CloseableHttpClient getHttpClient(final String url) {
if (url != null && url.startsWith("https://") && sslContext != null) {
return HttpClientBuilder.create()
.setHostnameVerifier(SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER)
.setSslcontext(sslContext).build();
} else {
return HttpClientBuilder.create().build();
}
return new PoolingHttpClientConnectionManager(RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", plainSf).register("https", sslSocketFactory).build());
}

/**
Expand All @@ -96,19 +124,22 @@ public Future<PingStatus> ping(final PingDestination destination) {
Log.LOG.debugf("About to ping %s", destination.getUrl());
HttpUriRequest request = RequestBuilder.create(destination.getMethod()).setUri(destination.getUrl()).build();

try (CloseableHttpClient client = getHttpClient(destination.getUrl())) {
try (CloseableHttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build()) {
long start = System.currentTimeMillis();
HttpResponse httpResponse = client.execute(request);
StatusLine statusLine = httpResponse.getStatusLine();
EntityUtils.consumeQuietly(httpResponse.getEntity());
long now = System.currentTimeMillis();

final int code = statusLine.getStatusCode();
final int duration = (int) (now - start);
Traits traits = Traits.collect(httpResponse, now);
PingStatus result = new PingStatus(destination, code, now, duration, traits);
Log.LOG.debugf("Got status code %d from %s", code, destination.getUrl());
return new AsyncResult<>(result);
HttpClientContext context = HttpClientContext.create();
try (CloseableHttpResponse httpResponse = client.execute(request, context)) {
InetAddress remoteAddress = (InetAddress) context.getAttribute(REMOTE_ADDRESS_ATTRIBUTE);
StatusLine statusLine = httpResponse.getStatusLine();
EntityUtils.consumeQuietly(httpResponse.getEntity());
long now = System.currentTimeMillis();

final int code = statusLine.getStatusCode();
final int duration = (int) (now - start);
Traits traits = Traits.collect(httpResponse, now, remoteAddress);
PingStatus result = new PingStatus(destination, code, now, duration, traits);
Log.LOG.debugf("Got status code %d from %s", code, destination.getUrl());
return new AsyncResult<>(result);
}
} catch (UnknownHostException e) {
PingStatus result = PingStatus.error(destination, 404, System.currentTimeMillis());
Log.LOG.debugf("Got UnknownHostException for %s", destination.getUrl());
Expand Down

0 comments on commit d86ae0e

Please sign in to comment.