/rule/akka-http/src/main/java/io/opentracing/contrib/specialagent/rule/akka/http/AkkaHttpServerAgentRule.java

https://github.com/opentracing-contrib/java-specialagent · Java · 61 lines · 41 code · 6 blank · 14 comment · 2 complexity · 6ded20bac61d1848502c3a479a2aab70 MD5 · raw file

  1. /* Copyright 2019 The OpenTracing Authors
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. package io.opentracing.contrib.specialagent.rule.akka.http;
  16. import static net.bytebuddy.matcher.ElementMatchers.*;
  17. import io.opentracing.contrib.specialagent.AgentRule;
  18. import net.bytebuddy.agent.builder.AgentBuilder;
  19. import net.bytebuddy.agent.builder.AgentBuilder.Transformer;
  20. import net.bytebuddy.asm.Advice;
  21. import net.bytebuddy.description.type.TypeDescription;
  22. import net.bytebuddy.dynamic.DynamicType.Builder;
  23. import net.bytebuddy.implementation.bytecode.assign.Assigner.Typing;
  24. import net.bytebuddy.utility.JavaModule;
  25. public class AkkaHttpServerAgentRule extends AgentRule {
  26. @Override
  27. public AgentBuilder buildAgentChainedGlobal1(final AgentBuilder builder) {
  28. return builder
  29. .type(hasSuperType(named("akka.http.javadsl.Http")))
  30. .transform(new Transformer() {
  31. @Override
  32. public Builder<?> transform(final Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
  33. return builder.visit(advice(typeDescription).to(SyncHandler.class).on(named("bindAndHandleSync").and(takesArgument(0, named("akka.japi.Function")))));
  34. }})
  35. .transform(new Transformer() {
  36. @Override
  37. public Builder<?> transform(final Builder<?> builder, final TypeDescription typeDescription, final ClassLoader classLoader, final JavaModule module) {
  38. return builder.visit(advice(typeDescription).to(AsyncHandler.class).on(named("bindAndHandleAsync").and(takesArgument(0, named("akka.japi.Function")))));
  39. }});
  40. }
  41. public static class SyncHandler {
  42. @Advice.OnMethodEnter
  43. public static void enter(final @ClassName String className, final @Advice.Origin String origin, @Advice.Argument(value = 0, readOnly = false, typing = Typing.DYNAMIC) Object arg0) {
  44. if (isAllowed(className, origin))
  45. arg0 = AkkaAgentIntercept.bindAndHandleSync(arg0);
  46. }
  47. }
  48. public static class AsyncHandler {
  49. @Advice.OnMethodEnter
  50. public static void enter(final @ClassName String className, final @Advice.Origin String origin, @Advice.Argument(value = 0, readOnly = false, typing = Typing.DYNAMIC) Object arg0) {
  51. if (isAllowed(className, origin))
  52. arg0 = AkkaAgentIntercept.bindAndHandleAsync(arg0);
  53. }
  54. }
  55. }