2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【SpringBootActuator】prometheusのエンドポイントがテストで404になってしまう

Posted at

やりたいこと

GET /prometheusを叩いてHttpStatus200を確認するテストを実装したい。
Spring Bootのバージョンアップ等でうっかり消えてしまっていたというミスを無くすため。

問題

実際にアプリケーションを起動すると叩けるのに、テストだと404になってしまう。。。
GET /info, GET /healthは200なのに:astonished:

問題のコード

テストフレームワークはSpockを使用しています。

application.yml
management:
  endpoints:
    web:
      base-path: /
      exposure:
        include: info, health, prometheus
PrometheusSpec.groovy
@SpringBootTest
@AutoConfigureMockMvc
class PrometheusSpec extends Specification {
    @Autowired
    MockMvc mockMvc

    def "GET /prometheus"() {
        when:
        ResultActions actual = mockMvc.perform(MockMvcRequestBuilders
                .get("/prometheus"))
        then:
        actual.andReturn().getResponse().getStatus() == HttpStatus.OK.value()
    }
}

実行結果

actual.andReturn().getResponse().getStatus() == HttpStatus.OK.value()
|      |           |             |           |  |          |  |
|      |           |             404         |  |          |  200

解決方法

@AutoConfigureMetrics をclassに付与することで解決。

クラスパスに関係なく、@SpringBootTest を使用する場合、メモリ内でバックアップされているものを除いて、メーターレジストリは自動構成されません。

統合テストの一環としてメトリックを別のバックエンドにエクスポートする必要がある場合は、@AutoConfigureMetrics でアノテーションを付けます。

あとがき

ドキュメント読むの大事。

2
0
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?