Skip to content

Commit

Permalink
Merge pull request #62 from evilpilaf/nulls
Browse files Browse the repository at this point in the history
Ignore properties with null values
  • Loading branch information
evilpilaf authored Jan 26, 2021
2 parents 97c1c98 + c988a16 commit 7ced0e2
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 4 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
[*]
indent_style = space
indent_size = 2
end_of_line = lf

# Code files
[*.{cs,csx,vb,vbx}]
Expand Down
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* text=auto
* text eol=lf
Binary file modified assets/icon124x124.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 10 additions & 1 deletion src/Honeycomb.Serilog.Sink/Formatters/RawJsonFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,17 @@ public static void FormatContent(LogEvent logEvent, TextWriter output)
private static void WriteProperties(IReadOnlyDictionary<string, LogEventPropertyValue> 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);
Expand Down
2 changes: 2 additions & 0 deletions src/Honeycomb.Serilog.Sink/HoneycombSerilogSink.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ public async Task EmitBatchAsync(IEnumerable<LogEvent> 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))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.0.1">
<PackageReference Include="coverlet.collector" Version="3.0.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
Expand Down
40 changes: 39 additions & 1 deletion test/Honeycomb.Serilog.Sink.Tests/HoneycombSerilogSinkTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

using Serilog.Events;
using Serilog.Parsing;
using Serilog.Sinks.PeriodicBatching;

using Xunit;

Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 7ced0e2

Please sign in to comment.