2525import java .util .HashMap ;
2626import java .util .List ;
2727import java .util .Map ;
28+ import java .util .Set ;
2829import net .bytebuddy .agent .builder .AgentBuilder ;
2930import net .bytebuddy .asm .Advice ;
3031import net .bytebuddy .asm .AsmVisitorWrapper ;
3334import net .bytebuddy .dynamic .DynamicType ;
3435import net .bytebuddy .matcher .ElementMatcher ;
3536import net .bytebuddy .utility .JavaModule ;
37+ import org .slf4j .Logger ;
38+ import org .slf4j .LoggerFactory ;
3639
3740/**
3841 * Builds {@link InstrumenterModule}s into a single combining-matcher and splitting-transformer.
4346public final class CombiningTransformerBuilder
4447 implements Instrumenter .TypeTransformer , Instrumenter .MethodTransformer {
4548
49+ private static final Logger log = LoggerFactory .getLogger (CombiningTransformerBuilder .class );
50+
4651 // Added here instead of byte-buddy's ignores because it's relatively
4752 // expensive. https://github.com/DataDog/dd-trace-java/pull/1045
4853 private static final ElementMatcher .Junction <TypeDescription > NOT_DECORATOR_MATCHER =
@@ -57,6 +62,7 @@ public final class CombiningTransformerBuilder
5762 private final AgentBuilder agentBuilder ;
5863 private final InstrumenterIndex instrumenterIndex ;
5964 private final int knownTransformationCount ;
65+ private final Set <InstrumenterModule .TargetSystem > enabledSystems ;
6066
6167 private final List <MatchRecorder > matchers = new ArrayList <>();
6268 private final BitSet knownTypesMask ;
@@ -80,7 +86,9 @@ public final class CombiningTransformerBuilder
8086 private final List <AgentBuilder .Transformer > advice = new ArrayList <>();
8187
8288 public CombiningTransformerBuilder (
83- AgentBuilder agentBuilder , InstrumenterIndex instrumenterIndex ) {
89+ AgentBuilder agentBuilder ,
90+ InstrumenterIndex instrumenterIndex ,
91+ Set <InstrumenterModule .TargetSystem > enabledSystems ) {
8492 this .agentBuilder = agentBuilder ;
8593 this .instrumenterIndex = instrumenterIndex ;
8694 int knownInstrumentationCount = instrumenterIndex .instrumentationCount ();
@@ -89,6 +97,7 @@ public CombiningTransformerBuilder(
8997 this .transformers = new AdviceStack [knownTransformationCount ];
9098 this .nextRuntimeInstrumentationId = knownInstrumentationCount ;
9199 this .nextRuntimeTransformationId = knownTransformationCount ;
100+ this .enabledSystems = enabledSystems ;
92101 }
93102
94103 /** Builds matchers and transformers for an instrumentation module and its members. */
@@ -239,7 +248,26 @@ public void applyAdvice(Instrumenter.TransformingAdvice typeAdvice) {
239248 }
240249
241250 @ Override
242- public void applyAdvice (ElementMatcher <? super MethodDescription > matcher , String adviceClass ) {
251+ public void applyAdvices (
252+ ElementMatcher <? super MethodDescription > matcher ,
253+ String adviceClass ,
254+ String ... additionalAdviceClasses ) {
255+ addAdviceIfEnabled (matcher , adviceClass );
256+
257+ if (additionalAdviceClasses != null ) {
258+ for (String adviceClassName : additionalAdviceClasses ) {
259+ addAdviceIfEnabled (matcher , adviceClassName );
260+ }
261+ }
262+ }
263+
264+ private void addAdviceIfEnabled (
265+ ElementMatcher <? super MethodDescription > matcher , String adviceClass ) {
266+ if (!instrumenterIndex .isAdviceEnabled (adviceClass , enabledSystems )) {
267+ log .debug ("Skipping advice class {} as it is not enabled" , adviceClass );
268+ return ;
269+ }
270+ log .debug ("Installing advice class {}" , adviceClass );
243271 Advice .WithCustomMapping customMapping = Advice .withCustomMapping ();
244272 if (postProcessor != null ) {
245273 customMapping = customMapping .with (postProcessor );
0 commit comments