Skip to content

Commit

Permalink
Added craft 5 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Roel van Hintum committed Mar 29, 2024
1 parent e1d56e7 commit 2564569
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 41 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Craft Sentry Changelog

## 3.0.0-beta.1 - 2024-03-29
### Changed
- Added craft 5 support
- Moved to Sentry PHP SDK 4.x
- Moved to latest Sentry Browser SDK
- Client settings are removed in favor of Sentry's "Loader Script" settings page.
- Removed `performanceMonitoring` and `autoSessionTracking` settings.

## 2.0.1 - 2022-09-16
### Changed
- If the exception is a Twig Runtime exception, use the previous one instead. Thanks to @kringkaste
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
],
"require": {
"php": "^8.0.2",
"craftcms/cms": "^4.0.0-alpha",
"sentry/sdk": "^3.1.1"
"craftcms/cms": "^4.0.0-alpha|^5.0.0-beta.1",
"sentry/sdk": "^4.0.0"
},
"autoload": {
"psr-4": {
Expand Down
67 changes: 32 additions & 35 deletions src/Plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
use craft\events\TemplateEvent;
use craft\web\ErrorHandler;
use craft\web\View;

use Exception;
use Sentry;
use Sentry\State\Scope;

Expand Down Expand Up @@ -114,40 +114,37 @@ public function init()
* Init Sentry JS SDK (Front end)
*/
if (Craft::$app->request->isSiteRequest && $settings->reportJsErrors) {
Event::on(
View::class,
View::EVENT_BEFORE_RENDER_TEMPLATE,
function (TemplateEvent $event) {
$settings = $this->getSettings();
$view = Craft::$app->getView();

$view->registerScript(
"",
View::POS_END,
array_merge([
'src' => 'https://browser.sentry-cdn.com/6.3.5/bundle.tracing.min.js',
'crossorigin' => 'anonymous',
'integrity' => 'sha384-0RpBr4PNjUAqckh8BtmPUuFGNC082TAztkL1VE2ttmtsYJBUvqcZbThnfE5On6h1',
], $this->getScriptOptions())
);

// Returns devMode boolean as a string so it can be passed to the debug parameter properly.
$isDevMode = Craft::$app->config->general->devMode ? 'true' : 'false';
$autoSessionTracking = $settings->autoSessionTracking ? 'true' : 'false';
$performanceMonitoring = $settings->performanceMonitoring ? 'integrations: [new Sentry.Integrations.BrowserTracing()],' : '';

$view->registerScript("
Sentry.init({
dsn: '$settings->clientDsn',
release: '$settings->release',
environment: '".App::env('CRAFT_ENVIRONMENT')."',
debug: $isDevMode,
$performanceMonitoring
tracesSampleRate: $settings->sampleRate,
autoSessionTracking: $autoSessionTracking
});", View::POS_END, $this->getScriptOptions());
}
);
if (!isset($settings->clientKey)) {
throw new Exception('Failed to register Sentry browser client due to missing clientKey');
return;
} else {
Event::on(
View::class,
View::EVENT_BEFORE_RENDER_TEMPLATE,
function (TemplateEvent $event) {
$settings = $this->getSettings();
$view = Craft::$app->getView();

$view->registerScript("
// Configure sentryOnLoad before adding the Loader Script
window.sentryOnLoad = function () {
Sentry.init({
release: '$settings->release',
environment: '".App::env('CRAFT_ENVIRONMENT')."'
});
};", View::POS_END, $this->getScriptOptions());

$view->registerScript(
"",
View::POS_END,
array_merge([
'src' => "https://js.sentry-cdn.com/$settings->clientKey.min.js",
'crossorigin' => 'anonymous',
], $this->getScriptOptions())
);
}
);
}
}

/**
Expand Down
7 changes: 3 additions & 4 deletions src/models/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ class Settings extends Model
public $enabled = true;
public $anonymous = false; // Determines to log user info or not
public $clientDsn;
public $clientKey;
public $excludedCodes = ['404'];
public $release; // Release number/name used by sentry.
public $reportJsErrors = false; // Client only option
public $sampleRate = 1.0; // Client only option
public $performanceMonitoring = true; // Client only option
public $autoSessionTracking = false; // Client only option

/**
* @inheritdoc
*/
public function defineRules(): array
{
return [
[['enabled', 'anonymous', 'reportJsErrors', 'performanceMonitoring', 'autoSessionTracking'], 'boolean'],
[['clientDsn', 'excludedCodes', 'release'], 'string'],
[['enabled', 'anonymous', 'reportJsErrors'], 'boolean'],
[['clientDsn', 'clientKey', 'excludedCodes', 'release'], 'string'],
[['clientDsn'], 'required'],
[['sampleRate'], 'number', 'min' => 0, 'max' => 1],
];
Expand Down

0 comments on commit 2564569

Please sign in to comment.