|
| 1 | +package io.sentry.opentelemetry |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.opentelemetry.api.trace.SpanKind |
| 5 | +import io.opentelemetry.context.Context |
| 6 | +import io.opentelemetry.sdk.OpenTelemetrySdk |
| 7 | +import io.opentelemetry.sdk.trace.SdkTracerProvider |
| 8 | +import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor |
| 9 | +import io.opentelemetry.semconv.HttpAttributes |
| 10 | +import io.sentry.ISentryClient |
| 11 | +import io.sentry.NoOpScopes |
| 12 | +import io.sentry.Sentry |
| 13 | +import io.sentry.SentryOptions |
| 14 | +import io.sentry.SpanDataConvention |
| 15 | +import io.sentry.TransactionContext |
| 16 | +import io.sentry.TransactionOptions |
| 17 | +import io.sentry.protocol.SentryId |
| 18 | +import io.sentry.protocol.SentryTransaction |
| 19 | +import io.sentry.test.createSentryClientMock |
| 20 | +import io.sentry.test.createTestScopes |
| 21 | +import java.util.concurrent.TimeUnit |
| 22 | +import kotlin.test.AfterTest |
| 23 | +import kotlin.test.Test |
| 24 | +import org.mockito.kotlin.anyOrNull |
| 25 | +import org.mockito.kotlin.argumentCaptor |
| 26 | +import org.mockito.kotlin.atLeastOnce |
| 27 | +import org.mockito.kotlin.verify |
| 28 | +import org.mockito.kotlin.whenever |
| 29 | + |
| 30 | +class SentrySpanExporterTest { |
| 31 | + private val options = |
| 32 | + SentryOptions().apply { |
| 33 | + dsn = "https://key@sentry.io/proj" |
| 34 | + tracesSampleRate = 1.0 |
| 35 | + } |
| 36 | + private val client: ISentryClient = createSentryClientMock() |
| 37 | + private val scopes = createTestScopes(options).also { it.bindClient(client) } |
| 38 | + private val exporter = SentrySpanExporter(scopes) |
| 39 | + |
| 40 | + init { |
| 41 | + whenever( |
| 42 | + client.captureTransaction(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull()) |
| 43 | + ) |
| 44 | + .thenReturn(SentryId()) |
| 45 | + Sentry.setCurrentScopes(scopes) |
| 46 | + } |
| 47 | + |
| 48 | + @AfterTest |
| 49 | + fun tearDown() { |
| 50 | + Sentry.setCurrentScopes(NoOpScopes.getInstance()) |
| 51 | + } |
| 52 | + |
| 53 | + @Test |
| 54 | + fun `server roots and children get server kind and root response status`() { |
| 55 | + withOpenTelemetry { openTelemetry -> |
| 56 | + val tracer = openTelemetry.getTracer("test") |
| 57 | + val root = |
| 58 | + tracer |
| 59 | + .spanBuilder("server-root") |
| 60 | + .setSpanKind(SpanKind.SERVER) |
| 61 | + .setAttribute(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 404L) |
| 62 | + .startSpan() |
| 63 | + val child = |
| 64 | + tracer |
| 65 | + .spanBuilder("server-child") |
| 66 | + .setParent(root.storeInContext(Context.root())) |
| 67 | + .setSpanKind(SpanKind.SERVER) |
| 68 | + .startSpan() |
| 69 | + |
| 70 | + child.end() |
| 71 | + root.end() |
| 72 | + } |
| 73 | + |
| 74 | + val transaction = capturedTransactions().single() |
| 75 | + assertThat(transaction.contexts.trace!!.data[SpanDataConvention.SENTRY_KIND_KEY]) |
| 76 | + .isEqualTo("server") |
| 77 | + assertThat(transaction.contexts.trace!!.data[SpanDataConvention.HTTP_STATUS_CODE_KEY]) |
| 78 | + .isEqualTo(404L) |
| 79 | + assertThat(transaction.contexts.trace!!.data["otel.kind"]).isEqualTo("SERVER") |
| 80 | + assertThat(transaction.spans).hasSize(1) |
| 81 | + assertThat(transaction.spans.single().data!![SpanDataConvention.SENTRY_KIND_KEY]) |
| 82 | + .isEqualTo("server") |
| 83 | + assertThat(transaction.spans.single().data!!["otel.kind"]).isEqualTo("SERVER") |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + fun `client and internal roots do not get server data`() { |
| 88 | + withOpenTelemetry { openTelemetry -> |
| 89 | + val tracer = openTelemetry.getTracer("test") |
| 90 | + tracer |
| 91 | + .spanBuilder("client-root") |
| 92 | + .setSpanKind(SpanKind.CLIENT) |
| 93 | + .setAttribute(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 404L) |
| 94 | + .startSpan() |
| 95 | + .end() |
| 96 | + tracer |
| 97 | + .spanBuilder("internal-root") |
| 98 | + .setSpanKind(SpanKind.INTERNAL) |
| 99 | + .setAttribute(HttpAttributes.HTTP_RESPONSE_STATUS_CODE, 500L) |
| 100 | + .startSpan() |
| 101 | + .end() |
| 102 | + } |
| 103 | + |
| 104 | + val transactions = capturedTransactions().associateBy { it.transaction } |
| 105 | + assertThat(transactions.keys).containsExactly("client-root", "internal-root") |
| 106 | + assertThat(transactions["client-root"]!!.contexts.trace!!.data) |
| 107 | + .doesNotContainKey(SpanDataConvention.SENTRY_KIND_KEY) |
| 108 | + assertThat(transactions["client-root"]!!.contexts.trace!!.data) |
| 109 | + .doesNotContainKey(SpanDataConvention.HTTP_STATUS_CODE_KEY) |
| 110 | + assertThat(transactions["internal-root"]!!.contexts.trace!!.data) |
| 111 | + .doesNotContainKey(SpanDataConvention.SENTRY_KIND_KEY) |
| 112 | + assertThat(transactions["internal-root"]!!.contexts.trace!!.data) |
| 113 | + .doesNotContainKey(SpanDataConvention.HTTP_STATUS_CODE_KEY) |
| 114 | + } |
| 115 | + |
| 116 | + @Test |
| 117 | + fun `rpc server root does not fabricate HTTP response status`() { |
| 118 | + withOpenTelemetry { openTelemetry -> |
| 119 | + openTelemetry |
| 120 | + .getTracer("test") |
| 121 | + .spanBuilder("rpc-server") |
| 122 | + .setSpanKind(SpanKind.SERVER) |
| 123 | + .startSpan() |
| 124 | + .end() |
| 125 | + } |
| 126 | + |
| 127 | + val transaction = capturedTransactions().single() |
| 128 | + assertThat(transaction.contexts.trace!!.data[SpanDataConvention.SENTRY_KIND_KEY]) |
| 129 | + .isEqualTo("server") |
| 130 | + assertThat(transaction.contexts.trace!!.data) |
| 131 | + .doesNotContainKey(SpanDataConvention.HTTP_STATUS_CODE_KEY) |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + fun `Sentry server marker survives default OTel internal kind`() { |
| 136 | + withOpenTelemetry(includeSentrySpanProcessor = true) { openTelemetry -> |
| 137 | + options.spanFactory = OtelSpanFactory(openTelemetry) |
| 138 | + val transaction = |
| 139 | + scopes.startTransaction( |
| 140 | + TransactionContext("native-server", "http.server"), |
| 141 | + TransactionOptions(), |
| 142 | + ) |
| 143 | + transaction.setData(SpanDataConvention.SENTRY_KIND_KEY, "server") |
| 144 | + transaction.setData(SpanDataConvention.HTTP_STATUS_CODE_KEY, 404) |
| 145 | + transaction.finish() |
| 146 | + } |
| 147 | + |
| 148 | + val transaction = capturedTransactions().single() |
| 149 | + assertThat(transaction.contexts.trace!!.data[SpanDataConvention.SENTRY_KIND_KEY]) |
| 150 | + .isEqualTo("server") |
| 151 | + assertThat(transaction.contexts.trace!!.data[SpanDataConvention.HTTP_STATUS_CODE_KEY]) |
| 152 | + .isEqualTo(404) |
| 153 | + assertThat(transaction.contexts.trace!!.data["otel.kind"]).isEqualTo("INTERNAL") |
| 154 | + } |
| 155 | + |
| 156 | + private fun withOpenTelemetry( |
| 157 | + includeSentrySpanProcessor: Boolean = false, |
| 158 | + callback: (OpenTelemetrySdk) -> Unit, |
| 159 | + ) { |
| 160 | + val providerBuilder = SdkTracerProvider.builder() |
| 161 | + if (includeSentrySpanProcessor) { |
| 162 | + providerBuilder.addSpanProcessor(OtelSentrySpanProcessor(scopes)) |
| 163 | + } |
| 164 | + val provider = providerBuilder.addSpanProcessor(SimpleSpanProcessor.create(exporter)).build() |
| 165 | + val openTelemetry = OpenTelemetrySdk.builder().setTracerProvider(provider).build() |
| 166 | + try { |
| 167 | + callback(openTelemetry) |
| 168 | + provider.forceFlush().join(10, TimeUnit.SECONDS) |
| 169 | + } finally { |
| 170 | + provider.close() |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + private fun capturedTransactions(): List<SentryTransaction> { |
| 175 | + val transaction = argumentCaptor<SentryTransaction>() |
| 176 | + verify(client, atLeastOnce()) |
| 177 | + .captureTransaction( |
| 178 | + transaction.capture(), |
| 179 | + anyOrNull(), |
| 180 | + anyOrNull(), |
| 181 | + anyOrNull(), |
| 182 | + anyOrNull(), |
| 183 | + ) |
| 184 | + return transaction.allValues |
| 185 | + } |
| 186 | +} |
0 commit comments