|
| 1 | +package io.arex.inst.spring.data.redis; |
| 2 | + |
| 3 | +import static java.util.Arrays.asList; |
| 4 | +import static net.bytebuddy.matcher.ElementMatchers.namedOneOf; |
| 5 | +import io.arex.agent.bootstrap.model.MockResult; |
| 6 | +import io.arex.inst.extension.MethodInstrumentation; |
| 7 | +import io.arex.inst.extension.TypeInstrumentation; |
| 8 | +import io.arex.inst.redis.common.RedisKeyUtil; |
| 9 | +import java.util.List; |
| 10 | +import java.util.Objects; |
| 11 | +import net.bytebuddy.asm.Advice; |
| 12 | +import net.bytebuddy.description.type.TypeDescription; |
| 13 | +import net.bytebuddy.implementation.bytecode.assign.Assigner; |
| 14 | +import net.bytebuddy.matcher.ElementMatcher; |
| 15 | +import org.springframework.data.redis.core.RedisTemplate; |
| 16 | +import org.springframework.data.redis.core.query.SortQuery; |
| 17 | +import org.springframework.data.redis.core.script.RedisScript; |
| 18 | + |
| 19 | +/** |
| 20 | + * RedisTemplateInstrumentation |
| 21 | + */ |
| 22 | +public class RedisTemplateInstrumentation extends TypeInstrumentation { |
| 23 | + |
| 24 | + @Override |
| 25 | + protected ElementMatcher<TypeDescription> typeMatcher() { |
| 26 | + return namedOneOf("org.springframework.data.redis.core.RedisTemplate"); |
| 27 | + } |
| 28 | + |
| 29 | + @Override |
| 30 | + public List<MethodInstrumentation> methodAdvices() { |
| 31 | + return asList(MethodCollector.arg1IsObjectKey(OneKeyAdvice.class.getName()), |
| 32 | + MethodCollector.arg1IsCollectionKey(OneKeyAdvice.class.getName()), |
| 33 | + MethodCollector.arg1AndArg2AreObjectKey(TwoKeysAdvice.class.getName()), |
| 34 | + MethodCollector.execute(ExecuteAdvice.class.getName()), |
| 35 | + MethodCollector.sort(SortAdvice.class.getName()) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + public static class OneKeyAdvice { |
| 40 | + |
| 41 | + @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class, suppress = Throwable.class) |
| 42 | + public static boolean onEnter(@Advice.This RedisTemplate template, |
| 43 | + @Advice.Origin("#m") String methodName, |
| 44 | + @Advice.Argument(0) Object key, |
| 45 | + @Advice.Local("mockResult") MockResult mockResult) { |
| 46 | + mockResult = RedisTemplateProvider.methodOnEnter(Objects.toString(template.getConnectionFactory()), |
| 47 | + methodName, RedisKeyUtil.generate(key)); |
| 48 | + return mockResult != null && mockResult.notIgnoreMockResult(); |
| 49 | + } |
| 50 | + |
| 51 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 52 | + public static void onExit(@Advice.This RedisTemplate template, |
| 53 | + @Advice.Origin("#m") String methodName, |
| 54 | + @Advice.Argument(0) Object key, |
| 55 | + @Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object result, |
| 56 | + @Advice.Thrown(readOnly = false, typing = Assigner.Typing.DYNAMIC) Throwable throwable, |
| 57 | + @Advice.Local("mockResult") MockResult mockResult) { |
| 58 | + if (mockResult != null && mockResult.notIgnoreMockResult()) { |
| 59 | + if (mockResult.getThrowable() != null) { |
| 60 | + throwable = mockResult.getThrowable(); |
| 61 | + } else { |
| 62 | + result = mockResult.getResult(); |
| 63 | + } |
| 64 | + return; |
| 65 | + } |
| 66 | + RedisTemplateProvider.methodOnExit(Objects.toString(template.getConnectionFactory()), methodName, |
| 67 | + RedisKeyUtil.generate(key), result, throwable); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + public static class TwoKeysAdvice { |
| 72 | + |
| 73 | + @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class, suppress = Throwable.class) |
| 74 | + public static boolean onEnter(@Advice.This RedisTemplate template, |
| 75 | + @Advice.Origin("#m") String methodName, |
| 76 | + @Advice.Argument(0) Object key, |
| 77 | + @Advice.Argument(1) Object otherKey, |
| 78 | + @Advice.Local("mockResult") MockResult mockResult) { |
| 79 | + mockResult = RedisTemplateProvider.methodOnEnter(Objects.toString(template.getConnectionFactory()), |
| 80 | + methodName, RedisKeyUtil.generate(key, otherKey)); |
| 81 | + return mockResult != null && mockResult.notIgnoreMockResult(); |
| 82 | + } |
| 83 | + |
| 84 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 85 | + public static void onExit(@Advice.This RedisTemplate template, |
| 86 | + @Advice.Origin("#m") String methodName, |
| 87 | + @Advice.Argument(0) Object key, |
| 88 | + @Advice.Argument(1) Object otherKey, |
| 89 | + @Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object result, |
| 90 | + @Advice.Thrown(readOnly = false, typing = Assigner.Typing.DYNAMIC) Throwable throwable, |
| 91 | + @Advice.Local("mockResult") MockResult mockResult) { |
| 92 | + if (mockResult != null && mockResult.notIgnoreMockResult()) { |
| 93 | + if (mockResult.getThrowable() != null) { |
| 94 | + throwable = mockResult.getThrowable(); |
| 95 | + } else { |
| 96 | + result = mockResult.getResult(); |
| 97 | + } |
| 98 | + return; |
| 99 | + } |
| 100 | + RedisTemplateProvider.methodOnExit(Objects.toString(template.getConnectionFactory()), methodName, |
| 101 | + RedisKeyUtil.generate(key, otherKey), result, throwable); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + |
| 106 | + public static class ExecuteAdvice { |
| 107 | + |
| 108 | + @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class, suppress = Throwable.class) |
| 109 | + public static boolean onEnter(@Advice.This RedisTemplate template, |
| 110 | + @Advice.Origin("#m") String methodName, |
| 111 | + @Advice.AllArguments Object[] args, |
| 112 | + @Advice.Local("mockResult") MockResult mockResult) { |
| 113 | + List keys = args.length > 4 && args[3] instanceof List ? (List) args[3] : (List) args[1]; |
| 114 | + RedisScript redisScript = (RedisScript) args[0]; |
| 115 | + mockResult = RedisTemplateProvider.methodOnEnter(Objects.toString(template.getConnectionFactory()), |
| 116 | + methodName, RedisKeyUtil.generate(redisScript.getScriptAsString(), keys)); |
| 117 | + return mockResult != null && mockResult.notIgnoreMockResult(); |
| 118 | + } |
| 119 | + |
| 120 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 121 | + public static void onExit(@Advice.This RedisTemplate template, |
| 122 | + @Advice.Origin("#m") String methodName, |
| 123 | + @Advice.AllArguments Object[] args, |
| 124 | + @Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object result, |
| 125 | + @Advice.Thrown(readOnly = false, typing = Assigner.Typing.DYNAMIC) Throwable throwable, |
| 126 | + @Advice.Local("mockResult") MockResult mockResult) { |
| 127 | + if (mockResult != null && mockResult.notIgnoreMockResult()) { |
| 128 | + if (mockResult.getThrowable() != null) { |
| 129 | + throwable = mockResult.getThrowable(); |
| 130 | + } else { |
| 131 | + result = mockResult.getResult(); |
| 132 | + } |
| 133 | + return; |
| 134 | + } |
| 135 | + RedisScript redisScript = (RedisScript) args[0]; |
| 136 | + List keys = args.length > 4 && args[3] instanceof List ? (List) args[3] : (List) args[1]; |
| 137 | + RedisTemplateProvider.methodOnExit(Objects.toString(template.getConnectionFactory()), methodName, |
| 138 | + RedisKeyUtil.generate(redisScript.getScriptAsString(), keys), result, throwable); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + public static class SortAdvice { |
| 143 | + |
| 144 | + @Advice.OnMethodEnter(skipOn = Advice.OnNonDefaultValue.class, suppress = Throwable.class) |
| 145 | + public static boolean onEnter(@Advice.This RedisTemplate template, |
| 146 | + @Advice.Origin("#m") String methodName, |
| 147 | + @Advice.Argument(0) SortQuery sortQuery, |
| 148 | + @Advice.Local("mockResult") MockResult mockResult) { |
| 149 | + mockResult = RedisTemplateProvider.methodOnEnter(Objects.toString(template.getConnectionFactory()), |
| 150 | + methodName, RedisKeyUtil.generate(sortQuery)); |
| 151 | + return mockResult != null && mockResult.notIgnoreMockResult(); |
| 152 | + } |
| 153 | + |
| 154 | + @Advice.OnMethodExit(onThrowable = Throwable.class, suppress = Throwable.class) |
| 155 | + public static void onExit(@Advice.This RedisTemplate template, |
| 156 | + @Advice.Origin("#m") String methodName, |
| 157 | + @Advice.Argument(0) SortQuery sortQuery, |
| 158 | + @Advice.Return(readOnly = false, typing = Assigner.Typing.DYNAMIC) Object result, |
| 159 | + @Advice.Thrown(readOnly = false, typing = Assigner.Typing.DYNAMIC) Throwable throwable, |
| 160 | + @Advice.Local("mockResult") MockResult mockResult) { |
| 161 | + if (mockResult != null && mockResult.notIgnoreMockResult()) { |
| 162 | + if (mockResult.getThrowable() != null) { |
| 163 | + throwable = mockResult.getThrowable(); |
| 164 | + } else { |
| 165 | + result = mockResult.getResult(); |
| 166 | + } |
| 167 | + return; |
| 168 | + } |
| 169 | + RedisTemplateProvider.methodOnExit(Objects.toString(template.getConnectionFactory()), methodName, |
| 170 | + RedisKeyUtil.generate(sortQuery), result, throwable); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments