Skip to content

Commit

Permalink
feat: a new plugin to skip system traces
Browse files Browse the repository at this point in the history
  • Loading branch information
atimur committed Aug 14, 2024
1 parent abc5c9f commit 00f1d2a
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 4 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ phpspy --max-depth=-1 --time-limit-ms=59000 --threads=100 --rate-hz=25 --buffer-

namespace Zoon\PyroSpy\Plugins;

class MyAvesomePlugin implements PluginInterface {
class MyAwesomePlugin implements PluginInterface {

public function process(array $tags, array $trace): array {
//Modify tags and/or trace
Expand All @@ -92,6 +92,7 @@ Multiple plugins can be provided. Each plugin will get tags and trace from resul

2. Add `--request-info=QCuP` to phpspy args, to add uri string to tags.
3. Provide full path to it in pyrospy arguments.
4. To ignore a trace, return an empty trace.

Example:
```shell
Expand Down
35 changes: 35 additions & 0 deletions app/Plugins/SkipSleepTraces.php
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 SkipSleepTraces 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];
}
}
6 changes: 3 additions & 3 deletions app/Processor.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ private function runProducer(): Future

foreach ($this->plugins as $plugin) {
[$tags, $tracePrepared] = $plugin->process($tags, $tracePrepared);
}
if ($tracePrepared === []) {
continue;
if ($tracePrepared === []) {
continue 2;
}
}

$key = self::stringifyTrace($tracePrepared);
Expand Down
50 changes: 50 additions & 0 deletions tests/Plugins/SkipSleepTracesTest.php
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\SkipSleepTraces;

class SkipSleepTracesTest 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 SkipSleepTraces()],
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,
];
}
}

0 comments on commit 00f1d2a

Please sign in to comment.