Skip to content
This repository has been archived by the owner on Sep 11, 2024. It is now read-only.

Commit

Permalink
Fix: Enabling/Disabling Tracker with Configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
rankarpan committed Jul 13, 2018
1 parent 40034f9 commit 26cd135
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 48 deletions.
4 changes: 2 additions & 2 deletions src/Console/IndexCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@ public function handle(ElasticSearch $es)
$this->info("The {$index}_write alias for the {$index} index was created!");

$types = [
'log_queries',
'logs',
'sql_queries',
'error_queries'
'errors'
];

foreach ($types as $type) {
Expand Down
5 changes: 2 additions & 3 deletions src/Exceptions/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,13 @@ public function report(Exception $exception)
*
* @param \Illuminate\Http\Request $request
* @param \Exception $exception
*
* @return \Illuminate\Http\Response
*/
public function render($request, Exception $exception)
{
$exception = $this->prepareException($exception);

$tracker = new Tracker;
$tracker->errorQuery($request, $exception);
$tracker->trackError($request, $this->prepareException($exception));

return $this->previous ? $this->previous->render($request, $exception) : null;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Middleware/TrackerCookie.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public function handle($request, Closure $next)
{
$tracker = new Tracker;

if ($tracker->getTrackerDisabled() || ($tracker->getLogTrackerDisabled() && $tracker->getSqlTrackerDisabled())) {
/*if ($tracker->getTrackerDisabled()) {
return $next($request);
}
}*/

$cookie = $tracker->cookieTracker();

Expand Down
6 changes: 3 additions & 3 deletions src/Middleware/TrackerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ public function handle($request, Closure $next)
{
$tracker = new Tracker;

if ($tracker->getTrackerDisabled() || $tracker->getLogTrackerDisabled()) {
/*if ($tracker->getTrackerDisabled('logs')) {
return $next($request);
}
}*/

$cookie = $tracker->cookieTracker();

Expand All @@ -83,7 +83,7 @@ public function handle($request, Closure $next)

$response = $next($request);

$tracker->logQuery($request, $model);
$tracker->trackLog($request, $model);

return $response;
}
Expand Down
42 changes: 19 additions & 23 deletions src/Tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,23 +45,23 @@ public function __construct()
$this->es = new ElasticSearch;
}

public function logQuery($request, $model)
public function trackLog($request, $model)
{
if ($this->excludedTracker()) {
return false;
}

$model = $this->indexLogQueryDocument($request, $model);
$type = 'log_queries';
$model = $this->indexLogDocument($request, $model);
$type = 'logs';

if (Config::get('tracker.queue', false)) {
return $this->indexQueueLogQueryDocument($model, $type);
return $this->indexQueueLogDocument($model, $type);
}

return $this->es->indexDocument($model, $type);
}

public function indexQueueLogQueryDocument($model, $type)
public function indexQueueLogDocument($model, $type)
{
dispatch((new TrackerIndexQueuedModels($model, $type))
->onQueue($this->syncWithTrackerUsingQueue())
Expand All @@ -70,7 +70,7 @@ public function indexQueueLogQueryDocument($model, $type)
return true;
}

public function indexLogQueryDocument($request, $model)
public function indexLogDocument($request, $model)
{
$agent = new Agent;
$browser = $agent->browser();
Expand Down Expand Up @@ -169,23 +169,23 @@ public function indexSqlQueryDocument($sql, $bindings, $time, $connection_name)
return $model;
}

public function errorQuery($request, $exception)
public function trackError($request, $exception)
{
if ($this->excludedTracker()) {
return false;
}

$model = $this->indexErrorQueryDocument($request, $exception);
$type = 'error_queries';
$model = $this->indexErrorDocument($request, $exception);
$type = 'errors';

if (Config::get('tracker.queue', false)) {
return $this->indexQueueErrorQueryDocument($model, $type);
return $this->indexQueueErrorDocument($model, $type);
}

return $this->es->indexDocument($model, $type);
}

public function indexQueueErrorQueryDocument($model, $type)
public function indexQueueErrorDocument($model, $type)
{
dispatch((new TrackerIndexQueuedModels($model, $type))
->onQueue($this->syncWithTrackerUsingQueue())
Expand All @@ -194,7 +194,7 @@ public function indexQueueErrorQueryDocument($model, $type)
return true;
}

public function indexErrorQueryDocument($request, $exception)
public function indexErrorDocument($request, $exception)
{
$cookie = $this->cookieTracker();
$log = Cache::tags('tracker')->get($cookie);
Expand All @@ -215,19 +215,15 @@ public function indexErrorQueryDocument($request, $exception)
return $model;
}

public function getTrackerDisabled(): bool
public function getTrackerDisabled($type = true): bool
{
return Config::get('tracker.disabled.all_queries', false);
}

public function getLogTrackerDisabled(): bool
{
return Config::get('tracker.disabled.log_queries', false);
}
$disabled_type = Config::get('tracker.disabled', false);

public function getSqlTrackerDisabled(): bool
{
return Config::get('tracker.disabled.sql_queries', false);
if (! is_array($disabled_type)) {
return $type === $disabled_type;
} else {
return in_array($type, $disabled_type);
}
}

public function excludedTracker(): bool
Expand Down
27 changes: 17 additions & 10 deletions src/TrackerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,19 @@ public function boot()
__DIR__.'/config/tracker.php' => config_path('tracker.php'),
], 'tracker');

$this->registerMiddlewareToGroup('web', TrackerCookie::class);
$this->registerMiddlewareToGroup('web', TrackerMiddleware::class);
$tracker = new Tracker;

if (! $tracker->getTrackerDisabled()) {
$this->registerMiddlewareToGroup('web', TrackerCookie::class);
}

$this->registerErrorHandler();
if (! $tracker->getTrackerDisabled('logs')) {
$this->registerMiddlewareToGroup('web', TrackerMiddleware::class);
}

if (! $tracker->getTrackerDisabled('errors')) {
$this->registerErrorHandler();
}
}

/**
Expand All @@ -62,17 +71,15 @@ public function register()
]);
}

$this->registerSqlQueryLogWatcher();
}

protected function registerSqlQueryLogWatcher()
{
$tracker = new Tracker;

if ($tracker->getTrackerDisabled() || $tracker->getSqlTrackerDisabled()) {
return false;
if (! $tracker->getTrackerDisabled('sql_queries')) {
$this->registerSqlQueryLogWatcher($tracker);
}
}

protected function registerSqlQueryLogWatcher(Tracker $tracker)
{
if (class_exists('Illuminate\Database\Events\QueryExecuted')) {
$this->app['events']->listen('Illuminate\Database\Events\QueryExecuted', function ($query) use ($tracker) {
$tracker->sqlQuery($query->sql, $query->bindings, $query->time, $query->connectionName);
Expand Down
8 changes: 3 additions & 5 deletions src/config/tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,10 @@
|
| By setting this value to true, the tracker will be disabled completely.
|
| Supported: ['logs', 'sql_queries', 'errors']
|
*/
'disabled' => [
'all_queries' => env('TRACKER_DISABLED', false),
'log_queries' => env('TRACKER_LOG_QUERIES_DISABLED', false),
'sql_queries' => env('TRACKER_SQL_QUERIES_DISABLED', false)
],
'disabled' => env('TRACKER_DISABLED', false),

/*
|--------------------------------------------------------------------------
Expand Down

0 comments on commit 26cd135

Please sign in to comment.