Skip to content

Commit

Permalink
updated extension with handling null connection and time stamp logging
Browse files Browse the repository at this point in the history
  • Loading branch information
bhuvnesh17 committed Mar 23, 2018
1 parent 1444332 commit ad7f223
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 11 deletions.
16 changes: 16 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@

### Version 2.4.2

* Added timestamp logging
* Added checks for Null connection


## Version 2.4.1

* Updated Licenses

## Version 2.4.0

* Revamped complete extension
* Adds support to add your own queries
* Adds support to query tables
4 changes: 0 additions & 4 deletions CHANGES.md

This file was deleted.

2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

<groupId>com.appdynamics.extensions</groupId>
<artifactId>oracledb-monitoring-extension</artifactId>
<version>2.4.1</version>
<version>2.4.2</version>
<packaging>jar</packaging>
<name>oracle-monitoring-extension</name>
<url>http://maven.apache.org</url>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@


import com.google.common.base.Strings;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.Map;
Expand All @@ -20,6 +21,7 @@ public class JDBCConnectionAdapter {

private final String connUrl;
private final Map<String, String> connectionProperties;
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(JDBCConnectionAdapter.class);


private JDBCConnectionAdapter(String connStr, Map<String, String> connectionProperties) {
Expand All @@ -44,7 +46,12 @@ Connection open(String driver) throws SQLException, ClassNotFoundException {
properties.put(key, connectionProperties.get(key));
}
}
logger.debug("Passed all checks for properties and attempting to connect to: "+ connUrl);
long timestamp1 = System.currentTimeMillis();

connection = DriverManager.getConnection(connUrl, properties.getProperty("user"), properties.getProperty("password"));
long timestamp2 = System.currentTimeMillis();
logger.debug("Connection received in JDBC ConnectionAdapter in :"+ (timestamp2-timestamp1)+ " ms");
return connection;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.appdynamics.extensions.TaskInputArgs;
import com.appdynamics.extensions.TasksExecutionServiceProvider;
import com.appdynamics.extensions.crypto.CryptoUtil;
import com.appdynamics.extensions.util.AssertUtils;
import com.google.common.base.Strings;
import com.google.common.collect.Maps;
import com.singularity.ee.agent.systemagent.api.exception.TaskExecutionException;
Expand Down Expand Up @@ -77,6 +78,11 @@ protected int getTaskCount() {

private OracledbMonitorTask createTask(Map server, TasksExecutionServiceProvider serviceProvider) throws IOException {
String connUrl = createConnectionUrl(server);

AssertUtils.assertNotNull(serverName(server), "The 'displayName' field under the 'dbServers' section in config.yml is not initialised");
AssertUtils.assertNotNull(createConnectionUrl(server), "The 'connectionUrl' field under the 'dbServers' section in config.yml is not initialised");
AssertUtils.assertNotNull(driverName(server), "The 'driver' field under the 'dbServers' section in config.yml is not initialised");

Map<String, String> connectionProperties = getConnectionProperties(server);
JDBCConnectionAdapter jdbcAdapter = JDBCConnectionAdapter.create(connUrl, connectionProperties);

Expand All @@ -89,6 +95,15 @@ private OracledbMonitorTask createTask(Map server, TasksExecutionServiceProvider
.server(server).build();

}
private String serverName(Map server) {
String name = Util.convertToString(server.get("displayName"), "");
return name;
}

private String driverName(Map server) {
String name = Util.convertToString(server.get("driver"), "");
return name;
}

private String createConnectionUrl(Map server) {
String url = Util.convertToString(server.get("connectionUrl"), "");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,26 @@ public void run() {
Connection connection = null;
if (queries != null && !queries.isEmpty()) {
try {
long timestamp1 = System.currentTimeMillis();
connection = getConnection();
for (Map query : queries) {
try {
executeQuery(connection, query);
} catch (SQLException e) {
logger.error("Error during executing query.");
long timestamp2 = System.currentTimeMillis();
logger.debug("Time taken to get Connection: " + (timestamp2 - timestamp1));
String dbServerDisplayName = (String) server.get("displayName");
if(connection != null) {
logger.debug(" Connection successful for server: " + dbServerDisplayName);

for (Map query : queries) {
try {
executeQuery(connection, query);
} catch (SQLException e) {
logger.error("Error during executing query.");
}
}
} else {

logger.debug("Null Connection returned for server: " + dbServerDisplayName);
}

} catch (SQLException e) {
logger.error("Error Opening connection", e);
status = false;
Expand All @@ -56,7 +68,9 @@ public void run() {
status = false;
} finally {
try {
closeConnection(connection);
if (connection != null) {
closeConnection(connection);
}
} catch (Exception e) {
logger.error("Issue closing the connection", e);
}
Expand Down Expand Up @@ -112,7 +126,11 @@ private ResultSet getResultSet(Map query, Statement statement, ResultSet resultS
String queryStmt = (String) query.get("queryStmt");
queryStmt = substitute(queryStmt);

long timestamp1 = System.currentTimeMillis();
resultSet = jdbcAdapter.queryDatabase(queryStmt, statement);
long timestamp2 = System.currentTimeMillis();

logger.debug("Queried the database in :"+ (timestamp2-timestamp1)+ " ms for query: \n " + queryStmt);
return resultSet;
}

Expand Down

0 comments on commit ad7f223

Please sign in to comment.