|
| 1 | +/* |
| 2 | + * Copyright 2017, OpenSkywalking Organization All rights reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + * |
| 16 | + * Project repository: https://github.com/OpenSkywalking/skywalking |
| 17 | + */ |
| 18 | + |
| 19 | +package org.skywalking.apm.plugin.jdbc.postgresql.define; |
| 20 | + |
| 21 | +import net.bytebuddy.description.method.MethodDescription; |
| 22 | +import net.bytebuddy.matcher.ElementMatcher; |
| 23 | +import org.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint; |
| 24 | +import org.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint; |
| 25 | +import org.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine; |
| 26 | +import org.skywalking.apm.agent.core.plugin.match.ClassMatch; |
| 27 | + |
| 28 | +import static net.bytebuddy.matcher.ElementMatchers.named; |
| 29 | +import static net.bytebuddy.matcher.ElementMatchers.takesArguments; |
| 30 | +import static org.skywalking.apm.agent.core.plugin.match.NameMatch.byName; |
| 31 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.CLOSE_METHOD_NAME; |
| 32 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.COMMIT_METHOD_NAME; |
| 33 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.CREATE_STATEMENT_INTERCEPT_CLASS; |
| 34 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.CREATE_STATEMENT_METHOD_NAME; |
| 35 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_CALL_INTERCEPT_CLASS; |
| 36 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_CALL_METHOD_NAME; |
| 37 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_STATEMENT_INTERCEPT_CLASS; |
| 38 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.PREPARE_STATEMENT_METHOD_NAME; |
| 39 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.RELEASE_SAVE_POINT_METHOD_NAME; |
| 40 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.ROLLBACK_METHOD_NAME; |
| 41 | +import static org.skywalking.apm.plugin.jdbc.define.Constants.SERVICE_METHOD_INTERCEPT_CLASS; |
| 42 | + |
| 43 | +/** |
| 44 | + * {@link Jdbc4ConnectionInstrumentation} intercept the following methods that the class which extend {@link |
| 45 | + * org.postgresql.jdbc4.Jdbc4Connection}. <br/> |
| 46 | + * |
| 47 | + * 1. Enhance <code>prepareStatement</code> by <code>org.skywalking.apm.plugin.jdbc.define.JDBCPrepareStatementInterceptor</code> |
| 48 | + * 2. Enhance <code>prepareCall</code> by |
| 49 | + * <code>org.skywalking.apm.plugin.jdbc.define.JDBCPrepareCallInterceptor</code> |
| 50 | + * 3. Enhance <code>createStatement</code> |
| 51 | + * by <code>org.skywalking.apm.plugin.jdbc.define.JDBCStatementInterceptor</code> |
| 52 | + * 4. Enhance <code>commit, rollback, close, releaseSavepoint</code> by <code>org.skywalking.apm.plugin.jdbc.define.ConnectionServiceMethodInterceptor</code> |
| 53 | + * |
| 54 | + * @author zhangxin |
| 55 | + */ |
| 56 | +public class Jdbc4ConnectionInstrumentation extends ClassInstanceMethodsEnhancePluginDefine { |
| 57 | + |
| 58 | + public static final String ENHANCE_CLASS = "org.postgresql.jdbc4.Jdbc4Connection"; |
| 59 | + |
| 60 | + @Override protected ConstructorInterceptPoint[] getConstructorsInterceptPoints() { |
| 61 | + return new ConstructorInterceptPoint[0]; |
| 62 | + } |
| 63 | + |
| 64 | + @Override protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() { |
| 65 | + return new InstanceMethodsInterceptPoint[] { |
| 66 | + new InstanceMethodsInterceptPoint() { |
| 67 | + @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { |
| 68 | + return named(PREPARE_STATEMENT_METHOD_NAME).and(takesArguments(4)); |
| 69 | + } |
| 70 | + |
| 71 | + @Override public String getMethodsInterceptor() { |
| 72 | + return PREPARE_STATEMENT_INTERCEPT_CLASS; |
| 73 | + } |
| 74 | + |
| 75 | + @Override public boolean isOverrideArgs() { |
| 76 | + return false; |
| 77 | + } |
| 78 | + }, |
| 79 | + new InstanceMethodsInterceptPoint() { |
| 80 | + @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { |
| 81 | + return named(PREPARE_CALL_METHOD_NAME).and(takesArguments(4)); |
| 82 | + } |
| 83 | + |
| 84 | + @Override public String getMethodsInterceptor() { |
| 85 | + return PREPARE_CALL_INTERCEPT_CLASS; |
| 86 | + } |
| 87 | + |
| 88 | + @Override public boolean isOverrideArgs() { |
| 89 | + return false; |
| 90 | + } |
| 91 | + }, |
| 92 | + new InstanceMethodsInterceptPoint() { |
| 93 | + @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { |
| 94 | + return named(CREATE_STATEMENT_METHOD_NAME).and(takesArguments(3)); |
| 95 | + } |
| 96 | + |
| 97 | + @Override public String getMethodsInterceptor() { |
| 98 | + return CREATE_STATEMENT_INTERCEPT_CLASS; |
| 99 | + } |
| 100 | + |
| 101 | + @Override public boolean isOverrideArgs() { |
| 102 | + return false; |
| 103 | + } |
| 104 | + }, |
| 105 | + new InstanceMethodsInterceptPoint() { |
| 106 | + @Override public ElementMatcher<MethodDescription> getMethodsMatcher() { |
| 107 | + return named(COMMIT_METHOD_NAME).or(named(ROLLBACK_METHOD_NAME)).or(named(CLOSE_METHOD_NAME)).or(named(RELEASE_SAVE_POINT_METHOD_NAME)); |
| 108 | + } |
| 109 | + |
| 110 | + @Override public String getMethodsInterceptor() { |
| 111 | + return SERVICE_METHOD_INTERCEPT_CLASS; |
| 112 | + } |
| 113 | + |
| 114 | + @Override public boolean isOverrideArgs() { |
| 115 | + return false; |
| 116 | + } |
| 117 | + } |
| 118 | + }; |
| 119 | + } |
| 120 | + |
| 121 | + @Override protected ClassMatch enhanceClass() { |
| 122 | + return byName(ENHANCE_CLASS); |
| 123 | + } |
| 124 | +} |
0 commit comments