Skip to content

fix(exceptions): send truncated source lines instead of null - #1265

Open
Toraastrta wants to merge 1 commit into
PostHog:mainfrom
Toraastrta:fix/exception-long-context-lines
Open

Toraastrta wants to merge 1 commit into
PostHog:mainfrom
Toraastrta:fix/exception-long-context-lines

Conversation

@Toraastrta

Copy link
Copy Markdown

💡 Motivation and Context

When a source line in a stack frame is longer than 1024 bytes, exception autocapture sends null for it in context_line, pre_context or post_context, instead of the truncated line.

get_lines_from_file runs each line through strip_string, which returns an AnnotatedValue (copied from Sentry) when it truncates. Nothing unwraps it, so clean() turns the object into None. The truncated text, ending in ..., never reaches the event. The capture-exception spec types these fields as strings.

This keeps only the truncated string, and updates the two return type comments that still allowed AnnotatedValue.

💚 How did you test it?

  • Added test_source_context_truncates_long_lines_to_strings to posthog/test/test_exception_utils.py. It fails on main (an AnnotatedValue comes back) and passes with the fix.
  • End to end: sent a real $exception from a module whose raising line is 1,100 characters to a local HTTP server, using Posthog(..., host="http://127.0.0.1:<port>", sync_mode=True), and read the posted batch. On main the frame's context_line is null. With the fix it is the 1,024-character truncated line ending in .... Short lines are unchanged.
  • ruff format --check ., ruff check ., mypy ... | mypy-baseline filter and python -W error -c "import posthog" pass. The full pytest run passes except 3 fork tests in test_metrics_autocapture.py, which fail the same way on main on my machine (macOS).

📝 Checklist

  • All commits are signed (see Commit signing)
  • I reviewed the submitted code.
  • I added tests to verify the changes.
  • I updated the docs if needed.
  • No breaking change or entry added to the changelog.

If releasing new changes

  • Added a changeset: .sampo/changesets/exception-long-context-lines.md (written by hand in the format sampo add produces, pypi/posthog: patch)

🤖 Agent context

Autonomy: Human-driven (agent-assisted)

  • I used Claude Code to audit the SDK's exception capture code, write the failing test and the end-to-end check, and draft the change and this description.
  • I reviewed the diff, ran the tests and the end-to-end check myself, and checked the field types against the capture-exception spec.
  • I considered changing strip_string itself, but kept the fix in get_lines_from_file, its only caller, so the Sentry-derived helper stays as it is.

get_lines_from_file passed each line through strip_string, which wraps a truncated line in an AnnotatedValue. Nothing unwrapped it, so clean() turned it into None and lines over 1024 bytes reached the event as null in context_line, pre_context and post_context. Keep only the truncated string.
@Toraastrta
Toraastrta requested a review from a team as a code owner November 16, 2026 23:27

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant