22using System . Collections . Generic ;
33using System . Diagnostics ;
44using System . Diagnostics . Metrics ;
5+ using System . Linq ;
56using System . Threading ;
67using Microsoft . Extensions . Logging ;
78using OpenTelemetry ;
@@ -367,11 +368,7 @@ private static TracerProvider BuildTracerProvider(ResourceBuilder resourceBuilde
367368 {
368369 builder . AddOtlpExporter ( options =>
369370 {
370- options . Endpoint = new Uri ( _configuration . OtlpEndpoint ) ;
371- options . Protocol = _configuration . UseOtlpHttp
372- ? OtlpExportProtocol . HttpProtobuf
373- : OtlpExportProtocol . Grpc ;
374- options . TimeoutMilliseconds = _configuration . ExportTimeoutMilliseconds ;
371+ ConfigureOtlpExporter ( options ) ;
375372 } ) ;
376373 }
377374
@@ -395,11 +392,7 @@ private static OpenTelemetry.Metrics.MeterProvider BuildMeterProvider(ResourceBu
395392 {
396393 builder . AddOtlpExporter ( options =>
397394 {
398- options . Endpoint = new Uri ( _configuration . OtlpEndpoint ) ;
399- options . Protocol = _configuration . UseOtlpHttp
400- ? OtlpExportProtocol . HttpProtobuf
401- : OtlpExportProtocol . Grpc ;
402- options . TimeoutMilliseconds = _configuration . ExportTimeoutMilliseconds ;
395+ ConfigureOtlpExporter ( options ) ;
403396 } ) ;
404397 }
405398
@@ -427,11 +420,7 @@ private static ILoggerFactory BuildLoggerFactory(ResourceBuilder resourceBuilder
427420 {
428421 options . AddOtlpExporter ( exporterOptions =>
429422 {
430- exporterOptions . Endpoint = new Uri ( _configuration . OtlpEndpoint ) ;
431- exporterOptions . Protocol = _configuration . UseOtlpHttp
432- ? OtlpExportProtocol . HttpProtobuf
433- : OtlpExportProtocol . Grpc ;
434- exporterOptions . TimeoutMilliseconds = _configuration . ExportTimeoutMilliseconds ;
423+ ConfigureOtlpExporter ( exporterOptions ) ;
435424 } ) ;
436425 }
437426
@@ -444,6 +433,23 @@ private static ILoggerFactory BuildLoggerFactory(ResourceBuilder resourceBuilder
444433 } ) ;
445434 }
446435
436+ private static void ConfigureOtlpExporter ( OtlpExporterOptions options )
437+ {
438+ options . Endpoint = new Uri ( _configuration . OtlpEndpoint ) ;
439+ options . Protocol = _configuration . UseOtlpHttp
440+ ? OtlpExportProtocol . HttpProtobuf
441+ : OtlpExportProtocol . Grpc ;
442+ options . TimeoutMilliseconds = _configuration . ExportTimeoutMilliseconds ;
443+
444+ // Add custom headers if configured
445+ if ( _configuration . OtlpHeaders . Count > 0 )
446+ {
447+ var headerString = string . Join ( "," ,
448+ _configuration . OtlpHeaders . Select ( kvp => $ "{ kvp . Key } ={ kvp . Value } ") ) ;
449+ options . Headers = headerString ;
450+ }
451+ }
452+
447453 private static void ThrowIfNotInitialized ( )
448454 {
449455 if ( ! _isInitialized )
0 commit comments