-
-
Notifications
You must be signed in to change notification settings - Fork 58
feat: Replace time-based log debouncing with content-based event throttling #2479
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
Open
bitsandfoxes
wants to merge
11
commits into
main
Choose a base branch
from
feature/log-throttling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a85162
Replace time-based log debouncing with content-based event throttling
bitsandfoxes 13ba453
.
bitsandfoxes a8a2ea8
Mark old log debouncer as deprecated instead of removing it
bitsandfoxes a142224
Add changelog entry for content-based event throttling
bitsandfoxes ec98eac
Fix thread-safety issue in ContentBasedThrottler
bitsandfoxes 32e1154
Improvements
bitsandfoxes eb23cc7
Merged main
bitsandfoxes a85b8b4
Optimized the hot-path
bitsandfoxes e7371ee
Merge branch 'main' into feature/log-throttling
bitsandfoxes e665f68
Test
bitsandfoxes 47f15ed
Merge branch 'feature/log-throttling' of https://github.com/getsentry…
bitsandfoxes File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,162 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using UnityEngine; | ||
|
|
||
| namespace Sentry.Unity; | ||
|
|
||
| /// <summary> | ||
| /// Content-based throttler that deduplicates events based on message and stack trace fingerprint. | ||
| /// Only throttles LogType.Error, LogType.Exception, and LogType.Assert events. | ||
| /// </summary> | ||
| internal class ContentBasedThrottler : IErrorEventThrottler | ||
| { | ||
| private readonly Dictionary<int, LinkedListNode<LruEntry>> _cache = new(); | ||
| private readonly LinkedList<LruEntry> _accessOrder = new(); | ||
| private readonly object _lock = new(); | ||
| private readonly int _maxBufferSize; | ||
| private readonly TimeSpan _dedupeWindow; | ||
|
|
||
| private readonly struct LruEntry | ||
| { | ||
| public readonly int Hash; | ||
| public readonly DateTimeOffset Timestamp; | ||
|
|
||
| public LruEntry(int hash, DateTimeOffset timestamp) | ||
| { | ||
| Hash = hash; | ||
| Timestamp = timestamp; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a new content-based throttler. | ||
| /// </summary> | ||
| /// <param name="dedupeWindow">Time window for deduplicating repeated errors with the same fingerprint.</param> | ||
| /// <param name="maxBufferSize">Maximum number of fingerprints to track. Oldest entries are evicted when full.</param> | ||
| public ContentBasedThrottler(TimeSpan dedupeWindow, int maxBufferSize = 100) | ||
| { | ||
| _dedupeWindow = dedupeWindow; | ||
| _maxBufferSize = maxBufferSize; | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| public bool ShouldCapture(string message, string stackTrace, LogType logType) | ||
| { | ||
| // Only throttle Error, Exception, and Assert | ||
| if (logType is not (LogType.Error or LogType.Exception or LogType.Assert)) | ||
| { | ||
| return true; | ||
| } | ||
|
|
||
| var hash = ComputeHash(message, stackTrace); | ||
| return ShouldCaptureByHash(hash); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Checks if an exception should be captured without allocating a fingerprint string. | ||
| /// Computes hash directly from exception type name + message + stack trace. | ||
| /// </summary> | ||
| internal bool ShouldCaptureException(Exception exception) | ||
| { | ||
| var hash = ComputeExceptionHash(exception); | ||
| return ShouldCaptureByHash(hash); | ||
| } | ||
|
|
||
| private bool ShouldCaptureByHash(int hash) | ||
| { | ||
| var now = DateTimeOffset.UtcNow; | ||
|
|
||
| lock (_lock) | ||
| { | ||
| if (_cache.TryGetValue(hash, out var existingNode)) | ||
| { | ||
| // Entry exists - check if still within dedupe window | ||
| if (now - existingNode.Value.Timestamp < _dedupeWindow) | ||
| { | ||
| return false; // Throttle - seen recently | ||
| } | ||
|
|
||
| // Entry expired - update timestamp and move to end (most recently used) | ||
| _accessOrder.Remove(existingNode); | ||
| var newNode = _accessOrder.AddLast(new LruEntry(hash, now)); | ||
| _cache[hash] = newNode; | ||
| return true; // Allow capture | ||
| } | ||
|
|
||
| // New entry - evict oldest if buffer is full | ||
| if (_cache.Count >= _maxBufferSize) | ||
| { | ||
| EvictOldest(); | ||
| } | ||
|
|
||
| // Add new entry at end (most recently used) | ||
| var node = _accessOrder.AddLast(new LruEntry(hash, now)); | ||
| _cache[hash] = node; | ||
| return true; // Allow capture | ||
| } | ||
| } | ||
|
|
||
| private static int ComputeExceptionHash(Exception exception) | ||
| { | ||
| // Compute hash from exception type name + ":" + message + stack trace | ||
| // without allocating a combined string | ||
| var typeName = exception.GetType().Name; | ||
| var message = exception.Message; | ||
| var stackTrace = exception.StackTrace; | ||
|
|
||
| // Hash the type name | ||
| var hash = typeName?.GetHashCode() ?? 0; | ||
|
|
||
| // Add separator ":" | ||
| hash = hash * 31 + ':'; | ||
|
|
||
| // Add message hash | ||
| hash = hash * 31 + (message?.GetHashCode() ?? 0); | ||
|
|
||
| // Add stack trace prefix hash | ||
| if (!string.IsNullOrEmpty(stackTrace)) | ||
| { | ||
| hash = hash * 31 + ComputeStackTraceHash(stackTrace!, 200); | ||
| } | ||
|
|
||
| return hash; | ||
| } | ||
|
|
||
| private static int ComputeHash(string message, string? stackTrace) | ||
| { | ||
| // Start with message hash | ||
| var hash = message?.GetHashCode() ?? 0; | ||
|
|
||
| // Combine with stack trace prefix hash using multiplicative combining (not XOR) | ||
| // Process character-by-character to avoid Substring allocation | ||
| if (!string.IsNullOrEmpty(stackTrace)) | ||
| { | ||
| var stackTraceHash = ComputeStackTraceHash(stackTrace!, 200); | ||
| hash = hash * 31 + stackTraceHash; | ||
| } | ||
|
|
||
| return hash; | ||
| } | ||
|
|
||
| private static int ComputeStackTraceHash(string stackTrace, int maxLength) | ||
| { | ||
| var length = Math.Min(stackTrace.Length, maxLength); | ||
| var hash = 17; | ||
| for (var i = 0; i < length; i++) | ||
| { | ||
| hash = hash * 31 + stackTrace[i]; | ||
| } | ||
| return hash; | ||
| } | ||
|
|
||
| private void EvictOldest() | ||
| { | ||
| // O(1) eviction - remove from head of linked list | ||
| var oldest = _accessOrder.First; | ||
| if (oldest != null) | ||
| { | ||
| _cache.Remove(oldest.Value.Hash); | ||
| _accessOrder.RemoveFirst(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or 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,48 @@ | ||
| using System; | ||
| using UnityEngine; | ||
|
|
||
| namespace Sentry.Unity; | ||
|
|
||
| /// <summary> | ||
| /// Interface for throttling error and exception events to prevent quota exhaustion from high-frequency errors. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Throttling only affects error/exception event capture - breadcrumbs and structured logs are not affected. | ||
| /// </remarks> | ||
| public interface IErrorEventThrottler | ||
| { | ||
| /// <summary> | ||
| /// Determines whether an error or exception should be captured as a Sentry event. | ||
| /// </summary> | ||
| /// <param name="message">The error message or exception fingerprint</param> | ||
| /// <param name="stackTrace">Stack trace for fingerprinting</param> | ||
| /// <param name="logType">Unity LogType (Error, Exception, or Assert)</param> | ||
| /// <returns>True if the event should be captured, false to throttle</returns> | ||
| bool ShouldCapture(string message, string stackTrace, LogType logType); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Extension methods for <see cref="IErrorEventThrottler"/>. | ||
| /// </summary> | ||
| internal static class ErrorEventThrottlerExtensions | ||
| { | ||
| /// <summary> | ||
| /// Determines whether an exception should be captured as a Sentry event. | ||
| /// Uses an allocation-free path when the throttler is <see cref="ContentBasedThrottler"/>. | ||
| /// </summary> | ||
| /// <param name="throttler">The throttler instance</param> | ||
| /// <param name="exception">The exception to check</param> | ||
| /// <returns>True if the event should be captured, false to throttle</returns> | ||
| public static bool ShouldCaptureException(this IErrorEventThrottler throttler, Exception exception) | ||
| { | ||
| // Use allocation-free path for ContentBasedThrottler | ||
| if (throttler is ContentBasedThrottler contentBasedThrottler) | ||
| { | ||
| return contentBasedThrottler.ShouldCaptureException(exception); | ||
| } | ||
|
|
||
| // Fallback for custom implementations - requires string allocation | ||
| var fingerprint = $"{exception.GetType().Name}:{exception.Message}"; | ||
| return throttler.ShouldCapture(fingerprint, exception.StackTrace ?? string.Empty, LogType.Exception); | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.