@@ -86,12 +86,14 @@ public object Around(
8686 if ( TracingDisabled ( ) )
8787 return target ( args ) ;
8888
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 )
9395 : ( trigger . SegmentName , trigger . SegmentName ) ;
94-
96+
9597 BeginSegment ( segmentName , @namespace ) ;
9698
9799 try
@@ -100,68 +102,29 @@ public object Around(
100102
101103 if ( result is Task task )
102104 {
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 ) ;
124108
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- {
141109 _xRayRecorder . EndSubsegment ( ) ;
110+ return task ;
142111 }
143- }
144112
145- // Start the wrapper and return original task
146- _ = AsyncWrapper ( ) ;
147- return task ;
148- }
113+ HandleResponse ( metadataName , result , trigger . CaptureMode , @namespace ) ;
149114
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 ;
156117 }
157118 catch ( Exception ex )
158119 {
159- HandleException ( ex , name , captureMode , @namespace ) ;
120+ HandleException ( ex , metadataName , trigger . CaptureMode , @namespace ) ;
121+ _xRayRecorder . EndSubsegment ( ) ;
160122 throw ;
161123 }
162124 finally
163125 {
164- _xRayRecorder . EndSubsegment ( ) ;
126+ if ( _isAnnotationsCaptured )
127+ _captureAnnotations = true ;
165128 }
166129 }
167130
@@ -184,15 +147,16 @@ private void BeginSegment(string segmentName, string @namespace)
184147 _isColdStart = false ;
185148 }
186149
187- private void HandleResponse ( string segmentName , object result , TracingCaptureMode captureMode , string @namespace )
150+ private void HandleResponse ( string name , object result , TracingCaptureMode captureMode , string @namespace )
188151 {
189152 if ( ! CaptureResponse ( captureMode ) ) return ;
153+
190154#if NET8_0_OR_GREATER
191155 if ( ! RuntimeFeatureWrapper . IsDynamicCodeSupported ) // is AOT
192156 {
193157 _xRayRecorder . AddMetadata (
194158 @namespace ,
195- $ "{ segmentName } response",
159+ $ "{ name } response",
196160 Serializers . PowertoolsTracingSerializer . Serialize ( result )
197161 ) ;
198162 return ;
@@ -201,22 +165,34 @@ private void HandleResponse(string segmentName, object result, TracingCaptureMod
201165
202166 _xRayRecorder . AddMetadata (
203167 @namespace ,
204- $ "{ segmentName } response",
168+ $ "{ name } response",
205169 result
206170 ) ;
207171 }
208172
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 )
214174 {
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+ }
217190
218- if ( _isAnnotationsCaptured )
219- _captureAnnotations = true ;
191+ _xRayRecorder . AddMetadata (
192+ @namespace ,
193+ $ "{ name } error",
194+ sb . ToString ( )
195+ ) ;
220196 }
221197
222198 private bool TracingDisabled ( )
@@ -258,35 +234,6 @@ private bool CaptureError(TracingCaptureMode captureMode)
258234 } ;
259235 }
260236
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>
290237 internal static void ResetForTest ( )
291238 {
292239 _isColdStart = true ;
0 commit comments