diff --git a/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcConnectionInstrumentation.java b/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcConnectionInstrumentation.java index a88e76af..3fc85034 100644 --- a/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcConnectionInstrumentation.java +++ b/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcConnectionInstrumentation.java @@ -16,7 +16,7 @@ package com.solarwinds.opentelemetry.instrumentation; -import static com.solarwinds.opentelemetry.instrumentation.TraceContextInjector.buildMatcher; +import static com.solarwinds.opentelemetry.instrumentation.TraceContextInjector.isDbConfigured; 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; @@ -45,8 +45,23 @@ public ElementMatcher classLoaderOptimization() { public ElementMatcher typeMatcher() { Boolean sqlTagPrepared = ConfigManager.getConfigOptional(ConfigProperty.AGENT_SQL_TAG_PREPARED, false); + if (sqlTagPrepared) { - ElementMatcher.Junction matcher = buildMatcher(); + // Duplicating lines 51 - 62 across TypeInstrumentation impls due to runtime muzzle mismatch + // as a result of missing `net.bytebuddy.matcher.*` classes in consolidated method. + ElementMatcher.Junction matcher = null; + if (isDbConfigured(TraceContextInjector.Db.mysql)) { + matcher = nameStartsWith("com.mysql.cj.jdbc"); // only inject MySQL JDBC driver + } + + if (isDbConfigured(TraceContextInjector.Db.postgresql)) { + if (matcher != null) { + matcher = matcher.or(nameStartsWith("org.postgresql")); + } else { + matcher = nameStartsWith("org.postgresql"); + } + } + if (matcher != null) { return matcher.and(implementsInterface(named("java.sql.Connection"))); } diff --git a/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcPreparedStatementInstrumentation.java b/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcPreparedStatementInstrumentation.java index 132b436e..e5cff990 100644 --- a/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcPreparedStatementInstrumentation.java +++ b/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcPreparedStatementInstrumentation.java @@ -16,7 +16,7 @@ package com.solarwinds.opentelemetry.instrumentation; -import static com.solarwinds.opentelemetry.instrumentation.TraceContextInjector.buildMatcher; +import static com.solarwinds.opentelemetry.instrumentation.TraceContextInjector.isDbConfigured; 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; @@ -45,12 +45,26 @@ public ElementMatcher classLoaderOptimization() { public ElementMatcher typeMatcher() { Boolean sqlTagPrepared = ConfigManager.getConfigOptional(ConfigProperty.AGENT_SQL_TAG_PREPARED, false); + if (sqlTagPrepared) { - ElementMatcher.Junction matcher = buildMatcher(); + ElementMatcher.Junction matcher = null; + if (isDbConfigured(TraceContextInjector.Db.mysql)) { + matcher = nameStartsWith("com.mysql.cj.jdbc"); // only inject MySQL JDBC driver + } + + if (isDbConfigured(TraceContextInjector.Db.postgresql)) { + if (matcher != null) { + matcher = matcher.or(nameStartsWith("org.postgresql")); + } else { + matcher = nameStartsWith("org.postgresql"); + } + } + if (matcher != null) { return matcher.and(implementsInterface(named("java.sql.PreparedStatement"))); } } + return none(); } diff --git a/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcStatementInstrumentation.java b/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcStatementInstrumentation.java index 58d8d1e6..481c2139 100644 --- a/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcStatementInstrumentation.java +++ b/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/JdbcStatementInstrumentation.java @@ -16,7 +16,7 @@ package com.solarwinds.opentelemetry.instrumentation; -import static com.solarwinds.opentelemetry.instrumentation.TraceContextInjector.buildMatcher; +import static com.solarwinds.opentelemetry.instrumentation.TraceContextInjector.isDbConfigured; 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; @@ -49,8 +49,21 @@ public ElementMatcher classLoaderOptimization() { @Override public ElementMatcher typeMatcher() { Boolean sqlTag = ConfigManager.getConfigOptional(ConfigProperty.AGENT_SQL_TAG, false); + if (sqlTag) { - ElementMatcher.Junction matcher = buildMatcher(); + ElementMatcher.Junction matcher = null; + if (isDbConfigured(TraceContextInjector.Db.mysql)) { + matcher = nameStartsWith("com.mysql.cj.jdbc"); // only inject MySQL JDBC driver + } + + if (isDbConfigured(TraceContextInjector.Db.postgresql)) { + if (matcher != null) { + matcher = matcher.or(nameStartsWith("org.postgresql")); + } else { + matcher = nameStartsWith("org.postgresql"); + } + } + if (matcher != null) { return matcher.and(implementsInterface(named("java.sql.Statement"))); } diff --git a/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/TraceContextInjector.java b/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/TraceContextInjector.java index 7e65bed6..633d398d 100644 --- a/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/TraceContextInjector.java +++ b/instrumentation/jdbc/src/main/java/com/solarwinds/opentelemetry/instrumentation/TraceContextInjector.java @@ -16,8 +16,6 @@ package com.solarwinds.opentelemetry.instrumentation; -import static net.bytebuddy.matcher.ElementMatchers.nameStartsWith; - import com.solarwinds.joboe.config.ConfigManager; import com.solarwinds.joboe.config.ConfigProperty; import io.opentelemetry.api.trace.Span; @@ -25,8 +23,6 @@ import io.opentelemetry.context.Context; import java.util.HashSet; import java.util.Set; -import net.bytebuddy.description.type.TypeDescription; -import net.bytebuddy.matcher.ElementMatcher; public class TraceContextInjector { @@ -63,23 +59,6 @@ public static boolean isDbConfigured(Db db) { return configuredDbs.contains(db.name()); } - public static ElementMatcher.Junction buildMatcher() { - ElementMatcher.Junction matcher = null; - if (isDbConfigured(TraceContextInjector.Db.mysql)) { - matcher = nameStartsWith("com.mysql.cj.jdbc"); // only inject MySQL JDBC driver - } - - if (isDbConfigured(TraceContextInjector.Db.postgresql)) { - if (matcher != null) { - matcher = matcher.or(nameStartsWith("org.postgresql")); - } else { - matcher = nameStartsWith("org.postgresql"); - } - } - - return matcher; - } - public enum Db { mysql, postgresql