20
20
import io .opentelemetry .javaagent .tooling .config .AgentConfig ;
21
21
import io .opentelemetry .javaagent .tooling .field .VirtualFieldImplementationInstaller ;
22
22
import io .opentelemetry .javaagent .tooling .field .VirtualFieldImplementationInstallerFactory ;
23
+ import io .opentelemetry .javaagent .tooling .instrumentation .indy .IndyModuleRegistry ;
24
+ import io .opentelemetry .javaagent .tooling .instrumentation .indy .IndyTypeTransformerImpl ;
25
+ import io .opentelemetry .javaagent .tooling .instrumentation .indy .PatchByteCodeVersionTransformer ;
23
26
import io .opentelemetry .javaagent .tooling .muzzle .HelperResourceBuilderImpl ;
24
27
import io .opentelemetry .javaagent .tooling .muzzle .InstrumentationModuleMuzzle ;
25
28
import io .opentelemetry .javaagent .tooling .util .IgnoreFailedTypeMatcher ;
@@ -62,6 +65,52 @@ AgentBuilder install(
62
65
FINE , "Instrumentation {0} is disabled" , instrumentationModule .instrumentationName ());
63
66
return parentAgentBuilder ;
64
67
}
68
+
69
+ if (instrumentationModule .isIndyModule ()) {
70
+ return installIndyModule (instrumentationModule , parentAgentBuilder );
71
+ } else {
72
+ return installInjectingModule (instrumentationModule , parentAgentBuilder , config );
73
+ }
74
+ }
75
+
76
+ private AgentBuilder installIndyModule (
77
+ InstrumentationModule instrumentationModule , AgentBuilder parentAgentBuilder ) {
78
+
79
+ IndyModuleRegistry .registerIndyModule (instrumentationModule );
80
+
81
+ // TODO (Jonas): Adapt MuzzleMatcher to use the same type lookup strategy as the
82
+ // InstrumentationModuleClassLoader
83
+ // MuzzleMatcher muzzleMatcher = new MuzzleMatcher(logger, instrumentationModule, config);
84
+ VirtualFieldImplementationInstaller contextProvider =
85
+ virtualFieldInstallerFactory .create (instrumentationModule );
86
+
87
+ AgentBuilder agentBuilder = parentAgentBuilder ;
88
+ for (TypeInstrumentation typeInstrumentation : instrumentationModule .typeInstrumentations ()) {
89
+ AgentBuilder .Identified .Extendable extendableAgentBuilder =
90
+ setTypeMatcher (agentBuilder , instrumentationModule , typeInstrumentation )
91
+ .transform (new PatchByteCodeVersionTransformer ());
92
+
93
+ IndyTypeTransformerImpl typeTransformer =
94
+ new IndyTypeTransformerImpl (extendableAgentBuilder , instrumentationModule );
95
+ typeInstrumentation .transform (typeTransformer );
96
+ extendableAgentBuilder = typeTransformer .getAgentBuilder ();
97
+ // TODO (Jonas): make instrumentation of bytecode older than 1.4 opt-in via a config option
98
+ // TODO (Jonas): we are not calling
99
+ // contextProvider.rewriteVirtualFieldsCalls(extendableAgentBuilder) anymore
100
+ // As a result the advices should store `VirtualFields` as static variables instead of having
101
+ // the lookup inline
102
+ // We need to update our documentation on that
103
+ extendableAgentBuilder = contextProvider .injectFields (extendableAgentBuilder );
104
+
105
+ agentBuilder = extendableAgentBuilder ;
106
+ }
107
+ return agentBuilder ;
108
+ }
109
+
110
+ private AgentBuilder installInjectingModule (
111
+ InstrumentationModule instrumentationModule ,
112
+ AgentBuilder parentAgentBuilder ,
113
+ ConfigProperties config ) {
65
114
List <String > helperClassNames =
66
115
InstrumentationModuleMuzzle .getHelperClassNames (instrumentationModule );
67
116
HelperResourceBuilderImpl helperResourceBuilder = new HelperResourceBuilderImpl ();
@@ -78,8 +127,6 @@ AgentBuilder install(
78
127
return parentAgentBuilder ;
79
128
}
80
129
81
- ElementMatcher .Junction <ClassLoader > moduleClassLoaderMatcher =
82
- instrumentationModule .classLoaderMatcher ();
83
130
MuzzleMatcher muzzleMatcher = new MuzzleMatcher (logger , instrumentationModule , config );
84
131
AgentBuilder .Transformer helperInjector =
85
132
new HelperInjector (
@@ -93,32 +140,9 @@ AgentBuilder install(
93
140
94
141
AgentBuilder agentBuilder = parentAgentBuilder ;
95
142
for (TypeInstrumentation typeInstrumentation : typeInstrumentations ) {
96
- ElementMatcher <TypeDescription > typeMatcher =
97
- new NamedMatcher <>(
98
- instrumentationModule .getClass ().getSimpleName ()
99
- + "#"
100
- + typeInstrumentation .getClass ().getSimpleName (),
101
- new IgnoreFailedTypeMatcher (typeInstrumentation .typeMatcher ()));
102
- ElementMatcher <ClassLoader > classLoaderMatcher =
103
- new NamedMatcher <>(
104
- instrumentationModule .getClass ().getSimpleName ()
105
- + "#"
106
- + typeInstrumentation .getClass ().getSimpleName (),
107
- moduleClassLoaderMatcher .and (typeInstrumentation .classLoaderOptimization ()));
108
143
109
144
AgentBuilder .Identified .Extendable extendableAgentBuilder =
110
- agentBuilder
111
- .type (
112
- new LoggingFailSafeMatcher <>(
113
- typeMatcher ,
114
- "Instrumentation type matcher unexpected exception: " + typeMatcher ),
115
- new LoggingFailSafeMatcher <>(
116
- classLoaderMatcher ,
117
- "Instrumentation class loader matcher unexpected exception: "
118
- + classLoaderMatcher ))
119
- .and (
120
- (typeDescription , classLoader , module , classBeingRedefined , protectionDomain ) ->
121
- classLoader == null || NOT_DECORATOR_MATCHER .matches (typeDescription ))
145
+ setTypeMatcher (agentBuilder , instrumentationModule , typeInstrumentation )
122
146
.and (muzzleMatcher )
123
147
.transform (ConstantAdjuster .instance ())
124
148
.transform (helperInjector );
@@ -133,4 +157,37 @@ AgentBuilder install(
133
157
134
158
return agentBuilder ;
135
159
}
160
+
161
+ private static AgentBuilder .Identified .Narrowable setTypeMatcher (
162
+ AgentBuilder agentBuilder ,
163
+ InstrumentationModule instrumentationModule ,
164
+ TypeInstrumentation typeInstrumentation ) {
165
+
166
+ ElementMatcher .Junction <ClassLoader > moduleClassLoaderMatcher =
167
+ instrumentationModule .classLoaderMatcher ();
168
+
169
+ ElementMatcher <TypeDescription > typeMatcher =
170
+ new NamedMatcher <>(
171
+ instrumentationModule .getClass ().getSimpleName ()
172
+ + "#"
173
+ + typeInstrumentation .getClass ().getSimpleName (),
174
+ new IgnoreFailedTypeMatcher (typeInstrumentation .typeMatcher ()));
175
+ ElementMatcher <ClassLoader > classLoaderMatcher =
176
+ new NamedMatcher <>(
177
+ instrumentationModule .getClass ().getSimpleName ()
178
+ + "#"
179
+ + typeInstrumentation .getClass ().getSimpleName (),
180
+ moduleClassLoaderMatcher .and (typeInstrumentation .classLoaderOptimization ()));
181
+
182
+ return agentBuilder
183
+ .type (
184
+ new LoggingFailSafeMatcher <>(
185
+ typeMatcher , "Instrumentation type matcher unexpected exception: " + typeMatcher ),
186
+ new LoggingFailSafeMatcher <>(
187
+ classLoaderMatcher ,
188
+ "Instrumentation class loader matcher unexpected exception: " + classLoaderMatcher ))
189
+ .and (
190
+ (typeDescription , classLoader , module , classBeingRedefined , protectionDomain ) ->
191
+ classLoader == null || NOT_DECORATOR_MATCHER .matches (typeDescription ));
192
+ }
136
193
}
0 commit comments