opentelemetry-java-instrume.../instrumentation/jdbc/library
Trask Stalnaker 1077258263
Add InternalJavadoc custom error prone check (#5277)
* Add InternalJavadoc custom error prone check

* Add example usage

* Move to conventions

* Revert "Move to conventions"

This reverts commit d8a8209b59.

* Just get it working

* Clearer error message

* versions

* Apply almost everywhere

* feedback

* Always at the end of javadoc

* Fix test

* Missed (at least) one

* No longer internal

* Fix NPE

* Spotless

* Convert awslambda Java test to JUnit 5 so can reduce visibility

* Reduce visibility

* Rename the check

* More

* Move into errorprone-convention

* Fix UserExcludedClassesConfigurerTest
2022-02-01 17:54:57 -08:00
..
src Add InternalJavadoc custom error prone check (#5277) 2022-02-01 17:54:57 -08:00
NOTICE.txt OpenTelemetry JDBC instrumentation library (#3367) 2021-07-01 10:35:07 -07:00
README.md OpenTelemetry JDBC instrumentation library (#3367) 2021-07-01 10:35:07 -07:00
build.gradle.kts JDBC instrumentation: switch to use api instead of implementation for instrumentation-api. (#3494) 2021-07-06 09:20:41 -07:00

README.md

Manual Instrumentation for JDBC

Provides OpenTelemetry instrumentation for Java JDBC API.

Quickstart

Add these dependencies to your project.

Replace OPENTELEMETRY_VERSION with the latest stable release. Minimum version: 1.4.0

For Maven add to your pom.xml:

<dependencies>
  <dependency>
    <groupId>io.opentelemetry.instrumentation</groupId>
    <artifactId>opentelemetry-jdbc</artifactId>
    <version>OPENTELEMETRY_VERSION</version>
  </dependency>
</dependencies>

For Gradle add to your dependencies:

implementation("io.opentelemetry.instrumentation:opentelemetry-jdbc:OPENTELEMETRY_VERSION")
Usage

There are three possible ways to activate the OpenTelemetry JDBC instrumentation. The first way is more preferable for DI frameworks which uses connection pools, as it wraps a DataSource with a special OpenTelemetry wrapper. The second one requires to change the connection URL and switch to use a special OpenTelemetry driver.

Datasource way

If your application uses a DataSource, simply wrap your current DataSource object with OpenTelemetryDataSource. OpenTelemetryDataSource has a constructor method that accepts the DataSource to wrap. This is by far the simplest method especially if you use a dependency injection (DI) frameworks such as Spring Framework, Micronaut, Quarkus, or Guice.

import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Configuration;
import io.opentelemetry.instrumentation.jdbc.datasource.OpenTelemetryDataSource;

@Configuration
public class DataSourceConfig {

  @Bean
  public DataSource dataSource() {
    BasicDataSource dataSource = new BasicDataSource();
    dataSource.setDriverClassName("org.postgresql.Driver");
    dataSource.setUrl("jdbc:postgresql://127.0.0.1:5432/example");
    dataSource.setUsername("postgres");
    dataSource.setPassword("root");
    return new OpenTelemetryDataSource(dataSource);
  }

}

Driver way

  1. Activate tracing for JDBC connections by setting jdbc:otel: prefix to the JDBC URL:
jdbc:otel:h2:mem:test
  1. Set the driver class to io.opentelemetry.instrumentation.jdbc.OpenTelemetryDriver and initialize the driver with:
Class.forName("io.opentelemetry.instrumentation.jdbc.OpenTelemetryDriver");