-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #141 from AlbertoMonteiro/dev
Added OpenTelemetryEnvironment
- Loading branch information
Showing
5 changed files
with
163 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/Serilog.Sinks.OpenTelemetry/Serilog.Sinks.OpenTelemetry.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/OpenTelemetryEnvironment.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
namespace Serilog.Sinks.OpenTelemetry.Configuration; | ||
|
||
static class OpenTelemetryEnvironment | ||
{ | ||
private const string PROTOCOL = "OTEL_EXPORTER_OTLP_PROTOCOL"; | ||
private const string ENDPOINT = "OTEL_EXPORTER_OTLP_ENDPOINT"; | ||
private const string HEADERS = "OTEL_EXPORTER_OTLP_HEADERS"; | ||
private const string RESOURCE_ATTRIBUTES = "OTEL_RESOURCE_ATTRIBUTES"; | ||
|
||
public static void Configure(BatchedOpenTelemetrySinkOptions options, Func<string, string?> getEnvironmentVariable) | ||
{ | ||
options.Protocol = getEnvironmentVariable(PROTOCOL) switch | ||
{ | ||
"http/protobuf" => OtlpProtocol.HttpProtobuf, | ||
"grpc" => OtlpProtocol.Grpc, | ||
_ => options.Protocol | ||
}; | ||
|
||
if (getEnvironmentVariable(ENDPOINT) is { Length: > 1 } endpoint) | ||
options.Endpoint = endpoint; | ||
|
||
if (options.Protocol == OtlpProtocol.HttpProtobuf && !string.IsNullOrEmpty(options.Endpoint) && !options.Endpoint.EndsWith("/v1/logs")) | ||
options.Endpoint = $"{options.Endpoint}/v1/logs"; | ||
|
||
FillHeadersIfPresent(getEnvironmentVariable(HEADERS), options.Headers); | ||
|
||
FillHeadersResourceAttributesIfPresent(getEnvironmentVariable(RESOURCE_ATTRIBUTES), options.ResourceAttributes); | ||
} | ||
|
||
private static void FillHeadersIfPresent(string? config, IDictionary<string, string> headers) | ||
{ | ||
foreach (var part in config?.Split(',') ?? []) | ||
{ | ||
if (part.Split('=') is { Length: 2 } parts) | ||
headers.Add(parts[0], parts[1]); | ||
else | ||
throw new InvalidOperationException($"Invalid header format: {part} in {HEADERS} environment variable."); | ||
} | ||
} | ||
|
||
private static void FillHeadersResourceAttributesIfPresent(string? config, IDictionary<string, object> resourceAttributes) | ||
{ | ||
foreach (var part in config?.Split(',') ?? []) | ||
{ | ||
if (part.Split('=') is { Length: 2 } parts) | ||
resourceAttributes.Add(parts[0], parts[1]); | ||
else | ||
throw new InvalidOperationException($"Invalid resourceAttributes format: {part} in {RESOURCE_ATTRIBUTES} environment variable."); | ||
} | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
test/Serilog.Sinks.OpenTelemetry.Tests/OpenTelemetryEnvironmentTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
using Serilog.Sinks.OpenTelemetry.Configuration; | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.OpenTelemetry.Tests; | ||
|
||
public class OpenTelemetryEnvironmentTests | ||
{ | ||
[Fact] | ||
public void ConfigureFillOptionsWithEnvironmentVariablesValues() | ||
{ | ||
BatchedOpenTelemetrySinkOptions options = new(); | ||
var endpoint = "http://localhost"; | ||
var protocol = OtlpProtocol.Grpc; | ||
var headers = "header1=1,header2=2"; | ||
var resourceAttributes = "name1=1,name2=2"; | ||
|
||
OpenTelemetryEnvironment.Configure(options, GetEnvVar); | ||
|
||
Assert.Equal(endpoint, options.Endpoint); | ||
Assert.Equal(protocol, options.Protocol); | ||
Assert.Collection(options.Headers, | ||
e => Assert.Equal(("header1", "1"), (e.Key, e.Value)), | ||
e => Assert.Equal(("header2", "2"), (e.Key, e.Value))); | ||
Assert.Collection(options.ResourceAttributes, | ||
e => Assert.Equal(("name1", "1"), (e.Key, e.Value)), | ||
e => Assert.Equal(("name2", "2"), (e.Key, e.Value))); | ||
|
||
string? GetEnvVar(string name) | ||
=> name switch | ||
{ | ||
"OTEL_EXPORTER_OTLP_ENDPOINT" => endpoint, | ||
"OTEL_EXPORTER_OTLP_HEADERS" => headers, | ||
"OTEL_RESOURCE_ATTRIBUTES" => resourceAttributes, | ||
"OTEL_EXPORTER_OTLP_PROTOCOL" => "grpc", | ||
_ => null | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void ConfigureAppendPathToEndpointIfProtocolIsHttpProtobufAndEndpointDoesntEndsWithProperValue() | ||
{ | ||
BatchedOpenTelemetrySinkOptions options = new(); | ||
var endpoint = "http://localhost"; | ||
var protocol = OtlpProtocol.HttpProtobuf; | ||
|
||
OpenTelemetryEnvironment.Configure(options, GetEnvVar); | ||
|
||
Assert.Equal($"{endpoint}/v1/logs", options.Endpoint); | ||
Assert.Equal(protocol, options.Protocol); | ||
|
||
string? GetEnvVar(string name) | ||
=> name switch | ||
{ | ||
"OTEL_EXPORTER_OTLP_ENDPOINT" => endpoint, | ||
"OTEL_EXPORTER_OTLP_PROTOCOL" => "http/protobuf", | ||
_ => null | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void ConfigureThrowsIfHeaderEnvIsInvalidFormat() | ||
{ | ||
BatchedOpenTelemetrySinkOptions options = new(); | ||
var headers = "header1"; | ||
|
||
var exception = Assert.Throws<InvalidOperationException>(() => OpenTelemetryEnvironment.Configure(options, GetEnvVar)); | ||
|
||
Assert.Equal("Invalid header format: header1 in OTEL_EXPORTER_OTLP_HEADERS environment variable.", exception.Message); | ||
|
||
string? GetEnvVar(string name) | ||
=> name switch | ||
{ | ||
"OTEL_EXPORTER_OTLP_HEADERS" => headers, | ||
_ => null | ||
}; | ||
} | ||
|
||
[Fact] | ||
public void ConfigureThrowsIfResourceAttributesEnvIsInvalidFormat() | ||
{ | ||
BatchedOpenTelemetrySinkOptions options = new(); | ||
var resourceAttributes = "resource1"; | ||
|
||
var exception = Assert.Throws<InvalidOperationException>(() => OpenTelemetryEnvironment.Configure(options, GetEnvVar)); | ||
|
||
Assert.Equal("Invalid resourceAttributes format: resource1 in OTEL_RESOURCE_ATTRIBUTES environment variable.", exception.Message); | ||
|
||
string? GetEnvVar(string name) | ||
=> name switch | ||
{ | ||
"OTEL_RESOURCE_ATTRIBUTES" => resourceAttributes, | ||
_ => null | ||
}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters