Skip to content

Commit

Permalink
Allow custom (non-global) OpenTelemetry instance propagation
Browse files Browse the repository at this point in the history
Propagates a custom OpenTelemetry instance through the datasource, connection and statement(s) wrappers.
The OpenTelemetryDriver uses an OpenTelemetry instance provided by the GlobalOpenTelemetry.get() static method.
  • Loading branch information
FranPregernik committed Feb 1, 2023
1 parent 79a644d commit a33abc7
Show file tree
Hide file tree
Showing 13 changed files with 175 additions and 144 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

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

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

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter;
import io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer;
Expand Down Expand Up @@ -46,7 +48,8 @@ public static void start(
return;
}

context = instrumenter().start(parentContext, ds);
Instrumenter<DataSource, Void> instrumenter = createInstrumenter(GlobalOpenTelemetry.get());
context = instrumenter.start(parentContext, ds);
scope = context.makeCurrent();
}

Expand All @@ -60,7 +63,8 @@ public static void stopSpan(
return;
}
scope.close();
instrumenter().end(context, ds, null, throwable);
Instrumenter<DataSource, Void> instrumenter = createInstrumenter(GlobalOpenTelemetry.get());
instrumenter.end(context, ds, null, throwable);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@

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 io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.api.internal.EmbeddedInstrumentationProperties;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcConnectionUrlParser;
import io.opentelemetry.instrumentation.jdbc.internal.OpenTelemetryConnection;
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, GlobalOpenTelemetry.get());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@

package io.opentelemetry.instrumentation.jdbc.datasource;

import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceSingletons.instrumenter;
import static io.opentelemetry.instrumentation.jdbc.internal.DataSourceInstrumenterFactory.createInstrumenter;
import static io.opentelemetry.instrumentation.jdbc.internal.JdbcUtils.computeDbInfo;

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.OpenTelemetryConnection;
import io.opentelemetry.instrumentation.jdbc.internal.ThrowingSupplier;
import io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo;
Expand All @@ -40,29 +42,34 @@
public class OpenTelemetryDataSource implements DataSource, AutoCloseable {

private final DataSource delegate;
private final OpenTelemetry openTelemetry;
private final Instrumenter<DataSource, Void> instrumenter;

/**
* 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) {
this.delegate = delegate;
this.openTelemetry = openTelemetry;
this.instrumenter = createInstrumenter(this.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, openTelemetry);
}

@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, openTelemetry);
}

@Override
Expand Down Expand Up @@ -116,15 +123,15 @@ private <T, E extends SQLException> T wrapCall(ThrowingSupplier<T, E> callable)
return callable.call();
}

Context context = instrumenter().start(parentContext, delegate);
Context context = this.instrumenter.start(parentContext, delegate);
T result;
try (Scope ignored = context.makeCurrent()) {
result = callable.call();
} catch (Throwable t) {
instrumenter().end(context, delegate, null, t);
this.instrumenter.end(context, delegate, null, t);
throw t;
}
instrumenter().end(context, delegate, null, null);
this.instrumenter.end(context, delegate, null, null);
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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> createInstrumenter(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,45 @@
/*
* 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.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> createInstrumenter() {
return createInstrumenter(null);
}

public static Instrumenter<DbRequest, Void> createInstrumenter(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() {}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

package io.opentelemetry.instrumentation.jdbc.internal;

import io.opentelemetry.api.OpenTelemetry;
import io.opentelemetry.instrumentation.jdbc.internal.dbinfo.DbInfo;
import java.io.InputStream;
import java.io.Reader;
Expand Down Expand Up @@ -47,8 +48,9 @@
public class OpenTelemetryCallableStatement<S extends CallableStatement>
extends OpenTelemetryPreparedStatement<S> implements CallableStatement {

public OpenTelemetryCallableStatement(S delegate, DbInfo dbInfo, String query) {
super(delegate, dbInfo, query);
public OpenTelemetryCallableStatement(
S delegate, DbInfo dbInfo, String query, OpenTelemetry openTelemetry) {
super(delegate, dbInfo, query, openTelemetry);
}

@Override
Expand Down
Loading

0 comments on commit a33abc7

Please sign in to comment.