@@ -16,49 +16,81 @@ namespace Serilog.Sinks.OpenTelemetry.Configuration;
1616
1717static class OpenTelemetryEnvironment
1818{
19- const string ProtocolVarName = "OTEL_EXPORTER_OTLP_PROTOCOL" ;
20- const string EndpointVarName = "OTEL_EXPORTER_OTLP_ENDPOINT" ;
21- const string HeaderVarName = "OTEL_EXPORTER_OTLP_HEADERS" ;
22- const string ResourceAttributesVarName = "OTEL_RESOURCE_ATTRIBUTES" ;
23- const string ServiceNameVarName = "OTEL_SERVICE_NAME" ;
24-
2519 public static void Configure ( BatchedOpenTelemetrySinkOptions options , Func < string , string ? > getConfigurationVariable )
2620 {
27- options . Protocol = getConfigurationVariable ( ProtocolVarName ) switch
28- {
29- "http/protobuf" => OtlpProtocol . HttpProtobuf ,
30- "grpc" => OtlpProtocol . Grpc ,
31- _ => options . Protocol
32- } ;
21+ if ( ParseProtocol ( getConfigurationVariable ( OpenTelemetryEnvironmentVariables . DefaultProtocol ) ) is { } protocol )
22+ options . Protocol = protocol ;
3323
34- if ( getConfigurationVariable ( EndpointVarName ) is { Length : > 1 } endpoint )
24+ if ( getConfigurationVariable ( OpenTelemetryEnvironmentVariables . DefaultEndpoint ) is { Length : > 1 } endpoint )
3525 options . Endpoint = endpoint ;
3626
37- FillHeadersIfPresent ( getConfigurationVariable ( HeaderVarName ) , options . Headers ) ;
27+ FillLogsConfigUsingSpecificationEnvVars ( options , getConfigurationVariable ) ;
3828
39- FillHeadersResourceAttributesIfPresent ( getConfigurationVariable ( ResourceAttributesVarName ) , options . ResourceAttributes ) ;
29+ FillTracesConfigUsingSpecificationEnvVars ( options , getConfigurationVariable ) ;
4030
41- if ( getConfigurationVariable ( ServiceNameVarName ) is { Length : > 1 } serviceName )
31+ FillDictionary (
32+ options . Headers ,
33+ getConfigurationVariable ,
34+ OpenTelemetryEnvironmentVariables . DefaultHeaders ) ;
35+
36+ FillDictionary (
37+ options . ResourceAttributes ,
38+ getConfigurationVariable ,
39+ OpenTelemetryEnvironmentVariables . ResourceAttributes ) ;
40+
41+ if ( getConfigurationVariable ( OpenTelemetryEnvironmentVariables . ServiceName ) is { Length : > 1 } serviceName )
4242 {
4343 options . ResourceAttributes [ SemanticConventions . AttributeServiceName ] = serviceName ;
4444 }
4545 }
4646
47- static void FillHeadersIfPresent ( string ? config , IDictionary < string , string > headers )
47+ static OtlpProtocol ? ParseProtocol ( string ? protocol )
48+ {
49+ return protocol switch
50+ {
51+ "http/protobuf" => OtlpProtocol . HttpProtobuf ,
52+ "grpc" => OtlpProtocol . Grpc ,
53+ _ => null
54+ } ;
55+ }
56+
57+ static void FillLogsConfigUsingSpecificationEnvVars (
58+ BatchedOpenTelemetrySinkOptions options ,
59+ Func < string , string ? > getConfigurationVariable )
60+ {
61+ options . LogsEndpoint = getConfigurationVariable ( OpenTelemetryEnvironmentVariables . LogsEndpoint ) ;
62+ }
63+
64+ static void FillTracesConfigUsingSpecificationEnvVars (
65+ BatchedOpenTelemetrySinkOptions options ,
66+ Func < string , string ? > getConfigurationVariable )
67+ {
68+ options . TracesEndpoint = getConfigurationVariable ( OpenTelemetryEnvironmentVariables . TracesEndpoint ) ;
69+ }
70+
71+ static void FillDictionary (
72+ IDictionary < string , string > dictionary ,
73+ Func < string , string ? > getConfigurationVariable ,
74+ string environmentVariableName )
4875 {
76+ var config = getConfigurationVariable ( environmentVariableName ) ;
4977 if ( config == null ) return ;
50- foreach ( var ( key , value ) in BaggageFormat . DecodeBaggageString ( config , HeaderVarName ) )
78+ foreach ( var ( key , value ) in BaggageFormat . DecodeBaggageString ( config , environmentVariableName ) )
5179 {
52- headers [ key ] = value ;
80+ dictionary [ key ] = value ;
5381 }
5482 }
5583
56- static void FillHeadersResourceAttributesIfPresent ( string ? config , IDictionary < string , object > resourceAttributes )
84+ static void FillDictionary (
85+ IDictionary < string , object > dictionary ,
86+ Func < string , string ? > getConfigurationVariable ,
87+ string environmentVariableName )
5788 {
89+ var config = getConfigurationVariable ( environmentVariableName ) ;
5890 if ( config == null ) return ;
59- foreach ( var ( key , value ) in BaggageFormat . DecodeBaggageString ( config , ResourceAttributesVarName ) )
91+ foreach ( var ( key , value ) in BaggageFormat . DecodeBaggageString ( config , environmentVariableName ) )
6092 {
61- resourceAttributes [ key ] = value ;
93+ dictionary [ key ] = value ;
6294 }
6395 }
64- }
96+ }
0 commit comments