From 4e0836d6e48eb82a5c05cbf34d2038023dce9ca9 Mon Sep 17 00:00:00 2001 From: Vincent Rolfs Date: Wed, 29 Jul 2020 15:22:31 +0800 Subject: [PATCH 1/4] Remove call to Bootstrap::exceptionHandler and do the boilerplate work ourselves in order to be able to send more data to error reporting. Enable users to supply additional context data as they wish. --- src/StackdriverExceptionHandler.php | 64 ++++++++++++++++++++++++++++- 1 file changed, 62 insertions(+), 2 deletions(-) diff --git a/src/StackdriverExceptionHandler.php b/src/StackdriverExceptionHandler.php index fb8abd2..27a2ed0 100644 --- a/src/StackdriverExceptionHandler.php +++ b/src/StackdriverExceptionHandler.php @@ -5,11 +5,17 @@ use Google\Cloud\Core\Report\SimpleMetadataProvider; use Google\Cloud\ErrorReporting\Bootstrap; use Google\Cloud\Logging\LoggingClient; +use Illuminate\Support\Facades\Auth; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\Request; class StackdriverExceptionHandler { - public static function report($exception): void + /** + * @param mixed $exception \Throwable (PHP 7) or \Exception (PHP 5) + * @param array $additionalContext + */ + public static function report($exception, array $additionalContext = []): void { if (config('stackdriver.enabled') === false || config('stackdriver.error_reporting.enabled') === false) { return; @@ -40,6 +46,60 @@ public static function report($exception): void $psrLogger = $logging->psrLogger('error-log', $psrOptions); Bootstrap::init($psrLogger); - Bootstrap::exceptionHandler($exception); + self::logError($psrLogger, $exception, $additionalContext); + } + + private static function logError($psrLogger, $exception, array $additionalContext): void + { + $message = sprintf('PHP Notice: %s', (string) $exception); + + $psrLogger->error($message, array_merge_recursive([ + 'context' => [ + 'reportLocation' => [ + 'filePath' => $exception->getFile(), + 'lineNumber' => $exception->getLine(), + 'functionName' => self::getFunctionNameForReport($exception->getTrace()), + ], + 'user' => (string) (Auth::user()->id ?? ''), + 'httpRequest' => [ + 'url' => Request::url(), + 'method' => Request::method(), + 'userAgent' => Request::userAgent(), + 'referrer' => Request::server('HTTP_REFERER'), + 'remoteIp' => Request::ip(), + ], + ], + 'serviceContext' => [ + 'service' => $psrLogger->getMetadataProvider()->serviceId(), + 'version' => $psrLogger->getMetadataProvider()->versionId(), + ], + ], $additionalContext)); + } + + /** + * Copied from Google\Cloud\ErrorReporting\Bootstrap::getFunctionNameForReport as that function is private + * + * Format the function name from a stack trace. This could be a global + * function (function_name), a class function (Class->function), or a static + * function (Class::function). + * + * @param array $trace The stack trace returned from Exception::getTrace() + */ + private static function getFunctionNameForReport(array $trace = null) + { + if (null === $trace) { + return ''; + } + if (empty($trace[0]['function'])) { + return ''; + } + $functionName = [$trace[0]['function']]; + if (isset($trace[0]['type'])) { + $functionName[] = $trace[0]['type']; + } + if (isset($trace[0]['class'])) { + $functionName[] = $trace[0]['class']; + } + return implode('', array_reverse($functionName)); } } From dbd85a61e86801dc5ec5aa39e08c400ced77e48d Mon Sep 17 00:00:00 2001 From: Vincent Rolfs Date: Wed, 29 Jul 2020 15:23:37 +0800 Subject: [PATCH 2/4] Update README.md with instructions on how to supply additional context data to error reporting. --- README.md | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 689cd0a..081ce07 100644 --- a/README.md +++ b/README.md @@ -74,6 +74,21 @@ public function report(Exception $exception) } ``` +You can optionally supply a second argument to `StackdriverExceptionHandler::report` with which you can send additional information to the error reporting. +The [Google Cloud Error Reporting Reference][link-error-reporting-reference] specifies the correct format. + +For example, if you want to add information about the deployed revision in which the error occurred, you may use the following: + +``` +StackdriverExceptionHandler::report($exception, [ + 'context' => [ + 'sourceReferences' => [ + 'revisionId' => 'example' + ] + ] +]); +``` + Log in to Google Cloud Console and you should start seeing logs, traces and errors appear. ### Batch daemon @@ -117,4 +132,5 @@ Please see the [license file](LICENSE.md) for more information. [link-packagist]: https://packagist.org/packages/gluedev/laravel-stackdriver [link-downloads]: https://packagist.org/packages/gluedev/laravel-stackdriver [link-author]: https://github.com/diederikvandenb -[link-contributors]: ../../contributors] +[link-contributors]: ../../contributors +[link-error-reporting-reference]: https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorContext From 0524791eae9036f2f059e54b2fdd8728b8fa3f53 Mon Sep 17 00:00:00 2001 From: Vincent Rolfs Date: Wed, 29 Jul 2020 16:03:11 +0800 Subject: [PATCH 3/4] Use array_replace_recursive instead of array_merge_recursive --- src/StackdriverExceptionHandler.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/StackdriverExceptionHandler.php b/src/StackdriverExceptionHandler.php index 27a2ed0..8a92b12 100644 --- a/src/StackdriverExceptionHandler.php +++ b/src/StackdriverExceptionHandler.php @@ -53,7 +53,7 @@ private static function logError($psrLogger, $exception, array $additionalContex { $message = sprintf('PHP Notice: %s', (string) $exception); - $psrLogger->error($message, array_merge_recursive([ + $psrLogger->error($message, array_replace_recursive([ 'context' => [ 'reportLocation' => [ 'filePath' => $exception->getFile(), From 9dd09336301d407b6cc5b2584af0a198b0be69c0 Mon Sep 17 00:00:00 2001 From: Vincent Rolfs Date: Wed, 26 Apr 2023 17:48:25 +0300 Subject: [PATCH 4/4] Update opencensus/opencensus dependency to ^0.7.0 See https://github.com/GlueDev/laravel-stackdriver/pull/31 --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0332941..4a66fb4 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,7 @@ "illuminate/support": ">=5.7", "google/cloud-error-reporting": "^0.16.2", "google/cloud-logging": "^1.14", - "opencensus/opencensus": "^0.5.2", + "opencensus/opencensus": "^0.7.0", "opencensus/opencensus-exporter-stackdriver": "^0.1", "php": ">=7.1" },