Skip to content

Commit 6cc650c

Browse files
committed
events for exceptions during execution instead of baggage
1 parent 0f15752 commit 6cc650c

File tree

2 files changed

+18
-4
lines changed

2 files changed

+18
-4
lines changed

src/System.CommandLine.Tests/ObservabilityTests.cs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,18 @@
55
using System.Diagnostics;
66
using System.Collections.Generic;
77
using System.Threading.Tasks;
8+
using Xunit.Abstractions;
89

910
namespace System.CommandLine.Tests
1011
{
1112
public class ObservabilityTests
1213
{
14+
private readonly ITestOutputHelper log;
15+
16+
public ObservabilityTests(ITestOutputHelper output)
17+
{
18+
log = output;
19+
}
1320

1421
[Fact]
1522
public void It_creates_activity_spans_for_parsing()
@@ -64,7 +71,6 @@ public async Task It_creates_activity_spans_for_invocations()
6471
command.SetAction(async (pr, ctok) => await Task.FromResult(0));
6572

6673
var result = await command.Parse(Array.Empty<string>()).InvokeAsync();
67-
6874
activities
6975
.Should()
7076
.ContainSingle(
@@ -90,7 +96,10 @@ public async Task It_creates_activity_spans_for_invocation_errors()
9096
#pragma warning restore CS1998 // Async method lacks 'await' operators and will run synchronously
9197

9298
var result = await command.Parse(Array.Empty<string>()).InvokeAsync();
93-
99+
foreach (var x in activities)
100+
{
101+
log.WriteLine($"{x.DisplayName}({x.OperationName})/{x.Status}({x.Duration}) - {x.TagObjects} - {string.Join(",", x.Events.Select((k) => $"{k.Name},{k.Tags}"))}");
102+
}
94103
activities
95104
.Should()
96105
.ContainSingle(
@@ -100,7 +109,7 @@ public async Task It_creates_activity_spans_for_invocation_errors()
100109
&& a.Tags.Any(t => t.Key == "command" && t.Value == "the-command")
101110
&& a.Tags.Any(t => t.Key == "invoke.type" && t.Value == "async")
102111
&& a.TagObjects.Any(t => t.Key == "exitcode" && (int)t.Value == 1)
103-
&& a.Baggage.Any(t => t.Key == "exception"));
112+
&& a.Events.Any(t => t.Name == "exception"));
104113
}
105114

106115
private static List<Activity> SetupListener()

src/System.CommandLine/Invocation/InvocationPipeline.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,12 @@ private static void Error(this Diagnostics.Activity activity, Exception? excepti
201201
activity.AddTag(DiagnosticsStrings.ExitCode, 1);
202202
if (exception is not null)
203203
{
204-
activity.AddBaggage(DiagnosticsStrings.Exception, exception.ToString());
204+
var tagCollection = new Diagnostics.ActivityTagsCollection
205+
{
206+
{ DiagnosticsStrings.Exception, exception.ToString() }
207+
};
208+
var evt = new Diagnostics.ActivityEvent(DiagnosticsStrings.Exception, tags: tagCollection);
209+
activity.AddEvent(evt);
205210
}
206211
}
207212

0 commit comments

Comments
 (0)