Skip to content

Commit fdbd475

Browse files
committed
Refactor OpenTelemetryEnvironment to use respsect specific endpoint variables for logs and traces.
1 parent cf59d53 commit fdbd475

File tree

3 files changed

+106
-34
lines changed

3 files changed

+106
-34
lines changed

src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/OpenTelemetryEnvironment.cs

Lines changed: 55 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,49 +16,81 @@ namespace Serilog.Sinks.OpenTelemetry.Configuration;
1616

1717
static 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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace Serilog.Sinks.OpenTelemetry.Configuration;
2+
3+
static class OpenTelemetryEnvironmentVariables
4+
{
5+
public const string DefaultEndpoint = "OTEL_EXPORTER_OTLP_ENDPOINT";
6+
public const string DefaultHeaders = "OTEL_EXPORTER_OTLP_HEADERS";
7+
public const string DefaultProtocol = "OTEL_EXPORTER_OTLP_PROTOCOL";
8+
9+
public const string LogsEndpoint = "OTEL_EXPORTER_OTLP_LOGS_ENDPOINT";
10+
11+
public const string TracesEndpoint = "OTEL_EXPORTER_OTLP_TRACES_ENDPOINT";
12+
13+
public const string ServiceName = "OTEL_SERVICE_NAME";
14+
public const string ResourceAttributes = "OTEL_RESOURCE_ATTRIBUTES";
15+
}

test/Serilog.Sinks.OpenTelemetry.Tests/OpenTelemetryEnvironmentTests.cs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,31 @@ namespace Serilog.Sinks.OpenTelemetry.Tests;
55

66
public class OpenTelemetryEnvironmentTests
77
{
8+
[Fact]
9+
public void ConfigureRespectsSpecificEndpointEnvironmentVariableValues()
10+
{
11+
BatchedOpenTelemetrySinkOptions options = new();
12+
13+
var generalEndpoint = "http://general";
14+
var logSpecificEndpoint = "http://log-specific";
15+
var tracesSpecificEndpoint = "http://traces-specific";
16+
17+
OpenTelemetryEnvironment.Configure(options, GetEnvVar);
18+
19+
Assert.Equal(generalEndpoint, options.Endpoint);
20+
Assert.Equal(logSpecificEndpoint, options.LogsEndpoint);
21+
Assert.Equal(tracesSpecificEndpoint, options.TracesEndpoint);
22+
return;
23+
24+
string? GetEnvVar(string name) => name switch
25+
{
26+
OpenTelemetryEnvironmentVariables.DefaultEndpoint => generalEndpoint,
27+
OpenTelemetryEnvironmentVariables.LogsEndpoint => logSpecificEndpoint,
28+
OpenTelemetryEnvironmentVariables.TracesEndpoint => tracesSpecificEndpoint,
29+
_ => null
30+
};
31+
}
32+
833
[Fact]
934
public void ConfigureFillsOptionsWithEnvironmentVariableValues()
1035
{
@@ -30,11 +55,11 @@ public void ConfigureFillsOptionsWithEnvironmentVariableValues()
3055

3156
string? GetEnvVar(string name) => name switch
3257
{
33-
"OTEL_EXPORTER_OTLP_ENDPOINT" => endpoint,
34-
"OTEL_EXPORTER_OTLP_HEADERS" => headers,
35-
"OTEL_RESOURCE_ATTRIBUTES" => resourceAttributes,
36-
"OTEL_EXPORTER_OTLP_PROTOCOL" => "grpc",
37-
"OTEL_SERVICE_NAME" => serviceName,
58+
OpenTelemetryEnvironmentVariables.DefaultEndpoint => endpoint,
59+
OpenTelemetryEnvironmentVariables.DefaultHeaders => headers,
60+
OpenTelemetryEnvironmentVariables.ResourceAttributes => resourceAttributes,
61+
OpenTelemetryEnvironmentVariables.DefaultProtocol => "grpc",
62+
OpenTelemetryEnvironmentVariables.ServiceName => serviceName,
3863
_ => null
3964
};
4065
}
@@ -54,8 +79,8 @@ public void ExplicitServiceNameOverridesResourceAttribute()
5479

5580
string? GetEnvVar(string name) => name switch
5681
{
57-
"OTEL_RESOURCE_ATTRIBUTES" => resourceAttributes,
58-
"OTEL_SERVICE_NAME" => serviceName,
82+
OpenTelemetryEnvironmentVariables.ResourceAttributes => resourceAttributes,
83+
OpenTelemetryEnvironmentVariables.ServiceName => serviceName,
5984
_ => null
6085
};
6186
}
@@ -76,8 +101,8 @@ public void ConfigureAppendPathToEndpointIfProtocolIsHttpProtobufAndEndpointDoes
76101
string? GetEnvVar(string name)
77102
=> name switch
78103
{
79-
"OTEL_EXPORTER_OTLP_ENDPOINT" => endpoint,
80-
"OTEL_EXPORTER_OTLP_PROTOCOL" => "http/protobuf",
104+
OpenTelemetryEnvironmentVariables.DefaultEndpoint => endpoint,
105+
OpenTelemetryEnvironmentVariables.DefaultProtocol => "http/protobuf",
81106
_ => null
82107
};
83108
}
@@ -95,7 +120,7 @@ public void ConfigureThrowsIfHeaderEnvIsInvalidFormat()
95120
string? GetEnvVar(string name)
96121
=> name switch
97122
{
98-
"OTEL_EXPORTER_OTLP_HEADERS" => headers,
123+
OpenTelemetryEnvironmentVariables.DefaultHeaders => headers,
99124
_ => null
100125
};
101126
}
@@ -113,7 +138,7 @@ public void ConfigureThrowsIfResourceAttributesEnvIsInvalidFormat()
113138
string? GetEnvVar(string name)
114139
=> name switch
115140
{
116-
"OTEL_RESOURCE_ATTRIBUTES" => resourceAttributes,
141+
OpenTelemetryEnvironmentVariables.ResourceAttributes => resourceAttributes,
117142
_ => null
118143
};
119144
}

0 commit comments

Comments
 (0)