Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JAX-RS: add code.namespace and code.function attributes #2805

Merged
merged 4 commits into from
Apr 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import io.opentelemetry.instrumentation.api.tracer.BaseTracer;
import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
import io.opentelemetry.javaagent.instrumentation.api.ClassHierarchyIterable;
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.Map;
Expand Down Expand Up @@ -51,12 +52,16 @@ public Context startSpan(Context parentContext, Class<?> target, Method method)
Span span = spanBuilder(parentContext, "jax-rs.request", INTERNAL).startSpan();
updateSpanNames(
parentContext, span, ServerSpan.fromContextOrNull(parentContext), target, method);
setCodeAttributes(span, target, method);
trask marked this conversation as resolved.
Show resolved Hide resolved
return parentContext.with(span);
}

public void updateSpanNames(
Context context, Span span, Span serverSpan, Class<?> target, Method method) {
String pathBasedSpanName = ServletContextPath.prepend(context, getPathSpanName(target, method));
String pathBasedSpanName = getPathSpanName(target, method);
if (pathBasedSpanName != null) {
trask marked this conversation as resolved.
Show resolved Hide resolved
pathBasedSpanName = ServletContextPath.prepend(context, pathBasedSpanName);
}
if (serverSpan == null) {
updateSpanName(span, pathBasedSpanName);
} else {
Expand All @@ -66,11 +71,18 @@ public void updateSpanNames(
}

private void updateSpanName(Span span, String spanName) {
if (!spanName.isEmpty()) {
if (spanName != null && !spanName.isEmpty()) {
span.updateName(spanName);
}
}

private void setCodeAttributes(Span span, Class<?> target, Method method) {
span.setAttribute(SemanticAttributes.CODE_NAMESPACE, target.getName());
if (method != null) {
span.setAttribute(SemanticAttributes.CODE_FUNCTION, method.getName());
}
}

/**
* Returns the span name given a JaxRS annotated method. Results are cached so this method can be
* called multiple times without significantly impacting performance.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import static io.opentelemetry.instrumentation.test.utils.ClassUtils.getClassNam
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderServerTrace

import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import javax.ws.rs.DELETE
import javax.ws.rs.GET
import javax.ws.rs.HEAD
Expand All @@ -21,19 +22,22 @@ abstract class JaxRsAnnotationsInstrumentationTest extends AgentInstrumentationS

def "instrumentation can be used as root span and resource is set to METHOD PATH"() {
setup:
new Jax() {
def jax = new Jax() {
@POST
@Path("/a")
void call() {
}
}.call()
}
jax.call()

expect:
assertTraces(1) {
trace(0, 1) {
span(0) {
name "/a"
attributes {
"${SemanticAttributes.CODE_NAMESPACE.key}" jax.getClass().getName()
"${SemanticAttributes.CODE_FUNCTION.key}" "call"
}
}
}
Expand Down Expand Up @@ -61,6 +65,8 @@ abstract class JaxRsAnnotationsInstrumentationTest extends AgentInstrumentationS
name "${className}.call"
childOf span(0)
attributes {
"${SemanticAttributes.CODE_NAMESPACE.key}" obj.getClass().getName()
"${SemanticAttributes.CODE_FUNCTION.key}" "call"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import static io.opentelemetry.api.trace.SpanKind.SERVER
import static io.opentelemetry.instrumentation.test.utils.TraceUtils.runUnderServerTrace

import io.opentelemetry.instrumentation.test.AgentInstrumentationSpecification
import io.opentelemetry.semconv.trace.attributes.SemanticAttributes
import javax.ws.rs.container.ContainerRequestContext
import javax.ws.rs.container.ContainerRequestFilter
import javax.ws.rs.container.PreMatching
Expand Down Expand Up @@ -83,8 +84,15 @@ abstract class JaxRsFilterTest extends AgentInstrumentationSpecification {
span(1) {
childOf span(0)
name controllerName
if (!runsOnServer()) {
if (abortPrematch) {
attributes {
"${SemanticAttributes.CODE_NAMESPACE.key}" "JaxRsFilterTest\$PrematchRequestFilter"
"${SemanticAttributes.CODE_FUNCTION.key}" "filter"
}
} else {
attributes {
"${SemanticAttributes.CODE_NAMESPACE.key}" ~/Resource[$]Test*/
"${SemanticAttributes.CODE_FUNCTION.key}" "hello"
}
}
}
Expand Down Expand Up @@ -135,9 +143,9 @@ abstract class JaxRsFilterTest extends AgentInstrumentationSpecification {
childOf span(0)
name controller1Name
kind INTERNAL
if (!runsOnServer()) {
attributes {
}
attributes {
"${SemanticAttributes.CODE_NAMESPACE.key}" ~/Resource[$]Test*/
"${SemanticAttributes.CODE_FUNCTION.key}" "nested"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ abstract class JaxRsHttpServerTest<S> extends HttpServerTest<S> implements Agent
}
childOf((SpanData) parent)
attributes {
"${SemanticAttributes.CODE_NAMESPACE.key}" "JaxRsTestResource"
"${SemanticAttributes.CODE_FUNCTION.key}" methodName
if (isCancelled) {
"jaxrs.canceled" true
}
Expand Down