|
| 1 | +package io.sentry.spring.boot.jakarta.integrationtest |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.sentry.Sentry |
| 5 | +import io.sentry.protocol.FeatureFlag |
| 6 | +import io.sentry.test.event |
| 7 | +import io.sentry.test.hasFlag |
| 8 | +import io.sentry.test.hasLogWithAttribute |
| 9 | +import io.sentry.test.hasLogWithBody |
| 10 | +import io.sentry.test.hasOp |
| 11 | +import io.sentry.test.hasSpanWithOp |
| 12 | +import io.sentry.test.hasSpanWithOpAndDescription |
| 13 | +import io.sentry.test.logs |
| 14 | +import io.sentry.test.profileChunk |
| 15 | +import io.sentry.test.transaction |
| 16 | +import kotlin.test.Test |
| 17 | + |
| 18 | +class PersonIntegrationTest : IntegrationTest() { |
| 19 | + |
| 20 | + @Test |
| 21 | + fun `failed request reports errors, the transaction and logs`() { |
| 22 | + val response = restTemplate.getForEntity("/person/1", String::class.java) |
| 23 | + assertThat(response.statusCode.value()).isEqualTo(500) |
| 24 | + Sentry.flush(5000) |
| 25 | + |
| 26 | + val logged = |
| 27 | + server.awaitEnvelope { it.event()?.message?.formatted == "Trying person with id=1" }.event()!! |
| 28 | + assertThat(logged.hasFlag("my-feature-flag", true)).isTrue() |
| 29 | + |
| 30 | + val thrown = |
| 31 | + server |
| 32 | + .awaitEnvelope { |
| 33 | + it.event()?.exceptions?.any { e -> e.value == "Something went wrong [id=1]" } == true |
| 34 | + } |
| 35 | + .event()!! |
| 36 | + assertThat(thrown.hasFlag("my-feature-flag", true)).isTrue() |
| 37 | + |
| 38 | + val transaction = |
| 39 | + server.awaitEnvelope { it.transaction()?.transaction == "GET /person/{id}" }.transaction()!! |
| 40 | + assertThat( |
| 41 | + transaction.hasOp( |
| 42 | + "http.server", |
| 43 | + FeatureFlag("flag.evaluation.transaction-feature-flag", true), |
| 44 | + ) |
| 45 | + ) |
| 46 | + .isTrue() |
| 47 | + assertThat( |
| 48 | + transaction.hasSpanWithOp( |
| 49 | + "spanCreatedThroughSentryApi", |
| 50 | + FeatureFlag("flag.evaluation.my-feature-flag", true), |
| 51 | + ) |
| 52 | + ) |
| 53 | + .isTrue() |
| 54 | + |
| 55 | + val logs = |
| 56 | + server |
| 57 | + .awaitEnvelope { |
| 58 | + it.logs()?.items?.any { log -> log.body == "warn Sentry logging" } == true |
| 59 | + } |
| 60 | + .logs()!! |
| 61 | + assertThat(logs.hasLogWithBody("error Sentry logging")).isTrue() |
| 62 | + assertThat(logs.hasLogWithBody("hello there world!")).isTrue() |
| 63 | + assertThat(logs.hasLogWithAttribute("warn Sentry logging", "user.type", "admin")).isTrue() |
| 64 | + assertThat(logs.hasLogWithAttribute("warn Sentry logging", "feature.version", 2)).isTrue() |
| 65 | + assertThat(logs.hasLogWithAttribute("warn Sentry logging", "debug.enabled", true)).isTrue() |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun `creating a person traces the service method and the query`() { |
| 70 | + val response = |
| 71 | + restTemplate.postForEntity("/person/", Person("firstA", "lastB"), Person::class.java) |
| 72 | + assertThat(response.statusCode.value()).isEqualTo(200) |
| 73 | + assertThat(response.body!!.firstName).isEqualTo("firstA") |
| 74 | + assertThat(response.body!!.lastName).isEqualTo("lastB") |
| 75 | + |
| 76 | + val transaction = |
| 77 | + server.awaitEnvelope { it.transaction()?.transaction == "POST /person/" }.transaction()!! |
| 78 | + assertThat(transaction.hasSpanWithOp("PersonService.create")).isTrue() |
| 79 | + assertThat( |
| 80 | + transaction.hasSpanWithOpAndDescription( |
| 81 | + "db.query", |
| 82 | + "insert into person (firstName, lastName) values (?, ?)", |
| 83 | + ) |
| 84 | + ) |
| 85 | + .isTrue() |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + fun `creating a person starts a profile linked to the transaction`() { |
| 90 | + val response = |
| 91 | + restTemplate.postForEntity("/person/", Person("firstA", "lastB"), Person::class.java) |
| 92 | + assertThat(response.statusCode.value()).isEqualTo(200) |
| 93 | + |
| 94 | + val transaction = |
| 95 | + server.awaitEnvelope { it.transaction()?.transaction == "POST /person/" }.transaction()!! |
| 96 | + val profilerId = transaction.contexts.profile?.profilerId |
| 97 | + assertThat(profilerId).isNotNull() |
| 98 | + server.awaitEnvelope(timeoutMillis = 30_000) { it.profileChunk()?.profilerId == profilerId } |
| 99 | + } |
| 100 | +} |
0 commit comments