@@ -69,7 +69,7 @@ public static void onEnter(String originTypeName,
69
69
return ;
70
70
}
71
71
72
- final String key = MethodExecutionContextHelper .createContextKey ();
72
+ final String key = MethodExecutionContextHelper .createContextKey (ip );
73
73
LOG .trace (() -> "Initializing custom context setup for the call" );
74
74
final Object callContext = ScriptEngineSupplier .get ().invokePrepareContext (
75
75
ip ,
@@ -101,7 +101,7 @@ public static void onExit(
101
101
Throwable thrown ) {
102
102
Instant eventTime = Instant .now ();
103
103
Duration executionTime = Duration .ofNanos (System .nanoTime () - _startTime );
104
- boolean requireInitialContextCleanup = false ;
104
+ AutoCloseable closeable = null ;
105
105
106
106
try {
107
107
Optional <InformationPoint > optionalInformationPoint = InformationPointServiceSupplier .get ()
@@ -110,46 +110,49 @@ public static void onExit(
110
110
LOG .trace (() -> "onExit: skipping because information point is gone" );
111
111
return ;
112
112
}
113
- final InformationPoint informationPoint = optionalInformationPoint .get ();
114
- final long successExecutionThreshold = informationPoint .getSuccessExecutionThreshold ();
115
- final double sampleRate = getSampleRate (thrown , informationPoint );
113
+ final InformationPoint ip = optionalInformationPoint .get ();
114
+ final long successExecutionThreshold = ip .getSuccessExecutionThreshold ();
115
+ final double sampleRate = getSampleRate (thrown , ip );
116
116
117
117
LOG .trace (() -> "onExit: START "
118
118
+ "[origin=" + origin
119
119
+ ", informationPointClassName=" + informationPointClassName
120
120
+ ", informationPointMethodName=" + informationPointMethodName
121
- + ", baseScript=" + (informationPoint .getBaseScript ().isPresent ())
122
- + ", script=" + (informationPoint .getScript ().isPresent ())
123
- + ", sampleRate=" + informationPoint .getSampleRate ()
124
- + ", successSampleRate=" + informationPoint .getSuccessSampleRate ()
125
- + ", errorSampleRate=" + informationPoint .getErrorSampleRate ()
121
+ + ", baseScript=" + (ip .getBaseScript ().isPresent ())
122
+ + ", script=" + (ip .getScript ().isPresent ())
123
+ + ", sampleRate=" + ip .getSampleRate ()
124
+ + ", successSampleRate=" + ip .getSuccessSampleRate ()
125
+ + ", errorSampleRate=" + ip .getErrorSampleRate ()
126
126
+ ", successExecutionThreshold=" + successExecutionThreshold
127
127
+ ", instance=" + instance
128
128
+ ", arguments=" + Arrays .asList (arguments )
129
129
+ ", returned=" + returned
130
130
+ ", thrown=" + thrown
131
131
+ "]" );
132
132
133
- requireInitialContextCleanup = informationPoint .getRequiresInitialContext ();
133
+ boolean requireInitialContextCleanup = ip .getRequiresInitialContext ();
134
+ if (requireInitialContextCleanup ) {
135
+ closeable = () -> MethodExecutionContextHelper .removeContextKey (ip );
136
+ }
134
137
135
138
if ((sampleRate < 100 ) && (sampleRate < ThreadLocalRandom .current ().nextDouble () * 100 )) {
136
139
LOG .trace (() -> "onExit: Sample skipped (sampleRate=" + sampleRate + ")" );
137
140
return ;
138
141
}
139
142
140
143
final CompletableFuture <Object > initialContextAsyncProvider = requireInitialContextCleanup
141
- ? MethodExecutionContextHelper .getContextAsync (MethodExecutionContextHelper .getKeyForCurrentFrame ())
144
+ ? MethodExecutionContextHelper .getContextAsync (MethodExecutionContextHelper .getKeyForCurrentFrame (ip ))
142
145
: CompletableFuture .completedFuture (null );
143
146
144
- final String [] callStack = informationPoint .getRequiresCallStack () ? getCallStack () : EMPTY_CALL_STACK ;
147
+ final String [] callStack = ip .getRequiresCallStack () ? getCallStack () : EMPTY_CALL_STACK ;
145
148
if (thrown == null ) {
146
149
if (executionTime .toMillis () < successExecutionThreshold ) {
147
150
LOG .trace (() -> "onExit: ExecutionTime skipped (executionTime=" + executionTime .toMillis () + ")" );
148
151
return ;
149
152
}
150
153
LOG .trace (() -> "onExit: Invoking success handler" );
151
154
ScriptEngineSupplier .get ().invokeSuccessHandler (
152
- informationPoint ,
155
+ ip ,
153
156
origin ,
154
157
createParameterList (origin , arguments ),
155
158
instance ,
@@ -163,7 +166,7 @@ public static void onExit(
163
166
} else {
164
167
LOG .trace (() -> "onExit: Invoking error handler" );
165
168
ScriptEngineSupplier .get ().invokeErrorHandler (
166
- informationPoint ,
169
+ ip ,
167
170
origin ,
168
171
createParameterList (origin , arguments ),
169
172
instance ,
@@ -179,8 +182,12 @@ public static void onExit(
179
182
LOG .error ("Error executing onExit advice" , t );
180
183
throw t ;
181
184
} finally {
182
- if (requireInitialContextCleanup ) {
183
- MethodExecutionContextHelper .removeContextKey ();
185
+ try {
186
+ if (closeable != null ) {
187
+ closeable .close ();
188
+ }
189
+ } catch (Exception e ) {
190
+ LOG .error ("Error executing onExit advice (finally)" , e );
184
191
}
185
192
LOG .trace ("onExit: END" );
186
193
}
0 commit comments