sentry-php-sdk は何をしますか?
PHP向けSentry SDKの完全セットアップを提供します。「PHPにSentryを追加」「sentry/sentryをインストール」「PHPでSentryをセットアップ」などの依頼や、PHPアプリケーション向けのエラーモニタリング、トレーシング、プロファイリング、ロギング、メトリクス、cronsの設定に使用します。プレーンなPHP、Laravel、Symfonyに対応。
インストール
composer require sentry/sentry
基本設定 (プレーンなPHP)
\Sentry\init(['dsn' => 'https://[email protected]/0']);
Laravelでのセットアップ
サービスプロバイダーの登録 (config/app.php)
'providers' => [
// その他のサービスプロバイダー...
\Sentry\Laravel\ServiceProvider::class,
\Sentry\Laravel\Tracing\ServiceProvider::class,
],
'aliases' => [
'Sentry' => \Sentry\Laravel\Facade::class,
],
例外処理 (app/Exceptions/Handler.php)
public function register(): void
{
$this->reportable(function (\Throwable $e) {
if (app()->bound('sentry')) {
app('sentry')->captureException($e);
}
});
}
Symfonyでのセットアップ
bundles.php
return [
// その他のバンドル...
\Sentry\SentryBundle\SentryBundle::class => ['all' => true],
];
環境変数 (.env)
SENTRY_DSN=https://[email protected]/0
SENTRY_ENVIRONMENT=production
パフォーマンストレーシング
トレーシングを有効にするには、Sentryにトレースサンプルレートを設定します。
\Sentry\init([
'dsn' => 'https://[email protected]/0',
'traces_sample_rate' => 1.0,
]);
LaravelまたはSymfonyを使用している場合、追加の設定は不要です。メソッド呼び出しは自動的にトランザクションとスパンにトレースされます。
プロファイリング
プロファイリングはトレーシングと連動します。設定例:
\Sentry\init([
'dsn' => 'https://[email protected]/0',
'traces_sample_rate' => 1.0,
'profiles_sample_rate' => 1.0,
]);
ロギング
\Sentry\captureMessage('Something went wrong');
メトリクス
\Sentry\metrics\Metrics::increment('my_metric', 1);
crons (定期的なジョブの監視)
use function Sentry\crons\monitor;
monitor('check-in-slug', function () {
// ここに定期的に実行するロジックを記述
});