-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: a new plugin to skip system traces
- Loading branch information
atimur
committed
Aug 12, 2024
1 parent
abc5c9f
commit b86dddb
Showing
4 changed files
with
90 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
namespace Zoon\PyroSpy\Plugins; | ||
|
||
use Zoon\PyroSpy\Sample; | ||
|
||
/** | ||
* @psalm-import-type TagsArray from Sample | ||
* @psalm-import-type TraceStruct from Sample | ||
*/ | ||
final class SkipSystemTraces implements PluginInterface | ||
{ | ||
private const SYSTEM_FRAMES = [ | ||
'Fiber::start', | ||
'Fiber::resume', | ||
'pcntl_wait', | ||
'Pheanstalk::reserveWithTimeout', | ||
]; | ||
|
||
/** | ||
* @param TagsArray $tags | ||
* @param TraceStruct $trace | ||
* @return array{0: TagsArray, 1: TraceStruct} | ||
*/ | ||
public function process(array $tags, array $trace): array | ||
{ | ||
foreach ($trace as $frame) { | ||
if (\in_array($frame[0], self::SYSTEM_FRAMES, true)) { | ||
return [$tags, []]; | ||
} | ||
} | ||
|
||
return [$tags, $trace]; | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use Zoon\PyroSpy\Plugins\SkipSystemTraces; | ||
|
||
class SkipSystemTracesTest extends PHPUnit\Framework\TestCase | ||
{ | ||
#[DataProvider('dataProvider')] | ||
public function testPlugin(int $batchLimit, int $samplesSent, string $traces): void | ||
{ | ||
$sender = $this->createMock(\Zoon\PyroSpy\SampleSenderInterface::class); | ||
$sender->expects($this->exactly($samplesSent))->method('sendSample'); | ||
|
||
$processor = new \Zoon\PyroSpy\Processor( | ||
interval: 100500, | ||
batchLimit: $batchLimit, | ||
sender: $sender, | ||
plugins: [new SkipSystemTraces()], | ||
sendSampleFutureLimit: 999999999, | ||
concurrentRequestLimit: 1, | ||
dataReader: new \Amp\ByteStream\Payload($traces), | ||
); | ||
|
||
$processor->process(); | ||
} | ||
|
||
public static function dataProvider(): Generator | ||
{ | ||
yield | ||
'It should send 2 samples (one is skipped due to having a system trace)' => | ||
[ | ||
'batchLimit' => 1, | ||
'samplesSent' => 2, | ||
'traces' => <<<EOT | ||
0 usleep <internal>:-1 | ||
1 <main> <internal>:-1 | ||
0 pcntl_wait <internal>:-1 | ||
1 <main> <internal>:-1 | ||
0 strstr <internal>:-1 | ||
1 <main> <internal>:-1 | ||
EOT, | ||
]; | ||
} | ||
} |