Commit 5d7c4150 authored by shengnan hu's avatar shengnan hu
Browse files

init

parents
Pipeline #4715 failed with stage
in 30 seconds
def file = new File(basedir, "build.log")
return file.text.contains("Arthas agent start success.")
<?xml version="1.0" encoding="UTF-8"?>
<settings>
<profiles>
<profile>
<id>it-repo</id>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
<repositories>
<repository>
<id>local.central</id>
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>local.central</id>
<url>@localRepositoryUrl@</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</profile>
</profiles>
</settings>
package com.alibaba.arthas.spring;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment;
import com.taobao.arthas.agent.attach.ArthasAgent;
/**
*
* @author hengyunabc 2020-06-22
*
*/
@ConditionalOnProperty(name = "spring.arthas.enabled", matchIfMissing = true)
@EnableConfigurationProperties({ ArthasProperties.class })
public class ArthasConfiguration {
private static final Logger logger = LoggerFactory.getLogger(ArthasConfiguration.class);
@Autowired
ConfigurableEnvironment environment;
/**
* <pre>
* 1. 提取所有以 arthas.* 开头的配置项,再统一转换为Arthas配置
* 2. 避免某些配置在新版本里支持,但在ArthasProperties里没有配置的情况。
* </pre>
*/
@ConfigurationProperties(prefix = "arthas")
@ConditionalOnMissingBean(name="arthasConfigMap")
@Bean
public HashMap<String, String> arthasConfigMap() {
return new HashMap<String, String>();
}
@ConditionalOnMissingBean
@Bean
public ArthasAgent arthasAgent(@Autowired @Qualifier("arthasConfigMap") Map<String, String> arthasConfigMap,
@Autowired ArthasProperties arthasProperties) throws Throwable {
arthasConfigMap = StringUtils.removeDashKey(arthasConfigMap);
ArthasProperties.updateArthasConfigMapDefaultValue(arthasConfigMap);
/**
* @see org.springframework.boot.context.ContextIdApplicationContextInitializer#getApplicationId(ConfigurableEnvironment)
*/
String appName = environment.getProperty("spring.application.name");
if (arthasConfigMap.get("appName") == null && appName != null) {
arthasConfigMap.put("appName", appName);
}
// 给配置全加上前缀
Map<String, String> mapWithPrefix = new HashMap<String, String>(arthasConfigMap.size());
for (Entry<String, String> entry : arthasConfigMap.entrySet()) {
mapWithPrefix.put("arthas." + entry.getKey(), entry.getValue());
}
final ArthasAgent arthasAgent = new ArthasAgent(mapWithPrefix, arthasProperties.getHome(),
arthasProperties.isSlientInit(), null);
arthasAgent.init();
logger.info("Arthas agent start success.");
return arthasAgent;
}
}
package com.alibaba.arthas.spring;
import java.util.Map;
import org.springframework.boot.context.properties.ConfigurationProperties;
/**
*
* @author hengyunabc 2020-06-23
*
*/
@ConfigurationProperties(prefix = "arthas")
public class ArthasProperties {
private String ip;
private int telnetPort;
private int httpPort;
private String tunnelServer;
private String agentId;
private String appName;
/**
* report executed command
*/
private String statUrl;
/**
* session timeout seconds
*/
private long sessionTimeout;
private String username;
private String password;
private String home;
/**
* when arthas agent init error will throw exception by default.
*/
private boolean slientInit = false;
/**
* disabled commands,default disable stop command
*/
private String disabledCommands;
private static final String DEFAULT_DISABLEDCOMMANDS = "stop";
/**
* 因为 arthasConfigMap 只注入了用户配置的值,没有默认值,因些统一处理补全
*/
public static void updateArthasConfigMapDefaultValue(Map<String, String> arthasConfigMap) {
if (!arthasConfigMap.containsKey("disabledCommands")) {
arthasConfigMap.put("disabledCommands", DEFAULT_DISABLEDCOMMANDS);
}
}
public String getHome() {
return home;
}
public void setHome(String home) {
this.home = home;
}
public boolean isSlientInit() {
return slientInit;
}
public void setSlientInit(boolean slientInit) {
this.slientInit = slientInit;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public int getTelnetPort() {
return telnetPort;
}
public void setTelnetPort(int telnetPort) {
this.telnetPort = telnetPort;
}
public int getHttpPort() {
return httpPort;
}
public void setHttpPort(int httpPort) {
this.httpPort = httpPort;
}
public String getTunnelServer() {
return tunnelServer;
}
public void setTunnelServer(String tunnelServer) {
this.tunnelServer = tunnelServer;
}
public String getAgentId() {
return agentId;
}
public void setAgentId(String agentId) {
this.agentId = agentId;
}
public String getStatUrl() {
return statUrl;
}
public void setStatUrl(String statUrl) {
this.statUrl = statUrl;
}
public long getSessionTimeout() {
return sessionTimeout;
}
public void setSessionTimeout(long sessionTimeout) {
this.sessionTimeout = sessionTimeout;
}
public String getAppName() {
return appName;
}
public void setAppName(String appName) {
this.appName = appName;
}
public String getDisabledCommands() {
return disabledCommands;
}
public void setDisabledCommands(String disabledCommands) {
this.disabledCommands = disabledCommands;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
package com.alibaba.arthas.spring;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
/**
*
* @author hengyunabc 2020-06-24
*
*/
public class StringUtils {
public static Map<String, String> removeDashKey(Map<String, String> map) {
Map<String, String> result = new HashMap<String, String>(map.size());
for (Entry<String, String> entry : map.entrySet()) {
String key = entry.getKey();
if (key.contains("-")) {
StringBuilder sb = new StringBuilder(key.length());
for (int i = 0; i < key.length(); i++) {
if (key.charAt(i) == '-' && (i + 1 < key.length()) && Character.isAlphabetic(key.charAt(i + 1))) {
++i;
char upperChar = Character.toUpperCase(key.charAt(i));
sb.append(upperChar);
} else {
sb.append(key.charAt(i));
}
}
key = sb.toString();
}
result.put(key, entry.getValue());
}
return result;
}
}
package com.alibaba.arthas.spring.endpoints;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import com.taobao.arthas.agent.attach.ArthasAgent;
/**
*
* @author hengyunabc 2020-06-24
*
*/
@Endpoint(id = "arthas")
public class ArthasEndPoint {
@Autowired(required = false)
private ArthasAgent arthasAgent;
@Autowired(required = false)
private HashMap<String, String> arthasConfigMap;
@ReadOperation
public Map<String, Object> invoke() {
Map<String, Object> result = new HashMap<String, Object>();
if (arthasConfigMap != null) {
result.put("arthasConfigMap", arthasConfigMap);
}
String errorMessage = arthasAgent.getErrorMessage();
if (errorMessage != null) {
result.put("errorMessage", errorMessage);
}
return result;
}
}
package com.alibaba.arthas.spring.endpoints;
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
/**
*
* @author hengyunabc 2020-06-24
*
*/
@ConditionalOnProperty(name = "spring.arthas.enabled", matchIfMissing = true)
public class ArthasEndPointAutoConfiguration {
@Bean
@ConditionalOnMissingBean
@ConditionalOnAvailableEndpoint
public ArthasEndPoint arthasEndPoint() {
return new ArthasEndPoint();
}
}
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.arthas.spring.ArthasConfiguration,\
com.alibaba.arthas.spring.endpoints.ArthasEndPointAutoConfiguration
\ No newline at end of file
provides: arthas-spring-boot-starter
\ No newline at end of file
package com.alibaba.arthas.spring;
import java.util.HashMap;
import java.util.Map;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
/**
*
* @author hengyunabc 2020-06-24
*
*/
public class StringUtilsTest {
@Test
public void test() {
Map<String, String> map = new HashMap<String, String>();
map.put("telnet-port", "" + 9999);
map.put("aaa--bbb", "fff");
map.put("123", "123");
map.put("123-", "123");
map.put("123-abc", "123");
map.put("xxx-", "xxx");
map = StringUtils.removeDashKey(map);
Assertions.assertThat(map).containsEntry("telnetPort", "" + 9999);
Assertions.assertThat(map).containsEntry("aaa-Bbb", "fff");
Assertions.assertThat(map).containsEntry("123", "123");
Assertions.assertThat(map).containsEntry("123-", "123");
Assertions.assertThat(map).containsEntry("123Abc", "123");
Assertions.assertThat(map).containsEntry("xxx-", "xxx");
}
}
{
"groups": [
{
"name": "arthas",
"type": "java.util.HashMap",
"sourceType": "com.alibaba.arthas.spring.ArthasConfiguration",
"sourceMethod": "public HashMap<java.lang.String,java.lang.String> arthasConfigMap() "
},
{
"name": "arthas",
"type": "com.alibaba.arthas.spring.ArthasProperties",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "management.endpoint.arthas",
"type": "com.alibaba.arthas.spring.endpoints.ArthasEndPoint",
"sourceType": "com.alibaba.arthas.spring.endpoints.ArthasEndPoint"
}
],
"properties": [
{
"name": "arthas.agent-id",
"type": "java.lang.String",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.app-name",
"type": "java.lang.String",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.disabled-commands",
"type": "java.lang.String",
"description": "disabled commands,default disable stop command",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.home",
"type": "java.lang.String",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.http-port",
"type": "java.lang.Integer",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.ip",
"type": "java.lang.String",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.password",
"type": "java.lang.String",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.session-timeout",
"type": "java.lang.Long",
"description": "session timeout seconds",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.slient-init",
"type": "java.lang.Boolean",
"description": "when arthas agent init error will throw exception by default.",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.stat-url",
"type": "java.lang.String",
"description": "report executed command",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.telnet-port",
"type": "java.lang.Integer",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.tunnel-server",
"type": "java.lang.String",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "arthas.username",
"type": "java.lang.String",
"sourceType": "com.alibaba.arthas.spring.ArthasProperties"
},
{
"name": "management.endpoint.arthas.cache.time-to-live",
"type": "java.time.Duration",
"description": "Maximum time that a response can be cached.",
"sourceType": "com.alibaba.arthas.spring.endpoints.ArthasEndPoint",
"defaultValue": "0ms"
},
{
"name": "management.endpoint.arthas.enabled",
"type": "java.lang.Boolean",
"description": "Whether to enable the arthas endpoint.",
"sourceType": "com.alibaba.arthas.spring.endpoints.ArthasEndPoint",
"defaultValue": true
}
],
"hints": []
}
\ No newline at end of file
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.alibaba.arthas.spring.ArthasConfiguration,\
com.alibaba.arthas.spring.endpoints.ArthasEndPointAutoConfiguration
\ No newline at end of file
provides: arthas-spring-boot-starter
\ No newline at end of file
#Generated by Git-Commit-Id-Plugin
git.build.version=3.6.4
git.closest.tag.commit.count=
git.closest.tag.name=
git.commit.id=35f73a652cf4efc30cc57df74dbab7d5c1de757a
git.commit.id.abbrev=35f73a6
git.commit.id.describe=35f73a6
git.commit.id.describe-short=35f73a6
git.commit.message.full=ci-set-3
git.commit.message.short=ci-set-3
git.commit.user.email=shengnan.hu@ustchcs.com
git.commit.user.name=shengnan.hu
git.dirty=false
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment