Skip to content
This repository has been archived by the owner on Feb 20, 2023. It is now read-only.

Commit

Permalink
Track jdbc metrics independently from profiler and request monitor
Browse files Browse the repository at this point in the history
  • Loading branch information
Felix Barnsteiner committed Apr 26, 2016
1 parent c512d3c commit 5aa7095
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private static void initIncludesAndExcludes() {
* @param className The name of the class. For example java/lang/String
* @return <code>true</code>, if the class should be instrumented, <code>false</code> otherwise
*/
private boolean isIncluded(String className) {
public static boolean isIncluded(String className) {
for (String exclude : excludeContaining) {
if (className.contains(exclude)) {
return false;
Expand All @@ -88,7 +88,7 @@ private boolean isIncluded(String className) {
return false;
}

private boolean hasMoreSpecificExclude(String className, String include) {
private static boolean hasMoreSpecificExclude(String className, String include) {
for (String exclude : excludes) {
if (exclude.length() > include.length() && exclude.startsWith(include) && className.startsWith(exclude)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ private SignatureUtils() {
}

public static String getSignature(String fullClassName, String methodName, String nameFromAnnotation, boolean absolute) {
String result = StringUtils.isEmpty(nameFromAnnotation) ? methodName : nameFromAnnotation;

if (!absolute) {
result = ClassUtils.shorten(fullClassName) + "#" + result;
String name = StringUtils.isEmpty(nameFromAnnotation) ? methodName : nameFromAnnotation;
if (absolute) {
return name;
}
return result;
return getSignature(fullClassName, name);
}

public static String getSignature(String fullClassName, String methodName) {
return ClassUtils.shorten(fullClassName) + "#" + methodName;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,26 @@

import com.p6spy.engine.logging.Category;
import com.p6spy.engine.spy.appender.P6Logger;
import org.stagemonitor.core.CorePlugin;
import org.stagemonitor.core.configuration.Configuration;
import org.stagemonitor.core.instrument.StagemonitorClassNameMatcher;
import org.stagemonitor.core.metrics.aspects.SignatureUtils;
import org.stagemonitor.core.metrics.metrics2.Metric2Registry;
import org.stagemonitor.core.util.StringUtils;
import org.stagemonitor.jdbc.JdbcPlugin;
import org.stagemonitor.requestmonitor.RequestMonitor;
import org.stagemonitor.requestmonitor.RequestTrace;
import org.stagemonitor.requestmonitor.profiler.CallStackElement;
import org.stagemonitor.requestmonitor.profiler.Profiler;

public class StagemonitorP6Logger implements P6Logger {

private final JdbcPlugin jdbcPlugin;
private final CorePlugin corePlugin;
private final Metric2Registry metricRegistry;

public StagemonitorP6Logger(Configuration configuration, Metric2Registry metricRegistry) {
this.jdbcPlugin = configuration.getConfig(JdbcPlugin.class);
this.corePlugin = configuration.getConfig(CorePlugin.class);
this.metricRegistry = metricRegistry;
}

Expand All @@ -31,26 +35,40 @@ public void logSQL(int connectionId, String now, long elapsed, Category category
if (StringUtils.isEmpty(sql)) {
sql = prepared;
}
trackDbMetrics(elapsed);
RequestTrace request = RequestMonitor.getRequest();
if (request != null) {
request.dbCallCompleted(elapsed);
trackDbMetrics(elapsed);
addSqlToCallStack(elapsed, prepared, sql);
}
}
}

private void trackDbMetrics(long elapsed) {
CallStackElement currentCall = Profiler.getMethodCallParent();
metricRegistry.timer(name("jdbc_statement").tag("signature", "All").build()).update(elapsed, TimeUnit.MILLISECONDS);
if (currentCall != null) {
String shortSignature = currentCall.getShortSignature();
if (shortSignature != null) {
metricRegistry
.timer(name("jdbc_statement").tag("signature", shortSignature).build())
.update(elapsed, TimeUnit.MILLISECONDS);
String daoMethodSignature = getDaoMethodSignature();
if (daoMethodSignature != null) {
metricRegistry
.timer(name("jdbc_statement").tag("signature", daoMethodSignature).build())
.update(elapsed, TimeUnit.MILLISECONDS);
}
}

/**
* Returns the signature of the method (inside the monitored codebase) which triggered the execution of the SQL statement.
*/
private String getDaoMethodSignature() {
if (corePlugin.getIncludePackages().isEmpty()) {
return null;
}
String daoMethodSignature = null;
for (StackTraceElement stackTraceElement : Thread.currentThread().getStackTrace()) {
if (StagemonitorClassNameMatcher.isIncluded(stackTraceElement.getClassName())) {
daoMethodSignature = SignatureUtils.getSignature(stackTraceElement.getClassName(), stackTraceElement.getMethodName());
break;
}
}
return daoMethodSignature;
}

private void addSqlToCallStack(long elapsed, String prepared, String sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.junit.Assert.assertTrue;
import static org.stagemonitor.core.metrics.metrics2.MetricName.name;

import java.sql.SQLException;
import java.util.Map;

import javax.sql.DataSource;
Expand All @@ -24,7 +25,6 @@
import org.stagemonitor.requestmonitor.RequestMonitorPlugin;
import org.stagemonitor.requestmonitor.RequestTrace;
import org.stagemonitor.requestmonitor.profiler.CallStackElement;
import org.stagemonitor.requestmonitor.profiler.Profiler;

public class ConnectionMonitoringTransformerTest {

Expand Down Expand Up @@ -87,47 +87,60 @@ public Object execute() throws Exception {
@Test
public void testRecordSqlPreparedStatement() throws Exception {
final RequestMonitor.RequestInformation<RequestTrace> requestInformation = requestMonitor
.monitor(new MonitoredMethodRequest("testRecordSql()", new MonitoredMethodRequest.MethodExecution() {
.monitor(new MonitoredMethodRequest("testRecordSqlPreparedStatement", new MonitoredMethodRequest.MethodExecution() {
@Override
public Object execute() throws Exception {
Profiler.start("public void org.stagemonitor.jdbc.ConnectionMonitoringTransformerTest.testRecordSql()");
dataSource.getConnection().prepareStatement("SELECT * from STAGEMONITOR").execute();
Profiler.stop();
executePreparedStatement();
return null;
}
}));
final Map<MetricName, Timer> timers = Stagemonitor.getMetric2Registry().getTimers();
assertTrue(timers.keySet().toString(), timers.size() > 1);
assertNotNull(timers.keySet().toString(), timers.get(name("jdbc_statement").tag("signature", "All").build()));
assertNotNull(timers.keySet().toString(), timers.get(name("jdbc_statement").tag("signature", "ConnectionMonitoringTransformerTest#testRecordSql").build()));
assertNotNull(timers.keySet().toString(), timers.get(name("jdbc_statement").tag("signature", "ConnectionMonitoringTransformerTest#executePreparedStatement").build()));
final CallStackElement callStack = requestInformation.getRequestTrace().getCallStack();
assertEquals("testRecordSql()", callStack.getSignature());
assertEquals("public void org.stagemonitor.jdbc.ConnectionMonitoringTransformerTest.testRecordSql()",
callStack.getChildren().get(0).getSignature());
assertEquals("SELECT * from STAGEMONITOR ", callStack.getChildren().get(0).getChildren().get(0).getSignature());
assertEquals("testRecordSqlPreparedStatement", callStack.getSignature());
assertEquals("void org.stagemonitor.jdbc.ConnectionMonitoringTransformerTest.executePreparedStatement()",
callStack.getChildren().get(0).getChildren().get(0).getSignature());
assertEquals("SELECT * from STAGEMONITOR ", callStack.getChildren().get(0).getChildren().get(0).getChildren().get(0).getSignature());
}

private void executePreparedStatement() throws SQLException {
dataSource.getConnection().prepareStatement("SELECT * from STAGEMONITOR").execute();
}

@Test
public void testRecordSqlStatement() throws Exception {
final RequestMonitor.RequestInformation<RequestTrace> requestInformation = requestMonitor
.monitor(new MonitoredMethodRequest("testRecordSql()", new MonitoredMethodRequest.MethodExecution() {
.monitor(new MonitoredMethodRequest("testRecordSqlStatement", new MonitoredMethodRequest.MethodExecution() {
@Override
public Object execute() throws Exception {
Profiler.start("public void org.stagemonitor.jdbc.ConnectionMonitoringTransformerTest.testRecordSql()");
dataSource.getConnection().createStatement().execute("SELECT * from STAGEMONITOR");
Profiler.stop();
executeStatement();
return null;
}
}));
final Map<MetricName, Timer> timers = Stagemonitor.getMetric2Registry().getTimers();
assertTrue(timers.keySet().toString(), timers.size() > 1);
assertNotNull(timers.keySet().toString(), timers.get(name("jdbc_statement").tag("signature", "All").build()));
assertNotNull(timers.keySet().toString(), timers.get(name("jdbc_statement").tag("signature", "ConnectionMonitoringTransformerTest#testRecordSql").build()));
assertNotNull(timers.keySet().toString(), timers.get(name("jdbc_statement").tag("signature", "ConnectionMonitoringTransformerTest#executeStatement").build()));
final CallStackElement callStack = requestInformation.getRequestTrace().getCallStack();
assertEquals("testRecordSql()", callStack.getSignature());
assertEquals("public void org.stagemonitor.jdbc.ConnectionMonitoringTransformerTest.testRecordSql()",
callStack.getChildren().get(0).getSignature());
assertEquals("SELECT * from STAGEMONITOR ", callStack.getChildren().get(0).getChildren().get(0).getSignature());
assertEquals("testRecordSqlStatement", callStack.getSignature());
assertEquals("void org.stagemonitor.jdbc.ConnectionMonitoringTransformerTest.executeStatement()",
callStack.getChildren().get(0).getChildren().get(0).getSignature());
assertEquals("SELECT * from STAGEMONITOR ", callStack.getChildren().get(0).getChildren().get(0).getChildren().get(0).getSignature());
}

@Test
public void testTrackDBMetricsIndependentOfProfiler() throws Exception {
executeStatement();
final Map<MetricName, Timer> timers = Stagemonitor.getMetric2Registry().getTimers();
assertTrue(timers.keySet().toString(), timers.size() > 1);
assertNotNull(timers.keySet().toString(), timers.get(name("jdbc_statement").tag("signature", "All").build()));
assertNotNull(timers.keySet().toString(), timers.get(name("jdbc_statement").tag("signature", "ConnectionMonitoringTransformerTest#executeStatement").build()));
}

private void executeStatement() throws SQLException {
dataSource.getConnection().createStatement().execute("SELECT * from STAGEMONITOR");
}

}
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
stagemonitor.instrument.include=none
stagemonitor.instrument.include=org.stagemonitor.jdbc.ConnectionMonitoringTransformerTest
stagemonitor.instrument.debug=true
stagemonitor.requestmonitor.elasticsearch.onlyLogElasticsearchRequestTraceReports=true

0 comments on commit 5aa7095

Please sign in to comment.