Skip to content

Commit cfb4d8e

Browse files
committed
do not override span name if customized
1 parent 6ec8e1b commit cfb4d8e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

sentry-opentelemetry/sentry-opentelemetry-core/src/main/java/io/sentry/opentelemetry/SpanDescriptionExtractor.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,23 @@
1313
import org.jetbrains.annotations.NotNull;
1414
import org.jetbrains.annotations.Nullable;
1515

16+
import java.util.Arrays;
17+
1618
@ApiStatus.Internal
1719
public final class SpanDescriptionExtractor {
1820

19-
@SuppressWarnings("deprecation")
2021
public @NotNull OtelSpanInfo extractSpanInfo(
2122
final @NotNull SpanData otelSpan, final @Nullable IOtelSpanWrapper sentrySpan) {
23+
OtelSpanInfo spanInfo = extractSpanInfoInternal(otelSpan);
24+
if (spanInfo != null) {
25+
return spanInfo;
26+
}
27+
28+
return defaultInfo(otelSpan, sentrySpan);
29+
}
30+
31+
@SuppressWarnings("deprecation")
32+
private @Nullable OtelSpanInfo extractSpanInfoInternal(final @NotNull SpanData otelSpan) {
2233
final @NotNull Attributes attributes = otelSpan.getAttributes();
2334

2435
final @Nullable String httpMethod = attributes.get(HttpAttributes.HTTP_REQUEST_METHOD);
@@ -37,6 +48,10 @@ public final class SpanDescriptionExtractor {
3748
return descriptionForGraphql(otelSpan);
3849
}
3950

51+
return null;
52+
}
53+
54+
private @NotNull OtelSpanInfo defaultInfo(final @NotNull SpanData otelSpan, final @Nullable IOtelSpanWrapper sentrySpan) {
4055
final @NotNull String name = otelSpan.getName();
4156
final @Nullable String maybeDescription =
4257
sentrySpan != null ? sentrySpan.getDescription() : name;
@@ -113,7 +128,12 @@ private OtelSpanInfo descriptionForDbSystem(final @NotNull SpanData otelSpan) {
113128
return new OtelSpanInfo("db", otelSpan.getName(), TransactionNameSource.TASK);
114129
}
115130

116-
private OtelSpanInfo descriptionForGraphql(final @NotNull SpanData otelSpan) {
131+
private @Nullable OtelSpanInfo descriptionForGraphql(final @NotNull SpanData otelSpan) {
132+
final @NotNull String name = otelSpan.getName();
133+
// do not replace span name set by customer
134+
if (!Arrays.asList("query", "mutation", "subscription").contains(name)) {
135+
return null;
136+
}
117137
final @NotNull Attributes attributes = otelSpan.getAttributes();
118138
@Nullable
119139
String graphqlOperationType =
@@ -126,6 +146,6 @@ private OtelSpanInfo descriptionForGraphql(final @NotNull SpanData otelSpan) {
126146
return new OtelSpanInfo(description, description, TransactionNameSource.TASK);
127147
}
128148

129-
return new OtelSpanInfo(otelSpan.getName(), otelSpan.getName(), TransactionNameSource.TASK);
149+
return null;
130150
}
131151
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,23 @@ class SpanDescriptionExtractorTest {
270270
assertEquals(TransactionNameSource.TASK, info.transactionNameSource)
271271
}
272272

273+
@Test
274+
fun `does not set op to graphql for span with graphql operation type if span name has been overridden`() {
275+
givenAttributes(
276+
mapOf(
277+
GraphqlIncubatingAttributes.GRAPHQL_OPERATION_TYPE to "query",
278+
GraphqlIncubatingAttributes.GRAPHQL_OPERATION_NAME to "GreetingQuery",
279+
)
280+
)
281+
givenSpanName("my-custom-span-name")
282+
283+
val info = whenExtractingSpanInfo()
284+
285+
assertEquals("my-custom-span-name", info.op)
286+
assertEquals("my-custom-span-name", info.description)
287+
assertEquals(TransactionNameSource.CUSTOM, info.transactionNameSource)
288+
}
289+
273290
private fun createSpanContext(
274291
isRemote: Boolean,
275292
traceId: String = "f9118105af4a2d42b4124532cd1065ff",

0 commit comments

Comments
 (0)