Skip to content

Commit

Permalink
fix: no default limit/response limit
Browse files Browse the repository at this point in the history
  • Loading branch information
audunru committed Dec 2, 2022
1 parent 2cc5777 commit e3cef24
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 22 deletions.
11 changes: 3 additions & 8 deletions src/Listeners/LogMemoryUsage.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@ class LogMemoryUsage
/**
* Default paths where memory usage logging is enabled.
*/
private const DEFAULT_PATTERNS = ['*'];

/**
* Default memory usage limit.
*/
private const DEFAULT_LIMIT = 100;
private const DEFAULT_PATTERNS = [];

/**
* Default log channel.
Expand Down Expand Up @@ -62,9 +57,9 @@ public function handle(RequestHandled $event)
foreach (config('memory-usage.paths', []) as $options) {
$patterns = Arr::get($options, 'patterns', self::DEFAULT_PATTERNS);
$ignorePatternsForPath = Arr::get($options, 'ignore_patterns', self::DEFAULT_IGNORE_PATTERNS);
$limit = Arr::get($options, 'limit', self::DEFAULT_LIMIT);
$limit = Arr::get($options, 'limit');

if ($peakUsage > $limit && $event->request->is($patterns) && ! $event->request->is($ignorePatternsForPath)) {
if (! is_null($limit) && $peakUsage > $limit && $event->request->is($patterns) && ! $event->request->is($ignorePatternsForPath)) {
$channel = Arr::get($options, 'channel', self::DEFAULT_CHANNEL);
$level = Arr::get($options, 'level', self::DEFAULT_LEVEL);

Expand Down
11 changes: 3 additions & 8 deletions src/Listeners/LogSlowResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,7 @@ class LogSlowResponse
/**
* Default paths where memory usage logging is enabled.
*/
private const DEFAULT_PATTERNS = ['*'];

/**
* Default slow response limit.
*/
private const DEFAULT_SLOW_RESPONSE_LIMIT = 30;
private const DEFAULT_PATTERNS = [];

/**
* Default log channel.
Expand Down Expand Up @@ -51,9 +46,9 @@ public function handle(RequestHandled $event)
foreach (config('memory-usage.paths', []) as $options) {
$patterns = Arr::get($options, 'patterns', self::DEFAULT_PATTERNS);
$ignorePatternsForPath = Arr::get($options, 'ignore_patterns', self::DEFAULT_IGNORE_PATTERNS);
$slowResponseLimit = Arr::get($options, 'slow_response_limit', self::DEFAULT_SLOW_RESPONSE_LIMIT);
$slowResponseLimit = Arr::get($options, 'slow_response_limit');

if ($responseTime > $slowResponseLimit && $event->request->is($patterns) && ! $event->request->is($ignorePatternsForPath)) {
if (! is_null($slowResponseLimit) && $responseTime > $slowResponseLimit && $event->request->is($patterns) && ! $event->request->is($ignorePatternsForPath)) {
$channel = Arr::get($options, 'channel', self::DEFAULT_CHANNEL);
$level = Arr::get($options, 'level', self::DEFAULT_LEVEL);

Expand Down
6 changes: 6 additions & 0 deletions tests/Feature/MemoryUsageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace audunru\MemoryUsage\Tests\Feature;

use audunru\MemoryUsage\Helpers\MemoryHelper;
use audunru\MemoryUsage\Helpers\TimeHelper;
use audunru\MemoryUsage\Tests\TestCase;
use Illuminate\Log\Logger;
use Illuminate\Support\Facades\Log;
Expand Down Expand Up @@ -60,6 +61,11 @@ protected function setUp(): void
'ignore*',
],
]);

$this->mock(TimeHelper::class, function (MockInterface $mock) {
$mock->shouldReceive('getResponseTime')
->andReturn(1000);
});
}

public function testItLogsWarningToDefaultChannel()
Expand Down
8 changes: 2 additions & 6 deletions tests/Feature/SlowResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,27 @@ protected function setUp(): void
[
'patterns' => ['include*'],
'ignore_patterns' => ['include/higher*', 'include/lower*'],
'limit' => 10,
'slow_response_limit' => 3,
'channel' => null,
'level' => 'warning',
],
[
'patterns' => ['include/higher'],
'ignore_patterns' => [],
'limit' => 10,
'slow_response_limit' => 15,
'channel' => null,
'level' => 'warning',
],
[
'patterns' => ['include/lower'],
'ignore_patterns' => [],
'limit' => 10,
'slow_response_limit' => 1,
'channel' => null,
'level' => 'warning',
],
[
'patterns' => ['include*'],
'limit' => 10,
'ignore_patterns' => [],
'slow_response_limit' => 30,
'channel' => 'slack',
'level' => 'emergency',
Expand All @@ -54,10 +51,9 @@ protected function setUp(): void
],
]);

// Exclude memory usage logging when testing slow responses
$this->mock(MemoryHelper::class, function (MockInterface $mock) {
$mock->shouldReceive('getPeakUsage')
->andReturn(0);
->andReturn(1000);
});
}

Expand Down

0 comments on commit e3cef24

Please sign in to comment.