-
Notifications
You must be signed in to change notification settings - Fork 121
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from ArturMoczulski/github-38-truncate-payload
GitHub Issue #38: truncate payload
- Loading branch information
Showing
13 changed files
with
483 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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,16 @@ | ||
<?php namespace Rollbar\Truncation; | ||
|
||
class AbstractStrategy implements IStrategy | ||
{ | ||
protected $dataBuilder; | ||
|
||
public function __construct($dataBuilder) | ||
{ | ||
$this->dataBuilder = $dataBuilder; | ||
} | ||
|
||
public function execute(array $payload) | ||
{ | ||
return $payload; | ||
} | ||
} |
This file contains 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,32 @@ | ||
<?php namespace Rollbar\Truncation; | ||
|
||
class FramesStrategy extends AbstractStrategy | ||
{ | ||
|
||
const FRAMES_OPTIMIZATION_RANGE = 75; | ||
|
||
public function execute(array $payload) | ||
{ | ||
$frames = array(); | ||
|
||
if (isset($payload['data']['body']['trace_chain']['frames'])) { | ||
$frames = $payload['data']['body']['trace_chain']['frames']; | ||
} elseif (isset($payload['data']['body']['trace']['frames'])) { | ||
$frames = $payload['data']['body']['trace']['frames']; | ||
} | ||
|
||
return $this->selectFrames($frames); | ||
} | ||
|
||
public function selectFrames($frames, $range = self::FRAMES_OPTIMIZATION_RANGE) | ||
{ | ||
if (count($frames) <= $range * 2) { | ||
return $frames; | ||
} | ||
|
||
return array_merge( | ||
array_splice($frames, 0, $range), | ||
array_splice($frames, -$range, $range) | ||
); | ||
} | ||
} |
This file contains 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,11 @@ | ||
<?php namespace Rollbar\Truncation; | ||
|
||
interface IStrategy | ||
{ | ||
|
||
/** | ||
* @param array $payload | ||
* @return array | ||
*/ | ||
public function execute(array $payload); | ||
} |
This file contains 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,45 @@ | ||
<?php namespace Rollbar\Truncation; | ||
|
||
class MinBodyStrategy extends AbstractStrategy | ||
{ | ||
|
||
const EXCEPTION_MESSAGE_LIMIT = 256; | ||
const EXCEPTION_FRAMES_RANGE = 1; | ||
|
||
public function execute(array $payload) | ||
{ | ||
|
||
$traceData = null; | ||
|
||
if (isset($payload['data']['body']['trace'])) { | ||
$traceData = &$payload['data']['body']['trace']; | ||
} elseif (isset($payload['data']['body']['trace_chain'])) { | ||
$traceData = &$payload['data']['body']['trace_chain']; | ||
} | ||
|
||
/** | ||
* Delete exception description | ||
*/ | ||
unset($traceData['exception']['description']); | ||
|
||
/** | ||
* Truncate exception message | ||
*/ | ||
$traceData['exception']['message'] = substr( | ||
$traceData['exception']['message'], | ||
0, | ||
static::EXCEPTION_MESSAGE_LIMIT | ||
); | ||
|
||
/** | ||
* Limit trace frames | ||
*/ | ||
$framesStrategy = new FramesStrategy($this->dataBuilder); | ||
$traceData['frames'] = $framesStrategy->selectFrames( | ||
$traceData['frames'], | ||
static::EXCEPTION_FRAMES_RANGE | ||
); | ||
|
||
return $payload; | ||
} | ||
} |
This file contains 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,9 @@ | ||
<?php namespace Rollbar\Truncation; | ||
|
||
class RawStrategy extends AbstractStrategy | ||
{ | ||
public function execute(array $payload) | ||
{ | ||
return $payload; | ||
} | ||
} |
This file contains 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,28 @@ | ||
<?php namespace Rollbar\Truncation; | ||
|
||
class StringsStrategy extends AbstractStrategy | ||
{ | ||
|
||
public static function getThresholds() | ||
{ | ||
return array(1024, 512, 256); | ||
} | ||
|
||
public function execute(array $payload) | ||
{ | ||
foreach (static::getThresholds() as $threshold) { | ||
if (!$this->dataBuilder->needsTruncating($payload)) { | ||
break; | ||
} | ||
|
||
array_walk_recursive($payload, function (&$value) use ($threshold) { | ||
|
||
if (is_string($value) && strlen($value) > $threshold) { | ||
$value = substr($value, 0, $threshold); | ||
} | ||
}); | ||
} | ||
|
||
return $payload; | ||
} | ||
} |
This file contains 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 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,82 @@ | ||
<?php | ||
|
||
namespace Rollbar\Truncation; | ||
|
||
use Rollbar\DataBuilder; | ||
|
||
class FramesStrategyTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
/** | ||
* @dataProvider executeProvider | ||
*/ | ||
public function testExecute($data, $expected) | ||
{ | ||
$dataBuilder = new DataBuilder(array( | ||
'accessToken' => 'abcd1234efef5678abcd1234567890be', | ||
'environment' => 'tests' | ||
)); | ||
|
||
$strategy = new FramesStrategy($dataBuilder); | ||
$result = $strategy->execute($data); | ||
|
||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
public function executeProvider() | ||
{ | ||
$data = array( | ||
'nothing to truncate using trace key' => array( | ||
array('data' => array('body' => | ||
array('trace' => array('frames' => range(1, 6))) | ||
)), | ||
range(1, 6) | ||
), | ||
'nothing to truncate using trace_chain key' => array( | ||
array('data' => array('body' => | ||
array('trace_chain' => array('frames' => range(1, 6))) | ||
)), | ||
range(1, 6) | ||
), | ||
'truncate middle using trace key' => array( | ||
array('data' => array('body' => | ||
array( | ||
'trace' => array( | ||
'frames' => range( | ||
1, | ||
FramesStrategy::FRAMES_OPTIMIZATION_RANGE * 2 + 1 | ||
) | ||
) | ||
) | ||
)), | ||
array_merge( | ||
range(1, FramesStrategy::FRAMES_OPTIMIZATION_RANGE), | ||
range( | ||
FramesStrategy::FRAMES_OPTIMIZATION_RANGE + 2, | ||
FramesStrategy::FRAMES_OPTIMIZATION_RANGE * 2 + 1 | ||
) | ||
) | ||
), | ||
'truncate middle using trace_chain key' => array( | ||
array('data' => array('body' => | ||
array( | ||
'trace_chain' => array( | ||
'frames' => range( | ||
1, | ||
FramesStrategy::FRAMES_OPTIMIZATION_RANGE * 2 + 1 | ||
) | ||
) | ||
) | ||
)), | ||
array_merge( | ||
range(1, FramesStrategy::FRAMES_OPTIMIZATION_RANGE), | ||
range( | ||
FramesStrategy::FRAMES_OPTIMIZATION_RANGE + 2, | ||
FramesStrategy::FRAMES_OPTIMIZATION_RANGE * 2 + 1 | ||
) | ||
) | ||
) | ||
); | ||
|
||
return $data; | ||
} | ||
} |
Oops, something went wrong.