-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Roel van Hintum
committed
Nov 26, 2019
0 parents
commit 6aadd9d
Showing
9 changed files
with
271 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
.DS_Store | ||
/vendor |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
# Craft Sentry Changelog | ||
|
||
## 1.0.0-beta.1 - 2019-11-25 | ||
### Added | ||
- Initial Release |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 Born05 | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Sentry plugin for Craft CMS 3 | ||
|
||
Pushes Craft CMS errors to [Sentry](https://sentry.io/). | ||
Does not log exceptions in devMode. | ||
|
||
## Installation | ||
|
||
### Plugin Store | ||
1. Search for 'Sentry SDK'. | ||
2. Hit install | ||
3. Create a config file as explained below. | ||
|
||
### Composer | ||
1. Run: `composer require born05/craft-sentry` | ||
2. Hit install in Admin > Settings > Plugins | ||
3. Create a config file as explained below. | ||
|
||
## Requirements | ||
- Craft 3.1 or later | ||
- PHP 7.1 at least | ||
|
||
## Configuring Sentry | ||
Create a `config/sentry-sdk.php` config file with the following contents: | ||
|
||
```php | ||
<?php | ||
|
||
return [ | ||
'enabled' => true, | ||
'anonymous' => false, // Determines to log user info or not | ||
'clientDsn' => getenv('SENTRY_DSN') ?: 'https://example@sentry.io/123456789', // Set as string or use environment variable. | ||
'excludedCodes' => ['400', '404', '429'], | ||
'release' => getenv('SENTRY_RELEASE') ?: null, // Release number/name used by sentry. | ||
]; | ||
``` | ||
|
||
## Credits | ||
Based upon the sentry plugin by [Luke Youell](https://github.com/lukeyouell). | ||
|
||
## License | ||
Copyright © 2019 [Born05](https://www.born05.com/) | ||
|
||
See [license](https://github.com/born05/craft-sentry/blob/master/LICENSE.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
{ | ||
"name": "born05/craft-sentry", | ||
"description": "Pushes Craft CMS errors to Sentry.", | ||
"type": "craft-plugin", | ||
"version": "1.0.0-beta.1", | ||
"keywords": [ | ||
"craft", | ||
"cms", | ||
"craftcms", | ||
"craft-plugin", | ||
"sentry", | ||
"sentry sdk" | ||
], | ||
"support": { | ||
"docs": "https://github.com/born05/craft-sentry/blob/master/README.md", | ||
"issues": "https://github.com/born05/craft-sentry/issues" | ||
}, | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Born05", | ||
"homepage": "https://www.born05.com/" | ||
} | ||
], | ||
"require": { | ||
"craftcms/cms": "^3.1", | ||
"sentry/sdk": "^2.0.4" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"born05\\sentry\\": "src/" | ||
} | ||
}, | ||
"extra": { | ||
"name": "Sentry SDK", | ||
"handle": "sentry-sdk", | ||
"hasCpSettings": false, | ||
"hasCpSection": false, | ||
"changelogUrl": "https://raw.githubusercontent.com/born05/craft-sentry/master/CHANGELOG.md" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
namespace born05\sentry; | ||
|
||
use born05\sentry\models\Settings; | ||
use born05\sentry\services\SentryService; | ||
|
||
use Craft; | ||
use craft\base\Plugin as CraftPlugin; | ||
use craft\events\ExceptionEvent; | ||
use craft\web\ErrorHandler; | ||
|
||
use yii\base\Event; | ||
|
||
class Plugin extends CraftPlugin | ||
{ | ||
/** | ||
* Static property that is an instance of this plugin class so that it can be accessed via | ||
* Plugin::$plugin | ||
* | ||
* @var Plugin | ||
*/ | ||
public static $plugin; | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
public function init() | ||
{ | ||
parent::init(); | ||
self::$plugin = $this; | ||
|
||
$this->setComponents([ | ||
'sentry' => SentryService::class, | ||
]); | ||
|
||
Event::on( | ||
ErrorHandler::className(), | ||
ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, | ||
function(ExceptionEvent $event) { | ||
$this->sentry->handleException($event->exception); | ||
} | ||
); | ||
} | ||
|
||
/** | ||
* @inheritdoc | ||
*/ | ||
protected function createSettingsModel() | ||
{ | ||
return new Settings(); | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
|
||
namespace born05\sentry\models; | ||
|
||
use craft\base\Model; | ||
|
||
class Settings extends Model | ||
{ | ||
public $enabled = true; | ||
public $anonymous = false; // Determines to log user info or not | ||
public $clientDsn; | ||
public $excludedCodes = ['404']; | ||
public $release; // Release number/name used by sentry. | ||
|
||
public function rules() | ||
{ | ||
return [ | ||
[['enabled', 'anonymous'], 'boolean'], | ||
[['clientDsn', 'excludedCodes', 'release'], 'string'], | ||
[['clientDsn'], 'required'], | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
<?php | ||
|
||
namespace born05\sentry\services; | ||
|
||
use born05\sentry\Plugin as SentryPlugin; | ||
|
||
use Craft; | ||
use craft\base\Component; | ||
|
||
use Sentry; | ||
use Sentry\State\Scope; | ||
|
||
use yii\base\Exception; | ||
|
||
class SentryService extends Component | ||
{ | ||
public function handleException($exception) | ||
{ | ||
$app = Craft::$app; | ||
$info = $app->getInfo(); | ||
$plugin = SentryPlugin::$plugin; | ||
$settings = $plugin->getSettings(); | ||
|
||
if (!$settings->enabled || $app->getConfig()->getGeneral()->devMode) return; | ||
|
||
if (!$settings->clientDsn) { | ||
Craft::error('Failed to report exception due to missing client key (DSN)', $plugin->handle); | ||
return; | ||
} | ||
|
||
// If this is a Twig Runtime exception, use the previous one instead | ||
if ($exception instanceof \Twig_Error_Runtime && ($previousException = $exception->getPrevious()) !== null) { | ||
$exception = $previousException; | ||
} | ||
|
||
$statusCode = $exception->statusCode ?? null; | ||
|
||
if (in_array($statusCode, $settings->excludedCodes)) { | ||
Craft::info('Exception status code excluded from being reported to Sentry.', $plugin->handle); | ||
return; | ||
} | ||
|
||
Sentry\init([ | ||
'dsn' => $settings->clientDsn, | ||
'environment' => CRAFT_ENVIRONMENT, | ||
'release' => $settings->release, | ||
]); | ||
|
||
$user = $app->getUser()->getIdentity(); | ||
|
||
Sentry\configureScope(function (Scope $scope) use ($app, $info, $plugin, $settings, $user, $statusCode) { | ||
if ($user && !$settings->anonymous) { | ||
$scope->setUser([ | ||
'ID' => $user->id, | ||
'Username' => $user->username, | ||
'Email' => $user->email, | ||
'Admin' => $user->admin ? 'Yes' : 'No', | ||
]); | ||
} | ||
|
||
$scope->setExtra('App Type', 'Craft CMS'); | ||
$scope->setExtra('App Name', $info->name); | ||
$scope->setExtra('App Edition (licensed)', $app->getLicensedEditionName()); | ||
$scope->setExtra('App Edition (running)', $app->getEditionName()); | ||
$scope->setExtra('App Version', $info->version); | ||
$scope->setExtra('App Version (schema)', $info->schemaVersion); | ||
$scope->setExtra('PHP Version', phpversion()); | ||
$scope->setExtra('Status Code', $statusCode); | ||
}); | ||
|
||
Sentry\captureException($exception); | ||
} | ||
} |