Skip to content

Commit 088f392

Browse files
authored
feat: allow setting custom release version, standardize defaults (#38)
1 parent 604a853 commit 088f392

File tree

5 files changed

+34
-11
lines changed

5 files changed

+34
-11
lines changed

extend.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,13 @@
6262
}),
6363

6464
(new Flarum\Settings())
65-
->default('fof-sentry.monitor_performance', 0),
65+
->default('fof-sentry.monitor_performance', 0)
66+
->default('fof-sentry.send_emails_with_sentry_reports', false)
67+
->default('fof-sentry.user_feedback', false)
68+
->default('fof-sentry.javascript.console', false)
69+
->default('fof-sentry.javascript.trace_sample_rate', 0)
70+
->default('fof-sentry.javascript.replays_session_sample_rate', 0)
71+
->default('fof-sentry.javascript.replays_error_sample_rate', 0)
72+
->default('fof-sentry.profile_rate', 0)
73+
->default('fof-sentry.javascript', true),
6674
];

src/Content/SentryJavaScript.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ public function __invoke(Document $document)
4848

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

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

5555
$tracesSampleRate = max(0, min(100, $tracesSampleRate)) / 100;
5656
$replaysSessionSampleRate = max(0, min(100, $replaysSessionSampleRate)) / 100;

src/Formatters/SentryFormatter.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Flarum\Foundation\ErrorHandling\HandledError;
1515
use Flarum\Foundation\ErrorHandling\HttpFormatter;
1616
use Flarum\Foundation\ErrorHandling\ViewFormatter;
17+
use Flarum\Http\RequestUtil;
1718
use Flarum\Settings\SettingsRepositoryInterface;
1819
use Illuminate\Contracts\View\Factory as ViewFactory;
1920
use Psr\Http\Message\ResponseInterface as Response;
@@ -58,7 +59,7 @@ public function format(HandledError $error, Request $request): Response
5859
}
5960

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

7475
$body->write("
75-
<script src=\"https://browser.sentry-cdn.com/5.25.0/bundle.min.js\" integrity=\"sha384-2p7fXoWSRPG49ZgmmJlTEI/01BY1LgxCNFQFiWpImAERmS/bROOQm+cJMdq/kmWS\" crossorigin=\"anonymous\"></script>
76+
<script src=\"https://browser.sentry-cdn.com/7.91.0/bundle.min.js\" integrity=\"sha384-2p7fXoWSRPG49ZgmmJlTEI/01BY1LgxCNFQFiWpImAERmS/bROOQm+cJMdq/kmWS\" crossorigin=\"anonymous\"></script>
7677
7778
<script>
7879
Sentry.init({ dsn: '$dsn' });

src/Reporters/SentryReporter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public function report(Throwable $error)
5050
$data = $user->only('id', 'username');
5151

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

src/SentryServiceProvider.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,21 @@ class SentryServiceProvider extends AbstractServiceProvider
4646

4747
public function register()
4848
{
49+
$this->container->singleton('sentry.release', function () {
50+
return Application::VERSION;
51+
});
52+
4953
$this->container->singleton(HubInterface::class, function ($container) {
5054
/** @var SettingsRepositoryInterface $settings */
5155
$settings = $container->make(SettingsRepositoryInterface::class);
5256
/** @var UrlGenerator $url */
5357
$url = $container->make(UrlGenerator::class);
5458
$dsn = $settings->get('fof-sentry.dsn_backend');
59+
/** @var string $release */
60+
$release = $container->make('sentry.release');
5561
$environment = empty($settings->get('fof-sentry.environment')) ? str_replace(['https://', 'http://'], '', $url->to('forum')->base()) : $settings->get('fof-sentry.environment');
5662
$performanceMonitoring = (int) $settings->get('fof-sentry.monitor_performance');
57-
$profilesSampleRate = (int) $settings->get('fof-sentry.profile_rate', 0);
63+
$profilesSampleRate = (int) $settings->get('fof-sentry.profile_rate');
5864

5965
if (empty($dsn)) {
6066
$dsn = $settings->get('fof-sentry.dsn');
@@ -72,7 +78,7 @@ public function register()
7278
'traces_sample_rate' => $tracesSampleRate,
7379
'profiles_sample_rate' => $profilesSampleRate,
7480
'environment' => $environment,
75-
'release' => Application::VERSION,
81+
'release' => $release,
7682
]);
7783

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

96102
$hub->configureScope(function (Scope $scope) use ($config) {
97-
$scope->setTag('offline', Arr::get($config, 'offline', false));
98-
$scope->setTag('debug', Arr::get($config, 'debug', true));
103+
$scope->setTag('offline', $this->booleanToString(Arr::get($config, 'offline', false)));
104+
$scope->setTag('debug', $this->booleanToString(Arr::get($config, 'debug', true)));
99105
$scope->setTag('flarum', Application::VERSION);
100106

101107
if ($this->container->bound('sentry.stack')) {
@@ -209,4 +215,12 @@ public function __destruct()
209215
$transaction->finish();
210216
}
211217
}
218+
219+
/**
220+
* A simple helper to convert a boolean to a string.
221+
*/
222+
public function booleanToString(bool $value): string
223+
{
224+
return $value ? 'true' : 'false';
225+
}
212226
}

0 commit comments

Comments
 (0)