From 198538894085573d57e50b509b96983f1dcf37c2 Mon Sep 17 00:00:00 2001 From: Serge Latyntcev Date: Thu, 18 Jul 2019 16:03:22 +1200 Subject: [PATCH] Feature / support for multiple Raygun App Keys --- README.md | 57 +++++++++++++++++++++++++++++++++++- composer.json | 4 +-- src/CustomAppKeyProvider.php | 37 +++++++++++++++++++++++ src/LeftAndMainExtension.php | 8 +++-- src/RaygunClientFactory.php | 8 ++--- src/RaygunHandler.php | 3 +- 6 files changed, 106 insertions(+), 11 deletions(-) create mode 100644 src/CustomAppKeyProvider.php diff --git a/README.md b/README.md index b558a82..1f2c0d6 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ Name: raygunning-left-and-main SilverStripe\Admin\LeftAndMain: extensions: - SilverStripe\Raygun\LeftAndMainExtension - ``` #### Set Log Level @@ -40,6 +39,62 @@ SilverStripe\Core\Injector\Injector: level: 'error' ``` +#### Multiple Raygun API Keys (app keys) + +You may have more than one Raygun integration, each of which could use custom API keys, different +from the default one (set by `SS_RAYGUN_APP_KEY`). To do so you'll need to configure each one of them separately. Here are some examples: + +```yml +# Here's an example of the LeftAndMainExtension using a custom raygun +# API Key, set through a custom environment variable (SS_CUSTOM_RAYGUN_APP_KEY) + +--- +Name: custom-raygun-leftnmain-extension +--- +SilverStripe\Core\Injector\Injector: + SilverStripe\Raygun\LeftAndMainExtension.custom: + class: SilverStripe\Raygun\LeftAndMainExtension + properties: + # You'll have to set the SS_CUSTOM_RAYGUN_APP_KEY environment var + CustomRaygunAppKey: '`SS_CUSTOM_RAYGUN_APP_KEY`' + +--- +Name: raygunning-left-and-main +After: custom-raygun-leftnmain-extension +--- +SilverStripe\Admin\LeftAndMain: + extensions: + - SilverStripe\Raygun\LeftAndMainExtension.custom +``` + +```yml +# Here's an example of a custom Raygun handler for Monolog +# which uses API Key provided by a custom RaygunClientFactory + +--- +Name: custom-monolog-raygun-handler +--- +SilverStripe\Core\Injector\Injector: + SilverStripe\Raygun\RaygunClientFactory.custom: + class: SilverStripe\Raygun\RaygunClientFactory + properties: + # You'll have to set the SS_CUSTOM_RAYGUN_APP_KEY environment var + CustomRaygunAppKey: '`SS_CUSTOM_RAYGUN_APP_KEY`' + + Raygun4php\RaygunClient.custom: + factory: '%$SilverStripe\Raygun\RaygunClientFactory.custom' + + SilverStripe\Raygun\RaygunHandler.custom: + class: SilverStripe\Raygun\RaygunHandler + constructor: + client: '%$Raygun4php\RaygunClient.custom' + level: 'debug' + + Psr\Log\LoggerInterface: + calls: + - [ pushHandler, [ '%$SilverStripe\Raygun\RaygunHandler.custom'] ] +``` + ## Filtering Raygun will send the following data: diff --git a/composer.json b/composer.json index f7025f5..16b77f9 100644 --- a/composer.json +++ b/composer.json @@ -12,9 +12,9 @@ } ], "require": { - "php": ">=5.6.0", + "php": ">=7.1", "mindscape/raygun4php": "^1", - "silverstripe/framework": "^4.0", + "silverstripe/framework": "^4.3", "graze/monolog-extensions": "^2" }, "autoload": { diff --git a/src/CustomAppKeyProvider.php b/src/CustomAppKeyProvider.php new file mode 100644 index 0000000..d0cced6 --- /dev/null +++ b/src/CustomAppKeyProvider.php @@ -0,0 +1,37 @@ +customRaygunAppKey = $key; + return $this; + } + + /** + * Returns the custom raygun app key if set + * + * @return null|string + */ + public function getCustomRaygunAppKey(): ?string + { + return $this->customRaygunAppKey; + } +} diff --git a/src/LeftAndMainExtension.php b/src/LeftAndMainExtension.php index 40bba51..ded4556 100644 --- a/src/LeftAndMainExtension.php +++ b/src/LeftAndMainExtension.php @@ -11,6 +11,8 @@ */ class LeftAndMainExtension extends Extension { + use CustomAppKeyProvider; + /** * It may seem weird we're using this extension point to register raygun, but * that's important to register it before the other scripts start executing, @@ -18,10 +20,10 @@ class LeftAndMainExtension extends Extension */ public function accessedCMS() { - $apiKey = Environment::getEnv('SS_RAYGUN_APP_KEY'); + $apiKey = $this->getCustomRaygunAppKey() ?? Environment::getEnv(RaygunClientFactory::RAYGUN_APP_KEY_NAME); if (empty($apiKey)) { - Requirements::insertHeadTags(''); + Requirements::insertHeadTags(''); } else { $htmlBlock = << @@ -41,7 +43,7 @@ public function accessedCMS() HTML; - Requirements::insertHeadTags($htmlBlock, hash('crc32', $htmlBlock).'-'.$apiKey); + Requirements::insertHeadTags($htmlBlock, hash('crc32', $htmlBlock) . '-' . $apiKey); } } } diff --git a/src/RaygunClientFactory.php b/src/RaygunClientFactory.php index 2137a1e..6059ff3 100644 --- a/src/RaygunClientFactory.php +++ b/src/RaygunClientFactory.php @@ -10,6 +10,7 @@ class RaygunClientFactory implements Factory { + use CustomAppKeyProvider; /** * The environment variable used to assign the Raygun api key @@ -32,13 +33,13 @@ class RaygunClientFactory implements Factory public function create($service, array $params = []) { // extract api key from .env file - $apiKey = (string) Environment::getEnv(self::RAYGUN_APP_KEY_NAME); + $apiKey = $this->getCustomRaygunAppKey() ?? (string) Environment::getEnv(self::RAYGUN_APP_KEY_NAME); $disableTracking = Config::inst()->get( - RaygunClient::class, 'disable_user_tracking' + RaygunClient::class, + 'disable_user_tracking' ); $disableTracking = is_bool($disableTracking) ? $disableTracking : false; - // log error to warn user that exceptions will not be logged to Raygun if (empty($apiKey) && !Director::isDev()) { $name = self::RAYGUN_APP_KEY_NAME; @@ -74,5 +75,4 @@ protected function filterSensitiveData() 'Cookie' => true, ]); } - } diff --git a/src/RaygunHandler.php b/src/RaygunHandler.php index 7c11bf8..e5a2520 100644 --- a/src/RaygunHandler.php +++ b/src/RaygunHandler.php @@ -12,7 +12,8 @@ class RaygunHandler extends MonologRaygunHandler protected function write(array $record) { $disableTracking = Config::inst()->get( - RaygunClient::class, 'disable_user_tracking' + RaygunClient::class, + 'disable_user_tracking' ); $disableTracking = is_bool($disableTracking) ? $disableTracking : false;