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

This commit is contained in:
Nikita Salnikov-Tarnovski 2020-07-16 17:14:12 +03:00 committed by GitHub
parent 5bab5b9d7d
commit 0e2a85c6dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 2 deletions

View File

@ -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;
}
}

View File

@ -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));
}

View File

@ -17,9 +17,10 @@
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()

View File

@ -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!";
}
}