|
| 1 | +using Azure.Monitor.OpenTelemetry.Exporter; |
| 2 | +using Genocs.Common.Options; |
| 3 | +using Genocs.Core.Builders; |
| 4 | +using Genocs.Logging.Options; |
| 5 | +using Genocs.Tracing.Jaeger.Options; |
| 6 | +using Microsoft.Extensions.DependencyInjection; |
| 7 | +using OpenTelemetry; |
| 8 | +using OpenTelemetry.Resources; |
| 9 | +using OpenTelemetry.Trace; |
| 10 | + |
| 11 | +namespace Genocs.Tracing; |
| 12 | + |
| 13 | +/// <summary> |
| 14 | +/// The Open Telemetry and Tracing |
| 15 | +/// </summary> |
| 16 | +public static class Extensions |
| 17 | +{ |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Custom settings for OpenTelemetry |
| 21 | + /// </summary> |
| 22 | + /// <param name="builder">The genocs builder</param> |
| 23 | + /// <returns>the builder</returns> |
| 24 | + public static IGenocsBuilder AddOpenTelemetry(this IGenocsBuilder builder) |
| 25 | + { |
| 26 | + |
| 27 | + var appOptions = builder.GetOptions<AppSettings>(AppSettings.Position); |
| 28 | + |
| 29 | + // No OpenTelemetryTracing in case of missing ServiceName |
| 30 | + if (string.IsNullOrWhiteSpace(appOptions.Service)) |
| 31 | + { |
| 32 | + return builder; |
| 33 | + } |
| 34 | + |
| 35 | + var services = builder.Services; |
| 36 | + |
| 37 | + |
| 38 | + // Set Custom Open telemetry |
| 39 | + services.AddOpenTelemetry().WithTracing(x => |
| 40 | + { |
| 41 | + TracerProviderBuilder provider = x.SetResourceBuilder(ResourceBuilder.CreateDefault() |
| 42 | + .AddService(appOptions.Service) |
| 43 | + .AddTelemetrySdk() |
| 44 | + .AddEnvironmentVariableDetector()) |
| 45 | + .AddSource("*"); |
| 46 | + |
| 47 | + // Remove comment below to enable tracing on console |
| 48 | + // you should add MongoDB.Driver.Core.Extensions.OpenTelemetry NuGet package |
| 49 | + provider.AddMongoDBInstrumentation(); |
| 50 | + |
| 51 | + |
| 52 | + var loggerOptions = builder.GetOptions<LoggerSettings>(LoggerSettings.Position); |
| 53 | + |
| 54 | + |
| 55 | + // Check for Azure ApplicationInsights |
| 56 | + if (loggerOptions.Console != null && loggerOptions.Console.Enabled) |
| 57 | + { |
| 58 | + // OpenTelemetry.Exporter.Console NuGet package |
| 59 | + provider.AddConsoleExporter(); |
| 60 | + } |
| 61 | + |
| 62 | + // Check for Azure ApplicationInsights |
| 63 | + if (loggerOptions.Azure != null && loggerOptions.Azure.Enabled) |
| 64 | + { |
| 65 | + provider.AddAzureMonitorTraceExporter(o => |
| 66 | + { |
| 67 | + o.ConnectionString = loggerOptions.Azure.ConnectionString; |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + var jaegerOptions = builder.GetOptions<JaegerSettings>(JaegerSettings.Position); |
| 72 | + |
| 73 | + if (jaegerOptions != null && jaegerOptions.Enabled) |
| 74 | + { |
| 75 | + provider.AddJaegerExporter(o => |
| 76 | + { |
| 77 | + o.AgentHost = jaegerOptions.UdpHost; |
| 78 | + o.AgentPort = jaegerOptions.UdpPort; |
| 79 | + o.MaxPayloadSizeInBytes = jaegerOptions.MaxPacketSize; |
| 80 | + o.ExportProcessorType = ExportProcessorType.Batch; |
| 81 | + o.BatchExportProcessorOptions = new BatchExportProcessorOptions<System.Diagnostics.Activity> |
| 82 | + { |
| 83 | + MaxQueueSize = 2048, |
| 84 | + ScheduledDelayMilliseconds = 5000, |
| 85 | + ExporterTimeoutMilliseconds = 30000, |
| 86 | + MaxExportBatchSize = 512, |
| 87 | + }; |
| 88 | + }); |
| 89 | + } |
| 90 | + |
| 91 | + }); |
| 92 | + |
| 93 | + return builder; |
| 94 | + } |
| 95 | +} |
0 commit comments