3
3
using Microsoft . Extensions . DependencyInjection ;
4
4
using Microsoft . Extensions . Diagnostics . HealthChecks ;
5
5
using Microsoft . Extensions . Logging ;
6
+ using Microsoft . Extensions . ServiceDiscovery ;
6
7
using OpenTelemetry ;
7
8
using OpenTelemetry . Metrics ;
8
9
using OpenTelemetry . Trace ;
@@ -14,7 +15,7 @@ namespace Microsoft.Extensions.Hosting;
14
15
// To learn more about using this project, see https://aka.ms/dotnet/aspire/service-defaults
15
16
public static class Extensions
16
17
{
17
- public static IHostApplicationBuilder AddServiceDefaults ( this IHostApplicationBuilder builder )
18
+ public static TBuilder AddServiceDefaults < TBuilder > ( this TBuilder builder ) where TBuilder : IHostApplicationBuilder
18
19
{
19
20
builder . ConfigureOpenTelemetry ( ) ;
20
21
@@ -40,7 +41,7 @@ public static IHostApplicationBuilder AddServiceDefaults(this IHostApplicationBu
40
41
return builder ;
41
42
}
42
43
43
- public static IHostApplicationBuilder ConfigureOpenTelemetry ( this IHostApplicationBuilder builder )
44
+ public static TBuilder ConfigureOpenTelemetry < TBuilder > ( this TBuilder builder ) where TBuilder : IHostApplicationBuilder
44
45
{
45
46
builder . Logging . AddOpenTelemetry ( logging =>
46
47
{
@@ -49,26 +50,27 @@ public static IHostApplicationBuilder ConfigureOpenTelemetry(this IHostApplicati
49
50
} ) ;
50
51
51
52
builder . Services . AddOpenTelemetry ( )
52
- . WithMetrics ( metrics =>
53
- {
54
- metrics . AddAspNetCoreInstrumentation ( )
55
- . AddHttpClientInstrumentation ( )
56
- . AddRuntimeInstrumentation ( ) ;
57
- } )
58
- . WithTracing ( tracing =>
59
- {
60
- tracing . AddAspNetCoreInstrumentation ( )
61
- // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
62
- //.AddGrpcClientInstrumentation()
63
- . AddHttpClientInstrumentation ( ) ;
64
- } ) ;
53
+ . WithMetrics ( metrics =>
54
+ {
55
+ metrics . AddAspNetCoreInstrumentation ( )
56
+ . AddHttpClientInstrumentation ( )
57
+ . AddRuntimeInstrumentation ( ) ;
58
+ } )
59
+ . WithTracing ( tracing =>
60
+ {
61
+ tracing . AddSource ( builder . Environment . ApplicationName )
62
+ . AddAspNetCoreInstrumentation ( )
63
+ // Uncomment the following line to enable gRPC instrumentation (requires the OpenTelemetry.Instrumentation.GrpcNetClient package)
64
+ //.AddGrpcClientInstrumentation()
65
+ . AddHttpClientInstrumentation ( ) ;
66
+ } ) ;
65
67
66
68
builder . AddOpenTelemetryExporters ( ) ;
67
69
68
70
return builder ;
69
71
}
70
72
71
- private static IHostApplicationBuilder AddOpenTelemetryExporters ( this IHostApplicationBuilder builder )
73
+ private static TBuilder AddOpenTelemetryExporters < TBuilder > ( this TBuilder builder ) where TBuilder : IHostApplicationBuilder
72
74
{
73
75
var useOtlpExporter = ! string . IsNullOrWhiteSpace ( builder . Configuration [ "OTEL_EXPORTER_OTLP_ENDPOINT" ] ) ;
74
76
@@ -87,11 +89,11 @@ private static IHostApplicationBuilder AddOpenTelemetryExporters(this IHostAppli
87
89
return builder ;
88
90
}
89
91
90
- public static IHostApplicationBuilder AddDefaultHealthChecks ( this IHostApplicationBuilder builder )
92
+ public static TBuilder AddDefaultHealthChecks < TBuilder > ( this TBuilder builder ) where TBuilder : IHostApplicationBuilder
91
93
{
92
94
builder . Services . AddHealthChecks ( )
93
- // Add a default liveness check to ensure app is responsive
94
- . AddCheck ( "self" , ( ) => HealthCheckResult . Healthy ( ) , [ "live" ] ) ;
95
+ // Add a default liveness check to ensure app is responsive
96
+ . AddCheck ( "self" , ( ) => HealthCheckResult . Healthy ( ) , [ "live" ] ) ;
95
97
96
98
return builder ;
97
99
}
0 commit comments