Skip to content

Commit

Permalink
Using Metrics 2.0.0. Updated HTTP Monitoring to use a YAML config. Ke…
Browse files Browse the repository at this point in the history
…eping separate samples for Metrics and HTTP Monitoring
  • Loading branch information
chrishantha committed Jul 5, 2016
1 parent 5c79ae6 commit 16464d5
Show file tree
Hide file tree
Showing 41 changed files with 573 additions and 820 deletions.
Binary file removed analytics/das-setup/capps/metrics_capp.car
Binary file not shown.
Binary file not shown.
31 changes: 8 additions & 23 deletions analytics/msf4j-analytics/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,19 @@
</dependency>
<dependency>
<groupId>org.wso2.carbon.metrics</groupId>
<artifactId>org.wso2.carbon.metrics.manager</artifactId>
<artifactId>org.wso2.carbon.metrics.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.metrics</groupId>
<artifactId>org.wso2.carbon.metrics.impl</artifactId>
<exclusions>
<exclusion>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</exclusion>
<exclusion>
<groupId>commons-pool</groupId>
<artifactId>commons-pool</artifactId>
</exclusion>
<exclusion>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
</exclusion>
</exclusions>
<artifactId>org.wso2.carbon.metrics.jdbc.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.metrics</groupId>
<artifactId>org.wso2.carbon.metrics.annotation</artifactId>
<artifactId>org.wso2.carbon.metrics.das.core</artifactId>
</dependency>
<dependency>
<groupId>org.wso2.carbon.metrics</groupId>
<artifactId>org.wso2.carbon.metrics.config</artifactId>
</dependency>
</dependencies>

Expand Down Expand Up @@ -237,12 +227,7 @@
org.osgi.framework.*;version="${osgi.framework.import.version.range}",
org.osgi.util.tracker; version="${osgi.service.tracker.import.version.range}",
org.wso2.carbon.kernel.utils.*;version="${carbon.kernel.version.range}",
org.wso2.carbon.metrics.annotation.*;version="${carbon.metrics.version.range}",
org.wso2.carbon.metrics.common.*;version="${carbon.metrics.version.range}",
org.wso2.carbon.metrics.manager.*;version="${carbon.metrics.version.range}",
org.wso2.carbon.metrics.impl.*;version="${carbon.metrics.version.range}",
org.wso2.carbon.databridge.agent.*;version="${carbon.analytics.common.version.range}",
org.wso2.carbon.databridge.commons.*;version="${carbon.analytics.common.version.range}",
org.wso2.carbon.metrics.core.*;version="${carbon.metrics.version.range}",
org.wso2.carbon.kernel.startupresolver.*;version="${carbon.kernel.version.range}",
org.wso2.msf4j.*,
org.wso2.msf4j.util.*,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* 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;

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

private String type = "thrift";

private String receiverURL = "tcp://localhost:7611";

private String authURL;

private String username = "admin";

private String password = "admin";

private String dataAgentConfigPath = null;

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}

public String getReceiverURL() {
return receiverURL;
}

public void setReceiverURL(String receiverURL) {
this.receiverURL = receiverURL;
}

public String getAuthURL() {
return authURL;
}

public void setAuthURL(String authURL) {
this.authURL = authURL;
}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getDataAgentConfigPath() {
return dataAgentConfigPath;
}

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;

import org.wso2.carbon.metrics.core.utils.Utils;
import org.yaml.snakeyaml.Yaml;

import java.util.Optional;

/**
* Build {@link HTTPMonitoringConfig} from the YAML file
*/
public final class HTTPMonitoringConfigBuilder {

public static HTTPMonitoringConfig build() {
Optional<String> metricsConfigFileContent = Utils.readFile("httpmonitoring.conf", "httpmonitoring.yml");
if (metricsConfigFileContent.isPresent()) {
try {
Yaml yaml = new Yaml();
return yaml.loadAs(metricsConfigFileContent.get(), HTTPMonitoringConfig.class);
} catch (RuntimeException e) {
throw new RuntimeException("Failed to populate HTTP Monitoring Configuration", e);
}
} else {
return new HTTPMonitoringConfig();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,6 @@ public final class HTTPMonitoringDataPublisher {

private static final Logger logger = LoggerFactory.getLogger(HTTPMonitoringDataPublisher.class);

private static final String HTTP_MONITORING_DAS_TYPE = "HTTP_MONITORING_DAS_TYPE";
private static final String HTTP_MONITORING_DAS_RECEIVER_URL = "HTTP_MONITORING_DAS_RECEIVERURL";
private static final String HTTP_MONITORING_DAS_AUTH_URL = "HTTP_MONITORING_DAS_AUTHURL";
private static final String HTTP_MONITORING_DAS_USERNAME = "HTTP_MONITORING_DAS_USERNAME";
private static final String HTTP_MONITORING_DAS_PASSWORD = "HTTP_MONITORING_DAS_PASSWORD";
private static final String HTTP_MONITORING_DAS_DATAAGENTCONFIGPATH = "HTTP_MONITORING_DAS_DATAAGENTCONFIGPATH";

private static final String HTTP_MONITORING_STREAM = "org.wso2.msf4j.analytics.httpmonitoring";
private static final String VERSION = "1.0.0";
private static final String HTTP_MONITORING_STREAM_ID;
Expand All @@ -59,8 +52,8 @@ public final class HTTPMonitoringDataPublisher {

private static final String MICROSERVICE = "Microservice";

private static DataPublisher dataPublisher;
private static Map<String, String> arbitraryAttributes;
private DataPublisher dataPublisher;
private Map<String, String> arbitraryAttributes;

static {
HTTP_MONITORING_STREAM_ID = DataBridgeCommonsUtils.generateStreamId(HTTP_MONITORING_STREAM, VERSION);
Expand All @@ -74,6 +67,27 @@ public final class HTTPMonitoringDataPublisher {
}

private HTTPMonitoringDataPublisher() {
HTTPMonitoringConfig httpMonitoringConfig = HTTPMonitoringConfigBuilder.build();
init(httpMonitoringConfig);
// Destroy data publisher at shutdown
Thread thread = new Thread(() -> 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 {
Expand All @@ -94,31 +108,17 @@ private static InetAddress getLocalAddress() throws SocketException, UnknownHost
return InetAddress.getLocalHost();
}

static synchronized void init() {
// This method is synchronized to make sure that Data Publisher is created only once.
if (dataPublisher != null) {
if (logger.isDebugEnabled()) {
logger.debug("HTTP Monitoring Data Publisher is already initialized");
}
return;
}
private void init(HTTPMonitoringConfig httpMonitoringConfig) {
if (logger.isInfoEnabled()) {
logger.info("Initializing HTTP Monitoring Data Publisher");
}

String type = "thrift";
String receiverURL = "tcp://localhost:7611";
String authURL;
String username = "admin";
String password = "admin";
String dataAgentConfigPath;

type = SystemVariableUtil.getValue(HTTP_MONITORING_DAS_TYPE, type);
receiverURL = SystemVariableUtil.getValue(HTTP_MONITORING_DAS_RECEIVER_URL, receiverURL);
authURL = SystemVariableUtil.getValue(HTTP_MONITORING_DAS_AUTH_URL, null);
username = SystemVariableUtil.getValue(HTTP_MONITORING_DAS_USERNAME, username);
password = SystemVariableUtil.getValue(HTTP_MONITORING_DAS_PASSWORD, password);
dataAgentConfigPath = SystemVariableUtil.getValue(HTTP_MONITORING_DAS_DATAAGENTCONFIGPATH, null);
String type = httpMonitoringConfig.getType();
String receiverURL = httpMonitoringConfig.getReceiverURL();
String authURL = httpMonitoringConfig.getAuthURL();
String username = httpMonitoringConfig.getUsername();
String password = httpMonitoringConfig.getPassword();
String dataAgentConfigPath = httpMonitoringConfig.getDataAgentConfigPath();

if (type == null) {
throw new IllegalArgumentException("Type cannot be null");
Expand All @@ -145,7 +145,7 @@ static synchronized void init() {
}
}

static synchronized void destroy() {
private void destroy() {
if (dataPublisher != null) {
try {
dataPublisher.shutdownWithAgent();
Expand All @@ -157,7 +157,7 @@ static synchronized void destroy() {
}
}

static void publishEvent(HTTPMonitoringEvent httpMonitoringEvent) {
public void publishEvent(HTTPMonitoringEvent httpMonitoringEvent) {
Object[] meta = new Object[4];
meta[0] = httpMonitoringEvent.getTimestamp();
meta[1] = SERVER_HOST_ADDRESS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ public HTTPMonitoringInterceptor() {
}
}

public HTTPMonitoringInterceptor init() {
HTTPMonitoringDataPublisher.init();
// Destroy the publisher at shutdown
Runtime.getRuntime().addShutdownHook(new ShutdownHook());
return this;
}

/**
* Returns the final annotation that is application to the given method. For example,
* the {@link HTTPMonitored} annotation can be mentioned in class level, and also in
Expand Down Expand Up @@ -100,13 +93,6 @@ public void postCall(Request request, int status, ServiceMethodInfo serviceMetho
}
}

private static class ShutdownHook extends Thread {
@Override
public void run() {
HTTPMonitoringDataPublisher.destroy();
}
}

private static class HTTPInterceptor implements Interceptor {

private static final String DEFAULT_TRACE_ID = "DEFAULT";
Expand All @@ -126,7 +112,10 @@ 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 Expand Up @@ -200,7 +189,7 @@ public void postCall(Request request, int status, ServiceMethodInfo serviceMetho
httpMonitoringEvent.setResponseTime(
TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - httpMonitoringEvent.getStartNanoTime()));
httpMonitoringEvent.setResponseHttpStatusCode(status);
HTTPMonitoringDataPublisher.publishEvent(httpMonitoringEvent);
httpMonitoringDataPublisher.publishEvent(httpMonitoringEvent);
}
}
}
Loading

0 comments on commit 16464d5

Please sign in to comment.