Skip to content

Commit

Permalink
Configure HTTP Monitoring using a YAML file. Updated metrics and http…
Browse files Browse the repository at this point in the history
…-monitoring samples
  • Loading branch information
chrishantha committed Jul 6, 2016
1 parent 2dc47fe commit 313d8cc
Show file tree
Hide file tree
Showing 21 changed files with 463 additions and 286 deletions.
2 changes: 1 addition & 1 deletion analytics/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ From DAS_HOME, run, bin/wso2server.sh to start DAS and make sure that it starts

Run a sample that publishes data to DAS
------------------------------------------
Run the [Metrics and HTTP Monitoring Sample](../samples/metrics-httpmon/metrics-httpmon-fatjar)
Run the [HTTP Monitoring Sample](../samples/http-monitoring)
included in the distribution. This sample will publish data to DAS.

Accessing the dashboard
Expand Down
5 changes: 5 additions & 0 deletions analytics/msf4j-analytics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<groupId>org.wso2.carbon.metrics</groupId>
<artifactId>org.wso2.carbon.metrics.config</artifactId>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.wso2.carbon.databridge.commons.Event;
import org.wso2.carbon.databridge.commons.exception.TransportException;
import org.wso2.carbon.databridge.commons.utils.DataBridgeCommonsUtils;
import org.wso2.msf4j.analytics.httpmonitoring.config.model.DasConfig;
import org.wso2.msf4j.util.SystemVariableUtil;

import java.net.Inet4Address;
Expand Down Expand Up @@ -66,30 +67,13 @@ public final class HTTPMonitoringDataPublisher {
}
}

private HTTPMonitoringDataPublisher() {
HTTPMonitoringConfig httpMonitoringConfig = HTTPMonitoringConfigBuilder.build();
init(httpMonitoringConfig);
public HTTPMonitoringDataPublisher(DasConfig dasConfig) {
init(dasConfig);
// Destroy data publisher at shutdown
Thread thread = new Thread(() -> destroy());
Thread thread = new Thread(this::destroy);
Runtime.getRuntime().addShutdownHook(thread);
}

/**
* Initializes the HTTPMonitoringDataPublisher instance
*/
private static class HTTPMonitoringDataPublisherHolder {
private static final HTTPMonitoringDataPublisher INSTANCE = new HTTPMonitoringDataPublisher();
}

/**
* This returns the HTTPMonitoringDataPublisher singleton instance.
*
* @return The HTTPMonitoringDataPublisher instance
*/
public static HTTPMonitoringDataPublisher getInstance() {
return HTTPMonitoringDataPublisherHolder.INSTANCE;
}

private static InetAddress getLocalAddress() throws SocketException, UnknownHostException {
Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces();
while (interfaces.hasMoreElements()) {
Expand All @@ -108,17 +92,17 @@ private static InetAddress getLocalAddress() throws SocketException, UnknownHost
return InetAddress.getLocalHost();
}

private void init(HTTPMonitoringConfig httpMonitoringConfig) {
private void init(DasConfig dasConfig) {
if (logger.isInfoEnabled()) {
logger.info("Initializing HTTP Monitoring Data Publisher");
}

String type = httpMonitoringConfig.getType();
String receiverURL = httpMonitoringConfig.getReceiverURL();
String authURL = httpMonitoringConfig.getAuthURL();
String username = httpMonitoringConfig.getUsername();
String password = httpMonitoringConfig.getPassword();
String dataAgentConfigPath = httpMonitoringConfig.getDataAgentConfigPath();
String type = dasConfig.getType();
String receiverURL = dasConfig.getReceiverURL();
String authURL = dasConfig.getAuthURL();
String username = dasConfig.getUsername();
String password = dasConfig.getPassword();
String dataAgentConfigPath = dasConfig.getDataAgentConfigPath();

if (type == null) {
throw new IllegalArgumentException("Type cannot be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.wso2.msf4j.Request;
import org.wso2.msf4j.Response;
import org.wso2.msf4j.ServiceMethodInfo;
import org.wso2.msf4j.analytics.httpmonitoring.config.HTTPMonitoringConfigBuilder;
import org.wso2.msf4j.analytics.httpmonitoring.config.model.HTTPMonitoringConfig;

import java.lang.reflect.Method;
import java.util.Map;
Expand All @@ -40,15 +42,24 @@
immediate = true)
public class HTTPMonitoringInterceptor implements Interceptor {

public static final String REFERER = "Referer";
private static final Logger logger = LoggerFactory.getLogger(HTTPMonitoringInterceptor.class);

private Map<Method, Interceptor> map = new ConcurrentHashMap<>();
public static final String REFERER = "Referer";

private Map<Method, MethodInterceptor> map = new ConcurrentHashMap<>();

private final boolean enabled;

private final HTTPMonitoringDataPublisher httpMonitoringDataPublisher;

public HTTPMonitoringInterceptor() {
if (logger.isDebugEnabled()) {
logger.debug("Creating HTTP Monitoring Interceptor");
}
HTTPMonitoringConfig httpMonitoringConfig = HTTPMonitoringConfigBuilder.build();
enabled = httpMonitoringConfig.isEnabled();
httpMonitoringDataPublisher = enabled ? new HTTPMonitoringDataPublisher(httpMonitoringConfig.getDas()) :
null;
}

/**
Expand All @@ -67,33 +78,66 @@ private HTTPMonitored extractFinalAnnotation(Method method) {

@Override
public boolean preCall(Request request, Response responder, ServiceMethodInfo serviceMethodInfo) throws Exception {
if (!enabled) {
return true;
}
Method method = serviceMethodInfo.getMethod();
Interceptor interceptor = map.get(method);
if (interceptor == null) {
HTTPMonitored httpMon = this.extractFinalAnnotation(method);
MethodInterceptor methodInterceptor = map.get(method);
if (methodInterceptor == null || !methodInterceptor.annotationScanned) {
HTTPMonitored httpMon = extractFinalAnnotation(method);
Interceptor interceptor = null;
if (httpMon != null) {
interceptor = new HTTPInterceptor(httpMon.tracing());
map.put(method, interceptor);
}
}

if (interceptor != null) {
interceptor.preCall(request, responder, serviceMethodInfo);
methodInterceptor = new MethodInterceptor(true, interceptor);
map.put(method, methodInterceptor);
}

return true;
return methodInterceptor.preCall(request, responder, serviceMethodInfo);
}

@Override
public void postCall(Request request, int status, ServiceMethodInfo serviceMethodInfo) throws Exception {
if (!enabled) {
return;
}
Method method = serviceMethodInfo.getMethod();
Interceptor interceptor = map.get(method);
if (interceptor != null) {
interceptor.postCall(request, status, serviceMethodInfo);
MethodInterceptor methodInterceptor = map.get(method);
if (methodInterceptor != null) {
methodInterceptor.postCall(request, status, serviceMethodInfo);
}
}

private static class HTTPInterceptor implements Interceptor {
private static class MethodInterceptor implements Interceptor {

private final boolean annotationScanned;

private final Interceptor interceptor;

MethodInterceptor(boolean annotationScanned, Interceptor interceptor) {
this.annotationScanned = annotationScanned;
this.interceptor = interceptor;
}

@Override
public boolean preCall(Request request, Response responder, ServiceMethodInfo serviceMethodInfo)
throws Exception {
if (interceptor != null) {
return interceptor.preCall(request, responder, serviceMethodInfo);
}
return true;
}

@Override
public void postCall(Request request, int status, ServiceMethodInfo serviceMethodInfo) throws Exception {
if (interceptor != null) {
interceptor.postCall(request, status, serviceMethodInfo);
}
}
}

private class HTTPInterceptor implements Interceptor {

private static final String DEFAULT_TRACE_ID = "DEFAULT";

Expand All @@ -112,10 +156,7 @@ private static class HTTPInterceptor implements Interceptor {

private boolean tracing;

private final HTTPMonitoringDataPublisher httpMonitoringDataPublisher;

private HTTPInterceptor(boolean tracing) {
httpMonitoringDataPublisher = HTTPMonitoringDataPublisher.getInstance();
this.tracing = tracing;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.msf4j.analytics.httpmonitoring;
package org.wso2.msf4j.analytics.httpmonitoring.config;

import org.wso2.carbon.metrics.core.utils.Utils;
import org.wso2.msf4j.analytics.httpmonitoring.config.model.HTTPMonitoringConfig;
import org.yaml.snakeyaml.Yaml;

import java.util.Optional;
Expand All @@ -26,7 +27,7 @@
public final class HTTPMonitoringConfigBuilder {

public static HTTPMonitoringConfig build() {
Optional<String> metricsConfigFileContent = Utils.readFile("httpmonitoring.conf", "httpmonitoring.yml");
Optional<String> metricsConfigFileContent = Utils.readFile("http-monitoring.conf", "http-monitoring.yml");
if (metricsConfigFileContent.isPresent()) {
try {
Yaml yaml = new Yaml();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.msf4j.analytics.httpmonitoring;
package org.wso2.msf4j.analytics.httpmonitoring.config.model;

/**
* Configuration for HTTP Monitoring
* Configuration for connecting with Data Analytics Server (DAS)
*/
public class HTTPMonitoringConfig {
public class DasConfig {

private String type = "thrift";

Expand Down Expand Up @@ -79,4 +79,5 @@ public String getDataAgentConfigPath() {
public void setDataAgentConfigPath(String dataAgentConfigPath) {
this.dataAgentConfigPath = dataAgentConfigPath;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* Copyright (c) 2016, WSO2 Inc. (http://wso2.com) All Rights Reserved.
*
* 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.wso2.msf4j.analytics.httpmonitoring.config.model;

/**
* Configuration for HTTP Monitoring
*/
public class HTTPMonitoringConfig {

private boolean enabled;

private DasConfig das = new DasConfig();

public boolean isEnabled() {
return enabled;
}

public void setEnabled(boolean enabled) {
this.enabled = enabled;
}

public DasConfig getDas() {
return das;
}

public void setDas(DasConfig das) {
this.das = das;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,52 @@ public MetricsInterceptor() {
}
}

private Timed getTimedAnnotation(Method method) {
Timed annotation = method.getAnnotation(Timed.class);
if (annotation == null) {
annotation = method.getDeclaringClass().getAnnotation(Timed.class);
}
return annotation;
}

private Metered getMeteredAnnotation(Method method) {
Metered annotation = method.getAnnotation(Metered.class);
if (annotation == null) {
annotation = method.getDeclaringClass().getAnnotation(Metered.class);
}
return annotation;
}

private Counted getCountedAnnotation(Method method) {
Counted annotation = method.getAnnotation(Counted.class);
if (annotation == null) {
annotation = method.getDeclaringClass().getAnnotation(Counted.class);
}
return annotation;
}

@Override
public boolean preCall(Request request, Response responder, ServiceMethodInfo serviceMethodInfo) throws Exception {
Method method = serviceMethodInfo.getMethod();
MethodInterceptors methodInterceptors = map.get(method);
if (methodInterceptors == null || !methodInterceptors.isAnnotationScanned()) {
if (methodInterceptors == null || !methodInterceptors.annotationScanned) {
List<Interceptor> interceptors = new CopyOnWriteArrayList<>();
if (method.isAnnotationPresent(Timed.class)) {
Timed annotation = method.getAnnotation(Timed.class);
Timer timer = MetricAnnotation.timer(Metrics.getInstance().getMetricService(), annotation, method);
Timed timed = getTimedAnnotation(method);
if (timed != null) {
Timer timer = MetricAnnotation.timer(Metrics.getInstance().getMetricService(), timed, method);
Interceptor interceptor = new TimerInterceptor(timer);
interceptors.add(interceptor);
}

if (method.isAnnotationPresent(Metered.class)) {
Metered annotation = method.getAnnotation(Metered.class);
Meter meter = MetricAnnotation.meter(Metrics.getInstance().getMetricService(), annotation, method);
Metered metered = getMeteredAnnotation(method);
if (metered != null) {
Meter meter = MetricAnnotation.meter(Metrics.getInstance().getMetricService(), metered, method);
Interceptor interceptor = new MeterInterceptor(meter);
interceptors.add(interceptor);
}

if (method.isAnnotationPresent(Counted.class)) {
Counted annotation = method.getAnnotation(Counted.class);
Counter counter = MetricAnnotation.counter(Metrics.getInstance().getMetricService(), annotation,
method);
Interceptor interceptor = new CounterInterceptor(counter, annotation.monotonic());
Counted counted = getCountedAnnotation(method);
if (counted != null) {
Counter counter = MetricAnnotation.counter(Metrics.getInstance().getMetricService(), counted, method);
Interceptor interceptor = new CounterInterceptor(counter, counted.monotonic());
interceptors.add(interceptor);
}

Expand Down Expand Up @@ -112,10 +133,6 @@ private static class MethodInterceptors implements Interceptor {
}
}

private boolean isAnnotationScanned() {
return annotationScanned;
}

@Override
public boolean preCall(Request request, Response responder, ServiceMethodInfo serviceMethodInfo)
throws Exception {
Expand Down
Loading

0 comments on commit 313d8cc

Please sign in to comment.