Skip to content

Commit

Permalink
NH-82170: unroll buildMatcher method in TraceContextInjector due …
Browse files Browse the repository at this point in the history
…to muzzle mismatch failure in certain applications.
  • Loading branch information
cleverchuk committed Jun 13, 2024
1 parent b2e653b commit 1ddb110
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,8 +45,23 @@ public ElementMatcher<ClassLoader> classLoaderOptimization() {
public ElementMatcher<TypeDescription> typeMatcher() {
Boolean sqlTagPrepared =
ConfigManager.getConfigOptional(ConfigProperty.AGENT_SQL_TAG_PREPARED, false);

if (sqlTagPrepared) {
ElementMatcher.Junction<TypeDescription> matcher = buildMatcher();
// Duplicating lines 51 - 62 across TypeInstrumentation impls due runtime muzzle mismatch
// as a result of missing `net.bytebuddy.matcher.*` classes in consolidated method.
ElementMatcher.Junction<TypeDescription> 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")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -45,12 +45,26 @@ public ElementMatcher<ClassLoader> classLoaderOptimization() {
public ElementMatcher<TypeDescription> typeMatcher() {
Boolean sqlTagPrepared =
ConfigManager.getConfigOptional(ConfigProperty.AGENT_SQL_TAG_PREPARED, false);

if (sqlTagPrepared) {
ElementMatcher.Junction<TypeDescription> matcher = buildMatcher();
ElementMatcher.Junction<TypeDescription> 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();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -49,8 +49,21 @@ public ElementMatcher<ClassLoader> classLoaderOptimization() {
@Override
public ElementMatcher<TypeDescription> typeMatcher() {
Boolean sqlTag = ConfigManager.getConfigOptional(ConfigProperty.AGENT_SQL_TAG, false);

if (sqlTag) {
ElementMatcher.Junction<TypeDescription> matcher = buildMatcher();
ElementMatcher.Junction<TypeDescription> 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")));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,13 @@

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;
import io.opentelemetry.api.trace.SpanContext;
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 {

Expand Down Expand Up @@ -63,23 +59,6 @@ public static boolean isDbConfigured(Db db) {
return configuredDbs.contains(db.name());
}

public static ElementMatcher.Junction<TypeDescription> buildMatcher() {
ElementMatcher.Junction<TypeDescription> 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
Expand Down

0 comments on commit 1ddb110

Please sign in to comment.