io.opentracing.contrib:opentracing-jdbc

OpenTracing Instrumentation for JDBC API

License

License

GroupId

GroupId

io.opentracing.contrib
ArtifactId

ArtifactId

opentracing-jdbc
Last Version

Last Version

0.2.15
Release Date

Release Date

Type

Type

jar
Description

Description

io.opentracing.contrib:opentracing-jdbc
OpenTracing Instrumentation for JDBC API
Project URL

Project URL

https://github.com/opentracing-contrib/java-jdbc
Source Code Management

Source Code Management

http://github.com/opentracing-contrib/java-jdbc

Download opentracing-jdbc

How to add to project

<!-- https://jarcasting.com/artifacts/io.opentracing.contrib/opentracing-jdbc/ -->
<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-jdbc</artifactId>
    <version>0.2.15</version>
</dependency>
// https://jarcasting.com/artifacts/io.opentracing.contrib/opentracing-jdbc/
implementation 'io.opentracing.contrib:opentracing-jdbc:0.2.15'
// https://jarcasting.com/artifacts/io.opentracing.contrib/opentracing-jdbc/
implementation ("io.opentracing.contrib:opentracing-jdbc:0.2.15")
'io.opentracing.contrib:opentracing-jdbc:jar:0.2.15'
<dependency org="io.opentracing.contrib" name="opentracing-jdbc" rev="0.2.15">
  <artifact name="opentracing-jdbc" type="jar" />
</dependency>
@Grapes(
@Grab(group='io.opentracing.contrib', module='opentracing-jdbc', version='0.2.15')
)
libraryDependencies += "io.opentracing.contrib" % "opentracing-jdbc" % "0.2.15"
[io.opentracing.contrib/opentracing-jdbc "0.2.15"]

Dependencies

compile (2)

Group / Artifact Type Version
io.opentracing : opentracing-util jar 0.33.0
io.opentracing.contrib : common jar 0.1.4

test (11)

Group / Artifact Type Version
io.opentracing : opentracing-util test-jar 0.33.0
io.opentracing : opentracing-mock jar 0.33.0
com.h2database : h2 jar 1.4.200
org.hibernate : hibernate-core jar 5.4.10.Final
org.springframework : spring-jdbc jar 5.2.2.RELEASE
org.apache.commons : commons-dbcp2 jar 2.7.0
junit : junit jar 4.13.1
org.assertj : assertj-core jar 3.14.0
org.junit.jupiter : junit-jupiter jar 5.5.2
org.junit.platform : junit-platform-launcher jar 1.5.2
org.junit.vintage : junit-vintage-engine jar 5.5.2

Project Modules

There are no modules declared in this project.

Build Status Coverage Status Released Version Apache-2.0 license

OpenTracing JDBC Instrumentation

OpenTracing instrumentation for JDBC.

Installation

pom.xml

<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-jdbc</artifactId>
    <version>VERSION</version>
</dependency>

Usage

Non-interceptor

Tracing for JDBC connections of URLs starting with "jdbc:tracing:".

  1. Activate tracing for JDBC connections by adding tracing to the JDBC url:

    jdbc:tracing:h2:mem:test

    To trace calls with active Spans only, set property traceWithActiveSpanOnly=true.

    jdbc:tracing:h2:mem:test?traceWithActiveSpanOnly=true

    To ignore specific queries (such as health checks), use the property ignoreForTracing="SELECT 1". Double quotes can be escaped with \.

    SELECT * FROM \"TEST\"
    The property can be repeated for multiple statements.

  2. Set driver class to io.opentracing.contrib.jdbc.TracingDriver.

    Class.forName("io.opentracing.contrib.jdbc.TracingDriver");

    or

    io.opentracing.contrib.jdbc.TracingDriver.load();
  3. Instantiate tracer and register it with GlobalTracer.

    // Instantiate tracer
    Tracer tracer = ...
    
    // Register tracer with GlobalTracer
    GlobalTracer.register(tracer);
    

Interceptor

Tracing for all JDBC connections without modifying the URL.

In "interceptor mode", the TracingDriver will intercept calls to DriverManager.getConnection(url,...) for all URLs. The TracingDriver provides connections to the DriverManager that are instrumented. Please note that the TracingDriver must be registered before the underlying driver, It's recommended to turn on "interceptor mode" in the first place.

For standalone applications:

public static void main(String[] args) {
   io.opentracing.contrib.jdbc.TracingDriver.setInterceptorMode(true);
   // some jdbc operation here
}

For web applications:

public void contextInitialized(ServletContextEvent event) {
   io.opentracing.contrib.jdbc.TracingDriver.setInterceptorMode(true);
}

Or call TracingDriver.ensureRegisteredAsTheFirstDriver() along with TracingDriver.setInterceptorMode(true) at any place, Please note driver like Oracle JDBC may fail since it's destroyed forever after deregistration.

The withActiveSpanOnly and ignoreStatements properties for "interceptor mode" can be configured with the TracingDriver via:

// Set withActiveSpanOnly=true
TracingDriver.setInterceptorProperty(true);

and

// Set ignoreStatements={"CREATE TABLE ignored (id INTEGER, TEST VARCHAR)"}
TracingDriver.setInterceptorProperty(Collections.singleton("CREATE TABLE ignored (id INTEGER, TEST VARCHAR)"));

Hibernate

<hibernate-configuration>
    <session-factory>
        <property name="hibernate.connection.driver_class">io.opentracing.contrib.jdbc.TracingDriver</property>
        <property name="hibernate.connection.url">jdbc:tracing:mysql://localhost:3306/test</property>
        ...
    </session-factory>
    ...
</hibernate-configuration>

JPA

<persistence-unit name="jpa">
    <properties>
        <property name="javax.persistence.jdbc.driver" value="io.opentracing.contrib.jdbc.TracingDriver"/>
        <property name="javax.persistence.jdbc.url" value="jdbc:tracing:mysql://localhost:3306/test"/>
        ...
    </properties>
</persistence-unit>

Spring

For dbcp2:

<bean id="dataSource" destroy-method="close" class="org.apache.commons.dbcp2.BasicDataSource">
    <property name="driverClassName" value="io.opentracing.contrib.jdbc.TracingDriver"/>
    <property name="url" value="jdbc:tracing:mysql://localhost:3306/test"/>
    ...
</bean>

Spring Boot 2

For Hikari (Postgresl):

### Spring JPA Datasource Connection
spring.datasource.username=postgres
spring.datasource.password=XXXXX
spring.datasource.hikari.driverClassName=io.opentracing.contrib.jdbc.TracingDriver
spring.datasource.hikari.jdbcUrl=jdbc:tracing:postgresql://localhost:5432/my_app_db

Configuration Bean:

@Component
public class OpenTracingConfig {

  @Bean
  public io.opentracing.Tracer jaegerTracer() {
    io.opentracing.contrib.jdbc.TracingDriver.load();
    return new Configuration("my_app").getTracer();
  }
}

Slow Query

Span is marked by tag slow=true if duration exceed slowQueryThresholdMs. slowQueryThresholdMs defaults to 0 which means disabled, can be enabled in two ways:

  1. Passing system property, E.g. -Dio.opentracing.contrib.jdbc.slowQueryThresholdMs=100
  2. Modify value by code, E.g. io.opentracing.contrib.jdbc.JdbcTracing.setSlowQueryThresholdMs(100)

Fast Query

Spans that complete faster than the optional excludeFastQueryThresholdMs flag will be not be reported. excludeFastQueryThresholdMs defaults to 0 which means disabled, can be enabled in two ways:

  1. Passing system property, E.g. -Dio.opentracing.contrib.jdbc.excludeFastQueryThresholdMs=100
  2. Modify value by code, E.g. io.opentracing.contrib.jdbc.JdbcTracing.setExcludeFastQueryThresholdMs(100)

Troubleshooting

In case of Unable to find a driver error the database driver should be registered before configuring the datasource. E.g. Class.forName("com.mysql.jdbc.Driver");

License

Apache 2.0 License.

io.opentracing.contrib

3rd-Party OpenTracing API Contributions

3rd-party contributions that use OpenTracing. **The repositories in this org are *not* affiliated with the CNCF.**

Versions

Version
0.2.15
0.2.14
0.2.13
0.2.12
0.2.11
0.2.10
0.2.9
0.2.8
0.2.7
0.2.6
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.8
0.1.7
0.1.6
0.1.5
0.1.4
0.1.3
0.1.2
0.1.1
0.1.0
0.0.12
0.0.11
0.0.10
0.0.9
0.0.8
0.0.7
0.0.6
0.0.5
0.0.4
0.0.3
0.0.2
0.0.1