Skip to content

Commit 3c3f390

Browse files
adinauerclaude
andcommitted
fix(opentelemetry): Use stable Attributes API in core tests
Replace test usage of sdk.internal.AttributesMap with public Attributes builders.\n\nThe OTel dependency bump removed the internal class, which broke\n:sentry-opentelemetry:sentry-opentelemetry-core:compileTestKotlin and\ntherefore the full build. Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 0702e78 commit 3c3f390

File tree

3 files changed

+56
-12
lines changed

3 files changed

+56
-12
lines changed

sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/OpenTelemetryAttributesExtractorTest.kt

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package io.sentry.opentelemetry
22

33
import io.opentelemetry.api.common.AttributeKey
4-
import io.opentelemetry.sdk.internal.AttributesMap
5-
import io.opentelemetry.sdk.trace.SpanLimits
4+
import io.opentelemetry.api.common.Attributes
65
import io.opentelemetry.sdk.trace.data.SpanData
76
import io.opentelemetry.semconv.HttpAttributes
87
import io.opentelemetry.semconv.ServerAttributes
@@ -22,12 +21,12 @@ import org.mockito.kotlin.whenever
2221
class OpenTelemetryAttributesExtractorTest {
2322
private class Fixture {
2423
val spanData = mock<SpanData>()
25-
val attributes = AttributesMap.create(100, SpanLimits.getDefault().maxAttributeValueLength)
24+
var attributes: Attributes = Attributes.empty()
2625
val options = SentryOptions.empty()
2726
val scope = Scope(options)
2827

2928
init {
30-
whenever(spanData.attributes).thenReturn(attributes)
29+
whenever(spanData.attributes).thenAnswer { attributes }
3130
}
3231
}
3332

@@ -346,7 +345,22 @@ class OpenTelemetryAttributesExtractorTest {
346345
}
347346

348347
private fun givenAttributes(map: Map<AttributeKey<out Any>, Any>) {
349-
map.forEach { k, v -> fixture.attributes.put(k, v) }
348+
fixture.attributes = buildAttributes(map)
349+
}
350+
351+
private fun buildAttributes(map: Map<AttributeKey<out Any>, Any>): Attributes {
352+
val builder = Attributes.builder()
353+
map.forEach { (key, value) -> putAttribute(builder, key, value) }
354+
return builder.build()
355+
}
356+
357+
@Suppress("UNCHECKED_CAST")
358+
private fun putAttribute(
359+
builder: io.opentelemetry.api.common.AttributesBuilder,
360+
key: AttributeKey<out Any>,
361+
value: Any,
362+
) {
363+
builder.put(key as AttributeKey<Any>, value)
350364
}
351365

352366
private fun whenExtractingAttributes() {

sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/OtelInternalSpanDetectionUtilTest.kt

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package io.sentry.opentelemetry
22

33
import io.opentelemetry.api.common.AttributeKey
4+
import io.opentelemetry.api.common.Attributes
45
import io.opentelemetry.api.trace.SpanKind
5-
import io.opentelemetry.sdk.internal.AttributesMap
66
import io.opentelemetry.semconv.HttpAttributes
77
import io.opentelemetry.semconv.ServerAttributes
88
import io.opentelemetry.semconv.UrlAttributes
@@ -17,7 +17,7 @@ import org.mockito.kotlin.whenever
1717
class OtelInternalSpanDetectionUtilTest {
1818
private class Fixture {
1919
val scopes = mock<IScopes>()
20-
val attributes = AttributesMap.create(100, 100)
20+
var attributes: Attributes = Attributes.empty()
2121
val options = SentryOptions.empty()
2222
var spanKind: SpanKind = SpanKind.INTERNAL
2323

@@ -152,7 +152,22 @@ class OtelInternalSpanDetectionUtilTest {
152152
}
153153

154154
private fun givenAttributes(map: Map<AttributeKey<out Any>, Any>) {
155-
map.forEach { k, v -> fixture.attributes.put(k, v) }
155+
fixture.attributes = buildAttributes(map)
156+
}
157+
158+
private fun buildAttributes(map: Map<AttributeKey<out Any>, Any>): Attributes {
159+
val builder = Attributes.builder()
160+
map.forEach { (key, value) -> putAttribute(builder, key, value) }
161+
return builder.build()
162+
}
163+
164+
@Suppress("UNCHECKED_CAST")
165+
private fun putAttribute(
166+
builder: io.opentelemetry.api.common.AttributesBuilder,
167+
key: AttributeKey<out Any>,
168+
value: Any,
169+
) {
170+
builder.put(key as AttributeKey<Any>, value)
156171
}
157172

158173
private fun givenDsn(dsn: String) {

sentry-opentelemetry/sentry-opentelemetry-core/src/test/kotlin/SpanDescriptionExtractorTest.kt

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package io.sentry.opentelemetry
22

33
import io.opentelemetry.api.common.AttributeKey
4+
import io.opentelemetry.api.common.Attributes
45
import io.opentelemetry.api.trace.SpanContext
56
import io.opentelemetry.api.trace.SpanKind
67
import io.opentelemetry.api.trace.TraceFlags
78
import io.opentelemetry.api.trace.TraceState
8-
import io.opentelemetry.sdk.internal.AttributesMap
99
import io.opentelemetry.sdk.trace.data.SpanData
1010
import io.opentelemetry.semconv.HttpAttributes
1111
import io.opentelemetry.semconv.UrlAttributes
@@ -22,14 +22,14 @@ class SpanDescriptionExtractorTest {
2222
private class Fixture {
2323
val sentrySpan = mock<IOtelSpanWrapper>()
2424
val otelSpan = mock<SpanData>()
25-
val attributes = AttributesMap.create(100, 100)
25+
var attributes: Attributes = Attributes.empty()
2626
var parentSpanContext = SpanContext.getInvalid()
2727
var spanKind = SpanKind.INTERNAL
2828
var spanName: String? = null
2929
var spanDescription: String? = null
3030

3131
fun setup() {
32-
whenever(otelSpan.attributes).thenReturn(attributes)
32+
whenever(otelSpan.attributes).thenAnswer { attributes }
3333
whenever(otelSpan.parentSpanContext).thenReturn(parentSpanContext)
3434
whenever(otelSpan.kind).thenReturn(spanKind)
3535
spanName?.let { whenever(otelSpan.name).thenReturn(it) }
@@ -271,7 +271,22 @@ class SpanDescriptionExtractorTest {
271271
}
272272

273273
private fun givenAttributes(map: Map<AttributeKey<out Any>, Any>) {
274-
map.forEach { k, v -> fixture.attributes.put(k, v) }
274+
fixture.attributes = buildAttributes(map)
275+
}
276+
277+
private fun buildAttributes(map: Map<AttributeKey<out Any>, Any>): Attributes {
278+
val builder = Attributes.builder()
279+
map.forEach { (key, value) -> putAttribute(builder, key, value) }
280+
return builder.build()
281+
}
282+
283+
@Suppress("UNCHECKED_CAST")
284+
private fun putAttribute(
285+
builder: io.opentelemetry.api.common.AttributesBuilder,
286+
key: AttributeKey<out Any>,
287+
value: Any,
288+
) {
289+
builder.put(key as AttributeKey<Any>, value)
275290
}
276291

277292
private fun whenExtractingSpanInfo(): OtelSpanInfo {

0 commit comments

Comments
 (0)