Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
阿信在这里
SkyWalking
提交
d8e21adc
S
SkyWalking
项目概览
阿信在这里
/
SkyWalking
与 Fork 源项目一致
Fork自
山不在高_有仙则灵 / SkyWalking
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
SkyWalking
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
d8e21adc
编写于
10月 15, 2017
作者:
wu-sheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix startup issue.
上级
95bb65cd
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
64 addition
and
58 deletion
+64
-58
apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/boot/AgentPackagePath.java
.../org/skywalking/apm/agent/core/boot/AgentPackagePath.java
+17
-16
apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java
...walking/apm/agent/core/conf/SnifferConfigInitializer.java
+41
-38
apm-sniffer/apm-agent-core/src/test/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java
...ing/apm/agent/core/conf/SnifferConfigInitializerTest.java
+5
-3
apm-sniffer/apm-agent-core/src/test/resources/config/agent.config
...fer/apm-agent-core/src/test/resources/config/agent.config
+1
-1
未找到文件。
apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/boot/AgentPackagePath.java
浏览文件 @
d8e21adc
...
...
@@ -22,15 +22,11 @@ import java.io.File;
import
java.net.MalformedURLException
;
import
java.net.URL
;
import
org.skywalking.apm.agent.core.logging.SystemOutWriter
;
import
org.skywalking.apm.logging.ILog
;
import
org.skywalking.apm.logging.LogManager
;
/**
* @author wusheng
*/
public
class
AgentPackagePath
{
private
static
final
ILog
logger
=
LogManager
.
getLogger
(
AgentPackagePath
.
class
);
private
static
File
AGENT_PACKAGE_PATH
;
public
static
File
getPath
()
throws
AgentPackageNotFoundException
{
...
...
@@ -48,23 +44,28 @@ public class AgentPackagePath {
String
urlString
=
resource
.
toString
();
SystemOutWriter
.
INSTANCE
.
write
(
urlString
);
logger
.
debug
(
urlString
);
urlString
=
urlString
.
substring
(
urlString
.
indexOf
(
"file:"
),
urlString
.
indexOf
(
'!'
));
File
agentJarFile
=
null
;
try
{
agentJarFile
=
new
File
(
new
URL
(
urlString
).
getFile
());
}
catch
(
MalformedURLException
e
)
{
SystemOutWriter
.
INSTANCE
.
write
(
"Can not locate agent jar file by url:"
+
urlString
);
logger
.
error
(
e
,
"Can not locate agent jar file by url: {}"
,
urlString
);
}
if
(
agentJarFile
.
exists
())
{
return
agentJarFile
.
getParentFile
();
int
insidePathIndex
=
urlString
.
indexOf
(
'!'
);
boolean
isInJar
=
insidePathIndex
>
-
1
;
if
(
isInJar
)
{
urlString
=
urlString
.
substring
(
urlString
.
indexOf
(
"file:"
),
insidePathIndex
);
File
agentJarFile
=
null
;
try
{
agentJarFile
=
new
File
(
new
URL
(
urlString
).
getFile
());
}
catch
(
MalformedURLException
e
)
{
SystemOutWriter
.
INSTANCE
.
write
(
"Can not locate agent jar file by url:"
+
urlString
);
}
if
(
agentJarFile
.
exists
())
{
return
agentJarFile
.
getParentFile
();
}
}
else
{
String
classLocation
=
urlString
.
substring
(
urlString
.
indexOf
(
"file:"
),
urlString
.
length
()
-
classResourcePath
.
length
());
return
new
File
(
classLocation
);
}
}
SystemOutWriter
.
INSTANCE
.
write
(
"Can not locate agent jar file."
);
logger
.
info
(
"Can not locate agent jar file."
);
throw
new
AgentPackageNotFoundException
(
"Can not locate agent jar file."
);
}
...
...
apm-sniffer/apm-agent-core/src/main/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializer.java
浏览文件 @
d8e21adc
...
...
@@ -22,11 +22,12 @@ import java.io.File;
import
java.io.FileInputStream
;
import
java.io.FileNotFoundException
;
import
java.io.InputStream
;
import
java.util.Iterator
;
import
java.util.Map
;
import
java.util.Properties
;
import
org.skywalking.apm.agent.core.boot.AgentPackageNotFoundException
;
import
org.skywalking.apm.agent.core.boot.AgentPackagePath
;
import
org.skywalking.apm.logging.ILog
;
import
org.skywalking.apm.logging.LogManager
;
import
org.skywalking.apm.agent.core.logging.SystemOutWriter
;
import
org.skywalking.apm.util.ConfigInitializer
;
import
org.skywalking.apm.util.StringUtil
;
...
...
@@ -37,12 +38,12 @@ import org.skywalking.apm.util.StringUtil;
* @see {@link #initialize()}, to learn more about how to initialzie.
*/
public
class
SnifferConfigInitializer
{
private
static
final
ILog
logger
=
LogManager
.
getLogger
(
SnifferConfigInitializer
.
class
)
;
private
static
String
CONFIG_FILE_NAME
=
"/sky-walking.config
"
;
private
static
String
CONFIG_FILE_NAME
=
"/config/agent.config"
;
private
static
String
ENV_KEY_PREFIX
=
"skywalking.
"
;
/**
* Try to locate config file, named {@link #CONFIG_FILE_NAME}, in following order:
* 1. Path from SystemProperty. {@link #
loadConfigBySystemProperty
()}
* 1. Path from SystemProperty. {@link #
overrideConfigBySystemEnv
()}
* 2. class path.
* 3. Path, where agent is. {@link #loadConfigFromAgentFolder()}
* <p>
...
...
@@ -55,60 +56,61 @@ public class SnifferConfigInitializer {
public
static
void
initialize
()
throws
ConfigNotFoundException
,
AgentPackageNotFoundException
{
InputStream
configFileStream
;
configFileStream
=
loadConfigFromAgentFolder
();
try
{
configFileStream
=
loadConfigFromAgentFolder
();
Properties
properties
=
new
Properties
();
properties
.
load
(
configFileStream
);
ConfigInitializer
.
initialize
(
properties
,
Config
.
class
);
}
catch
(
Exception
e
)
{
logger
.
error
(
"Failed to read the config file, sky-walking is going to run in default config."
,
e
);
SystemOutWriter
.
INSTANCE
.
write
(
"Failed to read the config file, skywalking is going to run in default config."
);
e
.
printStackTrace
(
SystemOutWriter
.
INSTANCE
.
getStream
());
}
String
applicationCode
=
System
.
getProperty
(
"applicationCode"
);
if
(!
StringUtil
.
isEmpty
(
applicationCode
))
{
Config
.
Agent
.
APPLICATION_CODE
=
applicationCode
;
}
String
servers
=
System
.
getProperty
(
"servers"
);
if
(!
StringUtil
.
isEmpty
(
servers
))
{
Config
.
Collector
.
SERVERS
=
servers
;
try
{
overrideConfigBySystemEnv
();
}
catch
(
Exception
e
)
{
SystemOutWriter
.
INSTANCE
.
write
(
"Failed to read the system env."
);
e
.
printStackTrace
(
SystemOutWriter
.
INSTANCE
.
getStream
());
}
if
(
StringUtil
.
isEmpty
(
Config
.
Agent
.
APPLICATION_CODE
))
{
throw
new
ExceptionInInitializerError
(
"
'-DapplicationCode='
is missing."
);
throw
new
ExceptionInInitializerError
(
"
`agent.application_code`
is missing."
);
}
if
(
StringUtil
.
isEmpty
(
Config
.
Collector
.
SERVERS
))
{
throw
new
ExceptionInInitializerError
(
"
'-Dservers='
is missing."
);
throw
new
ExceptionInInitializerError
(
"
`collector.servers`
is missing."
);
}
}
/**
* Load the config file by the path, which is provided by system property, usually with a "-Dconfig=" arg.
* Override the config by system env. The env key must start with `skywalking`, the reuslt should be as same as in
* `agent.config`
*
* such as:
* Env key of `agent.application_code` shoule be `skywalking.agent.application_code`
*
* @return the config file {@link InputStream}, or null if not needEnhance.
*/
private
static
InputStream
loadConfigBySystemProperty
()
{
String
config
=
System
.
getProperty
(
"config"
);
if
(
StringUtil
.
isEmpty
(
config
))
{
return
null
;
}
File
configFile
=
new
File
(
config
);
if
(
configFile
.
exists
()
&&
configFile
.
isDirectory
())
{
logger
.
info
(
"check {} in path {}, according system property."
,
CONFIG_FILE_NAME
,
config
);
configFile
=
new
File
(
config
,
CONFIG_FILE_NAME
);
private
static
void
overrideConfigBySystemEnv
()
throws
IllegalAccessException
{
Properties
properties
=
new
Properties
();
Map
<
String
,
String
>
envs
=
System
.
getenv
();
for
(
String
envKey
:
envs
.
keySet
())
{
if
(
envKey
.
startsWith
(
ENV_KEY_PREFIX
))
{
String
realKey
=
envKey
.
substring
(
ENV_KEY_PREFIX
.
length
());
properties
.
setProperty
(
realKey
,
envs
.
get
(
envKey
));
}
}
if
(
configFile
.
exists
()
&&
configFile
.
isFile
())
{
try
{
logger
.
info
(
"found {}, according system property."
,
configFile
.
getAbsolutePath
()
);
return
new
FileInputStream
(
configFile
);
}
catch
(
FileNotFoundException
e
)
{
logger
.
error
(
e
,
"Fail to load {} , according system property."
,
config
);
Properties
systemProperties
=
System
.
getProperties
();
Iterator
<
Map
.
Entry
<
Object
,
Object
>>
entryIterator
=
systemProperties
.
entrySet
().
iterator
();
while
(
entryIterator
.
hasNext
())
{
Map
.
Entry
<
Object
,
Object
>
prop
=
entryIterator
.
next
(
);
if
(
prop
.
getKey
().
toString
().
startsWith
(
ENV_KEY_PREFIX
))
{
String
realKey
=
prop
.
getKey
().
toString
().
substring
(
ENV_KEY_PREFIX
.
length
());
properties
.
put
(
realKey
,
prop
.
getValue
()
);
}
}
logger
.
info
(
"No {} found, according system property."
,
config
);
return
null
;
if
(!
properties
.
isEmpty
())
{
ConfigInitializer
.
initialize
(
properties
,
Config
.
class
);
}
}
/**
...
...
@@ -120,7 +122,8 @@ public class SnifferConfigInitializer {
File
configFile
=
new
File
(
AgentPackagePath
.
getPath
(),
CONFIG_FILE_NAME
);
if
(
configFile
.
exists
()
&&
configFile
.
isFile
())
{
try
{
logger
.
info
(
"{} file found in agent folder."
,
CONFIG_FILE_NAME
);
SystemOutWriter
.
INSTANCE
.
write
(
CONFIG_FILE_NAME
+
" file found in agent folder."
);
return
new
FileInputStream
(
configFile
);
}
catch
(
FileNotFoundException
e
)
{
throw
new
ConfigNotFoundException
(
"Fail to load agent.config"
,
e
);
...
...
apm-sniffer/apm-agent-core/src/test/java/org/skywalking/apm/agent/core/conf/SnifferConfigInitializerTest.java
浏览文件 @
d8e21adc
...
...
@@ -20,6 +20,7 @@ package org.skywalking.apm.agent.core.conf;
import
org.junit.AfterClass
;
import
org.junit.Test
;
import
org.skywalking.apm.agent.core.boot.AgentPackageNotFoundException
;
import
org.skywalking.apm.agent.core.logging.LogLevel
;
import
static
org
.
hamcrest
.
CoreMatchers
.
is
;
...
...
@@ -28,9 +29,10 @@ import static org.hamcrest.MatcherAssert.assertThat;
public
class
SnifferConfigInitializerTest
{
@Test
public
void
testLoadConfigFromJavaAgentDir
()
{
System
.
setProperty
(
"applicationCode"
,
"testApp"
);
System
.
setProperty
(
"servers"
,
"127.0.0.1:8090"
);
public
void
testLoadConfigFromJavaAgentDir
()
throws
AgentPackageNotFoundException
,
ConfigNotFoundException
{
System
.
setProperty
(
"skywalking.agent.application_code"
,
"testApp"
);
System
.
setProperty
(
"skywalking.collector.servers"
,
"127.0.0.1:8090"
);
System
.
setProperty
(
"skywalking.logging.level"
,
"info"
);
SnifferConfigInitializer
.
initialize
();
assertThat
(
Config
.
Agent
.
APPLICATION_CODE
,
is
(
"testApp"
));
assertThat
(
Config
.
Collector
.
SERVERS
,
is
(
"127.0.0.1:8090"
));
...
...
apm-sniffer/apm-agent-core/src/test/resources/
sky-walking
.config
→
apm-sniffer/apm-agent-core/src/test/resources/
config/agent
.config
浏览文件 @
d8e21adc
agent
.
application_code
=
crmApp
collector
.
servers
=
127
.
0
.
0
.
1
:
8080
collector
.
servers
=
127
.
0
.
0
.
1
:
8080
logging
.
level
=
info
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录