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. #201

Merged
merged 3 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from 2 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
31 changes: 25 additions & 6 deletions polaris-agent-build/bin/start.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#!/bin/bash

version=$(cat /app/version.txt)
polaris_agent_dir_name="polaris-java-agent-${version}"
polaris_agent_zip_name="polaris-java-agent-${version}"
polaris_agent_dir_name="polaris-java-agent"
echo "polaris agent dir name ${polaris_agent_dir_name}"

java_agent_dir=${JAVA_AGENT_DIR}
Expand All @@ -12,13 +13,16 @@ mkdir -p ${java_agent_dir}

# 将 /app/lib 下的文件 copy 一份出去

cp -f /app/${polaris_agent_dir_name}.zip ${java_agent_dir}/
cp -f /app/${polaris_agent_zip_name}.zip ${java_agent_dir}/
cp -f /app/version.txt ${java_agent_dir}/


# 这里需要将 agent 起到需要的相关信息全部注入到对应 plugin 中
cd ${java_agent_dir}
unzip ${polaris_agent_dir_name}.zip
unzip ${polaris_agent_zip_name}.zip
# 创建 polaris-java-agent 目录
mkdir -p ${polaris_agent_dir_name}
cp -rf ${polaris_agent_zip_name}/* ${polaris_agent_dir_name}/

check_string_not_empty() {
local string_to_check="$1"
Expand Down Expand Up @@ -46,9 +50,22 @@ echo "plugins.enable=${custom_plugin_id}" > ${polaris_agent_dir_name}/conf/polar

# 第二步,将 plugin 所需要的配置注入到 plugin 对应的目录中去
echo "inject with default config ${JAVA_AGENT_PLUGIN_CONF}"
custom_plugin_properties=${JAVA_AGENT_PLUGIN_CONF}
target_config_file=${polaris_agent_dir_name}/conf/plugin/spring-cloud/application.properties
echo "${custom_plugin_properties}" > "${target_config_file}"
if check_string_not_empty "${JAVA_AGENT_PLUGIN_CONF}"; then
echo "${JAVA_AGENT_PLUGIN_CONF}" > "${target_config_file}"
else
echo "JAVA_AGENT_PLUGIN_CONF is empty"
echo "read polaris server ip: ${POLARIS_SERVER_IP}"
echo "read polaris discovery port: ${POLARIS_DISCOVER_PORT}"
echo "read polaris config ip: ${POLARIS_CONFIG_IP}"
echo "read polaris config port: ${POLARIS_CONFIG_PORT}"
polaris_address="grpc\\\:\/\/${POLARIS_SERVER_IP}\\\:${POLARIS_DISCOVER_PORT}"
polaris_config_address="grpc\\\:\/\/${POLARIS_CONFIG_IP}\\\:${POLARIS_CONFIG_PORT}"
echo "read polaris address: ${polaris_address}"
echo "read polaris config address: ${polaris_config_address}"
sed -i "s/spring.cloud.polaris.address=grpc\\\:\/\/127.0.0.1\\\:8091/spring.cloud.polaris.address=${polaris_address}/g" ${target_config_file}
sed -i "s/spring.cloud.polaris.config.address=grpc\\\:\/\/127.0.0.1\\\:8093/spring.cloud.polaris.config.address=${polaris_config_address}/g" ${target_config_file}
fi

# 第三步,将地域信息拉取并设置进配置文件
# 腾讯云不能拿到大区,因此腾讯云上的region对应的是北极星的zone,zone对应北极星的campus
Expand All @@ -66,4 +83,6 @@ zone_code=$?
echo "zone is ${zone}, return code is ${zone_code}"
if [ ${zone_code} -eq 0 ] && [ -n ${zone} ]; then
sed -i "s/spring.cloud.tencent.metadata.content.campus=\"\"/spring.cloud.tencent.metadata.content.campus=${zone}/g" ${target_config_file}
fi
fi

cat ${target_config_file}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,22 @@
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.StringUtils;

import java.io.IOException;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.jar.JarFile;
import java.util.jar.Manifest;

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 +47,9 @@ public boolean accept(PluginJar pluginJar) {

private static Set<String> getLoadablePluginNames() {
String enablePlugins = ConfigManager.INSTANCE.getConfigValue(ConfigManager.KEY_PLUGIN_ENABLE);
logger.info("Enable plugins: " + enablePlugins);
enablePlugins = appendSpringCloudPluginNameIfNeeded(enablePlugins);
logger.info("Enable plugins after appendSpringCloudPluginNameIfNeeded: " + enablePlugins);
if (StringUtils.isEmpty(enablePlugins)) {
return Collections.emptySet();
}
Expand All @@ -51,4 +63,63 @@ 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;
}
}
}

JarFile jarFile = null;
try {
String classPath = System.getProperty("java.class.path");
logger.info("Class path: " + classPath);
String[] paths = classPath.split(":");
String mainJarPath = paths[0];
logger.info("Main jar: " + mainJarPath);
jarFile = new JarFile(mainJarPath);
Manifest manifest = (jarFile).getManifest();
String versionStr = manifest.getMainAttributes().getValue("Spring-Boot-Version");
logger.info("Spring Boot Version: " + versionStr);
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("No compatible Spring Cloud version found for Spring Boot version: " + versionStr);
}
} catch (IOException ioException) {
logger.warn("Cannot get Spring Boot Version from MANIFEST.", ioException);
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException ioException) {
logger.warn("Cannot close jarFile", ioException);
}
}
}
return enablePlugins;
}
}
Loading