-
Notifications
You must be signed in to change notification settings - Fork 33
feat: canonical bottom-up stack frame order for error tracking #200
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
Draft
cat-ph
wants to merge
1
commit into
main
Choose a base branch
from
cat/canonical-frame-order
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.
Draft
Changes from all commits
Commits
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "posthog-php": minor | ||
| --- | ||
|
|
||
| Emit error tracking stack frames in the canonical bottom-up order: `stacktrace.frames[0]` is now the outermost/entry-point call and the last frame is the crash site. This aligns the PHP SDK with the cross-SDK stack frame ordering standard. | ||
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -153,8 +153,9 @@ public function testStacktraceUsesThrowableFileAndLineForMostRecentFrame(): void | |||||||||||||||||||||||||||||||||||||||||||||||||
| [$exception, $throwLine] = $this->throwHelperWithKnownLine(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $result = ExceptionPayloadBuilder::buildExceptionList($exception); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Canonical bottom-up order: the crash site (most recent frame) is the last frame. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $frames = $result[0]['stacktrace']['frames']; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $frame = $frames[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $frame = $frames[count($frames) - 1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals(__FILE__, $frame['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals($throwLine, $frame['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -165,9 +166,10 @@ public function testStacktracePreservesOriginalCallerFrame(): void | |||||||||||||||||||||||||||||||||||||||||||||||||
| [$exception, $throwLine, $callerLine] = $this->nestedThrowHelperWithKnownLines(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $result = ExceptionPayloadBuilder::buildExceptionList($exception); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Canonical bottom-up order: the innermost (crash) frame is last, its caller sits just before it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $frames = array_values($result[0]['stacktrace']['frames']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $innermostFrame = $frames[0]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $callerFrame = $frames[1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $innermostFrame = $frames[count($frames) - 1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $callerFrame = $frames[count($frames) - 2]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals(__FILE__, $innermostFrame['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals($throwLine, $innermostFrame['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -181,15 +183,18 @@ public function testInternalFunctionErrorDoesNotDuplicateTopFrame(): void | |||||||||||||||||||||||||||||||||||||||||||||||||
| [$exception, $arraySumLine, $callerLine] = $this->internalErrorHelperWithKnownLines(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $result = ExceptionPayloadBuilder::buildExceptionList($exception); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // Canonical bottom-up order: the internal function (crash site) is last, its caller before it. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $frames = array_values($result[0]['stacktrace']['frames']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $crashFrame = $frames[count($frames) - 1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $callerFrame = $frames[count($frames) - 2]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals('array_sum', $frames[0]['function']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals(__FILE__, $frames[0]['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals($arraySumLine, $frames[0]['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals(__CLASS__ . '->internalErrorLeaf', $frames[1]['function']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals(__FILE__, $frames[1]['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals($callerLine, $frames[1]['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertNotEquals($frames[0], $frames[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals('array_sum', $crashFrame['function']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals(__FILE__, $crashFrame['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals($arraySumLine, $crashFrame['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals(__CLASS__ . '->internalErrorLeaf', $callerFrame['function']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals(__FILE__, $callerFrame['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertEquals($callerLine, $callerFrame['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertNotEquals($crashFrame, $callerFrame); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| public function testStrictTypeErrorUsesCallsiteBeforeDeclaration(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -226,12 +231,14 @@ function requiresIntForTrace(int $value): int | |||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertInstanceOf(\Throwable::class, $exception); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $result = ExceptionPayloadBuilder::buildExceptionList($exception); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Canonical bottom-up order: the real callsite (crash site) is the last frame. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $frames = array_values($result[0]['stacktrace']['frames']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $crashFrame = $frames[count($frames) - 1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertSame($scriptPath, $frames[0]['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertSame('requiresIntForTrace', $frames[0]['function']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertSame($callLine, $frames[0]['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertNotSame($declarationLine, $frames[0]['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertSame($scriptPath, $crashFrame['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertSame('requiresIntForTrace', $crashFrame['function']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertSame($callLine, $crashFrame['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertNotSame($declarationLine, $crashFrame['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } finally { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| unlink($scriptPath); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -251,6 +258,59 @@ public function testFrameLimitCanBeConfigured(): void | |||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertArrayHasKey('context_line', $frames[1]); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| public function testStacktraceFramesUseCanonicalBottomUpOrder(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // Regression guard for the canonical wire order. PHP's getTrace() is innermost-first | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // (crash site at index 0); PostHog expects bottom-up frames where frames[0] is the | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // outermost/entry-point call and the last frame is the crash site. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $exception = $this->orderedNestedThrowHelper(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $result = ExceptionPayloadBuilder::buildExceptionList($exception); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $frames = array_values($result[0]['stacktrace']['frames']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertGreaterThanOrEqual(3, count($frames), 'nested throw should yield multiple frames'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $functions = array_map(static fn(array $frame) => $frame['function'] ?? null, $frames); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // The three helpers unwind inward: leaf (crash) is called by middle, called by the entry | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // point. Canonical order therefore reads entry -> middle -> leaf from first to last. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $entryIndex = array_search(__CLASS__ . '->orderedNestedThrowHelper', $functions, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $middleIndex = array_search(__CLASS__ . '->orderedThrowMiddle', $functions, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $leafIndex = array_search(__CLASS__ . '->orderedThrowLeaf', $functions, true); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertNotFalse($entryIndex); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertNotFalse($middleIndex); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertNotFalse($leafIndex); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertTrue( | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $entryIndex < $middleIndex && $middleIndex < $leafIndex, | ||||||||||||||||||||||||||||||||||||||||||||||||||
| 'frames must read bottom-up: entry point first, crash site last' | ||||||||||||||||||||||||||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+274
to
+286
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| // The very last frame is the crash site: it carries the throw location (file + line) and | ||||||||||||||||||||||||||||||||||||||||||||||||||
| // is the synthetic throw-site frame appended for the leaf. | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $crashFrame = $frames[count($frames) - 1]; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertSame(__FILE__, $crashFrame['abs_path']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->assertSame($exception->getLine(), $crashFrame['lineno']); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private function orderedNestedThrowHelper(): \RuntimeException | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->orderedThrowMiddle(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } catch (\RuntimeException $e) { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| return $e; | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private function orderedThrowMiddle(): void | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| $this->orderedThrowLeaf(); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private function orderedThrowLeaf(): never | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| throw new \RuntimeException('canonical order leaf'); | ||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
| private function throwHelper(): \RuntimeException | ||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This changes the wire meaning of
stacktrace.frames[0]from crash site to entry point. Consumers that follow normal version ranges can upgrade from4.8.xto4.9.0and silently read the wrong crash location, so the changeset should match the repo policy for breaking changes unless the release policy is also being changed.Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!