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 dbf8efc
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 25 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.isOutputEnhancedClasses=false
agent.config.enhancedClassesOutputPath=
agent.config.isShowEnhanceLog=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.isOutputEnhancedClasses=false
agent.config.enhancedClassesOutputPath=
agent.config.isShowEnhanceLog=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 = "enhancedClasses";

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 @@ -133,14 +138,14 @@ public AgentBuilder process(AgentBuilder builder) {
}

/**
* 设置输出日志的监听器,由{@link AgentConfig#isShowEnhanceLogEnable()}而定
* 设置输出日志的监听器,由{@link AgentConfig#isShowEnhanceLog()}而定
* <p>使用{@link AgentBuilder.Listener.StreamWriting}转化为字符串信息后输出为日志
* <p>注意,输出时使用的缓冲区将不会被释放,需要关注{@link AgentBuilder.Listener.StreamWriting}中单行信息的长度
*
* @return BufferedAgentBuilder本身
*/
private BufferedAgentBuilder setLogListener() {
if (!config.isShowEnhanceLogEnable()) {
if (!config.isShowEnhanceLog()) {
return this;
}
return addAction(new BuilderAction() {
Expand Down Expand Up @@ -187,30 +192,43 @@ private void logAndCollectEvent(String enhanceLog) {
* 设置输出增强后字节码的监听器,输出路径优先选择:
* <pre>
* 1.系统变量{@link #OUTPUT_PATH_SYSTEM_KEY}
* 2.配置{@link AgentConfig#getEnhancedClassOutputPath}
* 2.配置{@link AgentConfig#getEnhancedClassesOutputPath}
* </pre>
* 若两者都无法正确获取路径,则视为无需该监听器
*
* @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.isOutputEnhancedClasses()) {
return this;
}
final File folder = new File(FileUtils.validatePath(outputPath));
if (!folder.exists() && !folder.mkdirs()) {

String outputPath = config.getEnhancedClassesOutputPath();
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 @@ -51,12 +51,17 @@ public class AgentConfig implements BaseConfig {
/**
* 是否在增强过程中输出检索日志
*/
private boolean isShowEnhanceLogEnable = false;
private boolean isShowEnhanceLog = false;

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

/**
* 被增强类的输出路径,如果为空,则不输出
*/
private String enhancedClassOutputPath;
private String enhancedClassesOutputPath;

/**
* 插件的合并策略,定义{@link PluginDeclarer}插件声明器的合并策略
Expand Down Expand Up @@ -97,20 +102,28 @@ public void setIgnoredPrefixes(Set<String> ignoredPrefixes) {
this.ignoredPrefixes = ignoredPrefixes;
}

public boolean isShowEnhanceLogEnable() {
return isShowEnhanceLogEnable;
public boolean isShowEnhanceLog() {
return isShowEnhanceLog;
}

public void setShowEnhanceLog(boolean showEnhanceLog) {
isShowEnhanceLog = showEnhanceLog;
}

public boolean isOutputEnhancedClasses() {
return isOutputEnhancedClasses;
}

public void setShowEnhanceLogEnable(boolean showEnhanceLogEnable) {
isShowEnhanceLogEnable = showEnhanceLogEnable;
public void setOutputEnhancedClasses(boolean outputEnhancedClasses) {
isOutputEnhancedClasses = outputEnhancedClasses;
}

public String getEnhancedClassOutputPath() {
return enhancedClassOutputPath;
public String getEnhancedClassesOutputPath() {
return enhancedClassesOutputPath;
}

public void setEnhancedClassOutputPath(String enhancedClassOutputPath) {
this.enhancedClassOutputPath = enhancedClassOutputPath;
public void setEnhancedClassesOutputPath(String enhancedClassesOutputPath) {
this.enhancedClassesOutputPath = enhancedClassesOutputPath;
}

public Set<String> getServiceBlackList() {
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 dbf8efc

Please sign in to comment.