Skip to content

Commit

Permalink
Feature / support for multiple Raygun App Keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dnsl48 committed Jul 26, 2019
1 parent 94ed4b3 commit 1985388
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 11 deletions.
57 changes: 56 additions & 1 deletion README.md
Expand Up @@ -25,7 +25,6 @@ Name: raygunning-left-and-main
SilverStripe\Admin\LeftAndMain:
extensions:
- SilverStripe\Raygun\LeftAndMainExtension

```

#### Set Log Level
Expand All @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Expand Up @@ -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": {
Expand Down
37 changes: 37 additions & 0 deletions src/CustomAppKeyProvider.php
@@ -0,0 +1,37 @@
<?php declare(strict_types=1);

namespace SilverStripe\Raygun;

trait CustomAppKeyProvider
{
/**
* Use a custom APP Key instead of the globally defined SS_RAYGUN_APP_KEY
*
* @var null|string
*/
private $customRaygunAppKey;

/**
* Set a custom raygun app key that should be used instead of the
* global default
*
* @param string $key
*
* @return $this
*/
public function setCustomRaygunAppKey(string $key): self
{
$this->customRaygunAppKey = $key;
return $this;
}

/**
* Returns the custom raygun app key if set
*
* @return null|string
*/
public function getCustomRaygunAppKey(): ?string
{
return $this->customRaygunAppKey;
}
}
8 changes: 5 additions & 3 deletions src/LeftAndMainExtension.php
Expand Up @@ -11,17 +11,19 @@
*/
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,
* otherwise we may miss some errors in the bundles
*/
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('<!-- Raygun: SS_RAYGUN_APP_KEY is undefined -->');
Requirements::insertHeadTags('<!-- Raygun app key is undefined -->');
} else {
$htmlBlock = <<<HTML
<!-- Raygun -->
Expand All @@ -41,7 +43,7 @@ public function accessedCMS()
</script>
<!-- End Raygun crash reporting -->
HTML;
Requirements::insertHeadTags($htmlBlock, hash('crc32', $htmlBlock).'-'.$apiKey);
Requirements::insertHeadTags($htmlBlock, hash('crc32', $htmlBlock) . '-' . $apiKey);
}
}
}
8 changes: 4 additions & 4 deletions src/RaygunClientFactory.php
Expand Up @@ -10,6 +10,7 @@

class RaygunClientFactory implements Factory
{
use CustomAppKeyProvider;

/**
* The environment variable used to assign the Raygun api key
Expand All @@ -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;
Expand Down Expand Up @@ -74,5 +75,4 @@ protected function filterSensitiveData()
'Cookie' => true,
]);
}

}
3 changes: 2 additions & 1 deletion src/RaygunHandler.php
Expand Up @@ -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;

Expand Down

0 comments on commit 1985388

Please sign in to comment.