-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
89 changes: 89 additions & 0 deletions
89
...main/scala/de/innfactory/smithy4play/instrumentation/SmithyPlayRouterInstrumentation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package de.innfactory.smithy4play.instrumentation; | ||
|
||
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentContext; | ||
import static io.opentelemetry.javaagent.bootstrap.Java8BytecodeBridge.currentSpan; | ||
import static io.opentelemetry.javaagent.extension.matcher.AgentElementMatchers.*; | ||
import static net.bytebuddy.matcher.ElementMatchers.named; | ||
import static net.bytebuddy.matcher.ElementMatchers.returns; | ||
import static net.bytebuddy.matcher.ElementMatchers.takesArgument; | ||
|
||
import io.opentelemetry.api.GlobalOpenTelemetry; | ||
import io.opentelemetry.api.trace.SpanBuilder; | ||
import io.opentelemetry.context.Context; | ||
import io.opentelemetry.context.Scope; | ||
import io.opentelemetry.api.trace.Span; | ||
import io.opentelemetry.instrumentation.api.instrumenter.Instrumenter; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeInstrumentation; | ||
import io.opentelemetry.javaagent.extension.instrumentation.TypeTransformer; | ||
import net.bytebuddy.asm.Advice; | ||
import net.bytebuddy.description.type.TypeDescription; | ||
import net.bytebuddy.matcher.ElementMatcher; | ||
|
||
public class SmithyPlayRouterInstrumentation implements TypeInstrumentation { | ||
|
||
|
||
@Override | ||
public ElementMatcher<ClassLoader> classLoaderOptimization() { | ||
return hasClassesNamed("de.innfactory.smithy4play.routing.internal.Smithy4PlayRouterHandler"); | ||
} | ||
|
||
@Override | ||
public ElementMatcher<TypeDescription> typeMatcher() { | ||
// System.out.println("- - - SmithyPlayRouterInstrumentation typeMatcher - - -"); | ||
return named("de.innfactory.smithy4play.routing.internal.Smithy4PlayRouterHandler"); | ||
} | ||
|
||
@Override | ||
public void transform(TypeTransformer transformer) { | ||
transformer.applyAdviceToMethod( | ||
named("handleForInstrument"), | ||
this.getClass().getName() + "$ApplyAdvice"); | ||
//System.out.println("- - - SmithyPlayRouterInstrumentation transform - - -"); | ||
} | ||
|
||
@SuppressWarnings("unused") | ||
public static class ApplyAdvice { | ||
|
||
@Advice.OnMethodEnter() | ||
public static void onEnter( | ||
@Advice.Argument(0) java.lang.String path, | ||
@Advice.Argument(1) java.lang.String method, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
//System.out.println("ApplyAdvice Start applyHandler"); | ||
Context parentContext = currentContext(); | ||
Span mySpan = GlobalOpenTelemetry.get().getTracer("smithy4play").spanBuilder("smithy4play.Smithy4PlayRouter").startSpan(); | ||
context = mySpan.storeInContext(parentContext); | ||
Scope myScope = mySpan.makeCurrent(); | ||
scope = myScope; | ||
} | ||
|
||
@Advice.OnMethodExit(onThrowable = Throwable.class) | ||
public static void stopTraceOnResponse( | ||
@Advice.This Object currentClass, | ||
@Advice.Thrown Throwable throwable, | ||
@Advice.Argument(0) java.lang.String path, | ||
@Advice.Argument(1) java.lang.String method, | ||
@Advice.Return(readOnly = false) java.lang.String returnValue, | ||
@Advice.Local("otelContext") Context context, | ||
@Advice.Local("otelScope") Scope scope) { | ||
//System.out.println("ApplyAdvice End applyHandler"); | ||
Span mySpan = currentSpan(); | ||
if (scope != null) { | ||
scope.close(); | ||
} | ||
if (mySpan != null) { | ||
String routeName = path; | ||
String methodName = method; | ||
mySpan.setAttribute("class", currentClass.getClass().getName()); | ||
mySpan.updateName(methodName + " " + routeName); | ||
mySpan.setAttribute("http.request.method", methodName); | ||
mySpan.setAttribute("http.request.url", routeName); | ||
mySpan.end(); | ||
} | ||
if (throwable != null) { | ||
throwable.printStackTrace(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
.../src/main/scala/de/innfactory/smithy4play/routing/internal/Smithy4PlayRouterHandler.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package de.innfactory.smithy4play.routing.internal | ||
|
||
import cats.data.{EitherT, Kleisli} | ||
import de.innfactory.smithy4play.{ContextRoute, RoutingResult, logger} | ||
import de.innfactory.smithy4play.codecs.Codec | ||
import de.innfactory.smithy4play.routing.context.RoutingContextBase | ||
import de.innfactory.smithy4play.routing.middleware.Middleware | ||
import de.innfactory.smithy4play.routing.internal.{deconstructPath, getSmithy4sHttpMethod, toSmithy4sHttpRequest, toSmithy4sHttpUri} | ||
import de.innfactory.smithy4play.telemetry.Telemetry | ||
import play.api.mvc.* | ||
import play.api.routing.Router.Routes | ||
import smithy4s.* | ||
|
||
class Smithy4PlayRouterHandler(router: PartialFunction[RequestHeader, RequestWrapped => ContextRoute[Result]]) { | ||
|
||
def applyHandler(v1: RequestHeader, serviceHints: Hints): Request[RawBuffer] => RoutingResult[Result] = { request => | ||
val ctx: RoutingContextBase = RoutingContextBase.fromRequest(request,serviceHints, v1) | ||
handleForInstrument(v1.path, v1.method) | ||
router.apply(v1)(RequestWrapped(request, Map.empty)).run(ctx) | ||
} | ||
|
||
def handleForInstrument(path: String, method: String): String = { | ||
logger.debug(s"[${this.getClass.getName}] handle route for ${method} ${path}") | ||
"" | ||
} | ||
|
||
def isDefinedAtHandler(v1: RequestHeader): Boolean = { | ||
val isdefined = router.isDefinedAt(v1) | ||
if (!isdefined) logger.debug(s"[${this.getClass.getName}] router is not defined at ${isdefined} ${v1}") | ||
isdefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters