3

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?

kibowki
  • 4,206
  • 16
  • 48
  • 74
  • 1
    I added XRay support to Apache Camel recently and added some notes on how to achieve the same result as the MySQL statement interceptor in the [PR ticket](https://github.com/apache/camel/pull/2072#issuecomment-343239257) without the need of the Tomcat interceptor – Roman Vottner Apr 04 '18 at 18:18

1 Answers1

2

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.

Rohit Banga
  • 18,458
  • 31
  • 113
  • 191