Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Opt-in support to collect SQL queries. #283

Merged
merged 5 commits into from
Aug 4, 2021
Merged
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ public class TracingStatement {

private static final Log logger = LogFactory.getLog(TracingStatement.class);

private static final boolean COLLECT_SQL_ENV =
Boolean.parseBoolean(System.getenv("AWS_XRAY_COLLECT_SQL_QUERIES"));
private static final boolean COLLECT_SQL_PROP =
Boolean.parseBoolean(System.getProperty("com.amazonaws.xray.collectSqlQueries"));

/**
* Call {@code statement = TracingStatement.decorateStatement(statement)} to decorate your {@link Statement}
* in order to have the queries recorded with an X-Ray Subsegment. Do not use the method on {@link PreparedStatement}
Expand Down Expand Up @@ -87,8 +92,6 @@ private static class TracingStatementHandler implements InvocationHandler {

private final Statement delegate;

// TODO: https://github.com/aws/aws-xray-sdk-java/issues/28
@SuppressWarnings("UnusedVariable")
private final String sql;

TracingStatementHandler(Statement statement, String sql) {
Expand All @@ -106,7 +109,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
}

logger.debug(
String.format("Invoking statement execution with X-Ray tracing. Tracing active: %s", subsegment != null));
String.format("Invoking statement execution with X-Ray tracing. Tracing active: %s", subsegment != null));
try {
// execute the query "wrapped" in a XRay Subsegment
return method.invoke(delegate, args);
Expand Down Expand Up @@ -143,11 +146,15 @@ private boolean isExecution(Method method) {

private Subsegment createSubsegment() {
try {
return SqlSubsegments.forQuery(delegate.getConnection(), null);
return SqlSubsegments.forQuery(delegate.getConnection(), collectSqlQueries() ? sql : null);
} catch (SQLException exception) {
logger.warn("Failed to create X-Ray subsegment for the statement execution.", exception);
return null;
}
}

private boolean collectSqlQueries() {
return COLLECT_SQL_ENV || COLLECT_SQL_PROP;
}
}
}