|
| 1 | +package io.sentry.opentelemetry |
| 2 | + |
| 3 | +import io.opentelemetry.api.common.AttributeKey |
| 4 | +import io.opentelemetry.sdk.internal.AttributesMap |
| 5 | +import io.opentelemetry.sdk.trace.data.SpanData |
| 6 | +import io.opentelemetry.semconv.ServerAttributes |
| 7 | +import io.opentelemetry.semconv.UrlAttributes |
| 8 | +import io.sentry.ISpan |
| 9 | +import io.sentry.Scope |
| 10 | +import io.sentry.SentryOptions |
| 11 | +import io.sentry.protocol.Request |
| 12 | +import org.mockito.kotlin.mock |
| 13 | +import org.mockito.kotlin.whenever |
| 14 | +import kotlin.test.Test |
| 15 | +import kotlin.test.assertEquals |
| 16 | +import kotlin.test.assertNotNull |
| 17 | +import kotlin.test.assertNull |
| 18 | + |
| 19 | +class OpenTelemetryAttributesExtractorTest { |
| 20 | + |
| 21 | + private class Fixture { |
| 22 | + val spanData = mock<SpanData>() |
| 23 | + val attributes = AttributesMap.create(100, 100) |
| 24 | + val sentrySpan = mock<ISpan>() |
| 25 | + val options = SentryOptions.empty() |
| 26 | + val scope = Scope(options) |
| 27 | + |
| 28 | + init { |
| 29 | + whenever(spanData.attributes).thenReturn(attributes) |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + private val fixture = Fixture() |
| 34 | + |
| 35 | + @Test |
| 36 | + fun `sets URL based on OTel attributes`() { |
| 37 | + givenAttributes( |
| 38 | + mapOf( |
| 39 | + UrlAttributes.URL_SCHEME to "https", |
| 40 | + UrlAttributes.URL_PATH to "/path/to/123", |
| 41 | + UrlAttributes.URL_QUERY to "q=123456&b=X", |
| 42 | + ServerAttributes.SERVER_ADDRESS to "io.sentry", |
| 43 | + ServerAttributes.SERVER_PORT to 8081L |
| 44 | + ) |
| 45 | + ) |
| 46 | + |
| 47 | + whenExtractingAttributes() |
| 48 | + |
| 49 | + thenRequestIsSet() |
| 50 | + thenUrlIsSetTo("https://io.sentry:8081/path/to/123") |
| 51 | + thenQueryIsSetTo("q=123456&b=X") |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + fun `when there is an existing request on scope it is filled with more details`() { |
| 56 | + fixture.scope.request = Request().also { it.bodySize = 123L } |
| 57 | + givenAttributes( |
| 58 | + mapOf( |
| 59 | + UrlAttributes.URL_SCHEME to "https", |
| 60 | + UrlAttributes.URL_PATH to "/path/to/123", |
| 61 | + UrlAttributes.URL_QUERY to "q=123456&b=X", |
| 62 | + ServerAttributes.SERVER_ADDRESS to "io.sentry", |
| 63 | + ServerAttributes.SERVER_PORT to 8081L |
| 64 | + ) |
| 65 | + ) |
| 66 | + |
| 67 | + whenExtractingAttributes() |
| 68 | + |
| 69 | + thenRequestIsSet() |
| 70 | + thenUrlIsSetTo("https://io.sentry:8081/path/to/123") |
| 71 | + thenQueryIsSetTo("q=123456&b=X") |
| 72 | + assertEquals(123L, fixture.scope.request!!.bodySize) |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + fun `when there is an existing request with url on scope it is kept`() { |
| 77 | + fixture.scope.request = Request().also { |
| 78 | + it.url = "http://docs.sentry.io:3000/platform" |
| 79 | + it.queryString = "s=abc" |
| 80 | + } |
| 81 | + givenAttributes( |
| 82 | + mapOf( |
| 83 | + UrlAttributes.URL_SCHEME to "https", |
| 84 | + UrlAttributes.URL_PATH to "/path/to/123", |
| 85 | + UrlAttributes.URL_QUERY to "q=123456&b=X", |
| 86 | + ServerAttributes.SERVER_ADDRESS to "io.sentry", |
| 87 | + ServerAttributes.SERVER_PORT to 8081L |
| 88 | + ) |
| 89 | + ) |
| 90 | + |
| 91 | + whenExtractingAttributes() |
| 92 | + |
| 93 | + thenRequestIsSet() |
| 94 | + thenUrlIsSetTo("http://docs.sentry.io:3000/platform") |
| 95 | + thenQueryIsSetTo("s=abc") |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + fun `when there is an existing request with url on scope it is kept with URL_FULL`() { |
| 100 | + fixture.scope.request = Request().also { |
| 101 | + it.url = "http://docs.sentry.io:3000/platform" |
| 102 | + it.queryString = "s=abc" |
| 103 | + } |
| 104 | + givenAttributes( |
| 105 | + mapOf( |
| 106 | + UrlAttributes.URL_FULL to "https://io.sentry:8081/path/to/123?q=123456&b=X" |
| 107 | + ) |
| 108 | + ) |
| 109 | + |
| 110 | + whenExtractingAttributes() |
| 111 | + |
| 112 | + thenRequestIsSet() |
| 113 | + thenUrlIsSetTo("http://docs.sentry.io:3000/platform") |
| 114 | + thenQueryIsSetTo("s=abc") |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + fun `sets URL based on OTel attributes without port`() { |
| 119 | + givenAttributes( |
| 120 | + mapOf( |
| 121 | + UrlAttributes.URL_SCHEME to "https", |
| 122 | + UrlAttributes.URL_PATH to "/path/to/123", |
| 123 | + ServerAttributes.SERVER_ADDRESS to "io.sentry" |
| 124 | + ) |
| 125 | + ) |
| 126 | + |
| 127 | + whenExtractingAttributes() |
| 128 | + |
| 129 | + thenRequestIsSet() |
| 130 | + thenUrlIsSetTo("https://io.sentry/path/to/123") |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + fun `sets URL based on OTel attributes without path`() { |
| 135 | + givenAttributes( |
| 136 | + mapOf( |
| 137 | + UrlAttributes.URL_SCHEME to "https", |
| 138 | + ServerAttributes.SERVER_ADDRESS to "io.sentry" |
| 139 | + ) |
| 140 | + ) |
| 141 | + |
| 142 | + whenExtractingAttributes() |
| 143 | + |
| 144 | + thenRequestIsSet() |
| 145 | + thenUrlIsSetTo("https://io.sentry") |
| 146 | + } |
| 147 | + |
| 148 | + @Test |
| 149 | + fun `does not set URL if server address is missing`() { |
| 150 | + givenAttributes( |
| 151 | + mapOf( |
| 152 | + UrlAttributes.URL_SCHEME to "https" |
| 153 | + ) |
| 154 | + ) |
| 155 | + |
| 156 | + whenExtractingAttributes() |
| 157 | + |
| 158 | + thenRequestIsSet() |
| 159 | + thenUrlIsNotSet() |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + fun `does not set URL if scheme is missing`() { |
| 164 | + givenAttributes( |
| 165 | + mapOf( |
| 166 | + ServerAttributes.SERVER_ADDRESS to "io.sentry" |
| 167 | + ) |
| 168 | + ) |
| 169 | + |
| 170 | + whenExtractingAttributes() |
| 171 | + |
| 172 | + thenRequestIsSet() |
| 173 | + thenUrlIsNotSet() |
| 174 | + } |
| 175 | + |
| 176 | + private fun givenAttributes(map: Map<AttributeKey<out Any>, Any>) { |
| 177 | + map.forEach { k, v -> |
| 178 | + fixture.attributes.put(k, v) |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private fun whenExtractingAttributes() { |
| 183 | + OpenTelemetryAttributesExtractor().extract(fixture.spanData, fixture.sentrySpan, fixture.scope) |
| 184 | + } |
| 185 | + |
| 186 | + private fun thenRequestIsSet() { |
| 187 | + assertNotNull(fixture.scope.request) |
| 188 | + } |
| 189 | + |
| 190 | + private fun thenUrlIsSetTo(expected: String) { |
| 191 | + assertEquals(expected, fixture.scope.request!!.url) |
| 192 | + } |
| 193 | + |
| 194 | + private fun thenUrlIsNotSet() { |
| 195 | + assertNull(fixture.scope.request!!.url) |
| 196 | + } |
| 197 | + |
| 198 | + private fun thenQueryIsSetTo(expected: String) { |
| 199 | + assertEquals(expected, fixture.scope.request!!.queryString) |
| 200 | + } |
| 201 | +} |
0 commit comments