-
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 #164 from serilog/dev
4.1.1 Release
- Loading branch information
Showing
18 changed files
with
135 additions
and
125 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
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
8 changes: 0 additions & 8 deletions
8
.idea/.idea.serilog-sinks-opentelemetry/.idea/indexLayout.xml
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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
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
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
40 changes: 40 additions & 0 deletions
40
src/Serilog.Sinks.OpenTelemetry/Sinks/OpenTelemetry/Configuration/BaggageFormat.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,40 @@ | ||
namespace Serilog.Sinks.OpenTelemetry.Configuration; | ||
|
||
static class BaggageFormat | ||
{ | ||
/// <summary> | ||
/// Decode W3C Baggage-formatted key-value pairs as specified for handling of the `OTEL_EXPORTER_OTLP_HEADERS` and | ||
/// `OTEL_RESOURCE_ATTRIBUTES` environment variables. | ||
/// </summary> | ||
/// <returns>The property names and values encoded in the supplied <paramref name="baggageString"/>.</returns> | ||
public static IEnumerable<(string, string)> DecodeBaggageString(string baggageString, string environmentVariableName) | ||
{ | ||
// See: https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/resource/sdk.md#specifying-resource-information-via-an-environment-variable | ||
// See: https://www.w3.org/TR/baggage/#header-content | ||
|
||
if (string.IsNullOrWhiteSpace(baggageString)) yield break; | ||
|
||
foreach (var listMember in baggageString.Split(',')) | ||
{ | ||
// The baggage spec allows list members to carry additional key-value pair metadata after the initial | ||
// key and value and a trailing semicolon, but this is disallowed by the OTel spec. We're pretty loose with | ||
// validation, here, but could tighten up handling of invalid values in the future. | ||
|
||
var eq = listMember.IndexOf('='); | ||
if (eq == -1) RejectInvalidListMember(listMember, environmentVariableName, nameof(baggageString)); | ||
|
||
var key = listMember.Substring(0, eq).Trim(); | ||
if (string.IsNullOrEmpty(key)) RejectInvalidListMember(listMember, environmentVariableName, nameof(baggageString)); | ||
|
||
var escapedValue = eq == listMember.Length - 1 ? "" : listMember.Substring(eq + 1).Trim(); | ||
var value = Uri.UnescapeDataString(escapedValue); | ||
|
||
yield return (key, value); | ||
} | ||
} | ||
|
||
static void RejectInvalidListMember(string listMember, string environmentVariableName, string paramName) | ||
{ | ||
throw new ArgumentException($"Invalid item format `{listMember}` in {environmentVariableName} environment variable.", paramName); | ||
} | ||
} |
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
39 changes: 39 additions & 0 deletions
39
test/Serilog.Sinks.OpenTelemetry.Tests/BaggageFormatTests.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,39 @@ | ||
using Serilog.Sinks.OpenTelemetry.Configuration; | ||
using Serilog.Sinks.OpenTelemetry.Tests.Support; | ||
using Xunit; | ||
|
||
namespace Serilog.Sinks.OpenTelemetry.Tests; | ||
|
||
public class BaggageFormatTests | ||
{ | ||
public static TheoryData<string, (string, string)[]> Cases => new() | ||
{ | ||
{ "", [] }, | ||
{ " ", [] }, | ||
{ "a=", [("a", "")] }, | ||
{ "abc=def", [("abc", "def")] }, | ||
{ "abc= def ", [("abc", "def")] }, | ||
{ "abc=def,ghi=jkl", [("abc", "def"), ("ghi", "jkl")] }, | ||
{ "a=1%202", [("a", "1 2")] }, | ||
{ "a=b=c,d=e", [("a", "b=c"), ("d", "e")] }, | ||
{ "a=%2C,b=c", [("a", ","), ("b", "c")] } | ||
}; | ||
|
||
[Theory, MemberData(nameof(Cases))] | ||
public void BaggageStringsAreDecoded(string baggageString, IEnumerable<(string, string)> expected) | ||
{ | ||
var actual = BaggageFormat.DecodeBaggageString(baggageString, Some.String()); | ||
Assert.Equal(expected, actual); | ||
} | ||
|
||
[Theory] | ||
[InlineData(",")] | ||
[InlineData(", ")] | ||
[InlineData("a")] | ||
[InlineData("=")] | ||
[InlineData(",=")] | ||
public void InvalidBaggageStringsAreRejected(string baggageString) | ||
{ | ||
Assert.Throws<ArgumentException>(() => BaggageFormat.DecodeBaggageString(baggageString, Some.String()).ToList()); | ||
} | ||
} |
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
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
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
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
13 changes: 12 additions & 1 deletion
13
test/Serilog.Sinks.OpenTelemetry.Tests/Serilog.Sinks.OpenTelemetry.Tests.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