Skip to content

Commit

Permalink
【enhancement】优化增强后输出字节码文件的开关和路径配置
Browse files Browse the repository at this point in the history
  • Loading branch information
lilai23 committed Apr 1, 2023
1 parent 2675bdd commit 993b6e5
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ agent.config.ignoredInterfaces=org.springframework.cglib.proxy.Factory
agent.config.combineStrategy=ALL
agent.config.serviceBlackList=com.huaweicloud.sermant.implement.service.heartbeat.HeartbeatServiceImpl,com.huaweicloud.sermant.implement.service.send.netty.NettyGatewayClient,com.huaweicloud.sermant.implement.service.tracing.TracingServiceImpl
agent.config.serviceInjectList=com.huawei.discovery.service.lb.filter.NopInstanceFilter,com.huawei.discovery.service.lb.DiscoveryManager,com.huawei.discovery.service.util.ApplyUtil,com.huawei.discovery.service.lb.cache.InstanceCacheManager
agent.config.isOutputEnhancedClass=false
agent.config.enhancedClassOutputPath=
agent.config.isShowEnhanceLogEnable=false

# event config
event.config.enable=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ agent.config.ignoredInterfaces=org.springframework.cglib.proxy.Factory
agent.config.combineStrategy=ALL
agent.config.serviceBlackList=com.huaweicloud.sermant.implement.service.heartbeat.HeartbeatServiceImpl,com.huaweicloud.sermant.implement.service.send.netty.NettyGatewayClient,com.huaweicloud.sermant.implement.service.tracing.TracingServiceImpl
agent.config.serviceInjectList=com.huawei.discovery.service.lb.filter.NopInstanceFilter,com.huawei.discovery.service.lb.DiscoveryManager
agent.config.isOutputEnhancedClass=false
agent.config.enhancedClassOutputPath=
agent.config.isShowEnhanceLogEnable=false

# event config
event.config.enable=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,11 @@ public class CommonConstant {
*/
public static final String TRANSFORM = "TRANSFORM";

/**
* 默认的增强后字节码文件输出路径父目录
*/
public static final String ENHANCED_CLASS_OUTPUT_PARENT_DIR = "enhancedClass";

private CommonConstant() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
import java.io.IOException;
import java.io.PrintStream;
import java.lang.instrument.Instrumentation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.ProtectionDomain;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -194,23 +199,36 @@ private void logAndCollectEvent(String enhanceLog) {
* @return BufferedAgentBuilder本身
*/
private BufferedAgentBuilder setOutputListener() {
String outputPath = System.getProperty(OUTPUT_PATH_SYSTEM_KEY);
if (outputPath == null || outputPath.length() <= 0) {
outputPath = config.getEnhancedClassOutputPath();
}
if (outputPath == null || outputPath.length() <= 0) {
if (!config.isOutputEnhancedClass()) {
return this;
}
final File folder = new File(FileUtils.validatePath(outputPath));
if (!folder.exists() && !folder.mkdirs()) {

String outputPath = config.getEnhancedClassOutputPath();
final Path outputDirectory;
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");
String currentTime = LocalDateTime.now().format(formatter);
if (outputPath == null || outputPath.isEmpty()) {
outputDirectory = Paths.get(FileUtils.getAgentPath())
.resolve(CommonConstant.ENHANCED_CLASS_OUTPUT_PARENT_DIR)
.resolve(currentTime);
} else {
outputDirectory = Paths.get(outputPath)
.resolve(CommonConstant.ENHANCED_CLASS_OUTPUT_PARENT_DIR)
.resolve(currentTime);
}
final File file;
try {
file = Files.createDirectories(outputDirectory).toFile();
} catch (IOException e) {
LOGGER.warning("Create enhanced class output directory fail!");
return this;
}
return addAction(builder -> builder.with(new AgentBuilder.Listener.Adapter() {
@Override
public void onTransformation(TypeDescription typeDescription, ClassLoader classLoader,
JavaModule module, boolean loaded, DynamicType dynamicType) {
try {
dynamicType.saveIn(folder);
dynamicType.saveIn(file);
} catch (IOException e) {
LOGGER.warning(String.format(
"Save class [%s] byte code failed. ", typeDescription.getTypeName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ public class AgentConfig implements BaseConfig {
*/
private boolean isShowEnhanceLogEnable = false;

/**
* 是否输出被增强的类的字节码文件
*/
private boolean isOutputEnhancedClass = false;

/**
* 被增强类的输出路径,如果为空,则不输出
*/
Expand Down Expand Up @@ -105,6 +110,14 @@ public void setShowEnhanceLogEnable(boolean showEnhanceLogEnable) {
isShowEnhanceLogEnable = showEnhanceLogEnable;
}

public boolean isOutputEnhancedClass() {
return isOutputEnhancedClass;
}

public void setOutputEnhancedClass(boolean outputEnhancedClass) {
isOutputEnhancedClass = outputEnhancedClass;
}

public String getEnhancedClassOutputPath() {
return enhancedClassOutputPath;
}
Expand Down Expand Up @@ -152,8 +165,7 @@ public enum CombineStrategy {
NONE,

/**
* 仅合并{@link PluginDeclarer#getClassMatcher}为{@link
* ClassTypeMatcher}的插件声明器,即通过匹配的类名合并
* 仅合并{@link PluginDeclarer#getClassMatcher}为{@link ClassTypeMatcher}的插件声明器,即通过匹配的类名合并
* <p>插件较多,且主要是全限定名匹配的场景时,该策略有优势
*/
BY_NAME,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public class FileUtils {
private FileUtils() {
}

/**
* 获取sermant-agent-x.x.x/agent的文件夹绝对路径
*
* @return agent路径
*/
public static String getAgentPath() {
return AGENT_PATH;
}

/**
* 检验文件路径
*
Expand Down Expand Up @@ -121,7 +130,7 @@ public static void copyFile(File sourceFile, File targetFile) throws IOException
*
* @param sourceFile 源文件夹
* @param targetPath 目标文件夹
* @param isCover 是否覆盖
* @param isCover 是否覆盖
* @throws IOException 拷贝失败
*/
public static void copyAllFiles(File sourceFile, String targetPath, boolean isCover) throws IOException {
Expand Down Expand Up @@ -166,7 +175,7 @@ public static boolean deleteDirs(File file) {
/**
* 通过通配符的方式检索子文件
*
* @param dir 文件夹
* @param dir 文件夹
* @param wcStr 通配符模式,允许','拼接多个
* @return 子文件集
*/
Expand Down

0 comments on commit 993b6e5

Please sign in to comment.