Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion extend.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,5 +62,13 @@
}),

(new Flarum\Settings())
->default('fof-sentry.monitor_performance', 0),
->default('fof-sentry.monitor_performance', 0)
->default('fof-sentry.send_emails_with_sentry_reports', false)
->default('fof-sentry.user_feedback', false)
->default('fof-sentry.javascript.console', false)
->default('fof-sentry.javascript.trace_sample_rate', 0)
->default('fof-sentry.javascript.replays_session_sample_rate', 0)
->default('fof-sentry.javascript.replays_error_sample_rate', 0)
->default('fof-sentry.profile_rate', 0)
->default('fof-sentry.javascript', true),
];
6 changes: 3 additions & 3 deletions src/Content/SentryJavaScript.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public function __invoke(Document $document)

$shouldScrubEmailsFromUserData = !((bool) (int) $this->settings->get('fof-sentry.send_emails_with_sentry_reports'));

$tracesSampleRate = (int) $this->settings->get('fof-sentry.javascript.trace_sample_rate', 0);
$replaysSessionSampleRate = (int) $this->settings->get('fof-sentry.javascript.replays_session_sample_rate', 0);
$replaysErrorSampleRate = (int) $this->settings->get('fof-sentry.javascript.replays_error_sample_rate', 0);
$tracesSampleRate = (int) $this->settings->get('fof-sentry.javascript.trace_sample_rate');
$replaysSessionSampleRate = (int) $this->settings->get('fof-sentry.javascript.replays_session_sample_rate');
$replaysErrorSampleRate = (int) $this->settings->get('fof-sentry.javascript.replays_error_sample_rate');

$tracesSampleRate = max(0, min(100, $tracesSampleRate)) / 100;
$replaysSessionSampleRate = max(0, min(100, $replaysSessionSampleRate)) / 100;
Expand Down
5 changes: 3 additions & 2 deletions src/Formatters/SentryFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Flarum\Foundation\ErrorHandling\HandledError;
use Flarum\Foundation\ErrorHandling\HttpFormatter;
use Flarum\Foundation\ErrorHandling\ViewFormatter;
use Flarum\Http\RequestUtil;
use Flarum\Settings\SettingsRepositoryInterface;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Psr\Http\Message\ResponseInterface as Response;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function format(HandledError $error, Request $request): Response
}

$dsn = $settings->get('fof-sentry.dsn');
$user = resolve('sentry.request')->getAttribute('actor');
$user = RequestUtil::getActor(resolve('sentry.request'));
$locale = $this->translator->getLocale();
$eventId = $sentry->getLastEventId();
$userData = ($user != null && $user->id != 0) ?
Expand All @@ -72,7 +73,7 @@ public function format(HandledError $error, Request $request): Response
$body->seek($body->getSize());

$body->write("
<script src=\"https://browser.sentry-cdn.com/5.25.0/bundle.min.js\" integrity=\"sha384-2p7fXoWSRPG49ZgmmJlTEI/01BY1LgxCNFQFiWpImAERmS/bROOQm+cJMdq/kmWS\" crossorigin=\"anonymous\"></script>
<script src=\"https://browser.sentry-cdn.com/7.91.0/bundle.min.js\" integrity=\"sha384-2p7fXoWSRPG49ZgmmJlTEI/01BY1LgxCNFQFiWpImAERmS/bROOQm+cJMdq/kmWS\" crossorigin=\"anonymous\"></script>

<script>
Sentry.init({ dsn: '$dsn' });
Expand Down
2 changes: 1 addition & 1 deletion src/Reporters/SentryReporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function report(Throwable $error)
$data = $user->only('id', 'username');

// Only send email if enabled in settings
if ((int) @resolve('flarum.settings')->get('fof-sentry.send_emails_with_sentry_reports')) {
if ((bool) resolve('flarum.settings')->get('fof-sentry.send_emails_with_sentry_reports')) {
$data['email'] = $user->email;
}

Expand Down
22 changes: 18 additions & 4 deletions src/SentryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,21 @@ class SentryServiceProvider extends AbstractServiceProvider

public function register()
{
$this->container->singleton('sentry.release', function () {
return Application::VERSION;
});

$this->container->singleton(HubInterface::class, function ($container) {
/** @var SettingsRepositoryInterface $settings */
$settings = $container->make(SettingsRepositoryInterface::class);
/** @var UrlGenerator $url */
$url = $container->make(UrlGenerator::class);
$dsn = $settings->get('fof-sentry.dsn_backend');
/** @var string $release */
$release = $container->make('sentry.release');
$environment = empty($settings->get('fof-sentry.environment')) ? str_replace(['https://', 'http://'], '', $url->to('forum')->base()) : $settings->get('fof-sentry.environment');
$performanceMonitoring = (int) $settings->get('fof-sentry.monitor_performance');
$profilesSampleRate = (int) $settings->get('fof-sentry.profile_rate', 0);
$profilesSampleRate = (int) $settings->get('fof-sentry.profile_rate');

if (empty($dsn)) {
$dsn = $settings->get('fof-sentry.dsn');
Expand All @@ -72,7 +78,7 @@ public function register()
'traces_sample_rate' => $tracesSampleRate,
'profiles_sample_rate' => $profilesSampleRate,
'environment' => $environment,
'release' => Application::VERSION,
'release' => $release,
]);

return SentrySdk::getCurrentHub();
Expand All @@ -94,8 +100,8 @@ public function register()
$hub = $this->container->make(HubInterface::class);

$hub->configureScope(function (Scope $scope) use ($config) {
$scope->setTag('offline', Arr::get($config, 'offline', false));
$scope->setTag('debug', Arr::get($config, 'debug', true));
$scope->setTag('offline', $this->booleanToString(Arr::get($config, 'offline', false)));
$scope->setTag('debug', $this->booleanToString(Arr::get($config, 'debug', true)));
$scope->setTag('flarum', Application::VERSION);

if ($this->container->bound('sentry.stack')) {
Expand Down Expand Up @@ -209,4 +215,12 @@ public function __destruct()
$transaction->finish();
}
}

/**
* A simple helper to convert a boolean to a string.
*/
public function booleanToString(bool $value): string
{
return $value ? 'true' : 'false';
}
}
Loading