Update play-2.6 instrumentation to support Play version 2.8.x (DataDog/dd-trace-java#1521)
MODIFY
instrumentation/play/play-2.6/play-2.6.gradle
instrumentation/play/play-2.6/play-2.6.gradle
@@ -20,13 +20,13 @@ muzzle {
pass {
group = 'com.typesafe.play'
module = 'play_2.12'
- versions = '[2.6.0,2.8.0)'
+ versions = "[$playVersion,)"
assertInverse = true
}
pass {
group = 'com.typesafe.play'
module = 'play_2.13'
- versions = '[2.6.0,2.8.0)'
+ versions = "[$playVersion,)"
assertInverse = true
}
}

MODIFY
instrumentation/play/play-2.6/src/main/java8/io/opentelemetry/auto/instrumentation/play/v2_6/PlayHttpServerDecorator.java
instrumentation/play/play-2.6/src/main/java8/io/opentelemetry/auto/instrumentation/play/v2_6/PlayHttpServerDecorator.java
@@ -22,6 +22,7 @@ import io.opentelemetry.trace.Span;
import io.opentelemetry.trace.Status;
import io.opentelemetry.trace.Tracer;
import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
import java.lang.reflect.UndeclaredThrowableException;
import java.net.URI;
import java.net.URISyntaxException;
@@ -29,6 +30,7 @@ import lombok.extern.slf4j.Slf4j;
import play.api.mvc.Request;
import play.api.mvc.Result;
import play.api.routing.HandlerDef;
+import play.libs.typedmap.TypedKey;
import play.routing.Router;
import scala.Option;
@@ -39,6 +41,25 @@ public class PlayHttpServerDecorator extends HttpServerDecorator<Request, Reques
public static final Tracer TRACER =
OpenTelemetry.getTracerProvider().get("io.opentelemetry.auto.play-2.6");
+ private static final Method typedKeyGetUnderlying;
+
+ static {
+ Method typedKeyGetUnderlyingCheck = null;
+ try {
+ // This method was added in Play 2.6.8
+ typedKeyGetUnderlyingCheck = TypedKey.class.getMethod("asScala");
+ } catch (final NoSuchMethodException ignored) {
+ }
+ // Fallback
+ if (typedKeyGetUnderlyingCheck == null) {
+ try {
+ typedKeyGetUnderlyingCheck = TypedKey.class.getMethod("underlying");
+ } catch (final NoSuchMethodException ignored) {
+ }
+ }
+ typedKeyGetUnderlying = typedKeyGetUnderlyingCheck;
+ }
+
@Override
protected String method(final Request httpRequest) {
return httpRequest.method();
@@ -70,9 +91,19 @@ public class PlayHttpServerDecorator extends HttpServerDecorator<Request, Reques
if (request != null) {
// more about routes here:
// https://github.com/playframework/playframework/blob/master/documentation/manual/releases/release26/migration26/Migration26.md
- final Option<HandlerDef> defOption =
- request.attrs().get(Router.Attrs.HANDLER_DEF.underlying());
- if (!defOption.isEmpty()) {
+ Option<HandlerDef> defOption = null;
+ if (typedKeyGetUnderlying != null) { // Should always be non-null but just to make sure
+ try {
+ defOption =
+ request
+ .attrs()
+ .get(
+ (play.api.libs.typedmap.TypedKey<HandlerDef>)
+ typedKeyGetUnderlying.invoke(Router.Attrs.HANDLER_DEF));
+ } catch (final IllegalAccessException | InvocationTargetException ignored) {
+ }
+ }
+ if (defOption != null && !defOption.isEmpty()) {
final String path = defOption.get().path();
span.updateName(request.method() + " " + path);
}

Update play-2.6 instrumentation to support Play version 2.8.x (DataDog/dd-trace-java#1521)