From 6a9ac36d5d8cf757e3364cd922a1cd6ca1309631 Mon Sep 17 00:00:00 2001 From: evilpilaf Date: Tue, 26 Jan 2021 21:01:53 +0100 Subject: [PATCH 1/3] Skip properties in json where they have empty or null values --- .../Formatters/RawJsonFormatter.cs | 11 ++++- .../HoneycombSerilogSinkTests.cs | 40 ++++++++++++++++++- 2 files changed, 49 insertions(+), 2 deletions(-) diff --git a/src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs b/src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs index 30c0e08..19f6c8d 100644 --- a/src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs +++ b/src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs @@ -53,8 +53,17 @@ public static void FormatContent(LogEvent logEvent, TextWriter output) private static void WriteProperties(IReadOnlyDictionary properties, TextWriter output) { const string precedingDelimiter = ","; - foreach (var property in properties) + foreach (var property in properties.Where(p => p.Value != null && !string.IsNullOrWhiteSpace(p.Value.ToString()))) { + // Skip properties with empty values + if (property.Value is ScalarValue v) + { + if (v.Value == null || v.Value.ToString().Equals("")) + { + continue; + } + } + output.Write(precedingDelimiter); JsonValueFormatter.WriteQuotedJsonString(property.Key, output); diff --git a/test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs b/test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs index c5e5a97..e83d7d5 100644 --- a/test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs +++ b/test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs @@ -12,7 +12,6 @@ using Serilog.Events; using Serilog.Parsing; -using Serilog.Sinks.PeriodicBatching; using Xunit; @@ -186,6 +185,45 @@ public async Task Emit_GivenAMessageWithProperties_SendsThemAllAsync() } } + [Theory] + [InlineData("")] + [InlineData(null)] + public async Task Emit_GivenAMessageWithEmptyPropertyValue_SkipsSendingProperty(string property) + { + const string dataset = nameof(dataset); + const string apiKey = nameof(apiKey); + + HttpClientStub clientStub = A.HttpClient(); + + var sut = CreateSut(dataset, apiKey, clientStub); + + var level = LogEventLevel.Fatal; + + var messageTemplateString = $"Testing message property {{{nameof(property)}}}"; + + var eventToSend = Some.LogEvent(level, messageTemplateString, property); + + await sut.EmitTestable(eventToSend); + + var requestContent = clientStub.RequestContent; + using (var document = JsonDocument.Parse(requestContent)) + using (new AssertionScope()) + { + document.RootElement.ValueKind.Should().Be(JsonValueKind.Array); + document.RootElement.GetArrayLength().Should().Be(1); + JsonElement sentEvent = document.RootElement.EnumerateArray().Single(); + + sentEvent.GetProperty("time").GetDateTimeOffset().Should().Be(eventToSend.Timestamp); + sentEvent.GetProperty("data").ValueKind.Should().Be(JsonValueKind.Object); + + JsonElement data = sentEvent.GetProperty("data"); + + var exists = data.TryGetProperty(nameof(property), out _); + + exists.Should().BeFalse(); + } + } + private HoneycombSerilogSinkStub CreateSut(string dataset, string apiKey, HttpClient client = null) { return new HoneycombSerilogSinkStub(client, dataset, apiKey); From a293df0047688ad6d94980b497fe80360d76f10f Mon Sep 17 00:00:00 2001 From: evilpilaf Date: Tue, 26 Jan 2021 21:05:16 +0100 Subject: [PATCH 2/3] Bump coverlet.collector --- .../Honeycomb.Serilog.Sink.Tests.csproj | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/Honeycomb.Serilog.Sink.Tests/Honeycomb.Serilog.Sink.Tests.csproj b/test/Honeycomb.Serilog.Sink.Tests/Honeycomb.Serilog.Sink.Tests.csproj index 3647ba0..9cbf309 100644 --- a/test/Honeycomb.Serilog.Sink.Tests/Honeycomb.Serilog.Sink.Tests.csproj +++ b/test/Honeycomb.Serilog.Sink.Tests/Honeycomb.Serilog.Sink.Tests.csproj @@ -13,7 +13,7 @@ runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all From c988a16878d4943e604e27252954b32eacfac11c Mon Sep 17 00:00:00 2001 From: evilpilaf Date: Tue, 26 Jan 2021 21:08:02 +0100 Subject: [PATCH 3/3] normalize line endings --- .editorconfig | 1 + .gitattributes | 2 +- assets/icon124x124.png | Bin 5871 -> 5870 bytes .../HoneycombSerilogSink.cs | 2 ++ 4 files changed, 4 insertions(+), 1 deletion(-) diff --git a/.editorconfig b/.editorconfig index 5b064f9..3207a35 100644 --- a/.editorconfig +++ b/.editorconfig @@ -6,6 +6,7 @@ [*] indent_style = space indent_size = 2 +end_of_line = lf # Code files [*.{cs,csx,vb,vbx}] diff --git a/.gitattributes b/.gitattributes index 756b5d9..3bb8b39 100644 --- a/.gitattributes +++ b/.gitattributes @@ -1 +1 @@ -* text=auto +* text eol=lf diff --git a/assets/icon124x124.png b/assets/icon124x124.png index 5c6423b81c4db1481b00cbdd6e5ffdd8d8d43492..cd808e1207e812033742466007fe5c981b1f1907 100644 GIT binary patch delta 13 UcmaE_`%agoGr-S%BkL events) await SendBatchedEvents(writer!.ToString()).ConfigureAwait(false); } + // ReSharper disable UnusedMember.Local private async Task SendBatchedEvents(Stream events) + // ReSharper restore UnusedMember.Local { using var requestMessage = new HttpRequestMessage(HttpMethod.Post, string.Format(HoneycombBatchEndpointTemplate, _teamId)) {