Skip to content

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

Merged
hpouillot merged 1 commit into
PostHog:mainfrom
Toraastrta:fix/exception-long-context-lines
Nov 17, 2026
Merged

hpouillot merged 1 commit into
PostHog:mainfrom
Toraastrta:fix/exception-long-context-lines

Conversation

@Toraastrta

Copy link
Copy Markdown
Contributor

💡 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
@github-actions

Copy link
Copy Markdown
Contributor

SDK performance: posthog-python

Benchmarks completed. No changes flagged for review.

Flags require at least a 5% change in the median and all six pairs agreeing on direction. This is a noise filter, not proof of a performance change or a CI gate.

View all results
Test Measurement Before PR With PR Change Assessment
Capture and deliver events (32-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Queue an event (32-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (32-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocated memory per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Queue an event (4,096-byte payload)
Shard: capture
Allocations per operation — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​; head:​ Background preparation has no public completion boundary before flush; see capture.​drain for full-pipeline allocations.​
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Refresh 1,000 flag definitions
Shard: flags-memory
Managed heap after settling — — — unavailable; base:​ unsupported; head:​ unsupported; base:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​; head:​ CPython has no uninstrumented total managed-heap byte counter; tracing would distort this RSS pass.​
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocated memory per operation 115.72 KiB/op 115.72 KiB/op <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Allocations per operation 1,107.61 count/op 1,107.59 count/op <0.1% fewer Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Time per operation 63.28 µs/op 63.07 µs/op 0.3% less time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Idle process memory (RSS) 36.21 MiB 36.18 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 37.93 MiB 37.91 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 37.93 MiB 37.91 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Process CPU time per operation 66.68 µs/op 66.78 µs/op 0.2% more time Not flagged
Capture and deliver events (32-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 39.57 MiB 39.57 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocated memory per operation 168.28 KiB/op 168.28 KiB/op <0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Allocations per operation 1,100.59 count/op 1,100.58 count/op <0.1% fewer Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Time per operation 86.4 µs/op 86 µs/op 0.5% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Idle process memory (RSS) 41.12 MiB 41.13 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Observed peak process memory (RSS) 46.19 MiB 46.21 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process memory after settling (RSS) 46.19 MiB 46.21 MiB <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Process CPU time per operation 85.09 µs/op 84.59 µs/op 0.6% less time Not flagged
Capture and deliver events (4,096-byte payload, burst)
Shard: capture
Lifetime peak process memory (RSS) 51.01 MiB 51.01 MiB <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocated memory per operation 119.85 KiB/op 119.86 KiB/op <0.1% more memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Allocations per operation 1,147.48 count/op 1,147.49 count/op <0.1% more Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Time per operation 73.14 µs/op 72.91 µs/op 0.3% less time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Idle process memory (RSS) 36.19 MiB 36.18 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 36.28 MiB 36.27 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 36.28 MiB 36.27 MiB <0.1% less memory Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Process CPU time per operation 75.98 µs/op 75.98 µs/op <0.1% more time Not flagged
Capture and deliver events (32-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 39.57 MiB 39.57 MiB No observed change Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocated memory per operation 172.74 KiB/op 172.74 KiB/op <0.1% more memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Allocations per operation 1,140.32 count/op 1,140.33 count/op <0.1% more Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Time per operation 90.97 µs/op 91.2 µs/op 0.3% more time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Idle process memory (RSS) 40.97 MiB 40.91 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Observed peak process memory (RSS) 40.97 MiB 40.91 MiB 0.2% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process memory after settling (RSS) 40.96 MiB 40.9 MiB 0.1% less memory Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Process CPU time per operation 90.43 µs/op 89.66 µs/op 0.9% less time Not flagged
Capture and deliver events (4,096-byte payload, steady)
Shard: capture
Lifetime peak process memory (RSS) 41.33 MiB 41.25 MiB 0.2% less memory Not flagged
Queue an event (32-byte payload)
Shard: capture
Caller-thread CPU time per operation 43.06 µs/op 42.79 µs/op 0.6% less time Not flagged
Queue an event (32-byte payload)
Shard: capture
Time per operation 43.15 µs/op 42.79 µs/op 0.8% less time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Caller-thread CPU time per operation 42.91 µs/op 42.72 µs/op 0.4% less time Not flagged
Queue an event (4,096-byte payload)
Shard: capture
Time per operation 42.91 µs/op 42.72 µs/op 0.4% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocated memory per operation 226.56 KiB/op 226.56 KiB/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Allocations per operation 2,240.44 count/op 2,240.44 count/op No observed change Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Time per operation 83.91 µs/op 83.74 µs/op 0.2% less time Not flagged
Evaluate all 10 flags
Shard: flags-scaling
Process CPU time per operation 83.92 µs/op 83.75 µs/op 0.2% less time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocated memory per operation 2.17 MiB/op 2.17 MiB/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Allocations per operation 22,014.88 count/op 22,014.88 count/op No observed change Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Time per operation 786.87 µs/op 789.78 µs/op 0.4% more time Not flagged
Evaluate all 100 flags
Shard: flags-scaling
Process CPU time per operation 786.93 µs/op 789.85 µs/op 0.4% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocated memory per operation 21.69 MiB/op 21.69 MiB/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Allocations per operation 219,656.62 count/op 219,656.62 count/op No observed change Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Time per operation 7.82 ms/op 7.86 ms/op 0.5% more time Not flagged
Evaluate all 1,000 flags
Shard: flags-scaling
Process CPU time per operation 7.82 ms/op 7.86 ms/op 0.5% more time Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Idle process memory (RSS) 42.36 MiB 42.83 MiB 1.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Observed peak process memory (RSS) 42.65 MiB 43.11 MiB 1.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Process memory after settling (RSS) 42.65 MiB 43.11 MiB 1.1% more memory Not flagged
Evaluate 1,000 flags under concurrent load
Shard: flags-memory
Lifetime peak process memory (RSS) 43 MiB 43.07 MiB 0.2% more memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Idle process memory (RSS) 41.97 MiB 41.97 MiB <0.1% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Observed peak process memory (RSS) 42.04 MiB 41.97 MiB 0.2% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Process memory after settling (RSS) 41.99 MiB 41.38 MiB 1.5% less memory Not flagged
Refresh 1,000 flag definitions
Shard: flags-memory
Lifetime peak process memory (RSS) 42.44 MiB 42.34 MiB 0.2% less memory Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocated memory per operation 16.63 KiB/op 16.63 KiB/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Allocations per operation 164.77 count/op 164.77 count/op No observed change Not flagged
Evaluate one boolean flag
Shard: flags-single
Time per operation 8.68 µs/op 8.67 µs/op 0.1% less time Not flagged
Evaluate one boolean flag
Shard: flags-single
Process CPU time per operation 8.68 µs/op 8.67 µs/op 0.1% less time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocated memory per operation 25.49 KiB/op 25.49 KiB/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Allocations per operation 241.28 count/op 241.28 count/op No observed change Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Time per operation 12.05 µs/op 12.11 µs/op 0.5% more time Not flagged
Evaluate a cohort-filtered flag
Shard: flags-single
Process CPU time per operation 12.05 µs/op 12.11 µs/op 0.5% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocated memory per operation 40.7 KiB/op 40.7 KiB/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Allocations per operation 390.27 count/op 390.27 count/op No observed change Not flagged
Evaluate a flag (early match)
Shard: flags-single
Time per operation 16.73 µs/op 16.79 µs/op 0.4% more time Not flagged
Evaluate a flag (early match)
Shard: flags-single
Process CPU time per operation 16.73 µs/op 16.79 µs/op 0.4% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocated memory per operation 61.48 KiB/op 61.48 KiB/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Allocations per operation 586.27 count/op 586.27 count/op No observed change Not flagged
Evaluate a flag (late match)
Shard: flags-single
Time per operation 24.02 µs/op 24.12 µs/op 0.4% more time Not flagged
Evaluate a flag (late match)
Shard: flags-single
Process CPU time per operation 24.02 µs/op 24.12 µs/op 0.4% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocated memory per operation 60.11 KiB/op 60.11 KiB/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Allocations per operation 569.77 count/op 569.77 count/op No observed change Not flagged
Evaluate a flag (no match)
Shard: flags-single
Time per operation 22.84 µs/op 22.86 µs/op <0.1% more time Not flagged
Evaluate a flag (no match)
Shard: flags-single
Process CPU time per operation 22.84 µs/op 22.86 µs/op 0.1% more time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocated memory per operation 19.93 KiB/op 19.93 KiB/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Allocations per operation 194.28 count/op 194.28 count/op No observed change Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Time per operation 9.82 µs/op 9.68 µs/op 1.5% less time Not flagged
Evaluate a property-filtered flag
Shard: flags-single
Process CPU time per operation 9.82 µs/op 9.68 µs/op 1.4% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocated memory per operation 21.56 KiB/op 21.56 KiB/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Allocations per operation 199.78 count/op 199.78 count/op No observed change Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Time per operation 10.3 µs/op 10.21 µs/op 0.9% less time Not flagged
Evaluate a regex-filtered flag
Shard: flags-single
Process CPU time per operation 10.3 µs/op 10.21 µs/op 0.9% less time Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocated memory per operation 15.79 KiB/op 15.79 KiB/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Allocations per operation 154.77 count/op 154.77 count/op No observed change Not flagged
Evaluate a rollout flag
Shard: flags-single
Time per operation 8.17 µs/op 8.2 µs/op 0.4% more time Not flagged
Evaluate a rollout flag
Shard: flags-single
Process CPU time per operation 8.17 µs/op 8.2 µs/op 0.4% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocated memory per operation 16.79 KiB/op 16.79 KiB/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Allocations per operation 168.77 count/op 168.77 count/op No observed change Not flagged
Evaluate a multivariate flag
Shard: flags-single
Time per operation 9.05 µs/op 9.13 µs/op 0.8% more time Not flagged
Evaluate a multivariate flag
Shard: flags-single
Process CPU time per operation 9.05 µs/op 9.13 µs/op 0.8% more time Not flagged

Values are medians of paired measurements. Not flagged does not mean unchanged. Unavailable means a measurement could not be provided, not zero. Estimated allocation measurements retain their instrumentation uncertainty. RSS measures the whole process, not just SDK allocations; observed peaks are sampled checkpoints. Paired ranges in the diagnostics describe variability, not confidence intervals.

Each shard uses its own runner; compare Before PR against With PR within a row, not across shards.

Compared f22511891225fb15a4e6a0f889e723e9b5d0ebe4 → 2569bc66476c1840327349d6e5768c6fd1c1e645.

Full report and diagnostics

102 metric rows; 102 rows shown in all results. Omitted or shortened data remain in comparison.json, including per-runner environments, resource limits, measurement methods, samples and checkpoints.

@hpouillot
hpouillot merged commit 8a88a28 into PostHog:main Nov 17, 2026
56 checks passed
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.

2 participants