Skip to content

Commit

Permalink
Deduplicate SpanInfo extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
adinauer committed May 15, 2024
1 parent 9b900c7 commit b3919bd
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 57 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public final class io/sentry/opentelemetry/SentrySpanProcessor : io/opentelemetr

public final class io/sentry/opentelemetry/SpanDescriptionExtractor {
public fun <init> ()V
public fun extractSpanDescription (Lio/opentelemetry/sdk/trace/ReadableSpan;)Lio/sentry/opentelemetry/OtelSpanInfo;
public fun extractSpanInfo (Lio/opentelemetry/sdk/trace/data/SpanData;)Lio/sentry/opentelemetry/OtelSpanInfo;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ private boolean isSentryRequest(final @NotNull ReadableSpan otelSpan) {
private void updateTransactionWithOtelData(
final @NotNull ITransaction sentryTransaction, final @NotNull ReadableSpan otelSpan) {
final @NotNull OtelSpanInfo otelSpanInfo =
spanDescriptionExtractor.extractSpanDescription(otelSpan);
spanDescriptionExtractor.extractSpanInfo(otelSpan.toSpanData());
sentryTransaction.setOperation(otelSpanInfo.getOp());
sentryTransaction.setName(
otelSpanInfo.getDescription(), otelSpanInfo.getTransactionNameSource());
Expand Down Expand Up @@ -317,7 +317,7 @@ private void updateSpanWithOtelData(
});

final @NotNull OtelSpanInfo otelSpanInfo =
spanDescriptionExtractor.extractSpanDescription(otelSpan);
spanDescriptionExtractor.extractSpanInfo(otelSpan.toSpanData());
sentrySpan.setOperation(otelSpanInfo.getOp());
sentrySpan.setDescription(otelSpanInfo.getDescription());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.trace.SpanKind;
import io.opentelemetry.sdk.trace.ReadableSpan;
import io.opentelemetry.sdk.trace.data.SpanData;
import io.opentelemetry.semconv.SemanticAttributes;
import io.sentry.protocol.TransactionNameSource;
Expand All @@ -15,59 +14,6 @@
@ApiStatus.Internal
public final class SpanDescriptionExtractor {

// TODO [POTEL] remove these method overloads and pass in SpanData instead (span.toSpanData())
@SuppressWarnings("deprecation")
public @NotNull OtelSpanInfo extractSpanDescription(final @NotNull ReadableSpan otelSpan) {
final @NotNull String name = otelSpan.getName();

final @Nullable String httpMethod = otelSpan.getAttribute(SemanticAttributes.HTTP_METHOD);
if (httpMethod != null) {
return descriptionForHttpMethod(otelSpan, httpMethod);
}

final @Nullable String dbSystem = otelSpan.getAttribute(SemanticAttributes.DB_SYSTEM);
if (dbSystem != null) {
return descriptionForDbSystem(otelSpan);
}

return new OtelSpanInfo(name, name, TransactionNameSource.CUSTOM);
}

@SuppressWarnings("deprecation")
private OtelSpanInfo descriptionForHttpMethod(
final @NotNull ReadableSpan otelSpan, final @NotNull String httpMethod) {
final @NotNull String name = otelSpan.getName();
final @NotNull SpanKind kind = otelSpan.getKind();
final @NotNull StringBuilder opBuilder = new StringBuilder("http");

if (SpanKind.CLIENT.equals(kind)) {
opBuilder.append(".client");
} else if (SpanKind.SERVER.equals(kind)) {
opBuilder.append(".server");
}
final @Nullable String httpTarget = otelSpan.getAttribute(SemanticAttributes.HTTP_TARGET);
final @Nullable String httpRoute = otelSpan.getAttribute(SemanticAttributes.HTTP_ROUTE);
final @Nullable String httpPath = httpRoute != null ? httpRoute : httpTarget;
final @NotNull String op = opBuilder.toString();

if (httpPath == null) {
return new OtelSpanInfo(op, name, TransactionNameSource.CUSTOM);
}

final @NotNull String description = httpMethod + " " + httpPath;
final @NotNull TransactionNameSource transactionNameSource =
httpRoute != null ? TransactionNameSource.ROUTE : TransactionNameSource.URL;

return new OtelSpanInfo(op, description, transactionNameSource);
}

@SuppressWarnings("deprecation")
private OtelSpanInfo descriptionForDbSystem(final @NotNull ReadableSpan otelSpan) {
@Nullable String dbStatement = otelSpan.getAttribute(SemanticAttributes.DB_STATEMENT);
@NotNull String description = dbStatement != null ? dbStatement : otelSpan.getName();
return new OtelSpanInfo("db", description, TransactionNameSource.TASK);
}

@SuppressWarnings("deprecation")
public @NotNull OtelSpanInfo extractSpanInfo(final @NotNull SpanData otelSpan) {
OtelSpanInfo spanInfo = extractSpanDescription(otelSpan);
Expand Down

0 comments on commit b3919bd

Please sign in to comment.