I'm running some SQL queries in an AWS Lambda, and was hoping to utilize AWS-XRay's tracing capabilities to get some more detailed information on these calls.
This documentation shows examples of configuration with Spring and Tomcat, but neither of which makes sense to use in my obviously serverless and supposed-to-be lightweight Lambda. Here's how I establish my connections currently:
public Connection getDatabaseConnection(String jdbcUrl, String dbUser, String dbPassword) throws SQLException
{
return DriverManager.getConnection(jdbcUrl, dbUser, dbPassword);
}
try (Connection connection = getDatabaseConnection(getJdbcUrl(), getDbUser(), getDbPassword()))
{
try(ResultSet results = connection.createStatement().executeQuery("SELECT stuff FROM whatever LIMIT 1))
{
return (results.getLong(1));
}
}
Is there any way to utilize AWS-XRay SQL tracing in my use case?
I think one approach would be to use a MySQL statement interceptor: https://github.com/spullara/mysql-connector-java/blob/master/src/main/java/com/mysql/jdbc/StatementInterceptor.java
You can use AWSXRay.beginSubsegment() in preProcess() method and then AWSXRay.endSubsegment() in the postProcess().
Would be a nice addition to AWS X-Ray SDK for Java which is in open source. in case you get it working.
For reference of spring based implementation for X-Ray: DataSource based intereceptor
You can use the statementinterceptors property as part of the connection URL to intercept the statement as documented here: https://dev.mysql.com/doc/connector-j/5.1/en/connector-j-reference-configuration-properties.html
Update My teammate pointed out, a newer version of StatementInterceptor is available: https://github.com/spullara/mysql-connector-java/blob/master/src/main/java/com/mysql/jdbc/StatementInterceptorV2.java You may want to use that.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With