From 808955294dbfd957d33798edd9d4b447e60287d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20Duli=C3=A7i?= Date: Wed, 19 Oct 2022 01:29:40 +0300 Subject: [PATCH] improved error tracking --- app/Exceptions/Handler.php | 22 +------------ app/Exceptions/Trackers/Bugsnag.php | 23 +++++++++++++ app/Exceptions/Trackers/Sentry.php | 50 +++++++++++++++++++++++++++++ config/bugsnag.php | 12 +++++++ config/sentry.php | 4 +++ 5 files changed, 90 insertions(+), 21 deletions(-) create mode 100644 app/Exceptions/Trackers/Bugsnag.php create mode 100644 app/Exceptions/Trackers/Sentry.php diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index 493ddf538..7878ebe75 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -56,28 +56,8 @@ class Handler extends ExceptionHandler public function register() { $this->reportable(function (Throwable $e) { - if (config('logging.default') == 'sentry') { - app('sentry')->configureScope(function ($scope) { - $scope->setTag('company_id', (string) company_id()); - $scope->setTag('locale', (string) app()->getLocale()); - $scope->setTag('timezone', (string) config('app.timezone')); - }); - - //define('SENTRY_RELEASE', version('short')); - } - if (config('logging.default') == 'bugsnag') { - app('bugsnag')->registerCallback(function ($report) { - $report->setMetaData([ - 'akaunting' => [ - 'company_id' => (string) company_id(), - 'locale' => (string) app()->getLocale(), - 'timezone' => (string) config('app.timezone'), - ] - ]); - }); - - app('bugsnag')->setAppVersion(version('short')); + call_user_func(config('bugsnag.before_send'), $e); } }); } diff --git a/app/Exceptions/Trackers/Bugsnag.php b/app/Exceptions/Trackers/Bugsnag.php new file mode 100644 index 000000000..b98bb26ea --- /dev/null +++ b/app/Exceptions/Trackers/Bugsnag.php @@ -0,0 +1,23 @@ +setAppVersion(version('short')); + + app('bugsnag')->registerCallback(function ($report) { + $report->setMetaData([ + 'akaunting' => [ + 'company_id' => (string) company_id(), + 'locale' => (string) app()->getLocale(), + 'timezone' => (string) config('app.timezone'), + ] + ]); + }); + } +} diff --git a/app/Exceptions/Trackers/Sentry.php b/app/Exceptions/Trackers/Sentry.php new file mode 100644 index 000000000..d8b720d66 --- /dev/null +++ b/app/Exceptions/Trackers/Sentry.php @@ -0,0 +1,50 @@ +setRelease(version('short')); + + $event->setTags([ + 'company_id' => (string) company_id(), + 'locale' => (string) app()->getLocale(), + 'timezone' => (string) config('app.timezone'), + ]); + + return $event; + } + + public static function tracesSampler(SamplingContext $context): float + { + if (static::filterAgent()) { + return 0.0; + } + + return config('sentry.traces_sample_rate'); + } + + public static function filterAgent(): bool + { + $user_agent = request()->userAgent(); + + $filter_agents = explode(',', env('SENTRY_TRACES_FILTER_AGENTS')); + + foreach ($filter_agents as $filter_agent) { + if (! Str::contains($user_agent, $filter_agent)) { + continue; + } + + return true; + } + + return false; + } +} diff --git a/config/bugsnag.php b/config/bugsnag.php index 9435808e4..fb4b5b95b 100644 --- a/config/bugsnag.php +++ b/config/bugsnag.php @@ -349,4 +349,16 @@ return [ */ 'feature_flags' => [], + + /* + |-------------------------------------------------------------------------- + | Before send + |-------------------------------------------------------------------------- + | + | An array of callback class and method. + | + */ + + 'before_send' => [env('BUGSNAG_BEFORE_SEND_CLASS', 'App\\Exceptions\\Trackers\\Bugsnag'), 'beforeSend'], + ]; diff --git a/config/sentry.php b/config/sentry.php index 7ff3e3db4..b76b2ed9f 100644 --- a/config/sentry.php +++ b/config/sentry.php @@ -54,4 +54,8 @@ return [ 'controllers_base_namespace' => env('SENTRY_CONTROLLERS_BASE_NAMESPACE', 'App\\Http\\Controllers'), + 'before_send' => [env('SENTRY_BEFORE_SEND_CLASS', 'App\\Exceptions\\Trackers\\Sentry'), 'beforeSend'], + + 'traces_sampler' => [env('SENTRY_TRACES_SAMPLER_CLASS', 'App\\Exceptions\\Trackers\\Sentry'), 'tracesSampler'], + ];