|
| 1 | +using System.CommandLine.Parsing; |
| 2 | +using FluentAssertions; |
| 3 | +using System.Linq; |
| 4 | +using Xunit; |
| 5 | +using System.Diagnostics; |
| 6 | +using System.Collections.Generic; |
| 7 | +using System.Threading.Tasks; |
| 8 | + |
| 9 | +namespace System.CommandLine.Tests |
| 10 | +{ |
| 11 | + public class ObservabilityTests |
| 12 | + { |
| 13 | + |
| 14 | + [Fact] |
| 15 | + public void It_creates_activity_spans_for_parsing() |
| 16 | + { |
| 17 | + List<Activity> activities = SetupListener(); |
| 18 | + |
| 19 | + var command = new Command("the-command") |
| 20 | + { |
| 21 | + new Option<string>("--option") |
| 22 | + }; |
| 23 | + |
| 24 | + var args = new[] { "--option", "the-argument" }; |
| 25 | + |
| 26 | + var result = command.Parse(args); |
| 27 | + |
| 28 | + activities |
| 29 | + .Should() |
| 30 | + .ContainSingle( |
| 31 | + a => a.OperationName == "System.CommandLine.Parse" |
| 32 | + && a.Status == ActivityStatusCode.Ok |
| 33 | + && a.Tags.Any(t => t.Key == "command" && t.Value == "the-command")); |
| 34 | + } |
| 35 | + |
| 36 | + [Fact] |
| 37 | + public void It_creates_activity_spans_for_parsing_errors() |
| 38 | + { |
| 39 | + List<Activity> activities = SetupListener(); |
| 40 | + |
| 41 | + var command = new Command("the-command") |
| 42 | + { |
| 43 | + new Option<string>("--option") |
| 44 | + }; |
| 45 | + |
| 46 | + var args = new[] { "--opt", "the-argument" }; |
| 47 | + var result = command.Parse(args); |
| 48 | + |
| 49 | + activities |
| 50 | + .Should() |
| 51 | + .ContainSingle( |
| 52 | + a => a.OperationName == "System.CommandLine.Parse" |
| 53 | + && a.Status == ActivityStatusCode.Error |
| 54 | + && a.Tags.Any(t => t.Key == "command" && t.Value == "the-command") |
| 55 | + && a.Baggage.Any(t => t.Key == "errors")); |
| 56 | + } |
| 57 | + |
| 58 | + [Fact] |
| 59 | + public async Task It_creates_activity_spans_for_invocations() |
| 60 | + { |
| 61 | + List<Activity> activities = SetupListener(); |
| 62 | + |
| 63 | + var command = new Command("the-command"); |
| 64 | + command.SetAction(async (pr, ctok) => await Task.FromResult(0)); |
| 65 | + |
| 66 | + var result = await command.Parse(Array.Empty<string>()).InvokeAsync(); |
| 67 | + |
| 68 | + activities |
| 69 | + .Should() |
| 70 | + .ContainSingle( |
| 71 | + a => a.OperationName == "System.CommandLine.Invoke" |
| 72 | + && a.DisplayName == "the-command" |
| 73 | + && a.Status == ActivityStatusCode.Ok |
| 74 | + && a.Tags.Any(t => t.Key == "command" && t.Value == "the-command") |
| 75 | + && a.Tags.Any(t => t.Key == "invoke.type" && t.Value == "async") |
| 76 | + && a.TagObjects.Any(t => t.Key == "exitcode" && (int)t.Value == 0)); |
| 77 | + } |
| 78 | + |
| 79 | + [Fact] |
| 80 | + public async Task It_creates_activity_spans_for_invocation_errors() |
| 81 | + { |
| 82 | + List<Activity> activities = SetupListener(); |
| 83 | + |
| 84 | + var command = new Command("the-command"); |
| 85 | + command.SetAction(async (pr, ctok) => |
| 86 | + { |
| 87 | + throw new Exception("Something went wrong"); |
| 88 | + }); |
| 89 | + |
| 90 | + var result = await command.Parse(Array.Empty<string>()).InvokeAsync(); |
| 91 | + |
| 92 | + activities |
| 93 | + .Should() |
| 94 | + .ContainSingle( |
| 95 | + a => a.OperationName == "System.CommandLine.Invoke" |
| 96 | + && a.DisplayName == "the-command" |
| 97 | + && a.Status == ActivityStatusCode.Error |
| 98 | + && a.Tags.Any(t => t.Key == "command" && t.Value == "the-command") |
| 99 | + && a.Tags.Any(t => t.Key == "invoke.type" && t.Value == "async") |
| 100 | + && a.TagObjects.Any(t => t.Key == "exitcode" && (int)t.Value == 1) |
| 101 | + && a.Baggage.Any(t => t.Key == "exception")); |
| 102 | + } |
| 103 | + |
| 104 | + private static List<Activity> SetupListener() |
| 105 | + { |
| 106 | + List<Activity> activities = new(); |
| 107 | + var listener = new ActivityListener(); |
| 108 | + listener.ShouldListenTo = s => true; |
| 109 | + listener.Sample = (ref ActivityCreationOptions<ActivityContext> options) => ActivitySamplingResult.AllData; |
| 110 | + listener.ActivityStopped = a => activities.Add(a); |
| 111 | + ActivitySource.AddActivityListener(listener); |
| 112 | + return activities; |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments