Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sentinel #589

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions instrument-modules/user-modules/module-sentinel/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>io.shulie.instrument.module</groupId>
<artifactId>user-modules</artifactId>
<version>1.0.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>module-sentinel</artifactId>
<name>${project.artifactId} ${project.version}</name>

<version>2.0.0.0</version>
<properties>
<module-name>sentinel</module-name>
</properties>

<dependencies>
<dependency>
<groupId>io.shulie.instrument.module</groupId>
<artifactId>module-pradar-core</artifactId>
<version>${pradar.core.version}</version>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.shulie.instrument.simulator</groupId>
<artifactId>simulator-bootstrap-api</artifactId>
<version>${simulator.bootstrap.api.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.alibaba.csp</groupId>
<artifactId>sentinel-core</artifactId>
<version>1.8.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -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";
}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
注意!!!!!
每次更新插件请都更新内容到以下内容,
akka中间件支持模块,
新增模块版本信息,初始版本为1.0.0,README.md为模块更新内容描述文件,

Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions instrument-modules/user-modules/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<module>module-cus-trace</module>
<module>module-activemq</module>
<module>module-activemqv2</module>
<module>module-sentinel</module>
</modules>

<build>
Expand Down