|
| 1 | +package io.sentry.spring.boot.jakarta.log4j2integrationtest |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.sentry.Sentry |
| 5 | +import io.sentry.test.MockSentryServer |
| 6 | +import io.sentry.test.event |
| 7 | +import io.sentry.test.hasFlag |
| 8 | +import kotlin.test.Test |
| 9 | +import org.junit.runner.RunWith |
| 10 | +import org.springframework.boot.test.context.SpringBootTest |
| 11 | +import org.springframework.boot.test.web.client.TestRestTemplate |
| 12 | +import org.springframework.boot.test.web.server.LocalServerPort |
| 13 | +import org.springframework.boot.web.client.RestTemplateBuilder |
| 14 | +import org.springframework.test.context.DynamicPropertyRegistry |
| 15 | +import org.springframework.test.context.DynamicPropertySource |
| 16 | +import org.springframework.test.context.junit4.SpringRunner |
| 17 | + |
| 18 | +/** |
| 19 | + * Runs [IntegrationTestApplication] with Log4j2 instead of Logback, so Sentry's auto-configuration |
| 20 | + * attaches its Log4j2 appender, and asserts what reaches [server]. |
| 21 | + */ |
| 22 | +@RunWith(SpringRunner::class) |
| 23 | +@SpringBootTest( |
| 24 | + classes = [IntegrationTestApplication::class], |
| 25 | + webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, |
| 26 | + // Sentry only attaches its Log4j2 appender when asked to. |
| 27 | + properties = ["sentry.traces-sample-rate=1.0", "sentry.logging.enabled=true"], |
| 28 | +) |
| 29 | +class Log4j2IntegrationTest { |
| 30 | + @LocalServerPort private var port = 0 |
| 31 | + |
| 32 | + // The context's TestRestTemplate comes from the RestTemplateBuilder that Sentry customizes, so |
| 33 | + // it would add trace headers of its own to the requests tests send. Tests use a plain one. |
| 34 | + private val restTemplate: TestRestTemplate by lazy { |
| 35 | + TestRestTemplate(RestTemplateBuilder().rootUri("http://localhost:$port")) |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + fun `error logged through Log4j2 reaches Sentry with the request's flag`() { |
| 40 | + val response = restTemplate.getForEntity("/person/1", String::class.java) |
| 41 | + assertThat(response.statusCode.value()).isEqualTo(500) |
| 42 | + Sentry.flush(5000) |
| 43 | + |
| 44 | + val logged = |
| 45 | + server.awaitEnvelope { it.event()?.message?.formatted == "Trying person with id=1" }.event()!! |
| 46 | + assertThat(logged.hasFlag("my-feature-flag", true)).isTrue() |
| 47 | + assertThat(logged.sdk!!.integrationSet).contains("Log4j") |
| 48 | + |
| 49 | + val thrown = |
| 50 | + server |
| 51 | + .awaitEnvelope { |
| 52 | + it.event()?.exceptions?.any { e -> e.value == "Something went wrong [id=1]" } == true |
| 53 | + } |
| 54 | + .event()!! |
| 55 | + assertThat(thrown.hasFlag("my-feature-flag", true)).isTrue() |
| 56 | + } |
| 57 | + |
| 58 | + companion object { |
| 59 | + val server = MockSentryServer() |
| 60 | + |
| 61 | + @JvmStatic |
| 62 | + @DynamicPropertySource |
| 63 | + fun sentryDsn(registry: DynamicPropertyRegistry) { |
| 64 | + registry.add("sentry.dsn") { server.dsn } |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments