1212// See the License for the specific language governing permissions and
1313// limitations under the License.
1414
15+ using System . Net . Http ;
1516using Serilog . Configuration ;
1617using Serilog . Sinks . OpenTelemetry ;
18+ using Serilog . Sinks . OpenTelemetry . Exporters ;
1719using Serilog . Sinks . PeriodicBatching ;
20+ using Serilog . Collections ;
21+ using Serilog . Core ;
22+ using Serilog . Events ;
1823
1924namespace Serilog ;
2025
@@ -23,6 +28,13 @@ namespace Serilog;
2328/// </summary>
2429public static class OpenTelemetryLoggerConfigurationExtensions
2530{
31+ static HttpMessageHandler ? CreateDefaultHttpMessageHandler ( ) =>
32+ #if FEATURE_SOCKETS_HTTP_HANDLER
33+ new SocketsHttpHandler { ActivityHeadersPropagator = null } ;
34+ #else
35+ null ;
36+ #endif
37+
2638 /// <summary>
2739 /// Send log events to an OTLP exporter.
2840 /// </summary>
@@ -39,14 +51,17 @@ public static LoggerConfiguration OpenTelemetry(
3951 var options = new BatchedOpenTelemetrySinkOptions ( ) ;
4052 configure ( options ) ;
4153
42- var openTelemetrySink = new OpenTelemetrySink (
54+ var exporter = Exporter . Create (
4355 endpoint : options . Endpoint ,
4456 protocol : options . Protocol ,
57+ headers : new Dictionary < string , string > ( options . Headers ) ,
58+ httpMessageHandler : options . HttpMessageHandler ?? CreateDefaultHttpMessageHandler ( ) ) ;
59+
60+ var openTelemetrySink = new OpenTelemetrySink (
61+ exporter : exporter ,
4562 formatProvider : options . FormatProvider ,
4663 resourceAttributes : new Dictionary < string , object > ( options . ResourceAttributes ) ,
47- headers : new Dictionary < string , string > ( options . Headers ) ,
48- includedData : options . IncludedData ,
49- httpMessageHandler : options . HttpMessageHandler ) ;
64+ includedData : options . IncludedData ) ;
5065
5166 var sink = new PeriodicBatchingSink ( openTelemetrySink , options . BatchingOptions ) ;
5267
@@ -65,18 +80,44 @@ public static LoggerConfiguration OpenTelemetry(
6580 /// <param name="protocol">
6681 /// The OTLP protocol to use.
6782 /// </param>
83+ /// <param name="headers">
84+ /// Headers to send with network requests.
85+ /// </param>
86+ /// <param name="resourceAttributes">
87+ /// A attributes of the resource attached to the logs generated by the sink. The values must be simple primitive
88+ /// values: integers, doubles, strings, or booleans. Other values will be silently ignored.
89+ /// </param>
90+ /// <param name="includedData">
91+ /// Which fields should be included in the log events generated by the sink.
92+ /// </param>
93+ /// <param name="restrictedToMinimumLevel">
94+ /// The minimum level for events passed through the sink. Ignored when <paramref name="levelSwitch"/> is specified.
95+ /// </param>
96+ /// <param name="levelSwitch">
97+ /// A switch allowing the pass-through minimum level to be changed at runtime.
98+ /// </param>
6899 /// <returns>Logger configuration, allowing configuration to continue.</returns>
69100 public static LoggerConfiguration OpenTelemetry (
70101 this LoggerSinkConfiguration loggerSinkConfiguration ,
71102 string endpoint = OpenTelemetrySinkOptions . DefaultEndpoint ,
72- OtlpProtocol protocol = OpenTelemetrySinkOptions . DefaultProtocol )
103+ OtlpProtocol protocol = OpenTelemetrySinkOptions . DefaultProtocol ,
104+ IDictionary < string , string > ? headers = null ,
105+ IDictionary < string , object > ? resourceAttributes = null ,
106+ IncludedData ? includedData = null ,
107+ LogEventLevel restrictedToMinimumLevel = LevelAlias . Minimum ,
108+ LoggingLevelSwitch ? levelSwitch = null )
73109 {
74110 if ( loggerSinkConfiguration == null ) throw new ArgumentNullException ( nameof ( loggerSinkConfiguration ) ) ;
75111
76112 return loggerSinkConfiguration . OpenTelemetry ( options =>
77113 {
78114 options . Endpoint = endpoint ;
79115 options . Protocol = protocol ;
116+ options . IncludedData = includedData ?? options . IncludedData ;
117+ options . RestrictedToMinimumLevel = restrictedToMinimumLevel ;
118+ options . LevelSwitch = levelSwitch ;
119+ headers ? . AddTo ( options . Headers ) ;
120+ resourceAttributes ? . AddTo ( options . ResourceAttributes ) ;
80121 } ) ;
81122 }
82123
@@ -97,14 +138,17 @@ public static LoggerConfiguration OpenTelemetry(
97138
98139 configure ( options ) ;
99140
100- var sink = new OpenTelemetrySink (
141+ var exporter = Exporter . Create (
101142 endpoint : options . Endpoint ,
102143 protocol : options . Protocol ,
144+ headers : new Dictionary < string , string > ( options . Headers ) ,
145+ httpMessageHandler : options . HttpMessageHandler ?? CreateDefaultHttpMessageHandler ( ) ) ;
146+
147+ var sink = new OpenTelemetrySink (
148+ exporter : exporter ,
103149 formatProvider : options . FormatProvider ,
104150 resourceAttributes : new Dictionary < string , object > ( options . ResourceAttributes ) ,
105- headers : new Dictionary < string , string > ( options . Headers ) ,
106- includedData : options . IncludedData ,
107- httpMessageHandler : options . HttpMessageHandler ) ;
151+ includedData : options . IncludedData ) ;
108152
109153 return loggerAuditSinkConfiguration . Sink ( sink , options . RestrictedToMinimumLevel , options . LevelSwitch ) ;
110154 }
@@ -121,18 +165,34 @@ public static LoggerConfiguration OpenTelemetry(
121165 /// <param name="protocol">
122166 /// The OTLP protocol to use.
123167 /// </param>
168+ /// <param name="headers">
169+ /// Headers to send with network requests.
170+ /// </param>
171+ /// <param name="resourceAttributes">
172+ /// A attributes of the resource attached to the logs generated by the sink. The values must be simple primitive
173+ /// values: integers, doubles, strings, or booleans. Other values will be silently ignored.
174+ /// </param>
175+ /// <param name="includedData">
176+ /// Which fields should be included in the log events generated by the sink.
177+ /// </param>
124178 /// <returns>Logger configuration, allowing configuration to continue.</returns>
125179 public static LoggerConfiguration OpenTelemetry (
126180 this LoggerAuditSinkConfiguration loggerAuditSinkConfiguration ,
127181 string endpoint = OpenTelemetrySinkOptions . DefaultEndpoint ,
128- OtlpProtocol protocol = OpenTelemetrySinkOptions . DefaultProtocol )
182+ OtlpProtocol protocol = OpenTelemetrySinkOptions . DefaultProtocol ,
183+ IDictionary < string , string > ? headers = null ,
184+ IDictionary < string , object > ? resourceAttributes = null ,
185+ IncludedData ? includedData = null )
129186 {
130187 if ( loggerAuditSinkConfiguration == null ) throw new ArgumentNullException ( nameof ( loggerAuditSinkConfiguration ) ) ;
131188
132189 return loggerAuditSinkConfiguration . OpenTelemetry ( options =>
133190 {
134191 options . Endpoint = endpoint ;
135192 options . Protocol = protocol ;
193+ options . IncludedData = includedData ?? options . IncludedData ;
194+ headers ? . AddTo ( options . Headers ) ;
195+ resourceAttributes ? . AddTo ( options . ResourceAttributes ) ;
136196 } ) ;
137197 }
138198}
0 commit comments