Skip to content

Commit a9a4e09

Browse files
authored
Merge pull request #22 from aivis/set-limits-to-data-collectors
set limits to data collectors
2 parents 24da061 + 22c103f commit a9a4e09

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

src/Understand/UnderstandLaravel5/ExceptionEncoder.php

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,19 @@ class ExceptionEncoder
1313
*/
1414
protected $projectRoot;
1515

16+
/**
17+
* @var int
18+
*/
19+
protected $stackTraceLimit = 100;
20+
21+
/**
22+
* @param integer $limit
23+
*/
24+
public function setStackTraceLimit($limit)
25+
{
26+
$this->stackTraceLimit = $limit;
27+
}
28+
1629
/**
1730
* @param string $projectRoot
1831
*/
@@ -82,7 +95,7 @@ public function setCurrentStackTrace(array $errorLog)
8295
*/
8396
protected function getCurrentStackTrace()
8497
{
85-
$stackTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, 100);
98+
$stackTrace = debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT, $this->stackTraceLimit);
8699
$vendorExcluded = false;
87100

88101
foreach($stackTrace as $index => $trace)
@@ -126,6 +139,7 @@ protected function getCurrentStackTrace()
126139
public function stackTraceToArray(array $stackTrace, $topFile = null, $topLine = null)
127140
{
128141
$stack = [];
142+
$counter = 0;
129143

130144
foreach ($stackTrace as $trace)
131145
{
@@ -151,6 +165,13 @@ public function stackTraceToArray(array $stackTrace, $topFile = null, $topLine =
151165
'line' => $this->getStackTraceLine($trace),
152166
'code' => $this->getCode($this->getStackTraceFile($trace), $this->getStackTraceLine($trace)),
153167
];
168+
169+
$counter++;
170+
171+
if ($counter >= $this->stackTraceLimit)
172+
{
173+
break;
174+
}
154175
}
155176

156177
return $stack;

src/Understand/UnderstandLaravel5/FieldProvider.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,17 @@ protected function getLaravelVersion()
256256
*/
257257
protected function getSqlQueries()
258258
{
259-
if ($this->dataCollector)
259+
if ( ! $this->dataCollector)
260260
{
261-
return $this->dataCollector->getByKey('sql_queries');
261+
return [];
262262
}
263+
264+
if ($queries = $this->dataCollector->getByKey('sql_queries'))
265+
{
266+
return array_slice($queries, -500);
267+
}
268+
269+
return [];
263270
}
264271

265272
/**

tests/ExceptionEncoderTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,4 +100,21 @@ protected function assertIncompleteClassStackTrace($exception, $index)
100100

101101
$this->assertSame('object(__PHP_Incomplete_Class)', $stackTraceArray[$index]['args'][0]);
102102
}
103+
104+
public function testStackTraceLimit()
105+
{
106+
$exception = new \DomainException;
107+
108+
$encoder = new Understand\UnderstandLaravel5\ExceptionEncoder();
109+
110+
$originalStackTrace = $exception->getTrace();
111+
$stackTraceArray = $encoder->stackTraceToArray($originalStackTrace);
112+
113+
$this->assertTrue(count($stackTraceArray) > 2);
114+
115+
$encoder->setStackTraceLimit(2);
116+
$stackTraceArray2 = $encoder->stackTraceToArray($originalStackTrace);
117+
118+
$this->assertTrue(count($stackTraceArray2) === 2);
119+
}
103120
}

tests/FieldProviderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,13 @@ public function testSentryGetUser()
7070

7171
$this->assertSame($user->id, $currentUserId);
7272
}
73+
74+
public function testQueryCount()
75+
{
76+
$this->app['understand.dataCollector']->set('sql_queries', range(1, 1000));
77+
78+
$queries = $this->app['understand.fieldProvider']->getSqlQueries();
79+
80+
$this->assertCount(500, $queries);
81+
}
7382
}

0 commit comments

Comments
 (0)