|
1 | 1 | using ConsoleAppFramework; |
2 | | -using Microsoft.Extensions.DependencyInjection; |
3 | | -using Microsoft.Extensions.Hosting; |
4 | | -using Microsoft.Extensions.Logging; |
5 | | -using OpenTelemetry; |
6 | | -using OpenTelemetry.Logs; |
7 | | -using OpenTelemetry.Metrics; |
8 | | -using OpenTelemetry.Resources; |
9 | | -using OpenTelemetry.Trace; |
10 | | -using System.Diagnostics; |
11 | 2 |
|
12 | | -// Use SigNoz profiling: (view: http://localhost:8080/ ) |
13 | | -// git clone https://github.com/SigNoz/signoz.git |
14 | | -// cd signoz/deploy/docker |
15 | | -// docker compose up |
16 | | -Environment.SetEnvironmentVariable("OTEL_EXPORTER_OTLP_ENDPOINT", "http://localhost:4317"); // 4317 or 4318 |
17 | | - |
18 | | -// crate builder from Microsoft.Extensions.Hosting.Host |
19 | | -var builder = Host.CreateApplicationBuilder(args); |
20 | | - |
21 | | -builder.Logging.AddOpenTelemetry(logging => |
| 3 | +args = ["--x", "10", "y", "20"]; // missing argument |
| 4 | +ConsoleApp.Run(args, (int x, int y) => |
22 | 5 | { |
23 | | - logging.IncludeFormattedMessage = true; |
24 | | - logging.IncludeScopes = true; |
| 6 | + Console.WriteLine(new { x, y }); |
25 | 7 | }); |
26 | | - |
27 | | -builder.Services.AddOpenTelemetry() |
28 | | - .UseOtlpExporter() |
29 | | - .ConfigureResource(resource => |
30 | | - { |
31 | | - resource.AddService("ConsoleAppFramework Telemetry Sample"); |
32 | | - }) |
33 | | - .WithMetrics(metrics => |
34 | | - { |
35 | | - // configure for metrics |
36 | | - metrics.AddRuntimeInstrumentation() |
37 | | - .AddHttpClientInstrumentation(); |
38 | | - }) |
39 | | - .WithTracing(tracing => |
40 | | - { |
41 | | - // configure for tracing |
42 | | - tracing.SetSampler(new AlwaysOnSampler()) |
43 | | - .AddHttpClientInstrumentation() |
44 | | - .AddSource(ConsoleAppFrameworkSampleActivitySource.Name); |
45 | | - }) |
46 | | - .WithLogging(logging => |
47 | | - { |
48 | | - // configure for logging |
49 | | - }); |
50 | | - |
51 | | -var app = builder.ToConsoleAppBuilder(); |
52 | | - |
53 | | -app.Add<SampleCommand>(); |
54 | | - |
55 | | -// setup filter |
56 | | -app.UseFilter<CommandTracingFilter>(); |
57 | | - |
58 | | -await app.RunAsync(args); // Run |
59 | | - |
60 | | -public class SampleCommand(ILogger<SampleCommand> logger) |
61 | | -{ |
62 | | - [Command("")] |
63 | | - public async Task Run(CancellationToken cancellationToken) |
64 | | - { |
65 | | - using var httpClient = new HttpClient(); |
66 | | - |
67 | | - var ms = await httpClient.GetStringAsync("https://www.microsoft.com", cancellationToken); |
68 | | - var google = await httpClient.GetStringAsync("https://www.google.com", cancellationToken); |
69 | | - |
70 | | - logger.LogInformation("Sequential Query done."); |
71 | | - |
72 | | - var ms2 = httpClient.GetStringAsync("https://www.microsoft.com", cancellationToken); |
73 | | - var google2 = httpClient.GetStringAsync("https://www.google.com", cancellationToken); |
74 | | - var apple2 = httpClient.GetStringAsync("https://www.apple.com", cancellationToken); |
75 | | - await Task.WhenAll(ms2, google2, apple2); |
76 | | - |
77 | | - logger.LogInformation("Parallel Query done."); |
78 | | - } |
79 | | -} |
80 | | - |
81 | | - |
82 | | - |
83 | | -public static class ConsoleAppFrameworkSampleActivitySource |
84 | | -{ |
85 | | - public const string Name = "ConsoleAppFrameworkSample"; |
86 | | - |
87 | | - public static ActivitySource Instance { get; } = new ActivitySource(Name); |
88 | | -} |
89 | | - |
90 | | -// Sample Activity filter |
91 | | -internal class CommandTracingFilter(ConsoleAppFilter next) : ConsoleAppFilter(next) |
92 | | -{ |
93 | | - public override async Task InvokeAsync(ConsoleAppContext context, CancellationToken cancellationToken) |
94 | | - { |
95 | | - using var activity = ConsoleAppFrameworkSampleActivitySource.Instance.StartActivity("CommandStart"); |
96 | | - |
97 | | - if (activity == null) // Telemtry is not listened |
98 | | - { |
99 | | - await Next.InvokeAsync(context, cancellationToken); |
100 | | - } |
101 | | - else |
102 | | - { |
103 | | - activity.SetTag("console_app.command_name", context.CommandName); |
104 | | - activity.SetTag("console_app.command_args", string.Join(" ", context.EscapedArguments)); |
105 | | - |
106 | | - try |
107 | | - { |
108 | | - await Next.InvokeAsync(context, cancellationToken); |
109 | | - activity.SetStatus(ActivityStatusCode.Ok); |
110 | | - } |
111 | | - catch (Exception ex) |
112 | | - { |
113 | | - if (ex is OperationCanceledException) |
114 | | - { |
115 | | - activity.SetStatus(ActivityStatusCode.Error, "Canceled"); |
116 | | - } |
117 | | - else |
118 | | - { |
119 | | - activity.AddException(ex); |
120 | | - activity.SetStatus(ActivityStatusCode.Error); |
121 | | - } |
122 | | - throw; |
123 | | - } |
124 | | - } |
125 | | - } |
126 | | -} |
0 commit comments