-
Notifications
You must be signed in to change notification settings - Fork 868
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add JdbcTelemetry and JdbcTelemetryBuilder (#9685)
- Loading branch information
1 parent
3b08db7
commit 2a554bd
Showing
10 changed files
with
264 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...library/src/main/java/io/opentelemetry/instrumentation/jdbc/datasource/JdbcTelemetry.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.jdbc.datasource; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest; | ||
import io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo; | ||
import javax.sql.DataSource; | ||
|
||
/** Entrypoint for instrumenting a JDBC DataSources. */ | ||
public final class JdbcTelemetry { | ||
|
||
/** Returns a new {@link JdbcTelemetry} configured with the given {@link OpenTelemetry}. */ | ||
public static JdbcTelemetry create(OpenTelemetry openTelemetry) { | ||
return builder(openTelemetry).build(); | ||
} | ||
|
||
/** Returns a new {@link JdbcTelemetryBuilder} configured with the given {@link OpenTelemetry}. */ | ||
public static JdbcTelemetryBuilder builder(OpenTelemetry openTelemetry) { | ||
return new JdbcTelemetryBuilder(openTelemetry); | ||
} | ||
|
||
private final Instrumenter<DataSource, DbInfo> dataSourceInstrumenter; | ||
private final Instrumenter<DbRequest, Void> statementInstrumenter; | ||
|
||
JdbcTelemetry( | ||
Instrumenter<DataSource, DbInfo> dataSourceInstrumenter, | ||
Instrumenter<DbRequest, Void> statementInstrumenter) { | ||
this.dataSourceInstrumenter = dataSourceInstrumenter; | ||
this.statementInstrumenter = statementInstrumenter; | ||
} | ||
|
||
public DataSource wrap(DataSource dataSource) { | ||
return new OpenTelemetryDataSource( | ||
dataSource, this.dataSourceInstrumenter, this.statementInstrumenter); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../src/main/java/io/opentelemetry/instrumentation/jdbc/datasource/JdbcTelemetryBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.jdbc.datasource; | ||
|
||
import com.google.errorprone.annotations.CanIgnoreReturnValue; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.jdbc.internal.JdbcInstrumenterFactory; | ||
|
||
/** A builder of {@link JdbcTelemetry}. */ | ||
public final class JdbcTelemetryBuilder { | ||
|
||
private final OpenTelemetry openTelemetry; | ||
private boolean dataSourceInstrumenterEnabled = true; | ||
private boolean statementInstrumenterEnabled = true; | ||
private boolean statementSanitizationEnabled = true; | ||
|
||
JdbcTelemetryBuilder(OpenTelemetry openTelemetry) { | ||
this.openTelemetry = openTelemetry; | ||
} | ||
|
||
/** Configures whether spans are created for JDBC Connections. Enabled by default. */ | ||
@CanIgnoreReturnValue | ||
public JdbcTelemetryBuilder setDataSourceInstrumenterEnabled(boolean enabled) { | ||
this.dataSourceInstrumenterEnabled = enabled; | ||
return this; | ||
} | ||
|
||
/** Configures whether spans are created for JDBC Statements. Enabled by default. */ | ||
@CanIgnoreReturnValue | ||
public JdbcTelemetryBuilder setStatementInstrumenterEnabled(boolean enabled) { | ||
this.statementInstrumenterEnabled = enabled; | ||
return this; | ||
} | ||
|
||
/** Configures whether JDBC Statements are sanitized. Enabled by default. */ | ||
@CanIgnoreReturnValue | ||
public JdbcTelemetryBuilder setStatementSanitizationEnabled(boolean enabled) { | ||
this.statementSanitizationEnabled = enabled; | ||
return this; | ||
} | ||
|
||
/** Returns a new {@link JdbcTelemetry} with the settings of this {@link JdbcTelemetryBuilder}. */ | ||
public JdbcTelemetry build() { | ||
return new JdbcTelemetry( | ||
JdbcInstrumenterFactory.createDataSourceInstrumenter( | ||
openTelemetry, dataSourceInstrumenterEnabled), | ||
JdbcInstrumenterFactory.createStatementInstrumenter( | ||
openTelemetry, statementInstrumenterEnabled, statementSanitizationEnabled)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 0 additions & 34 deletions
34
...in/java/io/opentelemetry/instrumentation/jdbc/internal/DataSourceInstrumenterFactory.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
116 changes: 116 additions & 0 deletions
116
...ary/src/test/java/io/opentelemetry/instrumentation/jdbc/datasource/JdbcTelemetryTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.jdbc.datasource; | ||
|
||
import static io.opentelemetry.sdk.testing.assertj.OpenTelemetryAssertions.equalTo; | ||
|
||
import io.opentelemetry.instrumentation.testing.junit.InstrumentationExtension; | ||
import io.opentelemetry.instrumentation.testing.junit.LibraryInstrumentationExtension; | ||
import io.opentelemetry.semconv.SemanticAttributes; | ||
import java.sql.SQLException; | ||
import javax.sql.DataSource; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.RegisterExtension; | ||
|
||
class JdbcTelemetryTest { | ||
|
||
@RegisterExtension | ||
static final InstrumentationExtension testing = LibraryInstrumentationExtension.create(); | ||
|
||
@Test | ||
void buildWithDefaults() throws SQLException { | ||
JdbcTelemetry telemetry = JdbcTelemetry.builder(testing.getOpenTelemetry()).build(); | ||
DataSource dataSource = telemetry.wrap(new TestDataSource()); | ||
|
||
testing.runWithSpan( | ||
"parent", () -> dataSource.getConnection().createStatement().execute("SELECT 1;")); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> span.hasName("parent"), | ||
span -> span.hasName("TestDataSource.getConnection"), | ||
span -> | ||
span.hasName("SELECT dbname") | ||
.hasAttribute(equalTo(SemanticAttributes.DB_STATEMENT, "SELECT ?;")))); | ||
} | ||
|
||
@Test | ||
void buildWithAllInstrumentersDisabled() throws SQLException { | ||
JdbcTelemetry telemetry = | ||
JdbcTelemetry.builder(testing.getOpenTelemetry()) | ||
.setDataSourceInstrumenterEnabled(false) | ||
.setStatementInstrumenterEnabled(false) | ||
.build(); | ||
|
||
DataSource dataSource = telemetry.wrap(new TestDataSource()); | ||
|
||
testing.runWithSpan( | ||
"parent", () -> dataSource.getConnection().createStatement().execute("SELECT 1;")); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> trace.hasSpansSatisfyingExactly(span -> span.hasName("parent"))); | ||
} | ||
|
||
@Test | ||
void buildWithDataSourceInstrumenterDisabled() throws SQLException { | ||
JdbcTelemetry telemetry = | ||
JdbcTelemetry.builder(testing.getOpenTelemetry()) | ||
.setDataSourceInstrumenterEnabled(false) | ||
.build(); | ||
|
||
DataSource dataSource = telemetry.wrap(new TestDataSource()); | ||
|
||
testing.runWithSpan( | ||
"parent", () -> dataSource.getConnection().createStatement().execute("SELECT 1;")); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> span.hasName("parent"), span -> span.hasName("SELECT dbname"))); | ||
} | ||
|
||
@Test | ||
void buildWithStatementInstrumenterDisabled() throws SQLException { | ||
JdbcTelemetry telemetry = | ||
JdbcTelemetry.builder(testing.getOpenTelemetry()) | ||
.setStatementInstrumenterEnabled(false) | ||
.build(); | ||
|
||
DataSource dataSource = telemetry.wrap(new TestDataSource()); | ||
|
||
testing.runWithSpan( | ||
"parent", () -> dataSource.getConnection().createStatement().execute("SELECT 1;")); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> span.hasName("parent"), | ||
span -> span.hasName("TestDataSource.getConnection"))); | ||
} | ||
|
||
@Test | ||
void buildWithSanitizationDisabled() throws SQLException { | ||
JdbcTelemetry telemetry = | ||
JdbcTelemetry.builder(testing.getOpenTelemetry()) | ||
.setStatementSanitizationEnabled(false) | ||
.build(); | ||
|
||
DataSource dataSource = telemetry.wrap(new TestDataSource()); | ||
|
||
testing.runWithSpan( | ||
"parent", () -> dataSource.getConnection().createStatement().execute("SELECT 1;")); | ||
|
||
testing.waitAndAssertTraces( | ||
trace -> | ||
trace.hasSpansSatisfyingExactly( | ||
span -> span.hasName("parent"), | ||
span -> span.hasName("TestDataSource.getConnection"), | ||
span -> | ||
span.hasName("SELECT dbname") | ||
.hasAttribute(equalTo(SemanticAttributes.DB_STATEMENT, "SELECT 1;")))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.