File tree Expand file tree Collapse file tree 3 files changed +56
-0
lines changed
Expand file tree Collapse file tree 3 files changed +56
-0
lines changed Original file line number Diff line number Diff line change 1+ using Microsoft . Extensions . DependencyInjection ;
2+ using Microsoft . Extensions . Diagnostics . HealthChecks ;
3+ using Microsoft . Extensions . Hosting ;
4+ using OpenTelemetry . Metrics ;
5+ using OpenTelemetry . Resources ;
6+ using OpenTelemetry . Trace ;
7+
8+ namespace ServiceDefaults ;
9+
10+ public static class Extensions
11+ {
12+ public static IHostApplicationBuilder AddServiceDefaults ( this IHostApplicationBuilder builder , string serviceName )
13+ {
14+ var resourceBuilder = ResourceBuilder . CreateDefault ( )
15+ . AddService ( serviceName : serviceName ) ;
16+
17+ var otlpEndpoint = builder . Configuration [ "OTLP_ENDPOINT" ] ;
18+ builder . Services . AddOpenTelemetry ( )
19+ . ConfigureResource ( rb => rb . AddService ( serviceName ) )
20+ . WithMetrics ( m =>
21+ {
22+ m . AddAspNetCoreInstrumentation ( ) ;
23+ m . AddRuntimeInstrumentation ( ) ;
24+ if ( ! string . IsNullOrWhiteSpace ( otlpEndpoint ) )
25+ {
26+ m . AddOtlpExporter ( o => o . Endpoint = new Uri ( otlpEndpoint ) ) ;
27+ }
28+ } )
29+ . WithTracing ( t =>
30+ {
31+ t . AddAspNetCoreInstrumentation ( ) ;
32+ if ( ! string . IsNullOrWhiteSpace ( otlpEndpoint ) )
33+ {
34+ t . AddOtlpExporter ( o => o . Endpoint = new Uri ( otlpEndpoint ) ) ;
35+ }
36+ } ) ;
37+
38+ builder . Services . AddHealthChecks ( ) . AddCheck ( "self" , ( ) => HealthCheckResult . Healthy ( ) ) ;
39+ return builder ;
40+ }
41+ }
Original file line number Diff line number Diff line change 1+ <Project Sdk =" Microsoft.NET.Sdk" >
2+ <PropertyGroup >
3+ <TargetFramework >net9.0</TargetFramework >
4+ <ImplicitUsings >enable</ImplicitUsings >
5+ <Nullable >enable</Nullable >
6+ </PropertyGroup >
7+ <ItemGroup >
8+ <PackageReference Include =" OpenTelemetry.Exporter.OpenTelemetryProtocol" Version =" 1.9.0" />
9+ <PackageReference Include =" OpenTelemetry.Extensions.Hosting" Version =" 1.9.0" />
10+ <PackageReference Include =" Microsoft.Extensions.Http.Resilience" Version =" 8.8.0" />
11+ <PackageReference Include =" Microsoft.Extensions.Diagnostics.HealthChecks" Version =" 9.0.0" />
12+ <PackageReference Include =" OpenTelemetry.Instrumentation.AspNetCore" Version =" 1.9.0" />
13+ <PackageReference Include =" OpenTelemetry.Instrumentation.Runtime" Version =" 1.9.0" />
14+ </ItemGroup >
15+ </Project >
You can’t perform that action at this time.
0 commit comments