55using OpenTelemetry . Resources ;
66using OpenTelemetry . Trace ;
77using System ;
8+ using System . Collections . Generic ;
9+ using Volo . Abp ;
810using Volo . Abp . Modularity ;
911
1012namespace LINGYUN . Abp . Telemetry . OpenTelemetry ;
1113
1214public class AbpTelemetryOpenTelemetryModule : AbpModule
1315{
16+ public override void PreConfigureServices ( ServiceConfigurationContext context )
17+ {
18+ var configuration = context . Services . GetConfiguration ( ) ;
19+
20+ PreConfigure < AbpTelemetryOpenTelemetryOptions > ( options =>
21+ {
22+ var ignureRequestLocalUrlPrefixs = configuration . GetSection ( "OpenTelemetry:IgnoreUrls:Local" ) . Get < List < string > > ( ) ;
23+ if ( ignureRequestLocalUrlPrefixs != null )
24+ {
25+ options . IgnoreLocalRequestUrls = ignureRequestLocalUrlPrefixs ;
26+ }
27+ var ignureRequestRemoteUrlPrefixs = configuration . GetSection ( "OpenTelemetry:IgnoreUrls:Remote" ) . Get < List < string > > ( ) ;
28+ if ( ignureRequestRemoteUrlPrefixs != null )
29+ {
30+ options . IgnoreRemoteRequestUrls = ignureRequestRemoteUrlPrefixs ;
31+ }
32+ } ) ;
33+ }
34+
1435 public override void ConfigureServices ( ServiceConfigurationContext context )
1536 {
1637 var configuration = context . Services . GetConfiguration ( ) ;
1738
1839 var openTelmetrySetup = context . Services . GetPreConfigureActions < OpenTelemetryBuilder > ( ) ;
1940
20- var openTelemetryEnabled = configuration [ "OpenTelemetry:IsEnabled" ] ;
21- if ( openTelemetryEnabled . IsNullOrWhiteSpace ( ) || "false" . Equals ( openTelemetryEnabled . ToLower ( ) ) )
41+ if ( ! configuration . GetValue ( "OpenTelemetry:IsEnabled" , false ) )
2242 {
2343 return ;
2444 }
2545
46+ var openTelemetyOptions = context . Services . ExecutePreConfiguredActions ( new AbpTelemetryOpenTelemetryOptions ( ) ) ;
47+
2648 var applicationName = configuration [ "OpenTelemetry:ServiceName" ] ;
2749 if ( applicationName . IsNullOrWhiteSpace ( ) )
2850 {
@@ -37,7 +59,7 @@ public override void ConfigureServices(ServiceConfigurationContext context)
3759 . WithTracing ( tracing =>
3860 {
3961 tracing . AddSource ( applicationName ) ;
40- ConfigureTracing ( tracing , configuration ) ;
62+ ConfigureTracing ( tracing , configuration , openTelemetyOptions ) ;
4163 } )
4264 . WithMetrics ( metrics =>
4365 {
@@ -47,15 +69,38 @@ public override void ConfigureServices(ServiceConfigurationContext context)
4769 openTelmetrySetup . Configure ( openTelmetryBuilder ) ;
4870 }
4971
50- private static void ConfigureTracing ( TracerProviderBuilder tracing , IConfiguration configuration )
72+ private static void ConfigureTracing ( TracerProviderBuilder tracing , IConfiguration configuration , AbpTelemetryOpenTelemetryOptions openTelemetyOptions )
5173 {
52- tracing . AddHttpClientInstrumentation ( ) ;
53- tracing . AddAspNetCoreInstrumentation ( ) ;
74+ tracing . AddHttpClientInstrumentation ( options =>
75+ {
76+ options . FilterHttpRequestMessage += ( request ) =>
77+ {
78+ if ( request . RequestUri != null &&
79+ openTelemetyOptions . IsIgnureRemoteRequestUrl ( request . RequestUri . AbsolutePath ) )
80+ {
81+ return false ;
82+ }
83+
84+ return true ;
85+ } ;
86+ } ) ;
87+ tracing . AddAspNetCoreInstrumentation ( options =>
88+ {
89+ options . Filter += ( ctx ) =>
90+ {
91+ if ( openTelemetyOptions . IsIgnureLocalRequestUrl ( ctx . Request . Path ) )
92+ {
93+ return false ;
94+ }
95+
96+ return true ;
97+ } ;
98+ } ) ;
5499 tracing . AddCapInstrumentation ( ) ;
55100 tracing . AddEntityFrameworkCoreInstrumentation ( efcore =>
56101 {
57102 efcore . SetDbStatementForText = configuration . GetValue (
58- "OpenTelemetry:EntityFrameworkCore:SetDbStatementForText" ,
103+ "OpenTelemetry:EntityFrameworkCore:SetDbStatementForText" ,
59104 efcore . SetDbStatementForText ) ;
60105
61106 efcore . SetDbStatementForStoredProcedure = configuration . GetValue (
@@ -68,24 +113,28 @@ private static void ConfigureTracing(TracerProviderBuilder tracing, IConfigurati
68113 tracing . AddConsoleExporter ( ) ;
69114 }
70115
71- var tracingOtlpEndpoint = configuration [ "OpenTelemetry:Otlp:Endpoint" ] ;
72- if ( ! tracingOtlpEndpoint . IsNullOrWhiteSpace ( ) )
116+ if ( configuration . GetValue ( "OpenTelemetry:Otlp:IsEnabled" , false ) )
73117 {
74118 tracing . AddOtlpExporter ( otlpOptions =>
75119 {
76- otlpOptions . Endpoint = new Uri ( tracingOtlpEndpoint ) ;
120+ var otlpEndPoint = configuration [ "OpenTelemetry:Otlp:Endpoint" ] ;
121+ Check . NotNullOrWhiteSpace ( otlpEndPoint , nameof ( otlpEndPoint ) ) ;
122+
123+ otlpOptions . Headers = configuration [ "OpenTelemetry:Otlp:Headers" ] ;
124+ otlpOptions . Endpoint = new Uri ( otlpEndPoint . EnsureEndsWith ( '/' ) + "v1/traces" ) ;
125+ otlpOptions . Protocol = configuration . GetValue ( "OpenTelemetry:Otlp:Protocol" , otlpOptions . Protocol ) ;
77126 } ) ;
78- return ;
79127 }
80128
81- var zipkinEndpoint = configuration [ "OpenTelemetry:ZipKin:Endpoint" ] ;
82- if ( ! zipkinEndpoint . IsNullOrWhiteSpace ( ) )
129+ if ( configuration . GetValue ( "OpenTelemetry:ZipKin:IsEnabled" , false ) )
83130 {
84131 tracing . AddZipkinExporter ( zipKinOptions =>
85132 {
86- zipKinOptions . Endpoint = new Uri ( zipkinEndpoint ) ;
133+ var zipkinEndPoint = configuration [ "OpenTelemetry:ZipKin:Endpoint" ] ;
134+ Check . NotNullOrWhiteSpace ( zipkinEndPoint , nameof ( zipkinEndPoint ) ) ;
135+
136+ zipKinOptions . Endpoint = new Uri ( zipkinEndPoint ) ;
87137 } ) ;
88- return ;
89138 }
90139 }
91140
@@ -95,19 +144,17 @@ private static void ConfigureMetrics(MeterProviderBuilder metrics, IConfiguratio
95144 metrics . AddHttpClientInstrumentation ( ) ;
96145 metrics . AddAspNetCoreInstrumentation ( ) ;
97146
98- if ( configuration . GetValue ( "OpenTelemetry:Console:IsEnabled" , false ) )
99- {
100- metrics . AddConsoleExporter ( ) ;
101- }
102-
103- var tracingOtlpEndpoint = configuration [ "OpenTelemetry:Otlp:Endpoint" ] ;
104- if ( ! tracingOtlpEndpoint . IsNullOrWhiteSpace ( ) )
147+ if ( configuration . GetValue ( "OpenTelemetry:Otlp:IsEnabled" , false ) )
105148 {
106149 metrics . AddOtlpExporter ( otlpOptions =>
107150 {
108- otlpOptions . Endpoint = new Uri ( tracingOtlpEndpoint ) ;
151+ var otlpEndPoint = configuration [ "OpenTelemetry:Otlp:Endpoint" ] ;
152+ Check . NotNullOrWhiteSpace ( otlpEndPoint , nameof ( otlpEndPoint ) ) ;
153+
154+ otlpOptions . Headers = configuration [ "OpenTelemetry:Otlp:Headers" ] ;
155+ otlpOptions . Endpoint = new Uri ( otlpEndPoint . EnsureEndsWith ( '/' ) + "v1/metrics" ) ;
156+ otlpOptions . Protocol = configuration . GetValue ( "OpenTelemetry:Otlp:Protocol" , otlpOptions . Protocol ) ;
109157 } ) ;
110- return ;
111158 }
112159 }
113160}
0 commit comments