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

feat:support spring cloud version auto-detection. #196

Merged
merged 1 commit into from
Jun 19, 2024
Merged
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
2 changes: 1 addition & 1 deletion polaris-agent-build/bin/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ cp "polaris-agent-core/polaris-agent-core-optional-java17/target/polaris-agent-c
cp "polaris-agent-core/polaris-agent-core-extension/target/polaris-agent-core-extension-${version}.jar" "${folder_name}/boot/"

pushd "polaris-agent-plugins"
plugin_folders=$(find . -maxdepth 2 | grep -E ".+-plugin$")
plugin_folders=$(find . -maxdepth 2 | grep -E ".+-plugin.+-")
for plugin_folder in ${plugin_folders}; do
file_name=${plugin_folder##*/}
cp "${plugin_folder}/target/${file_name}-${version}.jar" "../${folder_name}/plugins/"
Expand Down
13 changes: 6 additions & 7 deletions polaris-agent-build/bin/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,20 @@ unzip ${polaris_agent_dir_name}.zip
echo "inject with framework ${JAVA_AGENT_FRAMEWORK_NAME} and version ${JAVA_AGENT_FRAMEWORK_VERSION}"

# 第一步,需要确定 agent-plugin 启用哪个
custom_plugin_id="${JAVA_AGENT_FRAMEWORK_NAME}-${JAVA_AGENT_FRAMEWORK_VERSION}-plugin"
custom_plugin_id=""
if [ -n ${JAVA_AGENT_FRAMEWORK_NAME} ] && [ -n ${JAVA_AGENT_FRAMEWORK_VERSION} ]; then
custom_plugin_id="${JAVA_AGENT_FRAMEWORK_NAME}-${JAVA_AGENT_FRAMEWORK_VERSION}-plugin"
fi
echo "plugins.enable=${custom_plugin_id}" > ${polaris_agent_dir_name}/conf/polaris-agent.config

# 第二步,将 plugin 所需要的配置注入到 plugin 对应的目录中去
java_agent_config_dir="${JAVA_AGENT_FRAMEWORK_NAME}-${JAVA_AGENT_FRAMEWORK_VERSION}"
echo "inject with config dir ${java_agent_config_dir}"
echo "inject with default config ${JAVA_AGENT_PLUGIN_CONF}"

custom_plugin_properties=${JAVA_AGENT_PLUGIN_CONF}
echo "${custom_plugin_properties}" > ${polaris_agent_dir_name}/conf/plugin/${java_agent_config_dir}/application.properties
target_config_file=${polaris_agent_dir_name}/conf/plugin/spring-cloud/application.properties
echo "${custom_plugin_properties}" > "${target_config_file}"

# 第三步,将地域信息拉取并设置进配置文件
# 腾讯云不能拿到大区,因此腾讯云上的region对应的是北极星的zone,zone对应北极星的campus
target_config_file=${polaris_agent_dir_name}/conf/plugin/${java_agent_config_dir}/application.properties

echo "start to fetch region, target config file ${target_config_file}"
region="$(curl -s --connect-timeout 10 -m 10 http://metadata.tencentyun.com/latest/meta-data/placement/region)"
region_code=$?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,21 @@
package cn.polarismesh.agent.core.asm.instrument.plugin;

import cn.polarismesh.agent.core.common.conf.ConfigManager;
import cn.polarismesh.agent.core.common.logger.CommonLogger;
import cn.polarismesh.agent.core.common.logger.StdoutCommonLoggerFactory;
import cn.polarismesh.agent.core.common.utils.ReflectionUtils;
import cn.polarismesh.agent.core.common.utils.StringUtils;

import java.lang.reflect.Method;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

public class PluginNameFilter implements PluginFilter {

private static final CommonLogger logger = StdoutCommonLoggerFactory.INSTANCE
.getLogger(PluginNameFilter.class.getCanonicalName());

private final Set<String> pluginNames;

public PluginNameFilter() {
Expand All @@ -38,6 +46,7 @@ public boolean accept(PluginJar pluginJar) {

private static Set<String> getLoadablePluginNames() {
String enablePlugins = ConfigManager.INSTANCE.getConfigValue(ConfigManager.KEY_PLUGIN_ENABLE);
enablePlugins = appendSpringCloudPluginNameIfNeeded(enablePlugins);
if (StringUtils.isEmpty(enablePlugins)) {
return Collections.emptySet();
}
Expand All @@ -51,4 +60,52 @@ private static Set<String> getLoadablePluginNames() {
}
return values;
}

private static String appendSpringCloudPluginNameIfNeeded(String enablePlugins) {
if (StringUtils.hasText(enablePlugins)) {
String[] names = enablePlugins.split(",");
for (String name : names) {
if (StringUtils.hasText(name) && name.contains("spring-cloud-")) {
return enablePlugins;
}
}
}

// check if spring-cloud and spring-boot exist.
if (ReflectionUtils.checkClassExists("org.springframework.cloud.configuration.SpringBootVersionVerifier")
andrewshan marked this conversation as resolved.
Show resolved Hide resolved
&& ReflectionUtils.checkClassExists("org.springframework.boot.SpringBootVersion")) {
Method method = ReflectionUtils.findMethod(ReflectionUtils.findClass("org.springframework.boot.SpringBootVersion"), "getVersion");
Object version = ReflectionUtils.invokeMethod(method, null);

if (version instanceof String) {
String versionStr = (String) version;
logger.info("Spring Boot Version: " + version);
String springCloudPluginNamePattern = "spring-cloud-%s-plugin";
String springCloudVersion = "";
if (versionStr.startsWith("2.2") || versionStr.startsWith("2.3")) {
springCloudVersion = "hoxton";
} else if (versionStr.startsWith("2.4") || versionStr.startsWith("2.5")) {
springCloudVersion = "2020";
} else if (versionStr.startsWith("2.6") || versionStr.startsWith("2.7")) {
springCloudVersion = "2021";
} else if (versionStr.startsWith("3.0") || versionStr.startsWith("3.1")) {
springCloudVersion = "2022";
} else if (versionStr.startsWith("3.2") || versionStr.startsWith("3.3")) {
springCloudVersion = "2023";
}
if (StringUtils.hasText(springCloudVersion)) {
String springCloudPluginName = String.format(springCloudPluginNamePattern, springCloudVersion);
logger.info("Spring Cloud Version: " + springCloudVersion);
if (StringUtils.hasText(enablePlugins)) {
enablePlugins = enablePlugins + "," + springCloudPluginName;
} else {
enablePlugins = springCloudPluginName;
}
}
} else {
logger.warn("Cannot get Spring Boot Version from MANIFEST.");
}
}
return enablePlugins;
}
}
Loading