Skip to content
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 @@ -26,6 +26,7 @@
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.Locale;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -68,35 +69,52 @@ public static void addDbInfo(
@SuppressWarnings("unused")
public static class TransactionAdvice {

@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This Connection connection,
@Advice.Origin("#m") String methodName,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
Context parentContext = currentContext();
DbRequest request =
DbRequest.createTransaction(connection, methodName.toUpperCase(Locale.ROOT));

if (request == null || !transactionInstrumenter().shouldStart(parentContext, request)) {
return;
public static final class AdviceScope {
private final DbRequest request;
private final Context context;
private final Scope scope;

private AdviceScope(DbRequest request, Context context, Scope scope) {
this.request = request;
this.context = context;
this.scope = scope;
}

@Nullable
public static AdviceScope start(Connection connection, String methodName) {
DbRequest request =
DbRequest.createTransaction(connection, methodName.toUpperCase(Locale.ROOT));
if (request == null) {
return null;
}
Context parentContext = currentContext();
if (!transactionInstrumenter().shouldStart(parentContext, request)) {
return null;
}

Context context = transactionInstrumenter().start(parentContext, request);
return new AdviceScope(request, context, context.makeCurrent());
}

context = transactionInstrumenter().start(parentContext, request);
scope = context.makeCurrent();
public void end(@Nullable Throwable throwable) {
scope.close();
transactionInstrumenter().end(context, request, null, throwable);
}
}

@Advice.OnMethodEnter(suppress = Throwable.class)
public static AdviceScope onEnter(
@Advice.This Connection connection, @Advice.Origin("#m") String methodName) {
return AdviceScope.start(connection, methodName);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelRequest") DbRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (scope == null) {
return;
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable AdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(throwable);
}
scope.close();
transactionInstrumenter().end(context, request, null, throwable);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/

package io.opentelemetry.javaagent.instrumentation.jdbc;

import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext;
import static io.opentelemetry.javaagent.instrumentation.jdbc.JdbcSingletons.statementInstrumenter;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcData;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.Map;
import java.util.function.Supplier;
import javax.annotation.Nullable;

public class JdbcAdviceScope {
private final CallDepth callDepth;
private final DbRequest request;
private final Context context;
private final Scope scope;

private JdbcAdviceScope(CallDepth callDepth, DbRequest request, Context context, Scope scope) {
this.callDepth = callDepth;
this.request = request;
this.context = context;
this.scope = scope;
}

public static JdbcAdviceScope startBatch(CallDepth callDepth, Statement statement) {
return start(callDepth, () -> createBatchRequest(statement));
}

public static JdbcAdviceScope startStatement(
CallDepth callDepth, String sql, Statement statement) {
return start(callDepth, () -> DbRequest.create(statement, sql));
}

public static JdbcAdviceScope startPreparedStatement(
CallDepth callDepth, PreparedStatement preparedStatement) {
return start(
callDepth,
() -> DbRequest.create(preparedStatement, JdbcData.getParameters(preparedStatement)));
}

private static JdbcAdviceScope start(CallDepth callDepth, Supplier<DbRequest> requestSupplier) {
// Connection#getMetaData() may execute a Statement or PreparedStatement to retrieve DB info
// this happens before the DB CLIENT span is started (and put in the current context), so this
// instrumentation runs again and the shouldStartSpan() check always returns true - and so on
// until we get a StackOverflowError
// using CallDepth prevents this, because this check happens before Connection#getMetadata()
// is called - the first recursive Statement call is just skipped and we do not create a span
// for it
if (callDepth.getAndIncrement() > 0) {
return new JdbcAdviceScope(callDepth, null, null, null);
}

Context parentContext = currentContext();
DbRequest request = requestSupplier.get();
if (request == null || !statementInstrumenter().shouldStart(parentContext, request)) {
return new JdbcAdviceScope(callDepth, null, null, null);
}

Context context = statementInstrumenter().start(parentContext, request);
return new JdbcAdviceScope(callDepth, request, context, context.makeCurrent());
}

private static DbRequest createBatchRequest(Statement statement) {
if (statement instanceof PreparedStatement) {
String sql = JdbcData.preparedStatement.get((PreparedStatement) statement);
if (sql == null) {
return null;
}
Long batchSize = JdbcData.getPreparedStatementBatchSize((PreparedStatement) statement);
Map<String, String> parameters = JdbcData.getParameters((PreparedStatement) statement);
return DbRequest.create(statement, sql, batchSize, parameters);
} else {
JdbcData.StatementBatchInfo batchInfo = JdbcData.getStatementBatchInfo(statement);
if (batchInfo == null) {
return DbRequest.create(statement, null);
} else {
return DbRequest.create(statement, batchInfo.getStatements(), batchInfo.getBatchSize());
}
}
}

public void end(@Nullable Throwable throwable) {
if (callDepth.decrementAndGet() > 0) {
return;
}
if (scope == null) {
return;
}
scope.close();
statementInstrumenter().end(context, request, null, throwable);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.google.auto.service.AutoService;
import io.opentelemetry.javaagent.extension.instrumentation.InstrumentationModule;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
import io.opentelemetry.javaagent.extension.instrumentation.internal.ExperimentalInstrumentationModule;
import java.util.List;

@AutoService(InstrumentationModule.class)
public class JdbcInstrumentationModule extends InstrumentationModule {
public class JdbcInstrumentationModule extends InstrumentationModule
implements ExperimentalInstrumentationModule {
public JdbcInstrumentationModule() {
super("jdbc");
}
Expand All @@ -26,4 +28,9 @@ public List<TypeInstrumentation> typeInstrumentations() {
new PreparedStatementInstrumentation(),
new StatementInstrumentation());
}

@Override
public boolean isIndyReady() {
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@

package io.opentelemetry.javaagent.instrumentation.jdbc;

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.CAPTURE_QUERY_PARAMETERS;
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 All @@ -19,9 +17,6 @@
import static net.bytebuddy.matcher.ElementMatchers.takesArguments;
import static net.bytebuddy.matcher.ElementMatchers.takesNoArguments;

import io.opentelemetry.context.Context;
import io.opentelemetry.context.Scope;
import io.opentelemetry.instrumentation.jdbc.internal.DbRequest;
import io.opentelemetry.instrumentation.jdbc.internal.JdbcData;
import io.opentelemetry.javaagent.bootstrap.CallDepth;
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation;
Expand All @@ -34,7 +29,7 @@
import java.sql.Time;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.Map;
import javax.annotation.Nullable;
import net.bytebuddy.asm.Advice;
import net.bytebuddy.description.type.TypeDescription;
import net.bytebuddy.matcher.ElementMatcher;
Expand Down Expand Up @@ -105,57 +100,24 @@ public void transform(TypeTransformer transformer) {
@SuppressWarnings("unused")
public static class PreparedStatementAdvice {

@Nullable
@Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(
@Advice.This PreparedStatement statement,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelRequest") DbRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
public static JdbcAdviceScope onEnter(@Advice.This PreparedStatement statement) {
// skip prepared statements without attached sql, probably a wrapper around the actual
// prepared statement
if (JdbcData.preparedStatement.get(statement) == null) {
return;
}

// Connection#getMetaData() may execute a Statement or PreparedStatement to retrieve DB info
// this happens before the DB CLIENT span is started (and put in the current context), so this
// instrumentation runs again and the shouldStartSpan() check always returns true - and so on
// until we get a StackOverflowError
// using CallDepth prevents this, because this check happens before Connection#getMetadata()
// is called - the first recursive Statement call is just skipped and we do not create a span
// for it
callDepth = CallDepth.forClass(Statement.class);
if (callDepth.getAndIncrement() > 0) {
return;
}

Context parentContext = currentContext();
Map<String, String> parameters = JdbcData.getParameters(statement);
request = DbRequest.create(statement, parameters);

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

context = statementInstrumenter().start(parentContext, request);
scope = context.makeCurrent();
return JdbcAdviceScope.startPreparedStatement(CallDepth.forClass(Statement.class), statement);
}

@Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class)
public static void stopSpan(
@Advice.Thrown Throwable throwable,
@Advice.Local("otelCallDepth") CallDepth callDepth,
@Advice.Local("otelRequest") DbRequest request,
@Advice.Local("otelContext") Context context,
@Advice.Local("otelScope") Scope scope) {
if (callDepth == null || callDepth.decrementAndGet() > 0) {
return;
}

if (scope != null) {
scope.close();
statementInstrumenter().end(context, request, null, throwable);
@Advice.Thrown @Nullable Throwable throwable,
@Advice.Enter @Nullable JdbcAdviceScope adviceScope) {
if (adviceScope != null) {
adviceScope.end(throwable);
}
}
}
Expand Down
Loading