|
31 | 31 | import io.opentelemetry.javaagent.tooling.util.NamedMatcher;
|
32 | 32 | import io.opentelemetry.sdk.autoconfigure.spi.ConfigProperties;
|
33 | 33 | import java.lang.instrument.Instrumentation;
|
| 34 | +import java.util.Collections; |
34 | 35 | import java.util.List;
|
35 | 36 | import net.bytebuddy.agent.builder.AgentBuilder;
|
36 | 37 | import net.bytebuddy.description.annotation.AnnotationSource;
|
@@ -69,46 +70,75 @@ AgentBuilder install(
|
69 | 70 | }
|
70 | 71 |
|
71 | 72 | if (instrumentationModule.isIndyModule()) {
|
72 |
| - return installIndyModule(instrumentationModule, parentAgentBuilder); |
| 73 | + return installIndyModule(instrumentationModule, parentAgentBuilder, config); |
73 | 74 | } else {
|
74 | 75 | return installInjectingModule(instrumentationModule, parentAgentBuilder, config);
|
75 | 76 | }
|
76 | 77 | }
|
77 | 78 |
|
78 | 79 | private AgentBuilder installIndyModule(
|
79 |
| - InstrumentationModule instrumentationModule, AgentBuilder parentAgentBuilder) { |
80 |
| - |
81 |
| - IndyModuleRegistry.registerIndyModule(instrumentationModule); |
82 |
| - |
| 80 | + InstrumentationModule instrumentationModule, |
| 81 | + AgentBuilder parentAgentBuilder, |
| 82 | + ConfigProperties config) { |
| 83 | + List<String> helperClassNames = |
| 84 | + InstrumentationModuleMuzzle.getHelperClassNames(instrumentationModule); |
83 | 85 | HelperResourceBuilderImpl helperResourceBuilder = new HelperResourceBuilderImpl();
|
84 | 86 | instrumentationModule.registerHelperResources(helperResourceBuilder);
|
| 87 | + List<TypeInstrumentation> typeInstrumentations = instrumentationModule.typeInstrumentations(); |
| 88 | + if (typeInstrumentations.isEmpty()) { |
| 89 | + if (!helperClassNames.isEmpty() || !helperResourceBuilder.getResources().isEmpty()) { |
| 90 | + logger.log( |
| 91 | + WARNING, |
| 92 | + "Helper classes and resources won't be injected if no types are instrumented: {0}", |
| 93 | + instrumentationModule.instrumentationName()); |
| 94 | + } |
| 95 | + |
| 96 | + return parentAgentBuilder; |
| 97 | + } |
| 98 | + |
| 99 | + List<String> injectedHelperClassNames = Collections.emptyList(); |
| 100 | + if (instrumentationModule instanceof ExperimentalInstrumentationModule) { |
| 101 | + ExperimentalInstrumentationModule experimentalInstrumentationModule = |
| 102 | + (ExperimentalInstrumentationModule) instrumentationModule; |
| 103 | + injectedHelperClassNames = experimentalInstrumentationModule.injectedClassNames(); |
| 104 | + } |
| 105 | + |
| 106 | + IndyModuleRegistry.registerIndyModule(instrumentationModule); |
85 | 107 |
|
86 | 108 | ClassInjectorImpl injectedClassesCollector = new ClassInjectorImpl(instrumentationModule);
|
87 | 109 | if (instrumentationModule instanceof ExperimentalInstrumentationModule) {
|
88 | 110 | ((ExperimentalInstrumentationModule) instrumentationModule)
|
89 | 111 | .injectClasses(injectedClassesCollector);
|
90 | 112 | }
|
91 | 113 |
|
| 114 | + MuzzleMatcher muzzleMatcher = new MuzzleMatcher(logger, instrumentationModule, config); |
| 115 | + |
92 | 116 | AgentBuilder.Transformer helperInjector =
|
93 | 117 | new HelperInjector(
|
94 | 118 | instrumentationModule.instrumentationName(),
|
95 |
| - injectedClassesCollector.getClassesToInject(), |
| 119 | + injectedHelperClassNames, |
96 | 120 | helperResourceBuilder.getResources(),
|
97 | 121 | instrumentationModule.getClass().getClassLoader(),
|
98 | 122 | instrumentation);
|
| 123 | + AgentBuilder.Transformer indyHelperInjector = |
| 124 | + new HelperInjector( |
| 125 | + instrumentationModule.instrumentationName(), |
| 126 | + injectedClassesCollector.getClassesToInject(), |
| 127 | + Collections.emptyList(), |
| 128 | + instrumentationModule.getClass().getClassLoader(), |
| 129 | + instrumentation); |
99 | 130 |
|
100 |
| - // TODO (Jonas): Adapt MuzzleMatcher to use the same type lookup strategy as the |
101 |
| - // InstrumentationModuleClassLoader (see IndyModuleTypePool) |
102 |
| - // MuzzleMatcher muzzleMatcher = new MuzzleMatcher(logger, instrumentationModule, config); |
103 | 131 | VirtualFieldImplementationInstaller contextProvider =
|
104 | 132 | virtualFieldInstallerFactory.create(instrumentationModule);
|
105 | 133 |
|
106 | 134 | AgentBuilder agentBuilder = parentAgentBuilder;
|
107 | 135 | for (TypeInstrumentation typeInstrumentation : instrumentationModule.typeInstrumentations()) {
|
108 | 136 | AgentBuilder.Identified.Extendable extendableAgentBuilder =
|
109 | 137 | setTypeMatcher(agentBuilder, instrumentationModule, typeInstrumentation)
|
| 138 | + .and(muzzleMatcher) |
110 | 139 | .transform(new PatchByteCodeVersionTransformer())
|
111 |
| - .transform(helperInjector); |
| 140 | + .transform(helperInjector) |
| 141 | + .transform(indyHelperInjector); |
112 | 142 |
|
113 | 143 | // TODO (Jonas): we are not calling
|
114 | 144 | // contextProvider.rewriteVirtualFieldsCalls(extendableAgentBuilder) anymore
|
|
0 commit comments