-
Notifications
You must be signed in to change notification settings - Fork 26
Description
I've been testing for #81 and noticed frequent oversized error payloads whenever the error came from within Rails or a gem such as pending migrations.
Problem
When capturing exceptions, the payload can frequently exceed the 32KB message limit (Defaults::Message::MAX_BYTES), causing the message to be silently dropped with only a log warning:
[posthog-ruby] a message exceeded the maximum allowed size
Root Cause
In exception_capture.rb, the build_stacktrace method captures up to 50 stack frames:
frames = backtrace.first(50).map do |line|
parse_backtrace_line(line)
end.compact.reverseFor every frame where the file exists on disk, add_context_lines reads 11 lines of source code (5 pre + 1 current + 5 post):
def self.add_context_lines(frame, file_path, lineno, context_size = 5)
# ...reads actual source code from file
endThis is called regardless of whether the frame is in_app or from a gem/framework.
Worst case calculation:
- 50 frames × 11 lines × ~80 chars/line = ~44KB just for context lines
- Plus frame metadata, exception message, and any additional properties
For Rails applications, the backtrace typically includes many Rails framework frames, so context lines are read from Rails source files as well, bloating the payload.
Suggested Fixes
-
Only add context lines for
in_app: trueframes - Framework/gem code context is less useful and contributes most to size -
Reduce default max frames - 50 is excessive; 20-30 would cover most useful information
-
Add configurable limits - Allow users to control:
- Max frames (
max_exception_frames) - Context line count (
exception_context_lines) - Whether to include context at all (
include_exception_context)
- Max frames (
-
Add payload size awareness - Check size before adding to batch, truncate context if needed
Example Fix for Option 1
def self.parse_backtrace_line(line)
# ... existing parsing ...
# Only add context for in-app frames
if frame['in_app'] && File.exist?(file)
add_context_lines(frame, file, lineno)
end
frame
endEnvironment
- Ruby 3.4.5
- Rails 8.1.1
- posthog-ruby (local/dev version)