diff --git a/instrument-modules/user-modules/module-sentinel/pom.xml b/instrument-modules/user-modules/module-sentinel/pom.xml new file mode 100644 index 000000000..13fb94bb7 --- /dev/null +++ b/instrument-modules/user-modules/module-sentinel/pom.xml @@ -0,0 +1,40 @@ + + + + io.shulie.instrument.module + user-modules + 1.0.0 + + 4.0.0 + module-sentinel + ${project.artifactId} ${project.version} + + 2.0.0.0 + + sentinel + + + + + io.shulie.instrument.module + module-pradar-core + ${pradar.core.version} + provided + + + + io.shulie.instrument.simulator + simulator-bootstrap-api + ${simulator.bootstrap.api.version} + provided + + + com.alibaba.csp + sentinel-core + 1.8.6 + provided + + + \ No newline at end of file diff --git a/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/SentinelConstants.java b/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/SentinelConstants.java new file mode 100644 index 000000000..9b73d5100 --- /dev/null +++ b/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/SentinelConstants.java @@ -0,0 +1,28 @@ +/** + * Copyright 2021 Shulie Technology, Co.Ltd + * Email: shulie@shulie.io + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.pamirs.attach.plugin.sentinel; + +import com.pamirs.pradar.MiddlewareType; + +/** + * @author xiaobin.zfb + * @since 2020/8/14 10:06 下午 + */ +public class SentinelConstants { + public final static String PLUGIN_NAME = "sentinel"; + public final static int PLUGIN_TYPE = MiddlewareType.TYPE_LOCAL; + + public final static String DYNAMIC_FIELD_CONTEXT = "context"; +} diff --git a/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/SentinelPlugin.java b/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/SentinelPlugin.java new file mode 100644 index 000000000..6ace1f167 --- /dev/null +++ b/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/SentinelPlugin.java @@ -0,0 +1,49 @@ +/** + * Copyright 2021 Shulie Technology, Co.Ltd + * Email: shulie@shulie.io + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.pamirs.attach.plugin.sentinel; + +import com.pamirs.attach.plugin.sentinel.interceptor.SphUEntryInterceptor; +import com.shulie.instrument.simulator.api.ExtensionModule; +import com.shulie.instrument.simulator.api.ModuleInfo; +import com.shulie.instrument.simulator.api.ModuleLifecycleAdapter; +import com.shulie.instrument.simulator.api.instrument.EnhanceCallback; +import com.shulie.instrument.simulator.api.instrument.InstrumentClass; +import com.shulie.instrument.simulator.api.instrument.InstrumentMethod; +import com.shulie.instrument.simulator.api.listener.Listeners; +import org.kohsuke.MetaInfServices; + +/** + * @author xiaobin.zfb + * @since 2020/8/13 4:10 下午 + */ +@MetaInfServices(ExtensionModule.class) +@ModuleInfo(id = SentinelConstants.PLUGIN_NAME, version = "1.0.0", author = "xiaobin@shulie.io", + description = "sentinel 并发框架") +public class SentinelPlugin extends ModuleLifecycleAdapter implements ExtensionModule { + + @Override + public boolean onActive() throws Throwable { + this.enhanceTemplate.enhance(this, "com.alibaba.csp.sentinel.SphU", new EnhanceCallback() { + @Override + public void doEnhance(InstrumentClass target) { + InstrumentMethod enqueueMethod = target.getDeclaredMethod("entry", "java.lang.String"); + enqueueMethod.addInterceptor(Listeners.of(SphUEntryInterceptor.class)); + } + }); + + return true; + } + +} diff --git a/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/interceptor/SphUEntryInterceptor.java b/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/interceptor/SphUEntryInterceptor.java new file mode 100644 index 000000000..698ae060c --- /dev/null +++ b/instrument-modules/user-modules/module-sentinel/src/main/java/com/pamirs/attach/plugin/sentinel/interceptor/SphUEntryInterceptor.java @@ -0,0 +1,77 @@ +/** + * Copyright 2021 Shulie Technology, Co.Ltd + * Email: shulie@shulie.io + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.pamirs.attach.plugin.sentinel.interceptor; + +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeException; +import com.alibaba.csp.sentinel.slots.block.degrade.DegradeRuleManager; +import com.alibaba.csp.sentinel.slots.block.flow.FlowException; +import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager; + +import com.pamirs.attach.plugin.sentinel.SentinelConstants; +import com.pamirs.pradar.InvokeContext; +import com.pamirs.pradar.MiddlewareType; +import com.pamirs.pradar.Pradar; +import com.pamirs.pradar.ResultCode; +import com.pamirs.pradar.interceptor.AroundInterceptor; +import com.shulie.instrument.simulator.api.listener.ext.Advice; + +/** + * @author xiaobin.zfb|xiaobin@shulie.io + * @since 2021/5/28 6:02 下午 + */ +public class SphUEntryInterceptor extends AroundInterceptor { + + @Override + public void doBefore(Advice advice) throws Throwable { + InvokeContext invokeContext = Pradar.getInvokeContext(); + while (invokeContext != null && !isServer(invokeContext)) { + invokeContext = invokeContext.getParentInvokeContext(); + } + if (invokeContext == null) { + return; + } + String resource = (String)advice.getParameterArray()[0]; + boolean flow = FlowRuleManager.hasConfig(resource); + boolean degrade = DegradeRuleManager.hasConfig(resource); + + invokeContext.putLocalAttribute("sentinel.hasFlowRule", flow ? "1" : "0"); + invokeContext.putLocalAttribute("sentinel.hasDegradeRule", degrade ? "1" : "0"); + } + + @Override + public void doException(Advice advice) throws Throwable { + Throwable e = advice.getThrowable(); + String resource = (String)advice.getParameterArray()[0]; + if (e instanceof DegradeException) { + record(resource, e, "02"); + } + if (e instanceof FlowException) { + record(resource, e, "01"); + } + } + + private static void record(String resource, Throwable e, String number) { + Pradar.startClientInvoke(resource, "entry"); + Pradar.middlewareName(SentinelConstants.PLUGIN_NAME); + Pradar.response(e.getClass().getName()); + Pradar.endClientInvoke(number, SentinelConstants.PLUGIN_TYPE); + } + + private static boolean isServer(InvokeContext invokeContext) { + return invokeContext.getLogType() == Pradar.LOG_TYPE_INVOKE_SERVER + || invokeContext.getLogType() == Pradar.LOG_TYPE_TRACE; + } + +} diff --git a/instrument-modules/user-modules/module-sentinel/src/main/resources/README.md b/instrument-modules/user-modules/module-sentinel/src/main/resources/README.md new file mode 100644 index 000000000..ec49700ee --- /dev/null +++ b/instrument-modules/user-modules/module-sentinel/src/main/resources/README.md @@ -0,0 +1,5 @@ +注意!!!!! +每次更新插件请都更新内容到以下内容, +akka中间件支持模块, +新增模块版本信息,初始版本为1.0.0,README.md为模块更新内容描述文件, + diff --git a/instrument-modules/user-modules/module-sentinel/src/main/resources/module.config b/instrument-modules/user-modules/module-sentinel/src/main/resources/module.config new file mode 100644 index 000000000..55dee3ea6 --- /dev/null +++ b/instrument-modules/user-modules/module-sentinel/src/main/resources/module.config @@ -0,0 +1,12 @@ +module-id=sentinel +#export-class= +#import-class= +#export-package= +import-package=com.pamirs.pradar.* +#export-resource= +#import-resource= +# dependency of module or swither,multi split with comma +dependencies=pradar-core +simulator-version=1.0.0- +module-version=2.0.0.0 +dependencies-info=pradar-core@2.0.0.0 \ No newline at end of file diff --git a/instrument-modules/user-modules/pom.xml b/instrument-modules/user-modules/pom.xml index a7bf7e076..ad4ce19e8 100644 --- a/instrument-modules/user-modules/pom.xml +++ b/instrument-modules/user-modules/pom.xml @@ -106,6 +106,7 @@ module-cus-trace module-activemq module-activemqv2 + module-sentinel