LoginSignup
10
2

More than 3 years have passed since last update.

Sentryの情報にアプリケーションのバージョン情報を追加する方法

Posted at

前提

Sentryを使用してエラー情報を検知しているアプリケーションであること。

使用言語、フレームワーク

  • Java 8
  • Spring Boot(Maven)

概要

Sentryに送信されるエラー情報がいつのバージョンのアプリケーションで発生したエラーのものであるか確認できるようにしたい。

メリット

  • 発生したエラーのアプリケーションのバージョンの特定が一目でわかる。
  • Sentryの画面で特定のバージョンで発生したエラーの一覧を以下のように検索することができるようになる。
スクリーンショット 2020-10-15 11.33.52.png

前提条件

  • アプリケーションのバージョンが定義されていること。
  • リリースの度にバージョンが更新されていること。

以下の定義ファイルの <version> 内をSentryに送信します。
今回は、バージョン情報をSentryにタグとして送信します。

定義ファイル

pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmls="~">
     :
    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>100.0.100</version>
     :
</project>

修正手順

以下の順番で修正を行います。
1. 依存関係の追加
2. エンドポイントの追加
3. エンドポイントに表示する情報の追加
4. Sentryへの送信処理の追加

1. 依存関係の追加

pom.xml
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

2. エンドポイントの追加

application.yml
management:
  endpoints:
    web:
      exposure:
        include: "info"
      base-path: "/"

base-pathは環境に応じて変更してください。

3. エンドポイントに表示する情報の追加

application.yml
info:
  application:
    version: @project.version@

ここまで設定すると http://localhost:8080/info にアクセスすることで以下のようにここまでの手順で設定したバージョン情報を確認することができます。

{"application":{"version":"100.0.100"}}

4. Sentryへの送信処理の追加

設定ファイル:@SpringBootApplicationアノテーションを設定しているクラスに以下の処理を追加します。
※ すでにSentryへの送信処理を実装している場合は、version 関連の処理を追加してください。
 (引数の追加とtagの送信部分です。)
※ 戻り値の型は


  @Bean
  public HandlerExceptionResolver sentryExceptionResolver(
    @Value("${sentry.url:#{null}}") Optional<String> sentryUrl,
    @Value("${info.application.version:#{null}}") Optional<String> appVersion) {

    if (sentryUrl.isPresent()) {
      try {
        SentryClient sentryClient = Sentry.init(sentryUrl.get());
        // ここで、タグとしてバージョン情報を追加しています。
        appVersion.ifPresent(s -> sentryClient.addTag("version", s));
      } catch (InvalidDsnException e) {
        LOGGER.warn(e.getMessage(), e);
      }
    }
    return new SentryExceptionResolverImpl();
  }

sentry.urlinfo.application.version と同様に application.yml に定義しています。

設定後

上記設定を完了後、アプリケーション内でエラーが発生した場合、以下のような versionのタグが表示されるようになります。
versionの数値部分をクリックすることで、冒頭でメリットとして挙げた検索結果を確認できます。

スクリーンショット 2020-10-15 11.27.03.png
10
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
2