Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@
> - Java 8 support will be dropped. SDK 9.0.0 requires Java 11+. See [#6469](https://github.com/getsentry/sentry-java/issues/6469).
> - The Android `minSdk` will be raised from 21 to 24. See [#5508](https://github.com/getsentry/sentry-java/issues/5508).

## Unreleased

### Dependencies

- Bump OpenTelemetry ([#6691](https://github.com/getsentry/sentry-java/pull/6691))
- `opentelemetry` to `1.68.0` (was `1.67.0`)
- `opentelemetry-alpha` to `1.68.0-alpha` (was `1.67.0-alpha`)
- `opentelemetry-instrumentation` to `2.34.0` (was `2.33.1`)
- `opentelemetry-instrumentation-alpha` to `2.34.0-alpha` (was `2.33.1-alpha`)
- `opentelemetry-semconv` to `1.45.0` (was `1.44.0`)
- `opentelemetry-semconv-alpha` to `1.45.0-alpha` (was `1.44.0-alpha`)

## 8.66.0

### Fixes
Expand Down
12 changes: 6 additions & 6 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ nopen = "1.0.1"
# see https://developer.android.com/jetpack/androidx/releases/compose-kotlin
okhttp = "5.5.1"
openfeature = "1.18.2"
otel = "1.67.0"
otelAlpha = "1.67.0-alpha"
otelInstrumentation = "2.33.1"
otelInstrumentationAlpha = "2.33.1-alpha"
otel = "1.68.0"
otelAlpha = "1.68.0-alpha"
otelInstrumentation = "2.34.0"
otelInstrumentationAlpha = "2.34.0-alpha"
# check https://github.com/open-telemetry/opentelemetry-java-instrumentation/blob/main/dependencyManagement/build.gradle.kts#L49 for release version above to find a compatible version
otelSemanticConventions = "1.44.0"
otelSemanticConventionsAlpha = "1.44.0-alpha"
otelSemanticConventions = "1.45.0"
otelSemanticConventionsAlpha = "1.45.0-alpha"
retrofit = "3.0.0"
room2 = "2.8.5"
room3 = "3.0.3"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,28 @@
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes;
import io.sentry.ISpan;
import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

@ApiStatus.Internal
public final class OtelSpanUtils {

/**
* OpenTelemetry deprecated {@code messaging.system} in favor of {@code messaging.system.name}.
* Instrumentation may set either, so prefer {@code messaging.system} when both are set.
*/
@SuppressWarnings("deprecation")
static @Nullable String getMessagingSystem(final @NotNull Attributes attributes) {
final @Nullable String messagingSystem =
attributes.get(MessagingIncubatingAttributes.MESSAGING_SYSTEM);
return messagingSystem != null
? messagingSystem
: attributes.get(MessagingIncubatingAttributes.MESSAGING_SYSTEM_NAME);
}

public static <T> void maybeTransferOtelAttribute(
final @NotNull SpanData otelSpan,
final @NotNull ISpan sentrySpan,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static io.sentry.TransactionContext.DEFAULT_TRANSACTION_NAME;
import static io.sentry.opentelemetry.InternalSemanticAttributes.IS_REMOTE_PARENT;
import static io.sentry.opentelemetry.OtelInternalSpanDetectionUtil.isSentryRequest;
import static io.sentry.opentelemetry.OtelSpanUtils.getMessagingSystem;
import static io.sentry.opentelemetry.OtelSpanUtils.maybeTransferOtelAttribute;

import io.opentelemetry.api.common.Attributes;
Expand Down Expand Up @@ -242,6 +243,7 @@ private void createAndFinishSpanForOtelSpan(
setOtelInstrumentationInfo(spanData, sentryChildSpan);
setOtelSpanKind(spanData, sentryChildSpan);
transferSpanDetails(sentrySpanMaybe, sentryChildSpan);
transferMessagingSystem(spanData, sentryChildSpan);

for (SpanNode childNode : spanNode.getChildren()) {
createAndFinishSpanForOtelSpan(childNode, sentryChildSpan, remaining);
Expand Down Expand Up @@ -362,8 +364,7 @@ private void transferSpanDetails(
maybeTransferOtelAttribute(span, sentryTransaction, ThreadIncubatingAttributes.THREAD_ID);
maybeTransferOtelAttribute(span, sentryTransaction, ThreadIncubatingAttributes.THREAD_NAME);

maybeTransferOtelAttribute(
span, sentryTransaction, MessagingIncubatingAttributes.MESSAGING_SYSTEM);
transferMessagingSystem(span, sentryTransaction);
maybeTransferOtelAttribute(
span, sentryTransaction, MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME);
maybeTransferOtelAttribute(
Expand All @@ -382,6 +383,15 @@ private void transferSpanDetails(
return sentryTransaction;
}

@SuppressWarnings("deprecation")
private static void transferMessagingSystem(
final @NotNull SpanData span, final @NotNull ISpan sentrySpan) {
final @Nullable String messagingSystem = getMessagingSystem(span.getAttributes());
if (messagingSystem != null) {
sentrySpan.setData(MessagingIncubatingAttributes.MESSAGING_SYSTEM.getKey(), messagingSystem);
}
}

private List<SpanNode> findCompletedRootNodes(final @NotNull List<SpanNode> grouped) {
final @NotNull Predicate<SpanNode> isRootPredicate =
node -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,8 @@ public final class SpanDescriptionExtractor {
final @NotNull SentryOptions options) {
final @NotNull Attributes attributes = otelSpan.getAttributes();

if (options.isEnableQueueTracing()) {
final @Nullable String messagingSystem =
attributes.get(MessagingIncubatingAttributes.MESSAGING_SYSTEM);
if (messagingSystem != null) {
return descriptionForMessagingSystem(otelSpan);
}
if (options.isEnableQueueTracing() && OtelSpanUtils.getMessagingSystem(attributes) != null) {
return descriptionForMessagingSystem(otelSpan);
}

final @Nullable String httpMethod = attributes.get(HttpAttributes.HTTP_REQUEST_METHOD);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package io.sentry.opentelemetry

import com.google.common.truth.Truth.assertThat
import io.opentelemetry.context.Context
import io.opentelemetry.sdk.OpenTelemetrySdk
import io.opentelemetry.sdk.trace.SdkTracerProvider
import io.opentelemetry.sdk.trace.export.SimpleSpanProcessor
import io.opentelemetry.semconv.incubating.MessagingIncubatingAttributes
import io.sentry.ISentryClient
import io.sentry.NoOpScopes
import io.sentry.Sentry
import io.sentry.SentryOptions
import io.sentry.protocol.SentryId
import io.sentry.protocol.SentryTransaction
import io.sentry.test.createSentryClientMock
import io.sentry.test.createTestScopes
import java.util.concurrent.TimeUnit
import kotlin.test.AfterTest
import kotlin.test.Test
import org.mockito.kotlin.anyOrNull
import org.mockito.kotlin.argumentCaptor
import org.mockito.kotlin.atLeastOnce
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

class SentrySpanExporterMessagingTest {
private val options =
SentryOptions().apply {
dsn = "https://[email protected]/proj"
tracesSampleRate = 1.0
}
private val client: ISentryClient = createSentryClientMock()
private val scopes = createTestScopes(options).also { it.bindClient(client) }
private val exporter = SentrySpanExporter(scopes)

init {
whenever(
client.captureTransaction(anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull(), anyOrNull()),
)
.thenReturn(SentryId())
Sentry.setCurrentScopes(scopes)
}

@AfterTest
fun tearDown() {
Sentry.setCurrentScopes(NoOpScopes.getInstance())
}

@Test
fun `normalizes replacement messaging system on roots and children`() {
withOpenTelemetry { openTelemetry ->
val tracer = openTelemetry.getTracer("test")
val root =
tracer
.spanBuilder("root")
.setAttribute(MessagingIncubatingAttributes.MESSAGING_SYSTEM_NAME, "kafka")
.startSpan()
val child =
tracer
.spanBuilder("child")
.setParent(root.storeInContext(Context.root()))
.setAttribute(MessagingIncubatingAttributes.MESSAGING_SYSTEM_NAME, "rabbitmq")
.startSpan()

child.end()
root.end()
}

val transaction = capturedTransactions().single()
assertThat(transaction.contexts.trace!!.data["messaging.system"]).isEqualTo("kafka")
assertThat(transaction.spans).hasSize(1)
assertThat(transaction.spans.single().data!!["messaging.system"]).isEqualTo("rabbitmq")
}

@Suppress("DEPRECATION")
@Test
fun `prefers legacy messaging system on roots and children`() {
withOpenTelemetry { openTelemetry ->
val tracer = openTelemetry.getTracer("test")
val root =
tracer
.spanBuilder("root")
.setAttribute(MessagingIncubatingAttributes.MESSAGING_SYSTEM, "kafka")
.setAttribute(MessagingIncubatingAttributes.MESSAGING_SYSTEM_NAME, "ignored-root")
.startSpan()
val child =
tracer
.spanBuilder("child")
.setParent(root.storeInContext(Context.root()))
.setAttribute(MessagingIncubatingAttributes.MESSAGING_SYSTEM, "rabbitmq")
.setAttribute(MessagingIncubatingAttributes.MESSAGING_SYSTEM_NAME, "ignored-child")
.startSpan()

child.end()
root.end()
}

val transaction = capturedTransactions().single()
assertThat(transaction.contexts.trace!!.data["messaging.system"]).isEqualTo("kafka")
assertThat(transaction.spans).hasSize(1)
assertThat(transaction.spans.single().data!!["messaging.system"]).isEqualTo("rabbitmq")
}

private fun withOpenTelemetry(callback: (OpenTelemetrySdk) -> Unit) {
val provider =
SdkTracerProvider.builder().addSpanProcessor(SimpleSpanProcessor.create(exporter)).build()
val openTelemetry = OpenTelemetrySdk.builder().setTracerProvider(provider).build()
try {
callback(openTelemetry)
provider.forceFlush().join(10, TimeUnit.SECONDS)
} finally {
provider.close()
}
}

private fun capturedTransactions(): List<SentryTransaction> {
val transaction = argumentCaptor<SentryTransaction>()
verify(client, atLeastOnce())
.captureTransaction(
transaction.capture(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
anyOrNull(),
)
return transaction.allValues
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,63 @@ class SpanDescriptionExtractorTest {
assertEquals(TransactionNameSource.TASK, info.transactionNameSource)
}

@Test
fun `maps replacement messaging system name to queue publish op`() {
givenAttributes(
mapOf(
MessagingIncubatingAttributes.MESSAGING_SYSTEM_NAME to "kafka",
MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME to "my-topic",
MessagingIncubatingAttributes.MESSAGING_OPERATION_TYPE to "publish",
),
)

val info = whenExtractingSpanInfo(queueTracingEnabled = true)

assertEquals("queue.publish", info.op)
assertEquals("my-topic", info.description)
assertEquals(TransactionNameSource.TASK, info.transactionNameSource)
}

@Test
fun `replacement messaging system name wins over http when queue tracing enabled`() {
givenSpanKind(SpanKind.PRODUCER)
givenAttributes(
mapOf(
HttpAttributes.HTTP_REQUEST_METHOD to "POST",
UrlAttributes.URL_FULL to "https://sqs.us-east-1.amazonaws.com/",
MessagingIncubatingAttributes.MESSAGING_SYSTEM_NAME to "aws.sqs",
MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME to "my-queue",
MessagingIncubatingAttributes.MESSAGING_OPERATION_TYPE to "publish",
),
)

val info = whenExtractingSpanInfo(queueTracingEnabled = true)

assertEquals("queue.publish", info.op)
assertEquals("my-queue", info.description)
assertEquals(TransactionNameSource.TASK, info.transactionNameSource)
}

@Test
fun `http wins over replacement messaging system name when queue tracing disabled`() {
givenSpanKind(SpanKind.CLIENT)
givenAttributes(
mapOf(
HttpAttributes.HTTP_REQUEST_METHOD to "POST",
UrlAttributes.URL_FULL to "https://sqs.us-east-1.amazonaws.com/",
MessagingIncubatingAttributes.MESSAGING_SYSTEM_NAME to "aws.sqs",
MessagingIncubatingAttributes.MESSAGING_DESTINATION_NAME to "my-queue",
MessagingIncubatingAttributes.MESSAGING_OPERATION_TYPE to "publish",
),
)

val info = whenExtractingSpanInfo(queueTracingEnabled = false)

assertEquals("http.client", info.op)
assertEquals("POST https://sqs.us-east-1.amazonaws.com/", info.description)
assertEquals(TransactionNameSource.URL, info.transactionNameSource)
}

@Test
fun `maps messaging send operation type to queue publish op`() {
givenAttributes(
Expand Down
Loading