@@ -86,12 +86,14 @@ public object Around(
86
86
if ( TracingDisabled ( ) )
87
87
return target ( args ) ;
88
88
89
- var @namespace = ! string . IsNullOrWhiteSpace ( trigger . Namespace ) ? trigger . Namespace : _powertoolsConfigurations . Service ;
90
-
91
- var ( segmentName , metadataName ) = string . IsNullOrWhiteSpace ( trigger . SegmentName )
92
- ? ( $ "## { name } ", name )
89
+ var @namespace = ! string . IsNullOrWhiteSpace ( trigger . Namespace )
90
+ ? trigger . Namespace
91
+ : _powertoolsConfigurations . Service ;
92
+
93
+ var ( segmentName , metadataName ) = string . IsNullOrWhiteSpace ( trigger . SegmentName )
94
+ ? ( $ "## { name } ", name )
93
95
: ( trigger . SegmentName , trigger . SegmentName ) ;
94
-
96
+
95
97
BeginSegment ( segmentName , @namespace ) ;
96
98
97
99
try
@@ -100,68 +102,29 @@ public object Around(
100
102
101
103
if ( result is Task task )
102
104
{
103
- return WrapTask ( task , metadataName , trigger . CaptureMode , @namespace ) ;
104
- }
105
-
106
- HandleResponse ( metadataName , result , trigger . CaptureMode , @namespace ) ;
107
- _xRayRecorder . EndSubsegment ( ) ; // End segment here for sync methods
108
- return result ;
109
- }
110
- catch ( Exception ex )
111
- {
112
- HandleException ( ex , metadataName , trigger . CaptureMode , @namespace ) ;
113
- _xRayRecorder . EndSubsegment ( ) ; // End segment here for sync methods with exceptions
114
- throw ;
115
- }
116
- }
117
-
118
- private object WrapTask ( Task task , string name , TracingCaptureMode captureMode , string @namespace )
119
- {
120
- if ( task . GetType ( ) == typeof ( Task ) )
121
- {
122
- return WrapVoidTask ( task , name , captureMode , @namespace ) ;
123
- }
105
+ task . GetAwaiter ( ) . GetResult ( ) ;
106
+ var taskResult = task . GetType ( ) . GetProperty ( "Result" ) ? . GetValue ( task ) ;
107
+ HandleResponse ( metadataName , taskResult , trigger . CaptureMode , @namespace ) ;
124
108
125
- // Create an async wrapper task that returns the original task type
126
- async Task AsyncWrapper ( )
127
- {
128
- try
129
- {
130
- await task ;
131
- var result = task . GetType ( ) . GetProperty ( "Result" ) ? . GetValue ( task ) ;
132
- HandleResponse ( name , result , captureMode , @namespace ) ;
133
- }
134
- catch ( Exception ex )
135
- {
136
- HandleException ( ex , name , captureMode , @namespace ) ;
137
- throw ;
138
- }
139
- finally
140
- {
141
109
_xRayRecorder . EndSubsegment ( ) ;
110
+ return task ;
142
111
}
143
- }
144
112
145
- // Start the wrapper and return original task
146
- _ = AsyncWrapper ( ) ;
147
- return task ;
148
- }
113
+ HandleResponse ( metadataName , result , trigger . CaptureMode , @namespace ) ;
149
114
150
- private async Task WrapVoidTask ( Task task , string name , TracingCaptureMode captureMode , string @namespace )
151
- {
152
- try
153
- {
154
- await task ;
155
- HandleResponse ( name , null , captureMode , @namespace ) ;
115
+ _xRayRecorder . EndSubsegment ( ) ;
116
+ return result ;
156
117
}
157
118
catch ( Exception ex )
158
119
{
159
- HandleException ( ex , name , captureMode , @namespace ) ;
120
+ HandleException ( ex , metadataName , trigger . CaptureMode , @namespace ) ;
121
+ _xRayRecorder . EndSubsegment ( ) ;
160
122
throw ;
161
123
}
162
124
finally
163
125
{
164
- _xRayRecorder . EndSubsegment ( ) ;
126
+ if ( _isAnnotationsCaptured )
127
+ _captureAnnotations = true ;
165
128
}
166
129
}
167
130
@@ -184,15 +147,16 @@ private void BeginSegment(string segmentName, string @namespace)
184
147
_isColdStart = false ;
185
148
}
186
149
187
- private void HandleResponse ( string segmentName , object result , TracingCaptureMode captureMode , string @namespace )
150
+ private void HandleResponse ( string name , object result , TracingCaptureMode captureMode , string @namespace )
188
151
{
189
152
if ( ! CaptureResponse ( captureMode ) ) return ;
153
+
190
154
#if NET8_0_OR_GREATER
191
155
if ( ! RuntimeFeatureWrapper . IsDynamicCodeSupported ) // is AOT
192
156
{
193
157
_xRayRecorder . AddMetadata (
194
158
@namespace ,
195
- $ "{ segmentName } response",
159
+ $ "{ name } response",
196
160
Serializers . PowertoolsTracingSerializer . Serialize ( result )
197
161
) ;
198
162
return ;
@@ -201,22 +165,34 @@ private void HandleResponse(string segmentName, object result, TracingCaptureMod
201
165
202
166
_xRayRecorder . AddMetadata (
203
167
@namespace ,
204
- $ "{ segmentName } response",
168
+ $ "{ name } response",
205
169
result
206
170
) ;
207
171
}
208
172
209
- /// <summary>
210
- /// When Aspect Handler exits runs this code to end subsegment
211
- /// </summary>
212
- [ Advice ( Kind . After ) ]
213
- public void OnExit ( )
173
+ private void HandleException ( Exception exception , string name , TracingCaptureMode captureMode , string @namespace )
214
174
{
215
- if ( TracingDisabled ( ) )
216
- return ;
175
+ if ( ! CaptureError ( captureMode ) ) return ;
176
+
177
+ var sb = new StringBuilder ( ) ;
178
+ sb . AppendLine ( $ "Exception type: { exception . GetType ( ) } ") ;
179
+ sb . AppendLine ( $ "Exception message: { exception . Message } ") ;
180
+ sb . AppendLine ( $ "Stack trace: { exception . StackTrace } ") ;
181
+
182
+ if ( exception . InnerException != null )
183
+ {
184
+ sb . AppendLine ( "---BEGIN InnerException--- " ) ;
185
+ sb . AppendLine ( $ "Exception type { exception . InnerException . GetType ( ) } ") ;
186
+ sb . AppendLine ( $ "Exception message: { exception . InnerException . Message } ") ;
187
+ sb . AppendLine ( $ "Stack trace: { exception . InnerException . StackTrace } ") ;
188
+ sb . AppendLine ( "---END Inner Exception" ) ;
189
+ }
217
190
218
- if ( _isAnnotationsCaptured )
219
- _captureAnnotations = true ;
191
+ _xRayRecorder . AddMetadata (
192
+ @namespace ,
193
+ $ "{ name } error",
194
+ sb . ToString ( )
195
+ ) ;
220
196
}
221
197
222
198
private bool TracingDisabled ( )
@@ -258,35 +234,6 @@ private bool CaptureError(TracingCaptureMode captureMode)
258
234
} ;
259
235
}
260
236
261
- private void HandleException ( Exception exception , string name , TracingCaptureMode captureMode , string @namespace )
262
- {
263
- if ( ! CaptureError ( captureMode ) ) return ;
264
-
265
- var nameSpace = @namespace ;
266
- var sb = new StringBuilder ( ) ;
267
- sb . AppendLine ( $ "Exception type: { exception . GetType ( ) } ") ;
268
- sb . AppendLine ( $ "Exception message: { exception . Message } ") ;
269
- sb . AppendLine ( $ "Stack trace: { exception . StackTrace } ") ;
270
-
271
- if ( exception . InnerException != null )
272
- {
273
- sb . AppendLine ( "---BEGIN InnerException--- " ) ;
274
- sb . AppendLine ( $ "Exception type { exception . InnerException . GetType ( ) } ") ;
275
- sb . AppendLine ( $ "Exception message: { exception . InnerException . Message } ") ;
276
- sb . AppendLine ( $ "Stack trace: { exception . InnerException . StackTrace } ") ;
277
- sb . AppendLine ( "---END Inner Exception" ) ;
278
- }
279
-
280
- _xRayRecorder . AddMetadata (
281
- nameSpace ,
282
- $ "{ name } error",
283
- sb . ToString ( )
284
- ) ;
285
- }
286
-
287
- /// <summary>
288
- /// Resets static variables for test.
289
- /// </summary>
290
237
internal static void ResetForTest ( )
291
238
{
292
239
_isColdStart = true ;
0 commit comments