77using Microsoft . Extensions . Hosting ;
88using Microsoft . Extensions . Logging ;
99using OpenTelemetry ;
10+ using OpenTelemetry . Exporter ;
1011using OpenTelemetry . Metrics ;
1112using OpenTelemetry . Resources ;
1213using OpenTelemetry . Trace ;
@@ -111,7 +112,7 @@ public static IServiceCollection AddServiceDefaults(this IServiceCollection buil
111112 /// </summary>
112113 /// <param name="builder">主机应用程序构建器实例</param>
113114 /// <returns>更新后的构建器实例</returns>
114- public static ILoggingBuilder ConfigureOpenTelemetryLogger ( this ILoggingBuilder builder )
115+ public static ILoggingBuilder ConfigureOpenTelemetryLogger ( this ILoggingBuilder builder )
115116 {
116117 // 为日志添加OpenTelemetry支持
117118 builder . AddOpenTelemetry ( logging =>
@@ -151,61 +152,61 @@ public static IServiceCollection ConfigureOpenTelemetry(this IServiceCollection
151152 // 为日志添加OpenTelemetry支持
152153 var serviceName = Environment . GetEnvironmentVariable ( "ServerType" ) ?? AppDomain . CurrentDomain . FriendlyName ;
153154 // 添加并配置OpenTelemetry服务
154- serviceCollection . AddOpenTelemetry ( )
155- // 配置资源
156- . ConfigureResource ( configure =>
157- {
158- if ( ! string . IsNullOrWhiteSpace ( serviceName ) )
159- {
160- configure . AddService ( serviceName ) . AddTelemetrySdk ( ) ;
161- }
162-
163- // 添加操作系统检测器以收集操作系统相关遥测数据
164- configure . AddOperatingSystemDetector ( ) ;
165- // 添加进程运行时检测器以收集进程运行时信息
166- configure . AddProcessRuntimeDetector ( ) ;
167- // 添加主机检测器以收集主机环境信息
168- configure . AddHostDetector ( ) ;
169- // 添加进程检测器以收集当前进程信息
170- configure . AddProcessDetector ( ) ;
171- } )
172- // 配置指标
173- . WithMetrics ( metrics =>
174- {
175- // 添加ASP.NET Core、HTTP客户端和运行时指标检测
176- metrics . AddAspNetCoreInstrumentation ( )
177- . AddHttpClientInstrumentation ( )
178- . AddRuntimeInstrumentation ( ) ;
179- // Metrics provides by ASP.NET Core in .NET 8
180- metrics . AddMeter ( "Microsoft.AspNetCore.Hosting" ) ;
181- metrics . AddMeter ( "Microsoft.AspNetCore.Server.Kestrel" ) ;
182-
183- // Metrics provided by System.Net libraries
184- metrics . AddMeter ( "System.Net.Http" ) ;
185- metrics . AddMeter ( "System.Net.NameResolution" ) ;
186- } )
187- // 配置追踪
188- . WithTracing ( tracing =>
189- {
190- // 添加追踪源并配置ASP.NET Core追踪
191- tracing . AddSource ( serviceName )
192- . AddAspNetCoreInstrumentation ( aspNetCoreTraceInstrumentationOptions =>
193- // 排除健康检查请求的追踪
194- {
195- aspNetCoreTraceInstrumentationOptions . Filter = context => { return ! context . Request . Path . StartsWithSegments ( HealthEndpointPath ) && ! context . Request . Path . StartsWithSegments ( AlivenessEndpointPath ) ; } ;
196- } )
197- // 取消以下注释以启用gRPC检测(需要OpenTelemetry.Instrumentation.GrpcNetClient包)
198- //.AddGrpcClientInstrumentation()
199- . AddHttpClientInstrumentation ( ) ;
200- // 开发环境中增加控制台输出
201- if ( EnvironmentHelper . IsDevelopment ( ) )
202- {
203- tracing . AddConsoleExporter ( ) ;
204- }
205- } ) . UseGrafana ( ) ;
155+ var openTelemetryBuilder = serviceCollection . AddOpenTelemetry ( )
156+ // 配置资源
157+ . ConfigureResource ( configure =>
158+ {
159+ if ( ! string . IsNullOrWhiteSpace ( serviceName ) )
160+ {
161+ configure . AddService ( serviceName ) . AddTelemetrySdk ( ) ;
162+ }
163+
164+ // 添加操作系统检测器以收集操作系统相关遥测数据
165+ configure . AddOperatingSystemDetector ( ) ;
166+ // 添加进程运行时检测器以收集进程运行时信息
167+ configure . AddProcessRuntimeDetector ( ) ;
168+ // 添加主机检测器以收集主机环境信息
169+ configure . AddHostDetector ( ) ;
170+ // 添加进程检测器以收集当前进程信息
171+ configure . AddProcessDetector ( ) ;
172+ } )
173+ // 配置指标
174+ . WithMetrics ( metrics =>
175+ {
176+ // 添加ASP.NET Core、HTTP客户端和运行时指标检测
177+ metrics . AddAspNetCoreInstrumentation ( )
178+ . AddHttpClientInstrumentation ( )
179+ . AddRuntimeInstrumentation ( ) ;
180+ // Metrics provides by ASP.NET Core in .NET 8
181+ metrics . AddMeter ( "Microsoft.AspNetCore.Hosting" ) ;
182+ metrics . AddMeter ( "Microsoft.AspNetCore.Server.Kestrel" ) ;
183+
184+ // Metrics provided by System.Net libraries
185+ metrics . AddMeter ( "System.Net.Http" ) ;
186+ metrics . AddMeter ( "System.Net.NameResolution" ) ;
187+ } )
188+ // 配置追踪
189+ . WithTracing ( tracing =>
190+ {
191+ // 添加追踪源并配置ASP.NET Core追踪
192+ tracing . AddSource ( serviceName )
193+ . AddAspNetCoreInstrumentation ( aspNetCoreTraceInstrumentationOptions =>
194+ // 排除健康检查请求的追踪
195+ {
196+ aspNetCoreTraceInstrumentationOptions . Filter = context => { return ! context . Request . Path . StartsWithSegments ( HealthEndpointPath ) && ! context . Request . Path . StartsWithSegments ( AlivenessEndpointPath ) ; } ;
197+ } )
198+ // 取消以下注释以启用gRPC检测(需要OpenTelemetry.Instrumentation.GrpcNetClient包)
199+ //.AddGrpcClientInstrumentation()
200+ . AddHttpClientInstrumentation ( ) ;
201+ // 开发环境中增加控制台输出
202+ if ( EnvironmentHelper . IsDevelopment ( ) )
203+ {
204+ tracing . AddConsoleExporter ( ) ;
205+ }
206+ } ) . UseGrafana ( ) ;
206207
207208 // 添加OpenTelemetry导出器
208- serviceCollection . AddOpenTelemetryExporters ( ) ;
209+ // serviceCollection.AddOpenTelemetryExporters(openTelemetryBuilder );
209210
210211 return serviceCollection ;
211212 }
@@ -214,18 +215,26 @@ public static IServiceCollection ConfigureOpenTelemetry(this IServiceCollection
214215 /// 根据配置添加OpenTelemetry导出器
215216 /// </summary>
216217 /// <param name="builder">主机应用程序构建器实例</param>
218+ /// <param name="openTelemetryBuilder"></param>
217219 /// <returns>更新后的构建器实例</returns>
218- private static IServiceCollection AddOpenTelemetryExporters ( this IServiceCollection builder )
220+ private static IServiceCollection AddOpenTelemetryExporters ( this IServiceCollection builder , IOpenTelemetryBuilder openTelemetryBuilder )
219221 {
220- // 检查是否配置了OTLP导出端点
221- var useOtlpExporter = ! string . IsNullOrWhiteSpace ( Environment . GetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_ENDPOINT" ) ) ;
222-
223- if ( useOtlpExporter )
222+ if ( openTelemetryBuilder != null )
224223 {
225- // 使用OTLP导出器
226- builder . AddOpenTelemetry ( ) . UseOtlpExporter ( ) ;
224+ var otelExporterOtlpEndpoint = Environment . GetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_ENDPOINT" ) ;
225+ var otelExporterOtlpProtocol = Environment . GetEnvironmentVariable ( "OTEL_EXPORTER_OTLP_PROTOCOL" ) ;
226+ // 检查是否配置了OTLP导出端点
227+ var useOtlpExporter = ! string . IsNullOrWhiteSpace ( otelExporterOtlpEndpoint ) ;
228+ var otlpProtocol = Convert . ToInt32 ( otelExporterOtlpProtocol ) ;
229+
230+ if ( useOtlpExporter )
231+ {
232+ // 使用OTLP导出器
233+ openTelemetryBuilder . UseOtlpExporter ( otlpProtocol > 0 ? OtlpExportProtocol . HttpProtobuf : OtlpExportProtocol . Grpc , new Uri ( otelExporterOtlpEndpoint ) ) ;
234+ }
227235 }
228236
237+
229238 // builder.AddPrometheusExporter();
230239 // 取消以下注释以启用Azure Monitor导出器(需要Azure.Monitor.OpenTelemetry.AspNetCore包)
231240 //if (!string.IsNullOrEmpty(builder.Configuration["APPLICATIONINSIGHTS_CONNECTION_STRING"]))
0 commit comments