Add support for WithSpan.kind()
MODIFY
instrumentation/annotations/src/main/java/io/opentelemetry/auto/instrumentation/traceannotation/TraceDecorator.java
instrumentation/annotations/src/main/java/io/opentelemetry/auto/instrumentation/traceannotation/TraceDecorator.java
@@ -19,6 +19,8 @@ package io.opentelemetry.auto.instrumentation.traceannotation;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.auto.bootstrap.instrumentation.decorator.BaseDecorator;
import io.opentelemetry.extensions.auto.annotations.WithSpan;
+import io.opentelemetry.trace.Span;
+import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.Tracer;
import java.lang.reflect.Method;
@@ -42,4 +44,9 @@ public class TraceDecorator extends BaseDecorator {
return spanNameForMethod(method);
}
+
+ public Span.Kind extractSpanKind(final Method method) {
+ WithSpan annotation = method.getAnnotation(WithSpan.class);
+ return annotation != null ? annotation.kind() : Kind.INTERNAL;
+ }
}

MODIFY
instrumentation/annotations/src/main/java/io/opentelemetry/auto/instrumentation/traceannotation/WithSpanAdvice.java
instrumentation/annotations/src/main/java/io/opentelemetry/auto/instrumentation/traceannotation/WithSpanAdvice.java
@@ -36,7 +36,10 @@ public class WithSpanAdvice {
@Advice.OnMethodEnter(suppress = Throwable.class)
public static SpanWithScope onEnter(@Advice.Origin final Method method) {
final Span span =
- TRACER.spanBuilder(DECORATE.spanNameForMethodWithAnnotation(method)).startSpan();
+ TRACER
+ .spanBuilder(DECORATE.spanNameForMethodWithAnnotation(method))
+ .setSpanKind(DECORATE.extractSpanKind(method))
+ .startSpan();
DECORATE.afterStart(span);
return new SpanWithScope(span, currentContextWith(span));
}

MODIFY
instrumentation/annotations/src/test/groovy/WithSpanInstrumentationTest.groovy
instrumentation/annotations/src/test/groovy/WithSpanInstrumentationTest.groovy
import io.opentelemetry.auto.test.AgentTestRunner
import io.opentelemetry.auto.test.utils.ConfigUtils
import io.opentelemetry.test.annotation.TracedWithSpan
+import io.opentelemetry.trace.Span
/**
- * This test verifies that auto instrumentation supports {@link io.opentelemetry.contrib.auto.annotations.WithSpan} contrib annotation.
+ * This test verifies that auto instrumentation supports {@link io.opentelemetry.extensions.auto.annotations.WithSpan} contrib annotation.
*/
class WithSpanInstrumentationTest extends AgentTestRunner {
@@ -46,6 +47,7 @@ class WithSpanInstrumentationTest extends AgentTestRunner {
trace(0, 1) {
span(0) {
operationName "TracedWithSpan.otel"
+ spanKind Span.Kind.INTERNAL
parent()
errored false
attributes {
@@ -75,6 +77,27 @@ class WithSpanInstrumentationTest extends AgentTestRunner {
}
}
+ def "should take span kind from annotation"() {
+ setup:
+ new TracedWithSpan().oneOfAKind()
+
+ expect:
+ assertTraces(1) {
+ trace(0, 1) {
+ span(0) {
+ operationName "TracedWithSpan.oneOfAKind"
+ spanKind Span.Kind.PRODUCER
+ parent()
+ errored false
+ attributes {
+ "providerAttr" "Otel"
+ }
+ }
+ }
+ }
+ }
+
+
def "should ignore method excluded by trace.methods.exclude configuration"() {
setup:
new TracedWithSpan().ignored()

MODIFY
instrumentation/annotations/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java
instrumentation/annotations/src/test/java/io/opentelemetry/test/annotation/TracedWithSpan.java
@@ -18,6 +18,7 @@ package io.opentelemetry.test.annotation;
import io.opentelemetry.OpenTelemetry;
import io.opentelemetry.extensions.auto.annotations.WithSpan;
+import io.opentelemetry.trace.Span.Kind;
import io.opentelemetry.trace.Tracer;
public class TracedWithSpan {
@@ -42,4 +43,10 @@ public class TracedWithSpan {
TRACER.getCurrentSpan().setAttribute("providerAttr", "Otel");
return "hello!";
}
+
+ @WithSpan(kind = Kind.PRODUCER)
+ public String oneOfAKind() {
+ TRACER.getCurrentSpan().setAttribute("providerAttr", "Otel");
+ return "hello!";
+ }
}

Add support for WithSpan.kind() (#711)