-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathFrameContextifierIntegration.php
More file actions
164 lines (138 loc) · 4.81 KB
/
FrameContextifierIntegration.php
File metadata and controls
164 lines (138 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
declare(strict_types=1);
namespace Sentry\Integration;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Sentry\Event;
use Sentry\Frame;
use Sentry\SentrySdk;
use Sentry\Stacktrace;
use Sentry\State\Scope;
/**
* This integration reads excerpts of code around the line that originated an
* error.
*
* @author Stefano Arlandini <sarlandini@alice.it>
*/
final class FrameContextifierIntegration implements IntegrationInterface
{
/**
* @var LoggerInterface A PSR-3 logger
*/
private $logger;
/**
* Creates a new instance of this integration.
*
* @param LoggerInterface|null $logger A PSR-3 logger
*/
public function __construct(?LoggerInterface $logger = null)
{
$this->logger = $logger ?? new NullLogger();
}
/**
* {@inheritdoc}
*/
public function setupOnce(): void
{
Scope::addGlobalEventProcessor(static function (Event $event): Event {
$client = SentrySdk::getClient();
$maxContextLines = $client->getOptions()->getContextLines();
$integration = $client->getIntegration(self::class);
if ($integration === null || $maxContextLines === null) {
return $event;
}
$stacktrace = $event->getStacktrace();
if ($stacktrace !== null) {
$integration->addContextToStacktraceFrames($maxContextLines, $stacktrace);
}
foreach ($event->getExceptions() as $exception) {
if ($exception->getStacktrace() !== null) {
$integration->addContextToStacktraceFrames($maxContextLines, $exception->getStacktrace());
}
}
return $event;
});
}
/**
* Contextifies the frames of the given stacktrace.
*
* @param int $maxContextLines The maximum number of lines of code to read
* @param Stacktrace $stacktrace The stacktrace object
*/
private function addContextToStacktraceFrames(int $maxContextLines, Stacktrace $stacktrace): void
{
foreach ($stacktrace->getFrames() as $frame) {
if ($frame->isInternal()) {
continue;
}
$this->addContextToStacktraceFrame($maxContextLines, $frame);
}
}
/**
* Contextifies the given frame.
*
* @param int $maxContextLines The maximum number of lines of code to read
*/
private function addContextToStacktraceFrame(int $maxContextLines, Frame $frame): void
{
if ($frame->getAbsoluteFilePath() === null) {
return;
}
$sourceCodeExcerpt = $this->getSourceCodeExcerpt($maxContextLines, $frame->getAbsoluteFilePath(), $frame->getLine());
$frame->setPreContext($sourceCodeExcerpt['pre_context']);
$frame->setContextLine($sourceCodeExcerpt['context_line']);
$frame->setPostContext($sourceCodeExcerpt['post_context']);
}
/**
* Gets an excerpt of the source code around a given line.
*
* @param int $maxContextLines The maximum number of lines of code to read
* @param string $filePath The file path
* @param int $lineNumber The line to centre about
*
* @return array<string, mixed>
*
* @psalm-return array{
* pre_context: string[],
* context_line: string|null,
* post_context: string[]
* }
*/
private function getSourceCodeExcerpt(int $maxContextLines, string $filePath, int $lineNumber): array
{
$frame = [
'pre_context' => [],
'context_line' => null,
'post_context' => [],
];
$target = max(0, $lineNumber - ($maxContextLines + 1));
$currentLineNumber = $target + 1;
try {
$file = new \SplFileObject($filePath);
$file->seek($target);
while (!$file->eof()) {
/** @var string $line */
$line = $file->current();
$line = rtrim($line, "\r\n");
if ($currentLineNumber === $lineNumber) {
$frame['context_line'] = $line;
} elseif ($currentLineNumber < $lineNumber) {
$frame['pre_context'][] = $line;
} elseif ($currentLineNumber > $lineNumber) {
$frame['post_context'][] = $line;
}
++$currentLineNumber;
if ($currentLineNumber > $lineNumber + $maxContextLines) {
break;
}
$file->next();
}
} catch (\Throwable $exception) {
$this->logger->warning(
\sprintf('Failed to get the source code excerpt for the file "%s".', $filePath),
['exception' => $exception]
);
}
return $frame;
}
}