Add instance id to error reports (#14444)

* Add instance id to error reports

Alternative to #14261

* lint fix
This commit is contained in:
Tony Murray
2022-10-19 18:45:27 -05:00
committed by GitHub
parent b18ba17af0
commit da8befca06
2 changed files with 70 additions and 0 deletions

View File

@@ -0,0 +1,49 @@
<?php
/**
* SetInstanceId.php
*
* -Description-
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
* @link https://www.librenms.org
*/
namespace App\Logging\Reporting\Middleware;
use App\Providers\ErrorReportingProvider;
use Facade\FlareClient\Report;
class SetInstanceId
{
/**
* Middleware to add instance ID, piggybacks on the "user id" feature.
*
* @param \Facade\FlareClient\Report $report
* @param callable $next
* @return mixed
*/
public function handle(Report $report, $next)
{
try {
$user = $report->getGroup('user', []);
$user['id'] = ErrorReportingProvider::getInstanceId();
$report->group('user', $user);
} catch (\Exception $e) {
}
return $next($report);
}
}

View File

@@ -28,6 +28,8 @@ namespace App\Providers;
use App\Logging\Reporting\Middleware\AddGitInformation;
use App\Logging\Reporting\Middleware\CleanContext;
use App\Logging\Reporting\Middleware\SetGroups;
use App\Logging\Reporting\Middleware\SetInstanceId;
use App\Models\Callback;
use ErrorException;
use Facade\FlareClient\Report;
use Facade\Ignition\Facades\Flare;
@@ -44,6 +46,8 @@ class ErrorReportingProvider extends \Facade\Ignition\IgnitionServiceProvider
private $laravelErrorHandler;
/** @var bool */
private $reportingEnabled;
/** @var string|null */
private static $instanceId;
public function boot(): void
{
@@ -79,6 +83,7 @@ class ErrorReportingProvider extends \Facade\Ignition\IgnitionServiceProvider
// Add more LibreNMS related info
Flare::registerMiddleware(SetGroups::class);
Flare::registerMiddleware(SetInstanceId::class);
// Override the Laravel error handler but save it to call when in modern code
$this->laravelErrorHandler = set_error_handler([$this, 'handleError']);
@@ -170,4 +175,20 @@ class ErrorReportingProvider extends \Facade\Ignition\IgnitionServiceProvider
return true;
}
public static function getInstanceId(): string
{
if (is_null(self::$instanceId)) {
$uuid = Callback::get('error_reporting_uuid');
if (! $uuid) {
$uuid = Str::uuid();
Callback::set('error_reporting_uuid', $uuid);
}
self::$instanceId = $uuid;
}
return self::$instanceId;
}
}