react.jsへSentryを適用しようと思い、公式ドキュメントを見てみると対応言語の中にSpringBootがあったため、試してみました。
バージョン
- Spring Boot 2.4.1
- Sentry
- sentry-spring-boot-starter 4.3.0
Sentryへプロジェクト追加及び設定
Sentryへログイン後、プロジェクト作成。
build.gradle.ktsに依存関係を追加します。
- build.gradle.kts
implementation("io.sentry:sentry-spring-boot-starter:4.3.0")
- build.gradle.kts
application.properties에 DSN (Data Source Name)を設定します。
- application.properties
# DSN設定 sentry.dsn=https://xxxxxxxxxxxxxxxxxxxxxxxxxxx@xxxxxx.ingest.sentry.io/xxxxx # エラートレース設定 sentry.enable-tracing=true
- application-development.properties
# 各環境の設定も可能です。 sentry.environment=development
- application.properties
Sentryにエラーを送るための設定
- サンプルプロジェクトがRestApiプロジェクトなので、
@RestControllerAdvice
を利用して共通エラー処理を追加しました。@RestControllerAdvice class SampleControllerAdvice { @ResponseStatus(HttpStatus.BAD_REQUEST) @ExceptionHandler(MissingPathVariableException::class) fun handleMissingPathVariable(ex: MissingPathVariableException): Map<String, String> { Sentry.captureException(ex) val error: Map<String, String> = mapOf("code" to "E0001", "message" to "Parameter error") return error } @ResponseStatus(HttpStatus.UNSUPPORTED_MEDIA_TYPE) @ExceptionHandler(HttpMediaTypeNotSupportedException::class) fun handleHttpMediaTypeNotSupported(ex: HttpMediaTypeNotSupportedException): Map<String, String> { Sentry.captureException(ex) val error: Map<String, String> = mapOf("code" to "E0002", "message" to "Unsupported Media Type") return error } @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) @ExceptionHandler(Exception::class) fun handleExceptions(ex: Exception): Map<String, String> { Sentry.captureException(ex) val error: Map<String, String> = mapOf("code" to "E0003", "message" to "Internal Server Error") return error } }
- サンプルプロジェクトがRestApiプロジェクトなので、
エラーを発生させ、Sentry側を確認
- Content typeを追加し、エラーを発生させてみました。
- Search by Traceをクリックするとエラートレースも可能です。
- Dashboardも提供しています。カスタムDashboardはBusinessプランから使えるそうです。
slackへ通知
- slackとか他のサービスと連携するためには、
Team
プランにする必要があります。slackへのメッセージ通知のみならsentryのWebHooks
を利用すればDeveloper(無料)
ブランでも利用可能です。
Businessプランの場合、Amazon SQSへデータ転送もできると書いてあるので、便利そうです。 後で 公式サイド をよく見てみます。