-
Notifications
You must be signed in to change notification settings - Fork 964
Allow JDBC autoinstrumentation to use a custom OpenTelemetry instance to be more DI (e.g. Spring Boot) friendly #7697
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
Changes from all commits
8d2c676
5d929ff
f15cb24
167e94a
14246ac
a58d142
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.JdbcSingletons.dataSourceInstrumenter; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
|
||
import io.opentelemetry.context.Context; | ||
|
@@ -20,6 +20,7 @@ | |
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class DataSourceInstrumentation implements TypeInstrumentation { | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
return implementsInterface(named("javax.sql.DataSource")); | ||
|
@@ -46,7 +47,7 @@ public static void start( | |
return; | ||
} | ||
|
||
context = instrumenter().start(parentContext, ds); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh, it turns out the javaagent instrumentation was using the library singletons class. Can you add a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Are you sure about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep - the javaagent instrumentation has its own, separate |
||
context = dataSourceInstrumenter().start(parentContext, ds); | ||
scope = context.makeCurrent(); | ||
} | ||
|
||
|
@@ -60,7 +61,7 @@ public static void stopSpan( | |
return; | ||
} | ||
scope.close(); | ||
instrumenter().end(context, ds, null, throwable); | ||
dataSourceInstrumenter().end(context, ds, null, throwable); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,12 +20,17 @@ | |
|
||
package io.opentelemetry.instrumentation.jdbc.datasource; | ||
|
||
import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceSingletons.instrumenter; | ||
import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceInstrumenterFactory.createDataSourceInstrumenter; | ||
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcInstrumenterFactory.createStatementInstrumenter; | ||
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcUtils.computeDbInfo; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest; | ||
import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection; | ||
import io.opentelemetry.instrumentation.jdbc.internal.ThrowingSupplier; | ||
import io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo; | ||
|
@@ -40,29 +45,44 @@ | |
public class OpenTelemetryDataSource implements DataSource, AutoCloseable { | ||
|
||
private final DataSource delegate; | ||
private final Instrumenter<DataSource, Void> dataSourceInstrumenter; | ||
private final Instrumenter<DbRequest, Void> statementInstrumenter; | ||
|
||
/** | ||
* Create a OpenTelemetry DataSource wrapping another DataSource. | ||
* | ||
* @param delegate the DataSource to wrap | ||
*/ | ||
@Deprecated | ||
public OpenTelemetryDataSource(DataSource delegate) { | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
this(delegate, GlobalOpenTelemetry.get()); | ||
} | ||
|
||
/** | ||
* Create a OpenTelemetry DataSource wrapping another DataSource. This constructor is primarily | ||
* used by dependency injection frameworks. | ||
* | ||
* @param delegate the DataSource to wrap | ||
* @param openTelemetry the OpenTelemetry instance to setup for | ||
*/ | ||
public OpenTelemetryDataSource(DataSource delegate) { | ||
public OpenTelemetryDataSource(DataSource delegate, OpenTelemetry openTelemetry) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍🏽 👍🏽 👍🏽 👍🏽 👍🏽 👍🏽 👍🏽 👍🏽 👍🏽 |
||
this.delegate = delegate; | ||
this.dataSourceInstrumenter = createDataSourceInstrumenter(openTelemetry); | ||
this.statementInstrumenter = createStatementInstrumenter(openTelemetry); | ||
} | ||
|
||
@Override | ||
public Connection getConnection() throws SQLException { | ||
Connection connection = wrapCall(delegate::getConnection); | ||
DbInfo dbInfo = computeDbInfo(connection); | ||
return new OpenTelemetryConnection(connection, dbInfo); | ||
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter); | ||
} | ||
|
||
@Override | ||
public Connection getConnection(String username, String password) throws SQLException { | ||
Connection connection = wrapCall(() -> delegate.getConnection(username, password)); | ||
DbInfo dbInfo = computeDbInfo(connection); | ||
return new OpenTelemetryConnection(connection, dbInfo); | ||
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter); | ||
} | ||
|
||
@Override | ||
|
@@ -116,15 +136,15 @@ private <T, E extends SQLException> T wrapCall(ThrowingSupplier<T, E> callable) | |
return callable.call(); | ||
} | ||
|
||
Context context = instrumenter().start(parentContext, delegate); | ||
Context context = this.dataSourceInstrumenter.start(parentContext, delegate); | ||
T result; | ||
try (Scope ignored = context.makeCurrent()) { | ||
result = callable.call(); | ||
} catch (Throwable t) { | ||
instrumenter().end(context, delegate, null, t); | ||
this.dataSourceInstrumenter.end(context, delegate, null, t); | ||
throw t; | ||
} | ||
instrumenter().end(context, delegate, null, null); | ||
this.dataSourceInstrumenter.end(context, delegate, null, null); | ||
return result; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.jdbc.internal; | ||
|
||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeAttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.code.CodeSpanNameExtractor; | ||
import javax.sql.DataSource; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public final class DataSourceInstrumenterFactory { | ||
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.jdbc"; | ||
private static final DataSourceCodeAttributesGetter codeAttributesGetter = | ||
new DataSourceCodeAttributesGetter(); | ||
|
||
public static Instrumenter<DataSource, Void> createDataSourceInstrumenter( | ||
OpenTelemetry openTelemetry) { | ||
return Instrumenter.<DataSource, Void>builder( | ||
openTelemetry, INSTRUMENTATION_NAME, CodeSpanNameExtractor.create(codeAttributesGetter)) | ||
.addAttributesExtractor(CodeAttributesExtractor.create(codeAttributesGetter)) | ||
.buildInstrumenter(); | ||
} | ||
|
||
private DataSourceInstrumenterFactory() {} | ||
} |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright The OpenTelemetry Authors | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package io.opentelemetry.instrumentation.jdbc.internal; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.OpenTelemetry; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.db.DbClientSpanNameExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.db.SqlClientAttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.instrumenter.net.NetClientAttributesExtractor; | ||
import io.opentelemetry.instrumentation.api.internal.ConfigPropertiesUtil; | ||
|
||
/** | ||
* This class is internal and is hence not for public use. Its APIs are unstable and can change at | ||
* any time. | ||
*/ | ||
public final class JdbcInstrumenterFactory { | ||
public static final String INSTRUMENTATION_NAME = "io.opentelemetry.jdbc"; | ||
mateuszrzeszutek marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private static final JdbcAttributesGetter dbAttributesGetter = new JdbcAttributesGetter(); | ||
private static final JdbcNetAttributesGetter netAttributesGetter = new JdbcNetAttributesGetter(); | ||
|
||
public static Instrumenter<DbRequest, Void> createStatementInstrumenter() { | ||
return createStatementInstrumenter(GlobalOpenTelemetry.get()); | ||
} | ||
|
||
public static Instrumenter<DbRequest, Void> createStatementInstrumenter( | ||
OpenTelemetry openTelemetry) { | ||
return Instrumenter.<DbRequest, Void>builder( | ||
openTelemetry, | ||
INSTRUMENTATION_NAME, | ||
DbClientSpanNameExtractor.create(dbAttributesGetter)) | ||
.addAttributesExtractor( | ||
SqlClientAttributesExtractor.builder(dbAttributesGetter) | ||
.setStatementSanitizationEnabled( | ||
ConfigPropertiesUtil.getBoolean( | ||
"otel.instrumentation.common.db-statement-sanitizer.enabled", true)) | ||
.build()) | ||
.addAttributesExtractor(NetClientAttributesExtractor.create(netAttributesGetter)) | ||
.buildInstrumenter(SpanKindExtractor.alwaysClient()); | ||
} | ||
|
||
private JdbcInstrumenterFactory() {} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.