Skip to content

Commit 5e08595

Browse files
committed
Baggage format parsing tests
1 parent 13b1d05 commit 5e08595

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,19 @@ static class BaggageFormat
1212
// See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable
1313
// See: https://www.w3.org/TR/baggage/#header-content
1414

15+
if (string.IsNullOrWhiteSpace(baggageString)) yield break;
16+
1517
foreach (var listMember in baggageString.Split(','))
1618
{
1719
// The baggage spec allows list members to carry additional key-value pair metadata after the initial
1820
// key and value and a trailing semicolon, but this is disallowed by the OTel spec. We're pretty loose with
1921
// validation, here, but could tighten up handling of invalid values in the future.
2022

2123
var eq = listMember.IndexOf('=');
22-
if (eq == -1) RejectInvalidListMember(listMember, environmentVariableName);
24+
if (eq == -1) RejectInvalidListMember(listMember, environmentVariableName, nameof(baggageString));
2325

2426
var key = listMember.Substring(0, eq).Trim();
25-
if (string.IsNullOrEmpty(key)) RejectInvalidListMember(listMember, environmentVariableName);
27+
if (string.IsNullOrEmpty(key)) RejectInvalidListMember(listMember, environmentVariableName, nameof(baggageString));
2628

2729
var escapedValue = eq == listMember.Length - 1 ? "" : listMember.Substring(eq + 1).Trim();
2830
var value = Uri.UnescapeDataString(escapedValue);
@@ -31,8 +33,8 @@ static class BaggageFormat
3133
}
3234
}
3335

34-
static void RejectInvalidListMember(string listMember, string environmentVariableName)
36+
static void RejectInvalidListMember(string listMember, string environmentVariableName, string paramName)
3537
{
36-
throw new InvalidOperationException($"Invalid item format `{listMember}` in {environmentVariableName} environment variable.");
38+
throw new ArgumentException($"Invalid item format `{listMember}` in {environmentVariableName} environment variable.", paramName);
3739
}
3840
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using Serilog.Sinks.OpenTelemetry.Configuration;
2+
using Serilog.Sinks.OpenTelemetry.Tests.Support;
3+
using Xunit;
4+
5+
namespace Serilog.Sinks.OpenTelemetry.Tests;
6+
7+
public class BaggageFormatTests
8+
{
9+
public static TheoryData<string, (string, string)[]> Cases => new()
10+
{
11+
{ "", [] },
12+
{ " ", [] },
13+
{ "a=", [("a", "")] },
14+
{ "abc=def", [("abc", "def")] },
15+
{ "abc= def ", [("abc", "def")] },
16+
{ "abc=def,ghi=jkl", [("abc", "def"), ("ghi", "jkl")] },
17+
{ "a=1%202", [("a", "1 2")] },
18+
};
19+
20+
[Theory, MemberData(nameof(Cases))]
21+
public void BaggageStringsAreDecoded(string baggageString, IEnumerable<(string, string)> expected)
22+
{
23+
var actual = BaggageFormat.DecodeBaggageString(baggageString, Some.String());
24+
Assert.Equal(expected, actual);
25+
}
26+
27+
[Theory]
28+
[InlineData(",")]
29+
[InlineData(", ")]
30+
[InlineData("a")]
31+
[InlineData("=")]
32+
[InlineData(",=")]
33+
public void InvalidBaggageStringsAreRejected(string baggageString)
34+
{
35+
Assert.Throws<ArgumentException>(() => BaggageFormat.DecodeBaggageString(baggageString, Some.String()).ToList());
36+
}
37+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public void ConfigureThrowsIfHeaderEnvIsInvalidFormat()
8888
BatchedOpenTelemetrySinkOptions options = new();
8989
var headers = "header1";
9090

91-
var exception = Assert.Throws<InvalidOperationException>(() => OpenTelemetryEnvironment.Configure(options, GetEnvVar));
91+
var exception = Assert.Throws<ArgumentException>(() => OpenTelemetryEnvironment.Configure(options, GetEnvVar));
9292

93-
Assert.Equal("Invalid item format `header1` in OTEL_EXPORTER_OTLP_HEADERS environment variable.", exception.Message);
93+
Assert.StartsWith("Invalid item format `header1` in OTEL_EXPORTER_OTLP_HEADERS environment variable.", exception.Message);
9494

9595
string? GetEnvVar(string name)
9696
=> name switch
@@ -106,9 +106,9 @@ public void ConfigureThrowsIfResourceAttributesEnvIsInvalidFormat()
106106
BatchedOpenTelemetrySinkOptions options = new();
107107
var resourceAttributes = "resource1";
108108

109-
var exception = Assert.Throws<InvalidOperationException>(() => OpenTelemetryEnvironment.Configure(options, GetEnvVar));
109+
var exception = Assert.Throws<ArgumentException>(() => OpenTelemetryEnvironment.Configure(options, GetEnvVar));
110110

111-
Assert.Equal("Invalid item format `resource1` in OTEL_RESOURCE_ATTRIBUTES environment variable.", exception.Message);
111+
Assert.StartsWith("Invalid item format `resource1` in OTEL_RESOURCE_ATTRIBUTES environment variable.", exception.Message);
112112

113113
string? GetEnvVar(string name)
114114
=> name switch

0 commit comments

Comments
 (0)