29
29
import com .tomtom .james .common .api .informationpoint .InformationPoint ;
30
30
import com .tomtom .james .common .api .script .RuntimeInformationPointParameter ;
31
31
import com .tomtom .james .common .log .Logger ;
32
- import com .tomtom .james .newagent .MethodExecutionContextHelper ;
33
32
import org .apache .logging .log4j .util .StackLocatorUtil ;
34
33
35
34
/*
@@ -44,7 +43,8 @@ private ContextAwareAdvice() {
44
43
}
45
44
46
45
@ SuppressWarnings ("unused" )
47
- public static void onEnter (String originTypeName ,
46
+ public static void onEnter (ExecutionContext context ,
47
+ String originTypeName ,
48
48
String originMethodName ,
49
49
Method origin ,
50
50
Object instance ,
@@ -68,18 +68,15 @@ public static void onEnter(String originTypeName,
68
68
LOG .trace (() -> "onEnter: noInitialContextSupportRequired - skipping" );
69
69
return ;
70
70
}
71
-
72
- final String key = MethodExecutionContextHelper .createContextKey (ip );
73
71
LOG .trace (() -> "Initializing custom context setup for the call" );
74
72
final Object callContext = ScriptEngineSupplier .get ().invokePrepareContext (
75
73
ip ,
76
74
origin ,
77
75
createParameterList (origin , arguments ),
78
76
instance ,
79
77
Thread .currentThread (),
80
- key );
81
-
82
- MethodExecutionContextHelper .storeContextAsync (key , callContext );
78
+ context .toString ());
79
+ context .setInitialContext (callContext );
83
80
84
81
} catch (Throwable t ) {
85
82
LOG .error ("Error executing onEnter advice" , t );
@@ -91,7 +88,7 @@ public static void onEnter(String originTypeName,
91
88
92
89
@ SuppressWarnings ("unused" )
93
90
public static void onExit (
94
- long _startTime ,
91
+ ExecutionContext context ,
95
92
String informationPointClassName ,
96
93
String informationPointMethodName ,
97
94
Method origin ,
@@ -100,8 +97,7 @@ public static void onExit(
100
97
Object returned ,
101
98
Throwable thrown ) {
102
99
Instant eventTime = Instant .now ();
103
- Duration executionTime = Duration .ofNanos (System .nanoTime () - _startTime );
104
- AutoCloseable closeable = null ;
100
+ Duration executionTime = context .getElapsedTime ();
105
101
106
102
try {
107
103
Optional <InformationPoint > optionalInformationPoint = InformationPointServiceSupplier .get ()
@@ -130,20 +126,11 @@ public static void onExit(
130
126
+ ", thrown=" + thrown
131
127
+ "]" );
132
128
133
- boolean requireInitialContextCleanup = ip .getRequiresInitialContext ();
134
- if (requireInitialContextCleanup ) {
135
- closeable = () -> MethodExecutionContextHelper .removeContextKey (ip );
136
- }
137
-
138
129
if ((sampleRate < 100 ) && (sampleRate < ThreadLocalRandom .current ().nextDouble () * 100 )) {
139
130
LOG .trace (() -> "onExit: Sample skipped (sampleRate=" + sampleRate + ")" );
140
131
return ;
141
132
}
142
133
143
- final CompletableFuture <Object > initialContextAsyncProvider = requireInitialContextCleanup
144
- ? MethodExecutionContextHelper .getContextAsync (MethodExecutionContextHelper .getKeyForCurrentFrame (ip ))
145
- : CompletableFuture .completedFuture (null );
146
-
147
134
final String [] callStack = ip .getRequiresCallStack () ? getCallStack () : EMPTY_CALL_STACK ;
148
135
if (thrown == null ) {
149
136
if (executionTime .toMillis () < successExecutionThreshold ) {
@@ -161,7 +148,7 @@ public static void onExit(
161
148
executionTime ,
162
149
callStack ,
163
150
returned ,
164
- initialContextAsyncProvider
151
+ CompletableFuture . completedFuture ( context . getInitialContext ())
165
152
);
166
153
} else {
167
154
LOG .trace (() -> "onExit: Invoking error handler" );
@@ -175,20 +162,13 @@ public static void onExit(
175
162
executionTime ,
176
163
callStack ,
177
164
thrown ,
178
- initialContextAsyncProvider
165
+ CompletableFuture . completedFuture ( context . getInitialContext ())
179
166
);
180
167
}
181
168
} catch (Throwable t ) {
182
169
LOG .error ("Error executing onExit advice" , t );
183
170
throw t ;
184
171
} finally {
185
- try {
186
- if (closeable != null ) {
187
- closeable .close ();
188
- }
189
- } catch (Exception e ) {
190
- LOG .error ("Error executing onExit advice (finally)" , e );
191
- }
192
172
LOG .trace ("onExit: END" );
193
173
}
194
174
}
@@ -228,12 +208,6 @@ public static List<RuntimeInformationPointParameter> createParameterList(Method
228
208
return result ;
229
209
}
230
210
231
- @ SuppressWarnings ("unused" )
232
- public static void onEnter (String originTypeName ,
233
- String originMethodName ) {
234
- onEnter (originTypeName , originMethodName , null , null , null );
235
- }
236
-
237
211
238
212
@ SuppressWarnings ("unused" )
239
213
public static void onExit (
0 commit comments