Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filter requests by http method #406

Merged
merged 2 commits into from
Jul 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Clockwork/Support/Laravel/ClockworkSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ public function isCollectingRequests()
{
return ($this->isEnabled() || $this->getConfig('collect_data_always', false))
&& ! $this->app->runningInConsole()
&& ! $this->isMethodFiltered($this->app['request']->getMethod())
&& ! $this->isUriFiltered($this->app['request']->getRequestUri());
}

Expand Down Expand Up @@ -366,6 +367,14 @@ public function isUriFiltered($uri)
return false;
}

public function isMethodFiltered($method)
{
return in_array($method, array_map(
function ($method) { return strtoupper($method); },
$this->getConfig('filter_methods', [])
));
}

protected function isCommandFiltered($command)
{
$whitelist = $this->getConfig('artisan.only', []);
Expand Down
9 changes: 7 additions & 2 deletions Clockwork/Support/Laravel/config/clockwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -267,11 +267,12 @@

/*
|--------------------------------------------------------------------------
| Disable data collection for certain URIs
| Disable data collection for certain URIs or methods
|--------------------------------------------------------------------------
|
| You can disable data collection for specific URIs by adding matching
| regular expressions here.
| regular expressions here. You can also disable collecting all requests
| with a specific method.
|
*/

Expand All @@ -280,6 +281,10 @@
'/telescope/.*' // Laravel Telescope requests
],

'filter_methods' => [
'options' // mostly used in the csrf pre-flight requests and is rarely of interest
],

/*
|--------------------------------------------------------------------------
| Enable collecting of stack traces
Expand Down
25 changes: 25 additions & 0 deletions Clockwork/Support/Vanilla/Clockwork.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ public function requestProcessed()
{
if (! $this->config['enable'] && ! $this->config['collect_data_always']) return;

if (isset($_SERVER['REQUEST_METHOD']) && $this->isMethodFiltered($_SERVER['REQUEST_METHOD'])) return;
if (isset($_SERVER['REQUEST_URI']) && $this->isUriFiltered($_SERVER['REQUEST_URI'])) return;

$this->clockwork->getTimeline()->endEvent('total');

$this->clockwork->resolveRequest()->storeRequest();
Expand Down Expand Up @@ -206,6 +209,28 @@ protected function configureSerializer()
]);
}

protected function isUriFiltered($uri)
{
$filterUris = $this->config['filter_uris'];
$filterUris[] = rtrim($this->config['api'], '/'); // don't collect data for Clockwork requests

foreach ($filterUris as $filterUri) {
$regexp = '#' . str_replace('#', '\#', $filterUri) . '#';

if (preg_match($regexp, $uri)) return true;
}

return false;
}

protected function isMethodFiltered($method)
{
return in_array($method, array_map(
function ($method) { return strtoupper($method); },
$this->config['filter_methods']
));
}

protected function setHeader($header, $value)
{
if ($this->psrResponse) {
Expand Down
19 changes: 19 additions & 0 deletions Clockwork/Support/Vanilla/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,25 @@

'storage_expiration' => isset($_ENV['CLOCKWORK_STORAGE_EXPIRATION']) ? $_ENV['CLOCKWORK_STORAGE_EXPIRATION'] : 60 * 24 * 7,

/*
|--------------------------------------------------------------------------
| Disable data collection for certain URIs or methods
|--------------------------------------------------------------------------
|
| You can disable data collection for specific URIs by adding matching
| regular expressions here. You can also disable collecting all requests
| with a specific method.
|
*/

'filter_uris' => [
// '/some/uri/.*'
],

'filter_methods' => [
'options' // mostly used in the csrf pre-flight requests and is rarely of interest
],

/*
|--------------------------------------------------------------------------
| Enable collecting of stack traces
Expand Down