|
| 1 | +using Microsoft.AspNetCore.Builder; |
| 2 | +using Microsoft.AspNetCore.Diagnostics.HealthChecks; |
| 3 | +using Microsoft.Extensions.DependencyInjection; |
| 4 | +using Microsoft.Extensions.Diagnostics.HealthChecks; |
| 5 | +using Microsoft.Extensions.Logging; |
| 6 | +using Microsoft.Extensions.ServiceDiscovery; |
| 7 | +using OpenTelemetry; |
| 8 | +using OpenTelemetry.Metrics; |
| 9 | +using OpenTelemetry.Trace; |
| 10 | + |
| 11 | +namespace Microsoft.Extensions.Hosting; |
| 12 | + |
| 13 | +// Adds common .NET Aspire services: service discovery, resilience, health checks, and OpenTelemetry. |
| 14 | +// This project should be referenced by each service project in your solution. |
| 15 | +// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults |
| 16 | +public static class Extensions |
| 17 | +{ |
| 18 | + public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder |
| 19 | + { |
| 20 | + builder.ConfigureOpenTelemetry(); |
| 21 | + |
| 22 | + builder.Services.AddServiceDiscovery(); |
| 23 | + |
| 24 | + builder.Services.ConfigureHttpClientDefaults(http => |
| 25 | + { |
| 26 | + // Turn on resilience by default |
| 27 | + http.AddStandardResilienceHandler(); |
| 28 | + |
| 29 | + // Turn on service discovery by default |
| 30 | + http.AddServiceDiscovery(); |
| 31 | + }); |
| 32 | + |
| 33 | + // Uncomment the following to restrict the allowed schemes for service discovery. |
| 34 | + // builder.Services.Configure<ServiceDiscoveryOptions>(options => |
| 35 | + // { |
| 36 | + // options.AllowedSchemes = ["https"]; |
| 37 | + // }); |
| 38 | + |
| 39 | + return builder; |
| 40 | + } |
| 41 | + |
| 42 | + public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder |
| 43 | + { |
| 44 | + builder.Logging.AddOpenTelemetry(logging => |
| 45 | + { |
| 46 | + logging.IncludeFormattedMessage = true; |
| 47 | + logging.IncludeScopes = true; |
| 48 | + }); |
| 49 | + |
| 50 | + builder.Services.AddOpenTelemetry() |
| 51 | + .WithMetrics(metrics => |
| 52 | + { |
| 53 | + metrics.AddAspNetCoreInstrumentation() |
| 54 | + .AddHttpClientInstrumentation() |
| 55 | + .AddRuntimeInstrumentation(); |
| 56 | + }) |
| 57 | + .WithTracing(tracing => |
| 58 | + { |
| 59 | + tracing.AddSource(builder.Environment.ApplicationName) |
| 60 | + .AddAspNetCoreInstrumentation() |
| 61 | + // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package) |
| 62 | + //.AddGrpcClientInstrumentation() |
| 63 | + .AddHttpClientInstrumentation(); |
| 64 | + }); |
| 65 | + |
| 66 | + builder.AddOpenTelemetryExporters(); |
| 67 | + |
| 68 | + return builder; |
| 69 | + } |
| 70 | + |
| 71 | + private static TBuilder AddOpenTelemetryExporters<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder |
| 72 | + { |
| 73 | + var useOtlpExporter = !string.IsNullOrWhiteSpace(builder.Configuration["OTEL_EXPORTER_OTLP_ENDPOINT"]); |
| 74 | + |
| 75 | + if (useOtlpExporter) |
| 76 | + { |
| 77 | + builder.Services.AddOpenTelemetry().UseOtlpExporter(); |
| 78 | + } |
| 79 | + |
| 80 | + // Uncomment the following lines to enable the Azure Monitor exporter (requires the Azure.Monitor.OpenTelemetry.AspNetCore package) |
| 81 | + //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"])) |
| 82 | + //{ |
| 83 | + // builder.Services.AddOpenTelemetry() |
| 84 | + // .UseAzureMonitor(); |
| 85 | + //} |
| 86 | + |
| 87 | + return builder; |
| 88 | + } |
| 89 | +} |
0 commit comments