Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include a large event body sample when generating oversized event placeholders #193

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions src/Serilog.Sinks.Seq/Sinks/Seq/ConstrainedBufferedFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,8 @@ static bool CheckEventBodySize(string jsonLine, long? eventBodyLimitBytes)

static LogEvent CreateOversizeEventPlaceholder(LogEvent logEvent, string jsonLine, long eventBodyLimitBytes)
{
// If the limit is so constrained as to disallow sending 512 bytes + packaging, that's okay - we'll just drop
// the placeholder, too.
var sample = jsonLine.Substring(0, Math.Min(jsonLine.Length, 512));
var sampleLength = GetOversizeEventSampleLength(eventBodyLimitBytes);
var sample = jsonLine.Substring(0, Math.Min(jsonLine.Length, (int)sampleLength));
return new LogEvent(
logEvent.Timestamp,
LogEventLevel.Error,
Expand All @@ -121,5 +120,22 @@ static LogEvent CreateOversizeEventPlaceholder(LogEvent logEvent, string jsonLin
new LogEventProperty("EventBodySample", new ScalarValue(sample)),
});
}

internal static long GetOversizeEventSampleLength(long eventBodyLimitBytes)
{
// A quick estimate of how much of the original event payload we should send along with the oversized event
// placeholder. If the limit is so constrained as to disallow sending the sample, that's okay - we'll
// just drop the placeholder, too.

// In reality the timestamp and other envelope components won't be anything close to this.
const long packagingAllowance = 2048;
var byteBudget = eventBodyLimitBytes - packagingAllowance;

// Allow for multibyte characters and JSON escape sequences.
var withEncoding = byteBudget / 2;

const long minimumSampleSize = 512;
return Math.Max(withEncoding, minimumSampleSize);
}
}
}
14 changes: 14 additions & 0 deletions test/Serilog.Sinks.Seq.Tests/ConstrainedBufferedFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,19 @@ public void PlaceholdersAreLoggedWhenTheEventSizeLimitIsExceeded()
Assert.Contains("\"EventBodySample\"", jsonString);
Assert.Contains("aaaaa", jsonString);
}

[Theory]
[InlineData(0, 512)]
[InlineData(1, 512)]
[InlineData(512, 512)]
[InlineData(1000, 512)]
[InlineData(5000, 1476)]
[InlineData(10000, 3976)]
[InlineData(130048, 64000)]
public void PlaceholderSampleSizeIsComputedFromEventBodyLimitBytes(long eventBodyLimitBytes, long expectedSampleSize)
{
var actual = ConstrainedBufferedFormatter.GetOversizeEventSampleLength(eventBodyLimitBytes);
Assert.Equal(expectedSampleSize, actual);
}
}
}