Remove deprecated ServerSpanNaming.updateSource method (#4327)

* Remove deprecated ServerSpanNaming.updateSource method
This commit is contained in:
Nikita Salnikov-Tarnovski 2021-10-12 13:45:18 +03:00 committed by GitHub
parent 4031f9611a
commit 55f987613a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
10 changed files with 82 additions and 91 deletions

View File

@ -102,19 +102,6 @@ public final class ServerSpanNaming {
} }
} }
// TODO (trask) migrate the one usage (ServletHttpServerTracer) to ServerSpanNaming.init() once we
// migrate to new Instrumenters (see
// https://github.com/open-telemetry/opentelemetry-java-instrumentation/pull/2814#discussion_r617351334
// for the challenge with doing this now in the current Tracer structure, at least without some
// bigger changes, which we want to avoid in the Tracers as they are already deprecated)
@Deprecated
public static void updateSource(Context context, Source source) {
ServerSpanNaming serverSpanNaming = context.get(CONTEXT_KEY);
if (serverSpanNaming != null && source.order > serverSpanNaming.updatedBySource.order) {
serverSpanNaming.updatedBySource = source;
}
}
private boolean isBetterName(String name) { private boolean isBetterName(String name) {
return name.length() > nameLength; return name.length() > nameLength;
} }
@ -125,7 +112,10 @@ public final class ServerSpanNaming {
// filter that is called // filter that is called
FILTER(2, /* useFirst= */ false), FILTER(2, /* useFirst= */ false),
SERVLET(3), SERVLET(3),
CONTROLLER(4); CONTROLLER(4),
// Some frameworks, e.g. JaxRS, allow for nested controller/paths and we want to select the
// longest one
NESTED_CONTROLLER(5, false);
private final int order; private final int order;
private final boolean useFirst; private final boolean useFirst;

View File

@ -13,8 +13,8 @@ import org.checkerframework.checker.nullness.qual.Nullable;
* Helper container for storing context path for jax-rs requests. Jax-rs context path is the path * Helper container for storing context path for jax-rs requests. Jax-rs context path is the path
* where jax-rs servlet is mapped or the value of ApplicationPath annotation. Span name is built by * where jax-rs servlet is mapped or the value of ApplicationPath annotation. Span name is built by
* combining servlet context path from {@code * combining servlet context path from {@code
* io.opentelemetry.instrumentation.api.servlet.ServletContextPath} jax-rs context path and the Path * io.opentelemetry.instrumentation.api.servlet.ServletContextPath}, jax-rs context path and the
* annotation from called method or class. * Path annotation from called method or class.
*/ */
public final class JaxrsContextPath { public final class JaxrsContextPath {
private static final ContextKey<String> CONTEXT_KEY = private static final ContextKey<String> CONTEXT_KEY =

View File

@ -40,7 +40,7 @@ public class CxfJaxRsInvokerInstrumentation implements TypeInstrumentation {
@Advice.OnMethodEnter(suppress = Throwable.class) @Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter( public static void onEnter(
@Advice.Argument(0) Exchange exchange, @Advice.Local("otelScope") Scope scope) { @Advice.Argument(0) Exchange exchange, @Advice.Local("otelScope") Scope scope) {
Context context = CxfTracingUtil.updateServerSpanName(exchange); Context context = CxfSpanName.INSTANCE.updateServerSpanName(exchange);
if (context != null) { if (context != null) {
scope = context.makeCurrent(); scope = context.makeCurrent();
} }

View File

@ -7,41 +7,38 @@ package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;
import static io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0.JaxrsPathUtil.normalizePath; import static io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0.JaxrsPathUtil.normalizePath;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context; import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNameSupplier;
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming; import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath; import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
import io.opentelemetry.javaagent.bootstrap.jaxrs.JaxrsContextPath; import io.opentelemetry.javaagent.bootstrap.jaxrs.JaxrsContextPath;
import org.apache.cxf.jaxrs.model.ClassResourceInfo; import org.apache.cxf.jaxrs.model.ClassResourceInfo;
import org.apache.cxf.jaxrs.model.OperationResourceInfo; import org.apache.cxf.jaxrs.model.OperationResourceInfo;
import org.apache.cxf.jaxrs.model.URITemplate; import org.apache.cxf.jaxrs.model.URITemplate;
import org.apache.cxf.message.Exchange; import org.apache.cxf.message.Exchange;
public final class CxfTracingUtil { public final class CxfSpanName implements ServerSpanNameSupplier<String> {
private CxfTracingUtil() {} public static final CxfSpanName INSTANCE = new CxfSpanName();
public static Context updateServerSpanName(Exchange exchange) { public Context updateServerSpanName(Exchange exchange) {
Context context = Context.current(); Context context = Context.current();
Span serverSpan = ServerSpan.fromContextOrNull(context); String jaxrsName = calculateJaxrsName(context, exchange);
if (serverSpan == null) {
return null;
}
ServerSpanNaming.updateServerSpanName(
context, ServerSpanNaming.Source.NESTED_CONTROLLER, this, jaxrsName);
return JaxrsContextPath.init(context, jaxrsName);
}
private static String calculateJaxrsName(Context context, Exchange exchange) {
OperationResourceInfo ori = exchange.get(OperationResourceInfo.class); OperationResourceInfo ori = exchange.get(OperationResourceInfo.class);
ClassResourceInfo cri = ori.getClassResourceInfo(); ClassResourceInfo cri = ori.getClassResourceInfo();
String name = getName(cri.getURITemplate(), ori.getURITemplate()); String name = getName(cri.getURITemplate(), ori.getURITemplate());
if (name.isEmpty()) { if (name.isEmpty()) {
return null; return null;
} }
return JaxrsContextPath.prepend(context, name);
serverSpan.updateName(
ServletContextPath.prepend(context, JaxrsContextPath.prepend(context, name)));
// mark span name as updated from controller to avoid JaxRsAnnotationsTracer updating it
ServerSpanNaming.updateSource(context, ServerSpanNaming.Source.CONTROLLER);
return JaxrsContextPath.init(context, JaxrsContextPath.prepend(context, name));
} }
private static String getName(URITemplate classTemplate, URITemplate operationTemplate) { private static String getName(URITemplate classTemplate, URITemplate operationTemplate) {
@ -58,4 +55,11 @@ public final class CxfTracingUtil {
return normalizePath(uriTemplate.getValue()); return normalizePath(uriTemplate.getValue());
} }
@Override
public String get(Context context, String jaxrsName) {
return ServletContextPath.prepend(context, jaxrsName);
}
private CxfSpanName() {}
} }

View File

@ -41,7 +41,7 @@ public class JerseyResourceMethodDispatcherInstrumentation implements TypeInstru
@Advice.OnMethodEnter(suppress = Throwable.class) @Advice.OnMethodEnter(suppress = Throwable.class)
public static void onEnter(@Advice.Argument(1) Request request) { public static void onEnter(@Advice.Argument(1) Request request) {
JerseyTracingUtil.updateServerSpanName(request); JerseySpanName.INSTANCE.updateServerSpanName(request);
} }
} }
} }

View File

@ -7,41 +7,36 @@ package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;
import static io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0.JaxrsPathUtil.normalizePath; import static io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0.JaxrsPathUtil.normalizePath;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context; import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNameSupplier;
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming; import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath; import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
import io.opentelemetry.javaagent.bootstrap.jaxrs.JaxrsContextPath; import io.opentelemetry.javaagent.bootstrap.jaxrs.JaxrsContextPath;
import java.util.Optional;
import javax.ws.rs.core.Request; import javax.ws.rs.core.Request;
import javax.ws.rs.core.UriInfo; import javax.ws.rs.core.UriInfo;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.glassfish.jersey.server.ContainerRequest; import org.glassfish.jersey.server.ContainerRequest;
import org.glassfish.jersey.server.ExtendedUriInfo; import org.glassfish.jersey.server.ExtendedUriInfo;
public class JerseyTracingUtil { public class JerseySpanName implements ServerSpanNameSupplier<Request> {
public static void updateServerSpanName(Request request) { public static final JerseySpanName INSTANCE = new JerseySpanName();
public void updateServerSpanName(Request request) {
Context context = Context.current(); Context context = Context.current();
Span serverSpan = ServerSpan.fromContextOrNull(context); ServerSpanNaming.updateServerSpanName(
if (serverSpan == null) { context, ServerSpanNaming.Source.NESTED_CONTROLLER, this, request);
return; }
}
@Override
public @Nullable String get(Context context, Request request) {
ContainerRequest containerRequest = (ContainerRequest) request; ContainerRequest containerRequest = (ContainerRequest) request;
UriInfo uriInfo = containerRequest.getUriInfo(); UriInfo uriInfo = containerRequest.getUriInfo();
ExtendedUriInfo extendedUriInfo = (ExtendedUriInfo) uriInfo; ExtendedUriInfo extendedUriInfo = (ExtendedUriInfo) uriInfo;
Optional<String> name = return extendedUriInfo.getMatchedTemplates().stream()
extendedUriInfo.getMatchedTemplates().stream() .map((uriTemplate) -> normalizePath(uriTemplate.getTemplate()))
.map((uriTemplate) -> normalizePath(uriTemplate.getTemplate())) .reduce((a, b) -> b + a)
.reduce((a, b) -> b + a); .map(s -> ServletContextPath.prepend(context, JaxrsContextPath.prepend(context, s)))
if (!name.isPresent()) { .orElse(null);
return;
}
serverSpan.updateName(
ServletContextPath.prepend(context, JaxrsContextPath.prepend(context, name.get())));
// mark span name as updated from controller to avoid JaxRsAnnotationsTracer updating it
ServerSpanNaming.updateSource(context, ServerSpanNaming.Source.CONTROLLER);
} }
} }

View File

@ -49,7 +49,7 @@ public class ResteasyResourceLocatorInvokerInstrumentation implements TypeInstru
String name = String name =
VirtualField.find(ResourceLocatorInvoker.class, String.class).get(resourceInvoker); VirtualField.find(ResourceLocatorInvoker.class, String.class).get(resourceInvoker);
ResteasyTracingUtil.updateServerSpanName(currentContext, name); ResteasySpanName.INSTANCE.updateServerSpanName(currentContext, name);
// subresource locator returns a resources class that may have @Path annotations // subresource locator returns a resources class that may have @Path annotations
// append current path to jax-rs context path so that it would be present in the final path // append current path to jax-rs context path so that it would be present in the final path

View File

@ -41,7 +41,7 @@ public class ResteasyResourceMethodInvokerInstrumentation implements TypeInstrum
String name = String name =
VirtualField.find(ResourceMethodInvoker.class, String.class).get(resourceInvoker); VirtualField.find(ResourceMethodInvoker.class, String.class).get(resourceInvoker);
ResteasyTracingUtil.updateServerSpanName(Java8BytecodeBridge.currentContext(), name); ResteasySpanName.INSTANCE.updateServerSpanName(Java8BytecodeBridge.currentContext(), name);
} }
} }
} }

View File

@ -0,0 +1,36 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;
import static io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming.Source.NESTED_CONTROLLER;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNameSupplier;
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
import io.opentelemetry.javaagent.bootstrap.jaxrs.JaxrsContextPath;
import org.checkerframework.checker.nullness.qual.Nullable;
public final class ResteasySpanName implements ServerSpanNameSupplier<String> {
public static final ResteasySpanName INSTANCE = new ResteasySpanName();
public void updateServerSpanName(Context context, String name) {
if (name != null) {
ServerSpanNaming.updateServerSpanName(context, NESTED_CONTROLLER, this, name);
}
}
@Override
public @Nullable String get(Context context, String name) {
if (name.isEmpty()) {
return null;
}
return ServletContextPath.prepend(context, JaxrsContextPath.prepend(context, name));
}
private ResteasySpanName() {}
}

View File

@ -1,34 +0,0 @@
/*
* Copyright The OpenTelemetry Authors
* SPDX-License-Identifier: Apache-2.0
*/
package io.opentelemetry.javaagent.instrumentation.jaxrs.v2_0;
import io.opentelemetry.api.trace.Span;
import io.opentelemetry.context.Context;
import io.opentelemetry.instrumentation.api.servlet.ServerSpanNaming;
import io.opentelemetry.instrumentation.api.servlet.ServletContextPath;
import io.opentelemetry.instrumentation.api.tracer.ServerSpan;
import io.opentelemetry.javaagent.bootstrap.jaxrs.JaxrsContextPath;
public final class ResteasyTracingUtil {
private ResteasyTracingUtil() {}
public static void updateServerSpanName(Context context, String name) {
if (name == null || name.isEmpty()) {
return;
}
Span serverSpan = ServerSpan.fromContextOrNull(context);
if (serverSpan == null) {
return;
}
serverSpan.updateName(
ServletContextPath.prepend(context, JaxrsContextPath.prepend(context, name)));
// mark span name as updated from controller to avoid JaxRsAnnotationsTracer updating it
ServerSpanNaming.updateSource(context, ServerSpanNaming.Source.CONTROLLER);
}
}