From 3e46c1757d02f41af2805a76b0b3cf1242528224 Mon Sep 17 00:00:00 2001 From: Bruce Bujon Date: Thu, 26 Oct 2023 11:14:22 +0200 Subject: [PATCH] feat: Add span operation naming convention --- .../OpenTelemetryInstrumentation.java | 16 + .../OpenTelemetryContextInstrumentation.java | 16 + ...elemetryContextStorageInstrumentation.java | 16 + .../trace/OtelConventions.java | 319 ++++++++++++++++++ .../opentelemetry14/trace/OtelSpan.java | 5 +- .../trace/OtelSpanBuilder.java | 19 +- .../opentelemetry14/trace/OtelTracer.java | 6 +- .../test/groovy/OpenTelemetry14Test.groovy | 46 ++- .../context/ContextTest.groovy | 13 +- .../propagation/AbstractPropagatorTest.groovy | 5 +- 10 files changed, 418 insertions(+), 43 deletions(-) create mode 100644 dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelConventions.java diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java index 35f0425cb90..fc946fa36a5 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/OpenTelemetryInstrumentation.java @@ -64,6 +64,22 @@ public String[] helperClassNames() { packageName + ".context.propagation.AgentTextMapPropagator", packageName + ".context.propagation.OtelContextPropagators", packageName + ".trace.OtelExtractedContext", + packageName + ".trace.OtelConventions", + packageName + ".trace.OtelConventions$1", + packageName + ".trace.OtelConventions$Convention", + packageName + ".trace.OtelConventions$Convention$1", + packageName + ".trace.OtelConventions$Convention$2", + packageName + ".trace.OtelConventions$Convention$3", + packageName + ".trace.OtelConventions$Convention$4", + packageName + ".trace.OtelConventions$Convention$5", + packageName + ".trace.OtelConventions$Convention$6", + packageName + ".trace.OtelConventions$Convention$7", + packageName + ".trace.OtelConventions$Convention$8", + packageName + ".trace.OtelConventions$Convention$9", + packageName + ".trace.OtelConventions$Convention$10", + packageName + ".trace.OtelConventions$Convention$11", + packageName + ".trace.OtelConventions$Convention$12", + packageName + ".trace.OtelConventions$Convention$13", packageName + ".trace.OtelSpan", packageName + ".trace.OtelSpan$1", packageName + ".trace.OtelSpan$NoopSpan", diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java index df0676a5d6d..e3e32e58dee 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextInstrumentation.java @@ -56,6 +56,22 @@ public String[] helperClassNames() { packageName + ".OtelContext", packageName + ".OtelScope", ROOT_PACKAGE_NAME + ".trace.OtelExtractedContext", + ROOT_PACKAGE_NAME + ".trace.OtelConventions", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$1", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$1", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$2", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$3", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$4", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$5", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$6", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$7", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$8", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$9", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$10", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$11", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$12", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$13", ROOT_PACKAGE_NAME + ".trace.OtelSpan", ROOT_PACKAGE_NAME + ".trace.OtelSpan$1", ROOT_PACKAGE_NAME + ".trace.OtelSpan$NoopSpan", diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java index b7130cd55a3..dfaa8f7623d 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/context/OpenTelemetryContextStorageInstrumentation.java @@ -62,6 +62,22 @@ public String[] helperClassNames() { packageName + ".OtelContext", packageName + ".OtelScope", ROOT_PACKAGE_NAME + ".trace.OtelExtractedContext", + ROOT_PACKAGE_NAME + ".trace.OtelConventions", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$1", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$1", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$2", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$3", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$4", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$5", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$6", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$7", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$8", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$9", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$10", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$11", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$12", + ROOT_PACKAGE_NAME + ".trace.OtelConventions$Convention$13", ROOT_PACKAGE_NAME + ".trace.OtelSpan", ROOT_PACKAGE_NAME + ".trace.OtelSpan$1", ROOT_PACKAGE_NAME + ".trace.OtelSpan$NoopSpan", diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelConventions.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelConventions.java new file mode 100644 index 00000000000..18c1c12d628 --- /dev/null +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelConventions.java @@ -0,0 +1,319 @@ +package datadog.trace.instrumentation.opentelemetry14.trace; + +import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_CLIENT; +import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_CONSUMER; +import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_PRODUCER; +import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_SERVER; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.AWS_CLIENT; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.DATABASE; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.FAAS_CLIENT; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.FAAS_SERVER; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.GENERIC_CLIENT; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.GENERIC_SERVER; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.GRAPHQL_SERVER; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.HTTP_CLIENT; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.HTTP_SERVER; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.MESSAGE_CONSUMER; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.MESSAGE_PRODUCER; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.RPC_CLIENT; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.Convention.RPC_SERVER; +import static io.opentelemetry.api.trace.SpanKind.CLIENT; +import static io.opentelemetry.api.trace.SpanKind.CONSUMER; +import static io.opentelemetry.api.trace.SpanKind.INTERNAL; +import static io.opentelemetry.api.trace.SpanKind.PRODUCER; +import static io.opentelemetry.api.trace.SpanKind.SERVER; +import static java.util.Locale.ROOT; + +import datadog.trace.bootstrap.instrumentation.api.AgentSpan; +import io.opentelemetry.api.trace.SpanKind; +import javax.annotation.Nullable; + +public final class OtelConventions { + /** The Datadog span default operation name. */ + public static final String DEFAULT_OPERATION_NAME = "otel_unknown"; + + /** Span naming conventions, by priority order. */ + private static final Convention[] SPAN_NAMING_CONVENTIONS = + new Convention[] { + HTTP_SERVER, + HTTP_CLIENT, + DATABASE, + MESSAGE_CONSUMER, + MESSAGE_PRODUCER, + AWS_CLIENT, + RPC_CLIENT, + RPC_SERVER, + FAAS_SERVER, + FAAS_CLIENT, + GRAPHQL_SERVER, + GENERIC_SERVER, + GENERIC_CLIENT, + }; + + private OtelConventions() {} + + /** + * Convert OpenTelemetry {@link SpanKind} to Datadog span type. + * + * @param spanKind The OpenTelemetry span kind to convert. + * @return The related Datadog span type. + */ + public static String toSpanType(SpanKind spanKind) { + switch (spanKind) { + case CLIENT: + return SPAN_KIND_CLIENT; + case SERVER: + return SPAN_KIND_SERVER; + case PRODUCER: + return SPAN_KIND_PRODUCER; + case CONSUMER: + return SPAN_KIND_CONSUMER; + default: + case INTERNAL: + return spanKind.toString().toLowerCase(ROOT); + } + } + + /** + * Convert Datadog span type to OpenTelemetry {@link SpanKind}. + * + * @param spanType The span type to convert. + * @return The related OpenTelemetry {@link SpanKind}. + */ + public static SpanKind toSpanKind(String spanType) { + switch (spanType) { + case SPAN_KIND_CLIENT: + return CLIENT; + case SPAN_KIND_SERVER: + return SERVER; + case SPAN_KIND_PRODUCER: + return PRODUCER; + case SPAN_KIND_CONSUMER: + return CONSUMER; + default: + return INTERNAL; + } + } + + public static void applyOperationName(AgentSpan span) { + String operationName = computeOperationName(span).toLowerCase(ROOT); + span.setOperationName(operationName); + } + + public static String computeOperationName(AgentSpan span) { + for (Convention convention : SPAN_NAMING_CONVENTIONS) { + if (convention.appliesFor(span)) { + return convention.defaultOperationName(span); + } + } + // Fallback if no convention match + if (span.getSpanType() != null) { + return toSpanKind(span.getSpanType()).name(); + } else { + return DEFAULT_OPERATION_NAME; + } + } + + public enum Convention { + // HTTP conventions: https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/http/ + HTTP_SERVER() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, SERVER) && hasAttribute(span, "http.request.method"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return "http.server.request"; + } + }, + HTTP_CLIENT() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, CLIENT) && hasAttribute(span, "http.request.method"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return "http.client.request"; + } + }, + + // https://opentelemetry.io/docs/specs/semconv/database/database-spans/ + DATABASE() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, CLIENT) && hasAttribute(span, "db.system"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return getAttribute(span, "db.system") + ".query"; + } + }, + + // https://opentelemetry.io/docs/specs/semconv/messaging/messaging-spans/ + MESSAGE_PRODUCER() { // TODO Merge both producer and consumer? + + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, PRODUCER) + && hasAttribute(span, "messaging.system") + && hasAttribute(span, "messaging.operation"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return getAttribute(span, "messaging.system").toLowerCase(ROOT) + + "." + + getAttribute(span, "messaging.operation"); + } + }, + MESSAGE_CONSUMER() { + @Override + public boolean appliesFor(AgentSpan span) { + return (isKind(span, CLIENT) || isKind(span, CONSUMER)) + && hasAttribute(span, "messaging.system") + && hasAttribute(span, "messaging.operation"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return getAttribute(span, "messaging.system").toLowerCase(ROOT) + + "." + + getAttribute(span, "messaging.operation"); + } + }, + + // https://opentelemetry.io/docs/specs/semconv/cloud-providers/aws-sdk/ + AWS_CLIENT() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, CLIENT) + && hasAttribute(span, "rpc.system") + && "aws-api".equals(getAttribute(span, "rpc.system")); + } + + @Override + public String defaultOperationName(AgentSpan span) { + String service = getOptionalAttribute(span, "rpc.service"); + if (service == null) { + return "aws.request"; + } else { + return "aws." + service.toLowerCase(ROOT) + ".request"; + } + } + }, + // https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/rpc/ + RPC_SERVER() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, SERVER) && hasAttribute(span, "rpc.system"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return getAttribute(span, "rpc.system") + ".server.request"; + } + }, + RPC_CLIENT() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, CLIENT) && hasAttribute(span, "rpc.system"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return getAttribute(span, "rpc.system") + ".client.request"; + } + }, + // https://opentelemetry.io/docs/specs/semconv/faas/faas-spans/#incoming-faas-span-attributes + FAAS_SERVER() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, SERVER) && hasAttribute(span, "faas.trigger"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return getAttribute(span, "faas.trigger") + ".invoke"; + } + }, + // https://opentelemetry.io/docs/specs/semconv/faas/faas-spans/#outgoing-invocations + FAAS_CLIENT() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, CLIENT) + && hasAttribute(span, "faas.invoked_provider") + && hasAttribute(span, "faas.invoked_name"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return getAttribute(span, "faas.invoked_provider") + + "." + + getAttribute(span, "faas.invoked_name") + + ".invoke"; + } + }, + // https://opentelemetry.io/docs/specs/otel/trace/semantic_conventions/instrumentation/graphql/ + GRAPHQL_SERVER() { + @Override + public boolean appliesFor(AgentSpan span) { + return hasAttribute(span, "graphql.operation.type"); + } + + @Override + public String defaultOperationName(AgentSpan span) { + return "graphql.server.request"; + } + }, + GENERIC_SERVER() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, SERVER); + } + + @Override + public String defaultOperationName(AgentSpan span) { + String protocol = getOptionalAttribute(span, "network.protocol.name"); + return protocol == null ? "server.request" : protocol + "server.request"; + } + }, + GENERIC_CLIENT() { + @Override + public boolean appliesFor(AgentSpan span) { + return isKind(span, CLIENT); + } + + @Override + public String defaultOperationName(AgentSpan span) { + String protocol = getOptionalAttribute(span, "network.protocol.name"); + return protocol == null ? "client.request" : protocol + "client.request"; + } + }; + + public abstract boolean appliesFor(AgentSpan span); + + public abstract String defaultOperationName(AgentSpan span); + + public static boolean hasAttribute(AgentSpan span, String key) { + Object tag = span.getTag(key); + return tag instanceof String; + } + + public static String getAttribute(AgentSpan span, String key) { + Object tag = span.getTag(key); + return (String) tag; // TODO Handle type error, missing value, add logging, etc... + } + + public static @Nullable String getOptionalAttribute(AgentSpan span, String key) { + Object tag = span.getTag(key); + return tag instanceof String ? (String) tag : null; + } + + public static boolean isKind(AgentSpan span, SpanKind kind) { + return toSpanType(kind).equals(span.getSpanType()); + } + } +} diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelSpan.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelSpan.java index 1d4cb72aa16..d64f321e052 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelSpan.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelSpan.java @@ -1,6 +1,7 @@ package datadog.trace.instrumentation.opentelemetry14.trace; import static datadog.trace.bootstrap.instrumentation.api.AgentTracer.activateSpan; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.applyOperationName; import static io.opentelemetry.api.trace.StatusCode.ERROR; import static io.opentelemetry.api.trace.StatusCode.OK; import static io.opentelemetry.api.trace.StatusCode.UNSET; @@ -106,7 +107,7 @@ public Span recordException(Throwable exception, Attributes additionalAttributes @Override public Span updateName(String name) { if (this.recording) { - this.delegate.setOperationName(name); + this.delegate.setResourceName(name); } return this; } @@ -114,12 +115,14 @@ public Span updateName(String name) { @Override public void end() { this.recording = false; + applyOperationName(this.delegate); this.delegate.finish(); } @Override public void end(long timestamp, TimeUnit unit) { this.recording = false; + applyOperationName(this.delegate); this.delegate.finish(unit.toMicros(timestamp)); } diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelSpanBuilder.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelSpanBuilder.java index b39118edcc9..aca54bda20b 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelSpanBuilder.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelSpanBuilder.java @@ -1,10 +1,10 @@ package datadog.trace.instrumentation.opentelemetry14.trace; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.toSpanType; import static datadog.trace.instrumentation.opentelemetry14.trace.OtelExtractedContext.extract; import datadog.trace.bootstrap.instrumentation.api.AgentSpan; import datadog.trace.bootstrap.instrumentation.api.AgentTracer; -import datadog.trace.bootstrap.instrumentation.api.Tags; import io.opentelemetry.api.common.AttributeKey; import io.opentelemetry.api.common.Attributes; import io.opentelemetry.api.trace.Span; @@ -13,7 +13,6 @@ import io.opentelemetry.api.trace.SpanKind; import io.opentelemetry.context.Context; import java.util.List; -import java.util.Locale; import java.util.concurrent.TimeUnit; import javax.annotation.ParametersAreNonnullByDefault; @@ -110,22 +109,6 @@ public SpanBuilder setSpanKind(SpanKind spanKind) { return this; } - private static String toSpanType(SpanKind spanKind) { - switch (spanKind) { - case CLIENT: - return Tags.SPAN_KIND_CLIENT; - case SERVER: - return Tags.SPAN_KIND_SERVER; - case PRODUCER: - return Tags.SPAN_KIND_PRODUCER; - case CONSUMER: - return Tags.SPAN_KIND_CONSUMER; - default: - case INTERNAL: - return spanKind.toString().toLowerCase(Locale.ROOT); - } - } - @Override public SpanBuilder setStartTimestamp(long startTimestamp, TimeUnit unit) { this.delegate.withStartTimestamp(unit.toMicros(startTimestamp)); diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelTracer.java b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelTracer.java index 0ce1f69bbd9..9d551550063 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelTracer.java +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/main/java/datadog/trace/instrumentation/opentelemetry14/trace/OtelTracer.java @@ -1,5 +1,7 @@ package datadog.trace.instrumentation.opentelemetry14.trace; +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.DEFAULT_OPERATION_NAME; + import datadog.trace.bootstrap.instrumentation.api.AgentTracer; import io.opentelemetry.api.trace.SpanBuilder; import io.opentelemetry.api.trace.Tracer; @@ -19,7 +21,9 @@ public OtelTracer(String instrumentationScopeName) { @Override public SpanBuilder spanBuilder(String spanName) { AgentTracer.SpanBuilder delegate = - this.tracer.buildSpan(INSTRUMENTATION_NAME, spanName).withResourceName(spanName); + this.tracer + .buildSpan(INSTRUMENTATION_NAME, DEFAULT_OPERATION_NAME) + .withResourceName(spanName); return new OtelSpanBuilder(delegate); } } diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/OpenTelemetry14Test.groovy b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/OpenTelemetry14Test.groovy index ca0952048e5..fb529889e59 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/OpenTelemetry14Test.groovy +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/OpenTelemetry14Test.groovy @@ -10,6 +10,9 @@ import spock.lang.Subject import java.security.InvalidParameterException +import static datadog.trace.bootstrap.instrumentation.api.Tags.SPAN_KIND_SERVER +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.DEFAULT_OPERATION_NAME +import static io.opentelemetry.api.trace.SpanKind.SERVER import static io.opentelemetry.api.trace.StatusCode.ERROR import static io.opentelemetry.api.trace.StatusCode.OK import static io.opentelemetry.api.trace.StatusCode.UNSET @@ -41,12 +44,12 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(2) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME resourceName "some-name" } span { childOfPrevious() - operationName "other-name" + operationName DEFAULT_OPERATION_NAME resourceName "other-name" } } @@ -69,11 +72,13 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(2) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME + resourceName "some-name" } span { childOfPrevious() - operationName "other-name" + operationName DEFAULT_OPERATION_NAME + resourceName "other-name" } } } @@ -90,7 +95,6 @@ class OpenTelemetry14Test extends AgentTestRunner { TEST_WRITER.waitForTraces(1) def trace = TEST_WRITER.firstTrace() - then: trace.size() == 1 trace[0].spanId != 0 @@ -114,13 +118,15 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(1) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME + resourceName"some-name" } } trace(1) { span { parent() - operationName "other-name" + operationName DEFAULT_OPERATION_NAME + resourceName"other-name" } } } @@ -195,7 +201,7 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(1) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME if (tagSpan) { resourceName "other-resource" } else if (tagBuilder) { @@ -280,7 +286,7 @@ class OpenTelemetry14Test extends AgentTestRunner { SpanKind.CONSUMER | Tags.SPAN_KIND_CONSUMER SpanKind.INTERNAL | "internal" SpanKind.PRODUCER | Tags.SPAN_KIND_PRODUCER - SpanKind.SERVER | Tags.SPAN_KIND_SERVER + SERVER | Tags.SPAN_KIND_SERVER } def "test span error status"() { @@ -297,7 +303,7 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(1) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME resourceName "some-name" errored true @@ -349,7 +355,7 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(1) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME resourceName "some-name" errored false tags { @@ -390,7 +396,7 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(1) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME resourceName "some-name" errored false tags { @@ -405,16 +411,18 @@ class OpenTelemetry14Test extends AgentTestRunner { def "test span name update"() { setup: def builder = tracer.spanBuilder("some-name") - def result = builder.startSpan() + def result = builder.setSpanKind(SERVER).startSpan() expect: - result.delegate.operationName == "some-name" + result.delegate.operationName == DEFAULT_OPERATION_NAME + result.delegate.resourceName == "some-name" when: result.updateName("other-name") then: - result.delegate.operationName == "other-name" + result.delegate.operationName == DEFAULT_OPERATION_NAME + result.delegate.resourceName == "other-name" when: result.end() @@ -424,8 +432,9 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(1) { span { parent() - operationName "other-name" - resourceName "some-name" + spanType SPAN_KIND_SERVER + operationName "server.request" + resourceName "other-name" } } } @@ -449,7 +458,8 @@ class OpenTelemetry14Test extends AgentTestRunner { trace(1) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME + resourceName"some-name" errored true tags { defaultTags() diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/opentelemetry14/context/ContextTest.groovy b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/opentelemetry14/context/ContextTest.groovy index 3c1805133a8..bd834f9aefb 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/opentelemetry14/context/ContextTest.groovy +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/opentelemetry14/context/ContextTest.groovy @@ -12,6 +12,7 @@ import spock.lang.Subject import static datadog.trace.bootstrap.instrumentation.api.ScopeSource.MANUAL import static datadog.trace.instrumentation.opentelemetry14.context.OtelContext.DATADOG_CONTEXT_ROOT_SPAN_KEY import static datadog.trace.instrumentation.opentelemetry14.context.OtelContext.OTEL_CONTEXT_SPAN_KEY +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.DEFAULT_OPERATION_NAME class ContextTest extends AgentTestRunner { @Subject @@ -188,7 +189,8 @@ class ContextTest extends AgentTestRunner { def activeSpan = TEST_TRACER.activeSpan() then: - activeSpan.operationName == "some-name" + activeSpan.operationName == DEFAULT_OPERATION_NAME + activeSpan.resourceName == "some-name" DDSpanId.toHexStringPadded(activeSpan.spanId) == otelParentSpan.getSpanContext().spanId when: @@ -205,7 +207,8 @@ class ContextTest extends AgentTestRunner { activeSpan = TEST_TRACER.activeSpan() then: - activeSpan.operationName == "another-name" + activeSpan.operationName == DEFAULT_OPERATION_NAME + activeSpan.resourceName == "another-name" DDSpanId.toHexStringPadded(activeSpan.spanId) == otelGrandChildSpan.getSpanContext().spanId when: @@ -221,7 +224,8 @@ class ContextTest extends AgentTestRunner { trace(3) { span { parent() - operationName "some-name" + operationName DEFAULT_OPERATION_NAME + resourceName "some-name" } span { childOfPrevious() @@ -229,7 +233,8 @@ class ContextTest extends AgentTestRunner { } span { childOfPrevious() - operationName "another-name" + operationName DEFAULT_OPERATION_NAME + resourceName "another-name" } } } diff --git a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/opentelemetry14/context/propagation/AbstractPropagatorTest.groovy b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/opentelemetry14/context/propagation/AbstractPropagatorTest.groovy index 351ea547b24..977f5f9c40a 100644 --- a/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/opentelemetry14/context/propagation/AbstractPropagatorTest.groovy +++ b/dd-java-agent/instrumentation/opentelemetry/opentelemetry-1.4/src/test/groovy/opentelemetry14/context/propagation/AbstractPropagatorTest.groovy @@ -13,6 +13,8 @@ import spock.lang.Subject import javax.annotation.Nullable +import static datadog.trace.instrumentation.opentelemetry14.trace.OtelConventions.DEFAULT_OPERATION_NAME + abstract class AbstractPropagatorTest extends AgentTestRunner { @Subject def tracer = GlobalOpenTelemetry.get().tracerProvider.get("propagator" + Math.random()) // TODO FIX LATER @@ -86,7 +88,8 @@ abstract class AbstractPropagatorTest extends AgentTestRunner { assertTraces(1) { trace(1) { span { - operationName "some-name" + operationName DEFAULT_OPERATION_NAME + resourceName "some-name" traceDDId(DD128bTraceId.fromHex(traceId)) parentSpanId(DDSpanId.fromHex(spanId).toLong() as BigInteger) }