MCPcopy Create free account
hub / github.com/TryCatchLearn/Overflow / Extensions

Class Extensions

Overflow.ServiceDefaults/Extensions.cs:16–130  ·  view source on GitHub ↗

Adds common Aspire services: service discovery, resilience, health checks, and OpenTelemetry. This project should be referenced by each service project in your solution. To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults

Source from the content-addressed store, hash-verified

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
16public static class Extensions
17{
18 private const string HealthEndpointPath = "/health";
19 private const string AlivenessEndpointPath = "/alive";
20
21 public static TBuilder AddServiceDefaults<TBuilder>(this TBuilder builder) where TBuilder : IHostApplicationBuilder
22 {
23 builder.ConfigureOpenTelemetry();
24
25 builder.AddDefaultHealthChecks();
26
27 builder.Services.AddServiceDiscovery();
28
29 builder.Services.ConfigureHttpClientDefaults(http =>
30 {
31 // Turn on resilience by default
32 http.AddStandardResilienceHandler();
33
34 // Turn on service discovery by default
35 http.AddServiceDiscovery();
36 });
37
38 // Uncomment the following to restrict the allowed schemes for service discovery.
39 // builder.Services.Configure<ServiceDiscoveryOptions>(options =>
40 // {
41 // options.AllowedSchemes = ["https"];
42 // });
43
44 return builder;
45 }
46
47 public static TBuilder ConfigureOpenTelemetry<TBuilder>(this TBuilder builder)
48 where TBuilder : IHostApplicationBuilder
49 {
50 builder.Logging.AddOpenTelemetry(logging =>
51 {
52 logging.IncludeFormattedMessage = true;
53 logging.IncludeScopes = true;
54 });
55
56 builder.Services.AddOpenTelemetry()
57 .WithMetrics(metrics =>
58 {
59 metrics.AddAspNetCoreInstrumentation()
60 .AddHttpClientInstrumentation()
61 .AddRuntimeInstrumentation();
62 })
63 .WithTracing(tracing =>
64 {
65 tracing.AddSource(builder.Environment.ApplicationName)
66 .AddAspNetCoreInstrumentation(tracing =>
67 // Exclude health check requests from tracing
68 tracing.Filter = context =>
69 !context.Request.Path.StartsWithSegments(HealthEndpointPath)
70 && !context.Request.Path.StartsWithSegments(AlivenessEndpointPath)
71 )
72 // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
73 //.AddGrpcClientInstrumentation()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…