|
| 1 | +package io.sentry.opentelemetry |
| 2 | + |
| 3 | +import com.google.common.truth.Truth.assertThat |
| 4 | +import io.opentelemetry.api.trace.SpanContext |
| 5 | +import io.opentelemetry.api.trace.TraceFlags |
| 6 | +import io.opentelemetry.api.trace.TraceState |
| 7 | +import io.opentelemetry.sdk.trace.ReadWriteSpan |
| 8 | +import io.sentry.IScopes |
| 9 | +import io.sentry.ISpan |
| 10 | +import io.sentry.ISpanFactory |
| 11 | +import io.sentry.Instrumenter |
| 12 | +import io.sentry.SentryLongDate |
| 13 | +import io.sentry.SentryOptions |
| 14 | +import io.sentry.SpanOptions |
| 15 | +import kotlin.test.Test |
| 16 | +import org.mockito.kotlin.any |
| 17 | +import org.mockito.kotlin.argumentCaptor |
| 18 | +import org.mockito.kotlin.eq |
| 19 | +import org.mockito.kotlin.mock |
| 20 | +import org.mockito.kotlin.verify |
| 21 | +import org.mockito.kotlin.whenever |
| 22 | + |
| 23 | +class OtelSpanWrapperTest { |
| 24 | + @Test |
| 25 | + fun `startChild with timestamp forwards timestamp and Sentry instrumenter`() { |
| 26 | + val otelSpan = mock<ReadWriteSpan>() |
| 27 | + whenever(otelSpan.spanContext) |
| 28 | + .thenReturn( |
| 29 | + SpanContext.create( |
| 30 | + "2722d9f6ec019ade60c776169d9a8904", |
| 31 | + "cedf5b7571cb4972", |
| 32 | + TraceFlags.getSampled(), |
| 33 | + TraceState.getDefault(), |
| 34 | + ) |
| 35 | + ) |
| 36 | + whenever(otelSpan.name).thenReturn("parent") |
| 37 | + |
| 38 | + val spanFactory = mock<ISpanFactory>() |
| 39 | + val options = SentryOptions().apply { this.spanFactory = spanFactory } |
| 40 | + val scopes = mock<IScopes>() |
| 41 | + whenever(scopes.options).thenReturn(options) |
| 42 | + |
| 43 | + val parent = OtelSpanWrapper(otelSpan, scopes, SentryLongDate(0), null, null, null, null) |
| 44 | + val expectedChild = mock<ISpan>() |
| 45 | + whenever(spanFactory.createSpan(eq(scopes), any(), any(), eq(parent))).thenReturn(expectedChild) |
| 46 | + val timestamp = SentryLongDate(1234) |
| 47 | + |
| 48 | + val child = parent.startChild("child-op", "description", timestamp) |
| 49 | + |
| 50 | + val spanOptions = argumentCaptor<SpanOptions>() |
| 51 | + val spanContext = argumentCaptor<io.sentry.SpanContext>() |
| 52 | + verify(spanFactory) |
| 53 | + .createSpan(eq(scopes), spanOptions.capture(), spanContext.capture(), eq(parent)) |
| 54 | + assertThat(child).isSameInstanceAs(expectedChild) |
| 55 | + assertThat(spanOptions.firstValue.startTimestamp).isSameInstanceAs(timestamp) |
| 56 | + assertThat(spanContext.firstValue.operation).isEqualTo("child-op") |
| 57 | + assertThat(spanContext.firstValue.description).isEqualTo("description") |
| 58 | + assertThat(spanContext.firstValue.instrumenter).isEqualTo(Instrumenter.SENTRY) |
| 59 | + } |
| 60 | +} |
0 commit comments