Skip to content

Commit

Permalink
Merge pull request #193 from nblumhardt/larger-sample
Browse files Browse the repository at this point in the history
Include a large event body sample when generating oversized event placeholders
  • Loading branch information
nblumhardt authored Nov 22, 2022
2 parents 3e60b22 + 34a274f commit 1781ad8
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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);
}
}
}

0 comments on commit 1781ad8

Please sign in to comment.