Skip to content

Commit

Permalink
Cleanup service provider
Browse files Browse the repository at this point in the history
  • Loading branch information
imorland committed Jun 28, 2021
1 parent e8a2ba4 commit 9eda1c4
Showing 1 changed file with 45 additions and 48 deletions.
93 changes: 45 additions & 48 deletions src/SentryServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,52 +42,45 @@ class SentryServiceProvider extends AbstractServiceProvider

protected static $transactionStack = [];

public function boot()
public function register()
{
error_reporting(-1);
$this->container->singleton(HubInterface::class, function ($container) {
/** @var SettingsRepositoryInterface $settings */
$settings = $container->make(SettingsRepositoryInterface::class);
$dsn = $settings->get('fof-sentry.dsn');
$performanceMonitoring = (int) $settings->get('fof-sentry.monitor_performance', 0);

set_error_handler([$this, 'handleError']);
/** @var Paths $paths */
$paths = $container->make(Paths::class);

ini_set('display_errors', 'Off');
}
$tracesSampleRate = $performanceMonitoring > 0 ? round($performanceMonitoring / 100, 2) : 0;

public function register()
{
/** @var SettingsRepositoryInterface $settings */
$settings = resolve(SettingsRepositoryInterface::class);
$dsn = $settings->get('fof-sentry.dsn');
$performanceMonitoring = (int) $settings->get('fof-sentry.monitor_performance', 0);
/** @var Config $config */
$config = $container->make(Config::class);

$this->container->singleton(HubInterface::class, function () use ($dsn, $performanceMonitoring) {
$this->init($dsn, $performanceMonitoring);
init([
'dsn' => $dsn,
'in_app_include' => [$paths->base],
'traces_sample_rate' => $tracesSampleRate,
'environment' => str_replace(['https://', 'http://'], '', Arr::get($config, 'url')),
'release' => Application::VERSION,
]);

return SentrySdk::getCurrentHub();
});

if ($dsn && $performanceMonitoring > 0) {
/** @var HubInterface $hub */
$hub = $this->container->make(HubInterface::class);

$transaction = $hub->startTransaction(new TransactionContext('flarum'));

foreach ($this->measurements as $measurement) {
/** @var Measure $measure */
$measure = new $measurement($transaction, $this->container);
if ($span = $measure->handle()) {
static::$transactionStack[] = $span;
}
}

static::$transactionStack[] = $transaction;
}

$this->container->singleton('sentry', function () use ($dsn) {
$this->container->singleton('sentry', function ($container) {
/** @var SettingsRepositoryInterface $settings */
$settings = $container->make(SettingsRepositoryInterface::class);

$dsn = $settings->get('fof-sentry.dsn');
if (!$dsn) {
return null;
}

/** @var array $config */
$config = $this->container->make('flarum.config');
/** @var Config $config */
$config = $container->make('flarum.config');

/** @var HubInterface $hub */
$hub = $this->container->make(HubInterface::class);
Expand All @@ -105,8 +98,6 @@ public function register()
return $hub;
});

$this->container->alias('sentry', HubInterface::class);

$this->container->extend(
ViewFormatter::class,
function (ViewFormatter $formatter) {
Expand Down Expand Up @@ -135,26 +126,32 @@ function (Assets $assets) {
);
}

protected function init(string $dsn, int $performanceMonitoring)
public function boot(SettingsRepositoryInterface $settings)
{
/** @var Paths $paths */
$paths = $this->container->make(Paths::class);
set_error_handler([$this, 'handleError']);

$dsn = $settings->get('fof-sentry.dsn');
$performanceMonitoring = (int) $settings->get('fof-sentry.monitor_performance', 0);

if ($dsn && $performanceMonitoring > 0) {
/** @var HubInterface $hub */
$hub = $this->container->make(HubInterface::class);

$tracesSampleRate = $performanceMonitoring > 0 ? round($performanceMonitoring / 100, 2) : 0;
$transaction = $hub->startTransaction(new TransactionContext('flarum'));

/** @var Config $config */
$config = $this->container->make(Config::class);
foreach ($this->measurements as $measurement) {
/** @var Measure $measure */
$measure = new $measurement($transaction, $this->container);
if ($span = $measure->handle()) {
static::$transactionStack[] = $span;
}
}

init([
'dsn' => $dsn,
'in_app_include' => [$paths->base],
'traces_sample_rate' => $tracesSampleRate,
'environment' => str_replace(['https://', 'http://'], '', Arr::get($config, 'url')),
'release' => Application::VERSION,
]);
static::$transactionStack[] = $transaction;
}
}

public function handleError($level, $message, $file = '', $line = 0)
protected function handleError($level, $message, $file = '', $line = 0)
{
// ignore STMT_PREPARE errors because Eloquent automatically tries reconnecting
if (strpos($message, 'STMT_PREPARE packet') !== false) {
Expand Down

0 comments on commit 9eda1c4

Please sign in to comment.