@@ -87,20 +87,25 @@ public WebApplicationBuilder AddOtel(string serviceName)
8787 {
8888 var apiKey = builder . Configuration [ "OpenTelemetry:ApiKey" ] ;
8989 var authorization = string . IsNullOrEmpty ( apiKey ) ? null : $ "Authorization={ apiKey } ";
90- // 1. 配置通用服务名称和资源属性(推荐方式)
91- ResourceBuilder . CreateDefault ( )
92- . AddService (
93- serviceName : serviceName , // 替换为你的服务名
94- serviceVersion : "1.0.0" )
95- . AddAttributes ( new Dictionary < string , object >
96- {
97- [ "deployment.environment" ] = builder . Environment . EnvironmentName
98- } ) ;
99-
90+ var samplerProbability = builder . Configuration . GetSection ( "OpenTelemetry" )
91+ . GetValue < double ? > ( "SamplerProbability" ) ?? 0.5 ;
92+ var instanceId =
93+ $ "{ Environment . GetEnvironmentVariable ( "DAPR_HOST_IP" ) ?? Environment . GetEnvironmentVariable ( "HOST_IP" ) } :{ Environment . GetEnvironmentVariable ( "DAPR_HTTP_PORT" ) } ";
94+ instanceId = string . IsNullOrWhiteSpace ( instanceId ) ? null : instanceId ;
95+ var @namespace = builder . Configuration [ "OpenTelemetry:Namespace" ] ;
10096 // 2. 添加 OpenTelemetry 服务
10197 builder . Services . AddOpenTelemetry ( )
102- // 配置资源,让所有信号(Trace/Metrics/Logs)共享
103- . ConfigureResource ( r => r . AddService ( serviceName ) )
98+ . ConfigureResource ( configure =>
99+ {
100+ configure . AddService (
101+ serviceName : serviceName , // 替换为你的服务名
102+ serviceVersion : "1.0.0" , serviceInstanceId : instanceId , serviceNamespace : @namespace ,
103+ autoGenerateServiceInstanceId : true )
104+ . AddAttributes ( new Dictionary < string , object >
105+ {
106+ [ "deployment.environment" ] = builder . Environment . EnvironmentName
107+ } ) ;
108+ } )
104109 // 🔍 追踪(Traces)配置
105110 . WithTracing ( tracing => tracing
106111 . AddAspNetCoreInstrumentation ( options =>
@@ -110,13 +115,14 @@ public WebApplicationBuilder AddOtel(string serviceName)
110115 options . Filter = context => ! context . Request . Path . StartsWithSegments ( "/health" ) ;
111116 } )
112117 . AddHttpClientInstrumentation ( ) // 追踪 HttpClient 调用
113- . SetSampler ( new ParentBasedSampler ( new AlwaysOnSampler ( ) ) ) // 全量采样(生产可调整)
118+ . SetSampler ( new ParentBasedSampler ( new TraceIdRatioBasedSampler ( samplerProbability ) ) ) // 采样率
114119 . AddOtlpExporter ( options =>
115120 {
116121 options . Endpoint = new Uri ( otlpEndpoint ) ;
117122 options . ExportProcessorType = ExportProcessorType . Batch ; // 批量导出提升性能
118123 // 👇 关键:添加 Authorization Header
119124 options . Headers = authorization ;
125+ options . Protocol = OpenTelemetry . Exporter . OtlpExportProtocol . Grpc ;
120126 } ) )
121127 // 📊 指标(Metrics)配置
122128 . WithMetrics ( metrics => metrics
@@ -129,6 +135,8 @@ public WebApplicationBuilder AddOtel(string serviceName)
129135 options . Endpoint = new Uri ( otlpEndpoint ) ;
130136 // 👇 关键:添加 Authorization Header
131137 options . Headers = authorization ;
138+ options . ExportProcessorType = ExportProcessorType . Batch ; // 批量导出提升性能
139+ options . Protocol = OpenTelemetry . Exporter . OtlpExportProtocol . Grpc ;
132140 } ) )
133141 // 📝 日志(Logs)配置
134142 // 统一由 Serilog 管理
0 commit comments