1
1
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2
2
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
3
4
+ using System . Runtime . CompilerServices ;
4
5
using System . Threading ;
5
6
using System . Threading . Tasks ;
6
7
@@ -10,8 +11,16 @@ internal static class InvocationPipeline
10
11
{
11
12
internal static async Task < int > InvokeAsync ( ParseResult parseResult , CancellationToken cancellationToken )
12
13
{
14
+ using var invokeActivity = Activities . ActivitySource . StartActivity ( DiagnosticsStrings . InvokeMethod ) ;
15
+ if ( invokeActivity is not null )
16
+ {
17
+ invokeActivity . DisplayName = parseResult . CommandResult . FullCommandName ( ) ;
18
+ invokeActivity . AddTag ( DiagnosticsStrings . Command , parseResult . CommandResult . Command . Name ) ;
19
+ }
20
+
13
21
if ( parseResult . Action is null )
14
22
{
23
+ invokeActivity ? . SetStatus ( Diagnostics . ActivityStatusCode . Error ) ;
15
24
return ReturnCodeForMissingAction ( parseResult ) ;
16
25
}
17
26
@@ -41,7 +50,9 @@ internal static async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
41
50
switch ( parseResult . Action )
42
51
{
43
52
case SynchronousCommandLineAction syncAction :
44
- return syncAction . Invoke ( parseResult ) ;
53
+ var syncResult = syncAction . Invoke ( parseResult ) ;
54
+ invokeActivity ? . SetExitCode ( syncResult ) ;
55
+ return syncResult ;
45
56
46
57
case AsynchronousCommandLineAction asyncAction :
47
58
var startedInvocation = asyncAction . InvokeAsync ( parseResult , cts . Token ) ;
@@ -52,23 +63,30 @@ internal static async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
52
63
53
64
if ( terminationHandler is null )
54
65
{
55
- return await startedInvocation ;
66
+ var asyncResult = await startedInvocation ;
67
+ invokeActivity ? . SetExitCode ( asyncResult ) ;
68
+ return asyncResult ;
56
69
}
57
70
else
58
71
{
59
72
// Handlers may not implement cancellation.
60
73
// In such cases, when CancelOnProcessTermination is configured and user presses Ctrl+C,
61
74
// ProcessTerminationCompletionSource completes first, with the result equal to native exit code for given signal.
62
75
Task < int > firstCompletedTask = await Task . WhenAny ( startedInvocation , terminationHandler . ProcessTerminationCompletionSource . Task ) ;
63
- return await firstCompletedTask ; // return the result or propagate the exception
76
+ var asyncResult = await firstCompletedTask ; // return the result or propagate the exception
77
+ invokeActivity ? . SetExitCode ( asyncResult ) ;
78
+ return asyncResult ;
64
79
}
65
80
66
81
default :
67
- throw new ArgumentOutOfRangeException ( nameof ( parseResult . Action ) ) ;
82
+ var error = new ArgumentOutOfRangeException ( nameof ( parseResult . Action ) ) ;
83
+ invokeActivity ? . Error ( error ) ;
84
+ throw error ;
68
85
}
69
86
}
70
87
catch ( Exception ex ) when ( parseResult . Configuration . EnableDefaultExceptionHandler )
71
88
{
89
+ invokeActivity ? . Error ( ex ) ;
72
90
return DefaultExceptionHandler ( ex , parseResult . Configuration ) ;
73
91
}
74
92
finally
@@ -79,9 +97,17 @@ internal static async Task<int> InvokeAsync(ParseResult parseResult, Cancellatio
79
97
80
98
internal static int Invoke ( ParseResult parseResult )
81
99
{
100
+ using var invokeActivity = Activities . ActivitySource . StartActivity ( DiagnosticsStrings . InvokeMethod ) ;
101
+ if ( invokeActivity is not null )
102
+ {
103
+ invokeActivity . DisplayName = parseResult . CommandResult . FullCommandName ( ) ;
104
+ invokeActivity . AddTag ( DiagnosticsStrings . Command , parseResult . CommandResult . Command . Name ) ;
105
+ }
106
+
82
107
switch ( parseResult . Action )
83
108
{
84
109
case null :
110
+ invokeActivity ? . Error ( ) ;
85
111
return ReturnCodeForMissingAction ( parseResult ) ;
86
112
87
113
case SynchronousCommandLineAction syncAction :
@@ -112,15 +138,20 @@ internal static int Invoke(ParseResult parseResult)
112
138
}
113
139
}
114
140
115
- return syncAction . Invoke ( parseResult ) ;
141
+ var result = syncAction . Invoke ( parseResult ) ;
142
+ invokeActivity ? . SetExitCode ( result ) ;
143
+ return result ;
116
144
}
117
145
catch ( Exception ex ) when ( parseResult . Configuration . EnableDefaultExceptionHandler )
118
146
{
147
+ invokeActivity ? . Error ( ex ) ;
119
148
return DefaultExceptionHandler ( ex , parseResult . Configuration ) ;
120
149
}
121
150
122
151
default :
123
- throw new InvalidOperationException ( $ "{ nameof ( AsynchronousCommandLineAction ) } called within non-async invocation.") ;
152
+ var error = new InvalidOperationException ( $ "{ nameof ( AsynchronousCommandLineAction ) } called within non-async invocation.") ;
153
+ invokeActivity ? . Error ( error ) ;
154
+ throw error ;
124
155
}
125
156
}
126
157
@@ -150,5 +181,38 @@ private static int ReturnCodeForMissingAction(ParseResult parseResult)
150
181
return 0 ;
151
182
}
152
183
}
184
+
185
+ private static void Succeed ( this Diagnostics . Activity activity )
186
+ {
187
+ activity . SetStatus ( Diagnostics . ActivityStatusCode . Ok ) ;
188
+ activity . AddTag ( DiagnosticsStrings . ExitCode , 0 ) ;
189
+ }
190
+ private static void Error ( this Diagnostics . Activity activity , int statusCode )
191
+ {
192
+ activity . SetStatus ( Diagnostics . ActivityStatusCode . Error ) ;
193
+ activity . AddTag ( DiagnosticsStrings . ExitCode , statusCode ) ;
194
+ }
195
+
196
+ private static void Error ( this Diagnostics . Activity activity , Exception ? exception = null )
197
+ {
198
+ activity . SetStatus ( Diagnostics . ActivityStatusCode . Error ) ;
199
+ activity . AddTag ( DiagnosticsStrings . ExitCode , 1 ) ;
200
+ if ( exception is not null )
201
+ {
202
+ activity . AddBaggage ( DiagnosticsStrings . Exception , exception . ToString ( ) ) ;
203
+ }
204
+ }
205
+
206
+ private static void SetExitCode ( this Diagnostics . Activity activity , int exitCode )
207
+ {
208
+ if ( exitCode == 0 )
209
+ {
210
+ activity . Succeed ( ) ;
211
+ }
212
+ else
213
+ {
214
+ activity . Error ( exitCode ) ;
215
+ }
216
+ }
153
217
}
154
218
}
0 commit comments