|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | + |
| 4 | +using Azure.Monitor.OpenTelemetry.Exporter; |
| 5 | +using OpenTelemetry.Metrics; |
| 6 | +using OpenTelemetry.Resources; |
| 7 | +using OpenTelemetry.Trace; |
| 8 | + |
| 9 | +namespace ECommerce.AdminUI; |
| 10 | + |
| 11 | +public static class OpenTelemetryExtensions |
| 12 | +{ |
| 13 | +public static IServiceCollection AddOpenTelemetryConfiguration( |
| 14 | + this IServiceCollection services, |
| 15 | + IConfiguration configuration) |
| 16 | + { |
| 17 | + var openTelemetryConfig = configuration.GetSection("OpenTelemetry"); |
| 18 | + |
| 19 | + services.AddOpenTelemetry() |
| 20 | + .WithMetrics(builder => |
| 21 | + { |
| 22 | + builder |
| 23 | + .SetResourceBuilder(ResourceBuilder.CreateDefault() |
| 24 | + .AddService( |
| 25 | + serviceName: openTelemetryConfig["ServiceName"], |
| 26 | + serviceVersion: openTelemetryConfig["ServiceVersion"])) |
| 27 | + .AddRuntimeInstrumentation() |
| 28 | + .AddProcessInstrumentation(); |
| 29 | + |
| 30 | + // Conditional instrumentation |
| 31 | + if (openTelemetryConfig.GetValue<bool>("Instrumentation:AspNetCore")) |
| 32 | + builder.AddAspNetCoreInstrumentation(); |
| 33 | + |
| 34 | + if (openTelemetryConfig.GetValue<bool>("Instrumentation:HttpClient")) |
| 35 | + builder.AddHttpClientInstrumentation(); |
| 36 | + |
| 37 | + // Add exporters based on configuration |
| 38 | + var prometheusEnabled = openTelemetryConfig.GetValue<bool>("Exporters:Prometheus:Enabled"); |
| 39 | + var azureMonitorEnabled = openTelemetryConfig.GetValue<bool>("Exporters:AzureMonitor:Enabled"); |
| 40 | + |
| 41 | + if (prometheusEnabled) |
| 42 | + { |
| 43 | + builder.AddPrometheusExporter(); |
| 44 | + } |
| 45 | + |
| 46 | + if (azureMonitorEnabled) |
| 47 | + { |
| 48 | + var connectionString = openTelemetryConfig["Exporters:AzureMonitor:ConnectionString"]; |
| 49 | + if (!string.IsNullOrEmpty(connectionString)) |
| 50 | + { |
| 51 | + builder.AddAzureMonitorMetricExporter(options => |
| 52 | + { |
| 53 | + options.ConnectionString = connectionString; |
| 54 | + }); |
| 55 | + } |
| 56 | + } |
| 57 | + }) |
| 58 | + .WithTracing(builder => |
| 59 | + { |
| 60 | + builder |
| 61 | + .SetResourceBuilder(ResourceBuilder.CreateDefault() |
| 62 | + .AddService( |
| 63 | + serviceName: openTelemetryConfig["ServiceName"], |
| 64 | + serviceVersion: openTelemetryConfig["ServiceVersion"])); |
| 65 | + |
| 66 | + if (openTelemetryConfig.GetValue<bool>("Instrumentation:AspNetCore")) |
| 67 | + builder.AddAspNetCoreInstrumentation(); |
| 68 | + |
| 69 | + if (openTelemetryConfig.GetValue<bool>("Instrumentation:HttpClient")) |
| 70 | + builder.AddHttpClientInstrumentation(); |
| 71 | + |
| 72 | + if (openTelemetryConfig.GetValue<bool>("Instrumentation:SqlClient")) |
| 73 | + builder.AddSqlClientInstrumentation(); |
| 74 | + |
| 75 | + var azureMonitorEnabled = openTelemetryConfig.GetValue<bool>("Exporters:AzureMonitor:Enabled"); |
| 76 | + if (azureMonitorEnabled) |
| 77 | + { |
| 78 | + var connectionString = openTelemetryConfig["Exporters:AzureMonitor:ConnectionString"]; |
| 79 | + if (!string.IsNullOrEmpty(connectionString)) |
| 80 | + { |
| 81 | + builder.AddAzureMonitorTraceExporter(options => |
| 82 | + { |
| 83 | + options.ConnectionString = connectionString; |
| 84 | + }); |
| 85 | + } |
| 86 | + } |
| 87 | + }); |
| 88 | + |
| 89 | + return services; |
| 90 | + } |
| 91 | +} |
0 commit comments