Skip to content

Exception capture payload exceeds 32KB limit due to context lines for all stack frames #88

@johnnagro

Description

@johnnagro

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.reverse

For 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
end

This 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

  1. Only add context lines for in_app: true frames - Framework/gem code context is less useful and contributes most to size

  2. Reduce default max frames - 50 is excessive; 20-30 would cover most useful information

  3. 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)
  4. 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
end

Environment

  • Ruby 3.4.5
  • Rails 8.1.1
  • posthog-ruby (local/dev version)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions