44using OpenTelemetry . Metrics ;
55using OpenTelemetry . Resources ;
66using OpenTelemetry . Trace ;
7+ using TaskHub . Observability . OpenTelemetry . Options ;
8+ using TaskHub . Observability . OpenTelemetry . Tools ;
79
8- namespace TaskHub . Observability . OpenTelemetry ;
10+ namespace TaskHub . Observability . OpenTelemetry . Bootstrap ;
911
1012public static class OpenTelemetryBootstrap
1113{
12- public static void AddAppOpenTelemetry ( this IServiceCollection services , Action < IServiceProvider , OpenTelemetryOptions > action )
14+ public static void AddAppOpenTelemetry ( this IServiceCollection services , Action < OpenTelemetryOptions > action )
1315 {
14- using var sp = services . BuildServiceProvider ( ) ;
1516 var options = new OpenTelemetryOptions ( ) ;
16- action ( sp , options ) ;
17+ action ( options ) ;
1718
1819 services . AddOpenTelemetry ( ) . ConfigureResource ( t =>
1920 {
2021 t . AddService ( serviceName : options . ServiceName ) ;
2122 t . AddAttributes ( [ new ( "deployment.environment" , options . Environment ) ] ) ;
2223 } ) . WithTracing ( t =>
2324 {
24- t . AddSource ( "CommandsBus" , "NominatimClient" , "NominatimService" ) ;
25- t . AddSource ( [ .. options . Sources ] ) ;
26-
27- if ( options . IsHttpTracesEnabled )
25+ if ( options . Sampling . IsEnabled )
26+ {
27+ switch ( options . Sampling . Type )
28+ {
29+ case SamplingType . AlwaysOn :
30+ t . SetSampler ( new AlwaysOnSampler ( ) ) ;
31+ break ;
32+ case SamplingType . AlwaysOff :
33+ t . SetSampler ( new AlwaysOffSampler ( ) ) ;
34+ break ;
35+ case SamplingType . TraceIdRatio :
36+ t . SetSampler ( new TraceIdRatioBasedSampler ( options . Sampling . Probability ) ) ;
37+ break ;
38+ case SamplingType . ParentBased :
39+ t . SetSampler ( new ParentBasedSampler ( new TraceIdRatioBasedSampler ( options . Sampling . Probability ) ) ) ;
40+ break ;
41+ default :
42+ t . SetSampler ( new AlwaysOffSampler ( ) ) ;
43+ break ;
44+ }
45+ }
46+ else
2847 {
29- t . AddHttpClientInstrumentation ( o => o . RecordException = options . RecordException ) ;
48+ t . SetSampler ( new AlwaysOffSampler ( ) ) ;
3049 }
3150
32- t . AddHttpClientInstrumentation ( o =>
51+ t . SetSampler ( new TraceIdRatioBasedSampler ( 0.1f ) ) ;
52+ t . AddSource ( "CommandsBus" , "NominatimClient" , "NominatimService" ) ;
53+ t . AddSource ( [ .. options . Sources ] ) ;
54+
55+ if ( options . IsHttpTracesEnabled ) t . AddHttpClientInstrumentation ( o =>
3356 {
3457 o . RecordException = options . RecordException ;
3558 o . FilterHttpRequestMessage = req =>
@@ -45,10 +68,11 @@ public static void AddAppOpenTelemetry(this IServiceCollection services, Action<
4568 return false ;
4669 }
4770
48-
4971 return true ;
5072 } ;
51- } ) . AddAspNetCoreInstrumentation ( o =>
73+ } ) ;
74+
75+ if ( options . IsAspNetTracesEnabled ) t . AddAspNetCoreInstrumentation ( o =>
5276 {
5377 o . RecordException = options . RecordException ;
5478 o . Filter = ctx =>
@@ -61,22 +85,26 @@ public static void AddAppOpenTelemetry(this IServiceCollection services, Action<
6185
6286 return options . Ignore . Any ( ignore => path . StartsWithSegments ( ignore ) ) == false ;
6387 } ;
64- } ) . AddEntityFrameworkCoreInstrumentation ( cntx => cntx . EnrichWithIDbCommand = ( activity , command ) =>
88+ } ) ;
89+
90+ if ( options . IsEFCoreTracesEnabled ) t . AddEntityFrameworkCoreInstrumentation ( cntx => cntx . EnrichWithIDbCommand = ( activity , command ) =>
6591 {
6692 var sql = command . CommandText ? . TrimStart ( ) ?? string . Empty ;
6793 var space = sql . IndexOf ( ' ' ) ;
6894 var verb = space > 0 ? sql [ ..space ] . ToUpperInvariant ( ) : "SQL" ;
69- var table = Tools . ExtractTableName ( sql , verb ) ;
95+ var table = OtelTools . ExtractTableName ( sql , verb ) ;
7096
7197 activity . DisplayName = $ "SQL { verb } { table } ". Trim ( ) ;
7298 activity . SetTag ( "db.statement" , sql ) ;
7399 activity . SetTag ( "db.system" , command . Connection ? . GetType ( ) . Name ) ;
74100 activity . SetTag ( "db.table" , table ) ;
75101 activity . SetTag ( "db.name" , command . Connection ? . Database ) ;
76- } ) . AddOtlpExporter ( cntx =>
102+ } ) ;
103+
104+ t . AddOtlpExporter ( cntx =>
77105 {
78106 cntx . Endpoint = new Uri ( options . TracingEndpoint ) ;
79- cntx . Protocol = OtlpExportProtocol . Grpc ;
107+ cntx . Protocol = options . Protocol == "grpc" ? OtlpExportProtocol . Grpc : OtlpExportProtocol . HttpProtobuf ;
80108 } ) ;
81109 } ) ;
82110 }
0 commit comments