demetrio812
demetrio812

Reputation: 289

Tracing SQL Queries with X-Ray and Spring Boot 2

The current X-Ray SQL tracing interceptor uses Tomcat JDBC Pool but Spring Boot 2 uses HikariCP as default pool, is it possible to configure the jdbc tracing in HikariCP instead?

Here (https://forums.aws.amazon.com/thread.jspa?threadID=254847) they suggest to use both Datasources:

DataSource dataSource = new org.apache.tomcat.jdbc.pool.DataSource();
HikariDataSource hikariDataSource = new HikariDataSource();
... // data source configuration
dataSource.setJdbcInterceptors("com.amazonaws.xray.sql.postgres.TracingInterceptor;");
hikariDataSource.setDataSource(dataSource);

But if I have the HikariCP library in the classpath spring will configure that as datasource.

I've tried with a DatasourceBuilder and also forcing the type using the parameter spring.datasource.type

Any hint?

Upvotes: 1

Views: 1280

Answers (3)

arunjacob
arunjacob

Reputation: 1

Resolved the same using TracingDataSource, while still using HikariCP as the connection pool

Reference https://github.com/aws/aws-xray-sdk-java/issues/88#issuecomment-570328275

Code: (Note, I am using AWS Secrets Manager JDBC Library aws-secretsmanager-jdbc to connect to the database using secrets stored in AWS Secrets Manager)

import com.amazonaws.xray.sql.TracingDataSource;

...
...
@Bean
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource dataSource() {

        return TracingDataSource
                .decorate(DataSourceBuilder.create()
                        .driverClassName("com.amazonaws.secretsmanager.sql.AWSSecretsManagerPostgreSQLDriver")
                        .url("jdbc-secretsmanager:postgresql://" + System.getenv("PGHOST") + ":"
                                + System.getenv("PGPORT") + "/" + System.getenv("PGDATABASE"))
                        .username(System.getenv("SECRET_NAME")).build());

    }

Dependency:

<dependency>
    <groupId>com.amazonaws</groupId>
    <artifactId>aws-xray-recorder-sdk-sql</artifactId>
</dependency>

Upvotes: 0

We are currently investigating a solution that will work with both Tomcat JDBC and HikariCP. We are aware that there are currently no work arounds without having Tomcat JDBC as a dependency. Please stay tuned.

Upvotes: 0

Ori Marko
Ori Marko

Reputation: 58862

In Spring boot , you can use still use Tomcat over HikariCP as connection pool:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-jdbc</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.zaxxer</groupId>
            <artifactId>HikariCP</artifactId>
        </exclusion>
    </exclusions>
</dependency>    
<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>tomcat-jdbc</artifactId>
</dependency>

Upvotes: 1

Related Questions