Skip to content

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

Merged
merged 6 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package io.opentelemetry.javaagent.instrumentation.jdbc;

import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceInstrumenterFactory.createDataSourceInstrumenter;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.instrumentation.api.instrumenter.SpanKindExtractor;
Expand All @@ -17,17 +19,20 @@
import io.opentelemetry.instrumentation.jdbc.internal.JdbcNetAttributesGetter;
import io.opentelemetry.javaagent.bootstrap.internal.CommonConfig;
import io.opentelemetry.javaagent.bootstrap.internal.InstrumentationConfig;
import javax.sql.DataSource;

public final class JdbcSingletons {
private static final String INSTRUMENTATION_NAME = "io.opentelemetry.jdbc";

private static final Instrumenter<DbRequest, Void> INSTRUMENTER;
private static final Instrumenter<DbRequest, Void> STATEMENT_INSTRUMENTER;
public static final Instrumenter<DataSource, Void> DATASOURCE_INSTRUMENTER =
createDataSourceInstrumenter(GlobalOpenTelemetry.get());

static {
JdbcAttributesGetter dbAttributesGetter = new JdbcAttributesGetter();
JdbcNetAttributesGetter netAttributesGetter = new JdbcNetAttributesGetter();

INSTRUMENTER =
STATEMENT_INSTRUMENTER =
Instrumenter.<DbRequest, Void>builder(
GlobalOpenTelemetry.get(),
INSTRUMENTATION_NAME,
Expand All @@ -47,8 +52,12 @@ public final class JdbcSingletons {
.buildInstrumenter(SpanKindExtractor.alwaysClient());
}

public static Instrumenter<DbRequest, Void> instrumenter() {
return INSTRUMENTER;
public static Instrumenter<DbRequest, Void> statementInstrumenter() {
return STATEMENT_INSTRUMENTER;
}

public static Instrumenter<DataSource, Void> dataSourceInstrumenter() {
return DATASOURCE_INSTRUMENTER;
}

private JdbcSingletons() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.instrumenter;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.statementInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand Down Expand Up @@ -70,11 +70,11 @@ public static void onEnter(
Context parentContext = currentContext();
request = DbRequest.create(statement);

if (request == null || !instrumenter().shouldStart(parentContext, request)) {
if (request == null || !statementInstrumenter().shouldStart(parentContext, request)) {
return;
}

context = instrumenter().start(parentContext, request);
context = statementInstrumenter().start(parentContext, request);
scope = context.makeCurrent();
}

Expand All @@ -91,7 +91,7 @@ public static void stopSpan(

if (scope != null) {
scope.close();
instrumenter().end(context, request, null, throwable);
statementInstrumenter().end(context, request, null, throwable);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.hasClassesNamed;
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.implementsInterface;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.instrumenter;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.statementInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.isPublic;
import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith;
import static net.bytebuddy.matcher.ElementMatchers.named;
Expand Down Expand Up @@ -70,11 +70,11 @@ public static void onEnter(
Context parentContext = currentContext();
request = DbRequest.create(statement, sql);

if (request == null || !instrumenter().shouldStart(parentContext, request)) {
if (request == null || !statementInstrumenter().shouldStart(parentContext, request)) {
return;
}

context = instrumenter().start(parentContext, request);
context = statementInstrumenter().start(parentContext, request);
scope = context.makeCurrent();
}

Expand All @@ -91,7 +91,7 @@ public static void stopSpan(

if (scope != null) {
scope.close();
instrumenter().end(context, request, null, throwable);
statementInstrumenter().end(context, request, null, throwable);
}
}
}
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.JdbcSingletons.dataSourceInstrumenter;
import static net.bytebuddy.matcher.ElementMatchers.named;

import io.opentelemetry.context.Context;
Expand All @@ -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"));
Expand All @@ -46,7 +47,7 @@ public static void start(
return;
}

context = instrumenter().start(parentContext, ds);
Copy link
Member

Choose a reason for hiding this comment

The 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 dataSourceInstrumenter() method to the JdbcSingletons class? You should be able to reuse the DataSourceInstrumenterFactory class you've just added.

Copy link
Contributor Author

@FranPregernik FranPregernik Feb 4, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure about JdbcSingletons? Because It has been renamed to JdbcInstrumenterFactory.
I added it for now to the DataSourceInstrumentation class but I can move it to the JdbcSingletons class.

Copy link
Member

@mateuszrzeszutek mateuszrzeszutek Feb 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep - the javaagent instrumentation has its own, separate JdbcSingletons class.
By convention, we always create static methods for each instrumenter - usually it's just instrumenter(), but in this case, since we have >1 instrumenter here it'd be dataSourceInstrumenter()

context = dataSourceInstrumenter().start(parentContext, ds);
scope = context.makeCurrent();
}

Expand All @@ -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
Expand Up @@ -20,7 +20,8 @@

package io.opentelemetry.instrumentation.jdbc;

import static io.opentelemetry.instrumentation.jdbc.internal.JdbcSingletons.INSTRUMENTATION_NAME;
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcInstrumenterFactory.INSTRUMENTATION_NAME;
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcSingletons.statementInstrumenter;

import io.opentelemetry.instrumentation.api.internal.EmbeddedInstrumentationProperties;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcConnectionUrlParser;
Expand Down Expand Up @@ -211,7 +212,7 @@ public Connection connect(String url, Properties info) throws SQLException {

DbInfo dbInfo = JdbcConnectionUrlParser.parse(realUrl, info);

return new OpenTelemetryConnection(connection, dbInfo);
return new OpenTelemetryConnection(connection, dbInfo, statementInstrumenter());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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
Expand Down Expand Up @@ -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";
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() {}
}
Loading