Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OpenTelemetry JDBC instrumentation library #3367

Merged
merged 76 commits into from
Jul 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
5bf3376
Added jdbc library with ported version of OpenTracing JDBC Instrument…
donbeave Jun 18, 2021
e3df35b
Gradle 7.1.
donbeave Jun 18, 2021
af12c2a
Revert "Gradle 7.1."
donbeave Jun 18, 2021
ca929fc
Code style fixes.
donbeave Jun 18, 2021
464cbb7
Code style fixes.
donbeave Jun 18, 2021
e4fe822
Moved some JDBC utilities classes to the library module.
donbeave Jun 19, 2021
91e6b9a
javax.annotation.Nullable -> org.checkerframework.checker.nullness.qu…
donbeave Jun 19, 2021
7c903f3
Build fix.
donbeave Jun 19, 2021
573d3ce
Spotless fixes.
donbeave Jun 19, 2021
c357a57
Cache prepared statements.
donbeave Jun 19, 2021
1229c5f
Cache connection info.
donbeave Jun 19, 2021
d163b42
Tracing for JDBC statement and prepared statement.
donbeave Jun 19, 2021
8afb87d
Added some API dependencies.
donbeave Jun 19, 2021
034b537
Fixed ending the span.
donbeave Jun 19, 2021
f45f3f9
Removed proxy usage.
donbeave Jun 21, 2021
343643a
Cleanup.
donbeave Jun 21, 2021
95bfe7b
Implemented datasource with tracing support.
donbeave Jun 21, 2021
375b7ac
Renamed Tracing prefix with OpenTelemetry.
donbeave Jun 21, 2021
a124016
Renamed Tracing prefix with OpenTelemetry.
donbeave Jun 21, 2021
397bc4b
Moved utilities classes to io.opentelemetry.instrumentation.jdbc.inte…
donbeave Jun 21, 2021
09ce33f
Build fix.
donbeave Jun 21, 2021
2a9ce6c
Build fix.
donbeave Jun 21, 2021
751ddb4
Cleanup.
donbeave Jun 21, 2021
a2ae147
Moved some shared classes to instrumentation-api module.
donbeave Jun 22, 2021
2212831
Code review fixes.
donbeave Jun 22, 2021
a8f1553
Make OpenTelemetryDriver final.
donbeave Jun 22, 2021
5699f89
Spotless fixes.
donbeave Jun 22, 2021
60f6f23
Moved test classes from javaagent-unit-tests module to library module.
donbeave Jun 23, 2021
6997669
Removed javaagent-unit-tests module.
donbeave Jun 23, 2021
a89ffc5
Covered OpenTelemetryDriver with tests.
donbeave Jun 23, 2021
fb14f11
Cleanup.
donbeave Jun 23, 2021
0ef50c7
Covered OpenTelemetryDataSource with tests.
donbeave Jun 24, 2021
07e1ab7
Covered OpenTelemetryConnection with tests.
donbeave Jun 24, 2021
4112488
Revert TestPreparedStatement.
donbeave Jun 24, 2021
d6538bd
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
donbeave Jun 24, 2021
62e91fa
Merge branch 'open-telemetry-main'
donbeave Jun 24, 2021
fa9c60c
Added README.
donbeave Jun 24, 2021
02b73a4
Build fix.
donbeave Jun 24, 2021
53151f5
Checkstyle fixes.
donbeave Jun 24, 2021
edcb7f9
Spotless fixes.
donbeave Jun 24, 2021
11038fa
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
Jun 25, 2021
3acc14a
Rename to build.gradle
Jun 25, 2021
df40e80
Fix build.gradle format.
donbeave Jun 25, 2021
141bece
INSTANCE package scope.
donbeave Jun 25, 2021
f51900e
Documented OpenTelemetryDataSource usage.
donbeave Jun 25, 2021
f25ba73
Renamed CheckedCallable to ThrowingSupplier.
donbeave Jun 25, 2021
f189b5f
Update instrumentation/jdbc/library/README.md
donbeave Jun 25, 2021
66d34aa
Update instrumentation/jdbc/library/src/main/java/io/opentelemetry/in…
donbeave Jun 25, 2021
97b8b71
Update instrumentation/jdbc/library/src/main/java/io/opentelemetry/in…
donbeave Jun 25, 2021
dc76975
Update instrumentation/jdbc/library/src/main/java/io/opentelemetry/in…
donbeave Jun 25, 2021
b15f9c9
Update instrumentation/jdbc/library/README.md
donbeave Jun 25, 2021
0dd1178
Update instrumentation/jdbc/library/README.md
donbeave Jun 25, 2021
41e9561
Update instrumentation/jdbc/library/README.md
donbeave Jun 25, 2021
18beece
Removed javaagent-api module usage.
donbeave Jun 25, 2021
327d26f
Removed useless checking.
donbeave Jun 25, 2021
e6b7925
Moved javaagent test classes to testing module.
donbeave Jun 25, 2021
e14ac6a
Move common JDBC test classes to testing module.
donbeave Jun 25, 2021
bbbef33
Spotless fixes.
donbeave Jun 25, 2021
b35f6bb
Code format.
donbeave Jun 25, 2021
402637b
Moved PeerServiceAttributesExtractor back to javaagent-api module.
donbeave Jun 25, 2021
d0266d3
Rename JavaAgentJdbcSingletons to JdbcSingletons.
donbeave Jun 25, 2021
7e86a5a
Fixed JdbcSingletons import.
donbeave Jun 25, 2021
1720575
Cleanup.
donbeave Jun 27, 2021
d178c34
Codenarc fixes.
donbeave Jun 27, 2021
09851dc
Update instrumentation/jdbc/library/src/main/java/io/opentelemetry/in…
donbeave Jun 28, 2021
3f89463
Update instrumentation/jdbc/library/src/main/java/io/opentelemetry/in…
donbeave Jun 28, 2021
fc5299d
Parse InstrumentationVersion.VERSION.
donbeave Jun 28, 2021
de70de1
Store major and minor version in private static fields.
donbeave Jun 28, 2021
6e7d15a
Merge branch 'main' of github.com:open-telemetry/opentelemetry-java-i…
Jun 28, 2021
6abe892
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
donbeave Jul 1, 2021
286f48d
Make the datasource way a preferred way.
donbeave Jul 1, 2021
7e7ce1f
Removed interceptor mode.
donbeave Jul 1, 2021
4473948
Rename build.gradle -> build.gradle.kts.
donbeave Jul 1, 2021
df12ddc
Switch to Gradle Kotlin DSL.
donbeave Jul 1, 2021
da8d9e0
Merge branch 'main' of https://github.com/open-telemetry/opentelemetr…
donbeave Jul 1, 2021
a7a2557
Merge pull request #3 from open-telemetry/main
donbeave Jul 1, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/standalone-library-instrumentation.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ that can be used if you prefer that over using the Java agent:
* [AWS SDK 2.2+](../instrumentation/aws-sdk/aws-sdk-2.2/library)
* [gRPC](../instrumentation/grpc-1.6/library)
* [Guava](../instrumentation/guava-10.0/library)
* [JDBC](../instrumentation/jdbc/library)
* [Lettuce](../instrumentation/lettuce/lettuce-5.1/library)
* [Log4j](../instrumentation/log4j/log4j-2.13.2/library)
* [Logback](../instrumentation/logback-1.0/library)
Expand Down
7 changes: 0 additions & 7 deletions instrumentation/jdbc/javaagent-unit-tests/build.gradle.kts

This file was deleted.

3 changes: 3 additions & 0 deletions instrumentation/jdbc/javaagent/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ muzzle {
}

dependencies {
implementation(project(":instrumentation:jdbc:library"))

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")
Expand All @@ -26,6 +27,8 @@ dependencies {
testLibrary("com.mchange:c3p0:0.9.5")

latestDepTestLibrary("org.apache.derby:derby:10.14.+")

testImplementation(project(":instrumentation:jdbc:testing"))
}

tasks.withType<Test>().configureEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.instrumentation.jdbc.internal.JdbcMaps;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.sql.PreparedStatement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import static net.bytebuddy.matcher.ElementMatchers.returns;
import static net.bytebuddy.matcher.ElementMatchers.takesArgument;

import io.opentelemetry.instrumentation.jdbc.internal.DbInfo;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcConnectionUrlParser;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcMaps;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import java.sql.Connection;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import io.opentelemetry.instrumentation.api.instrumenter.SpanNameExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbAttributesExtractor;
import io.opentelemetry.instrumentation.api.instrumenter.db.DbSpanNameExtractor;
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcAttributesExtractor;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcNetAttributesExtractor;
import io.opentelemetry.javaagent.instrumentation.api.instrumenter.PeerServiceAttributesExtractor;

public final class JdbcSingletons {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
import io.opentelemetry.javaagent.instrumentation.api.CallDepthThreadLocalMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

package io.opentelemetry.javaagent.instrumentation.jdbc.datasource;

import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceSingletons.instrumenter;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.jdbc.datasource.DataSourceSingletons.instrumenter;
import static net.bytebuddy.matcher.ElementMatchers.named;

import io.opentelemetry.context.Context;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
* SPDX-License-Identifier: Apache-2.0
*/

import io.opentelemetry.instrumentation.jdbc.TestConnection
import io.opentelemetry.instrumentation.jdbc.TestDriver

import static io.opentelemetry.api.trace.SpanKind.CLIENT
import static io.opentelemetry.api.trace.SpanKind.INTERNAL
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.basicSpan
Expand All @@ -28,8 +31,6 @@ import org.h2.jdbcx.JdbcDataSource
import org.hsqldb.jdbc.JDBCDriver
import spock.lang.Shared
import spock.lang.Unroll
import test.TestConnection
import test.TestDriver

@Unroll
class JdbcInstrumentationTest extends AgentInstrumentationSpecification {
Expand Down Expand Up @@ -466,6 +467,7 @@ class JdbcInstrumentationTest extends AgentInstrumentationSpecification {
when:
try {
connection = new TestConnection(true)
connection.url = "jdbc:testdb://localhost"
} catch (Exception ignored) {
connection = driver.connect(jdbcUrl, null)
}
Expand Down Expand Up @@ -580,6 +582,7 @@ class JdbcInstrumentationTest extends AgentInstrumentationSpecification {
def "test getClientInfo exception"() {
setup:
Connection connection = new TestConnection(false)
connection.url = "jdbc:testdb://localhost"

when:
Statement statement = null
Expand Down Expand Up @@ -720,6 +723,7 @@ class JdbcInstrumentationTest extends AgentInstrumentationSpecification {
def "should handle recursive Statements inside Connection.getMetaData(): #desc"() {
given:
def connection = new DbCallingConnection(usePreparedStatementInConnection)
connection.url = "jdbc:testdb://localhost"

when:
runUnderTrace("parent") {
Expand Down
19 changes: 19 additions & 0 deletions instrumentation/jdbc/library/NOTICE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
This product contains a modified part of OpenTracing JDBC Instrumentation:

* License:

Copyright 2017-2021 The OpenTracing Authors

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

https://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

* Homepage: https://github.com/opentracing-contrib/java-jdbc
78 changes: 78 additions & 0 deletions instrumentation/jdbc/library/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
# Manual Instrumentation for JDBC

Provides OpenTelemetry instrumentation for
[Java JDBC API](https://docs.oracle.com/javase/8/docs/technotes/guides/jdbc/).

## Quickstart

### Add these dependencies to your project.

Replace `OPENTELEMETRY_VERSION` with the latest stable
[release](https://mvnrepository.com/artifact/io.opentelemetry). `Minimum version: 1.4.0`

For Maven add to your `pom.xml`:

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

For Gradle add to your dependencies:

```groovy
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](https://spring.io/projects/spring-framework), [Micronaut](https://micronaut.io),
[Quarkus](https://quarkus.io), or [Guice](https://github.com/google/guice).

```java
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
```

2. Set the driver class to `io.opentelemetry.instrumentation.jdbc.OpenTelemetryDriver` and initialize the driver with:

```java
Class.forName("io.opentelemetry.instrumentation.jdbc.OpenTelemetryDriver");
```
19 changes: 19 additions & 0 deletions instrumentation/jdbc/library/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

plugins {
id("otel.library-instrumentation")
}

dependencies {
implementation(project(":instrumentation-api"))

compileOnly("com.google.auto.value:auto-value-annotations")
annotationProcessor("com.google.auto.value:auto-value")

implementation("org.slf4j:slf4j-api")

testImplementation(project(":instrumentation:jdbc:testing"))
}
Loading