Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
月轩居士
SkyWalking
提交
4a98bc94
S
SkyWalking
项目概览
月轩居士
/
SkyWalking
与 Fork 源项目一致
Fork自
apache / SkyWalking
通知
4
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,发现更多精彩内容 >>
未验证
提交
4a98bc94
编写于
12月 28, 2017
作者:
wu-sheng
提交者:
GitHub
12月 28, 2017
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #709 from ascrutae/fix/okhttp-plugin-issue
[Agent] Adjust the buired point of okhttp plugin
上级
41e5160c
fcbc8e57
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
252 addition
and
43 deletion
+252
-43
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/AsyncCallInterceptor.java
...skywalking/apm/plugin/okhttp/v3/AsyncCallInterceptor.java
+103
-0
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnhanceRequiredInfo.java
.../skywalking/apm/plugin/okhttp/v3/EnhanceRequiredInfo.java
+47
-0
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnqueueInterceptor.java
...e/skywalking/apm/plugin/okhttp/v3/EnqueueInterceptor.java
+19
-33
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnFailureInterceptor.java
...skywalking/apm/plugin/okhttp/v3/OnFailureInterceptor.java
+1
-5
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnResponseInterceptor.java
...kywalking/apm/plugin/okhttp/v3/OnResponseInterceptor.java
+12
-5
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AsyncCallInstrumentation.java
...apm/plugin/okhttp/v3/define/AsyncCallInstrumentation.java
+69
-0
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/resources/skywalking-plugin.def
...khttp-3.x-plugin/src/main/resources/skywalking-plugin.def
+1
-0
未找到文件。
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/AsyncCallInterceptor.java
0 → 100644
浏览文件 @
4a98bc94
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.okhttp.v3
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
okhttp3.Headers
;
import
okhttp3.HttpUrl
;
import
okhttp3.Request
;
import
org.apache.skywalking.apm.agent.core.context.CarrierItem
;
import
org.apache.skywalking.apm.agent.core.context.ContextCarrier
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.context.tag.Tags
;
import
org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan
;
import
org.apache.skywalking.apm.agent.core.context.trace.SpanLayer
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.network.trace.component.ComponentsDefine
;
/**
* {@link AsyncCallInterceptor} get the `EnhanceRequiredInfo` instance from `SkyWalkingDynamicField` and then put it
* into `AsyncCall` instance when the `AsyncCall` constructor called.
*
* {@link AsyncCallInterceptor} also create an exit span by using the `EnhanceRequiredInfo` when the `execute` method
* called.
*
* @author zhangxin
*/
public
class
AsyncCallInterceptor
implements
InstanceConstructorInterceptor
,
InstanceMethodsAroundInterceptor
{
@Override
public
void
onConstruct
(
EnhancedInstance
objInst
,
Object
[]
allArguments
)
{
/**
* The first argument of constructor is not the `real` parameter when the enhance class is an inner class. This
* is the JDK compiler mechanism.
*/
EnhancedInstance
realCallInstance
=
(
EnhancedInstance
)
allArguments
[
1
];
Object
enhanceRequireInfo
=
realCallInstance
.
getSkyWalkingDynamicField
();
objInst
.
setSkyWalkingDynamicField
(
enhanceRequireInfo
);
}
@Override
public
void
beforeMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
MethodInterceptResult
result
)
throws
Throwable
{
EnhanceRequiredInfo
enhanceRequiredInfo
=
(
EnhanceRequiredInfo
)
objInst
.
getSkyWalkingDynamicField
();
Request
request
=
(
Request
)
enhanceRequiredInfo
.
getRealCallEnhance
().
getSkyWalkingDynamicField
();
ContextCarrier
contextCarrier
=
new
ContextCarrier
();
HttpUrl
requestUrl
=
request
.
url
();
AbstractSpan
span
=
ContextManager
.
createExitSpan
(
requestUrl
.
uri
().
getPath
(),
contextCarrier
,
requestUrl
.
host
()
+
":"
+
requestUrl
.
port
());
span
.
setComponent
(
ComponentsDefine
.
OKHTTP
);
Tags
.
HTTP
.
METHOD
.
set
(
span
,
request
.
method
());
Tags
.
URL
.
set
(
span
,
requestUrl
.
uri
().
toString
());
SpanLayer
.
asHttp
(
span
);
Field
headersField
=
Request
.
class
.
getDeclaredField
(
"headers"
);
Field
modifiersField
=
Field
.
class
.
getDeclaredField
(
"modifiers"
);
modifiersField
.
setAccessible
(
true
);
modifiersField
.
setInt
(
headersField
,
headersField
.
getModifiers
()
&
~
Modifier
.
FINAL
);
headersField
.
setAccessible
(
true
);
Headers
.
Builder
headerBuilder
=
request
.
headers
().
newBuilder
();
CarrierItem
next
=
contextCarrier
.
items
();
while
(
next
.
hasNext
())
{
next
=
next
.
next
();
headerBuilder
.
add
(
next
.
getHeadKey
(),
next
.
getHeadValue
());
}
headersField
.
set
(
request
,
headerBuilder
.
build
());
ContextManager
.
continued
(
enhanceRequiredInfo
.
getContextSnapshot
());
}
@Override
public
Object
afterMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Object
ret
)
throws
Throwable
{
ContextManager
.
stopSpan
();
return
ret
;
}
@Override
public
void
handleMethodException
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Throwable
t
)
{
ContextManager
.
activeSpan
().
log
(
t
);
}
}
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnhanceRequiredInfo.java
0 → 100644
浏览文件 @
4a98bc94
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.okhttp.v3
;
import
org.apache.skywalking.apm.agent.core.context.ContextSnapshot
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
/**
* {@link EnhanceRequiredInfo} storage the `ContextSnapshot` and `RealCall` instances for support the async function of
* okhttp client.
*
* @author zhangxin
*/
public
class
EnhanceRequiredInfo
{
private
ContextSnapshot
contextSnapshot
;
private
EnhancedInstance
realCallEnhance
;
public
EnhanceRequiredInfo
(
EnhancedInstance
realCallEnhance
,
ContextSnapshot
contextSnapshot
)
{
this
.
contextSnapshot
=
contextSnapshot
;
this
.
realCallEnhance
=
realCallEnhance
;
}
public
ContextSnapshot
getContextSnapshot
()
{
return
contextSnapshot
;
}
public
EnhancedInstance
getRealCallEnhance
()
{
return
realCallEnhance
;
}
}
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/EnqueueInterceptor.java
浏览文件 @
4a98bc94
...
...
@@ -18,54 +18,40 @@
package
org.apache.skywalking.apm.plugin.okhttp.v3
;
import
java.lang.reflect.Field
;
import
java.lang.reflect.Method
;
import
java.lang.reflect.Modifier
;
import
okhttp3.Headers
;
import
okhttp3.HttpUrl
;
import
okhttp3.Request
;
import
org.apache.skywalking.apm.agent.core.context.CarrierItem
;
import
org.apache.skywalking.apm.agent.core.context.ContextCarrier
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.context.tag.Tags
;
import
org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan
;
import
org.apache.skywalking.apm.agent.core.context.trace.SpanLayer
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceConstructorInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.network.trace.component.ComponentsDefine
;
/**
* {@link EnqueueInterceptor} create a local span and the prefix of the span operation name is start with `Async` when
* the `enqueue` method called and also put the `ContextSnapshot` and `RealCall` instance into the
* `SkyWalkingDynamicField`.
*
* @author zhangxin
*/
public
class
EnqueueInterceptor
implements
InstanceMethodsAroundInterceptor
,
InstanceConstructorInterceptor
{
@Override
public
void
beforeMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
MethodInterceptResult
result
)
throws
Throwable
{
EnhancedInstance
callbackInstance
=
(
EnhancedInstance
)
allArguments
[
0
];
Request
request
=
(
Request
)
objInst
.
getSkyWalkingDynamicField
();
ContextManager
.
createLocalSpan
(
"Async"
+
request
.
url
().
uri
().
getPath
());
ContextCarrier
contextCarrier
=
new
ContextCarrier
();
HttpUrl
requestUrl
=
request
.
url
();
AbstractSpan
span
=
ContextManager
.
createExitSpan
(
requestUrl
.
uri
().
getPath
(),
contextCarrier
,
requestUrl
.
host
()
+
":"
+
requestUrl
.
port
());
span
.
setComponent
(
ComponentsDefine
.
OKHTTP
);
Tags
.
HTTP
.
METHOD
.
set
(
span
,
request
.
method
());
Tags
.
URL
.
set
(
span
,
requestUrl
.
uri
().
toString
());
SpanLayer
.
asHttp
(
span
);
Field
headersField
=
Request
.
class
.
getDeclaredField
(
"headers"
);
Field
modifiersField
=
Field
.
class
.
getDeclaredField
(
"modifiers"
);
modifiersField
.
setAccessible
(
true
);
modifiersField
.
setInt
(
headersField
,
headersField
.
getModifiers
()
&
~
Modifier
.
FINAL
);
headersField
.
setAccessible
(
true
);
Headers
.
Builder
headerBuilder
=
request
.
headers
().
newBuilder
();
CarrierItem
next
=
contextCarrier
.
items
();
while
(
next
.
hasNext
())
{
next
=
next
.
next
();
headerBuilder
.
add
(
next
.
getHeadKey
(),
next
.
getHeadValue
());
}
headersField
.
set
(
request
,
headerBuilder
.
build
());
/**
* Here is the process about how to trace the async function.
*
* 1. Storage `Request` object into `RealCall` instance when the constructor of `RealCall` called.
* 2. Put the `RealCall` instance to `CallBack` instance
* 3. Get the `RealCall` instance from `CallBack` and then Put the `RealCall` into `AsyncCall` instance
* since the constructor of `RealCall` called.
* 5. Create the exit span by using the `RealCall` instance when `AsyncCall` method called.
*/
objInst
.
setSkyWalkingDynamicField
(
ContextManager
.
capture
(
));
callbackInstance
.
setSkyWalkingDynamicField
(
new
EnhanceRequiredInfo
(
objInst
,
ContextManager
.
capture
()
));
}
@Override
...
...
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnFailureInterceptor.java
浏览文件 @
4a98bc94
...
...
@@ -20,7 +20,6 @@ package org.apache.skywalking.apm.plugin.okhttp.v3;
import
java.lang.reflect.Method
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.context.ContextSnapshot
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
...
...
@@ -29,15 +28,12 @@ public class OnFailureInterceptor implements InstanceMethodsAroundInterceptor {
@Override
public
void
beforeMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
MethodInterceptResult
result
)
throws
Throwable
{
EnhancedInstance
realCallInstance
=
(
EnhancedInstance
)
allArguments
[
0
];
ContextManager
.
createLocalSpan
(
"CallBack/AsyncCall"
).
errorOccurred
();
ContextManager
.
continued
((
ContextSnapshot
)
realCallInstance
.
getSkyWalkingDynamicField
());
ContextManager
.
activeSpan
().
errorOccurred
();
}
@Override
public
Object
afterMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Object
ret
)
throws
Throwable
{
ContextManager
.
stopSpan
();
return
ret
;
}
...
...
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/OnResponseInterceptor.java
浏览文件 @
4a98bc94
...
...
@@ -19,25 +19,32 @@
package
org.apache.skywalking.apm.plugin.okhttp.v3
;
import
java.lang.reflect.Method
;
import
okhttp3.Response
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.context.ContextSnapshot
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.EnhancedInstance
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstanceMethodsAroundInterceptor
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
/**
* {@link OnResponseInterceptor} validate the response code if it is great equal than 400. if so. the transaction status
* chang to `error`, or do nothing.
*
* @author zhangxin
*/
public
class
OnResponseInterceptor
implements
InstanceMethodsAroundInterceptor
{
@Override
public
void
beforeMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
MethodInterceptResult
result
)
throws
Throwable
{
EnhancedInstance
realCallInstance
=
(
EnhancedInstance
)
allArguments
[
0
];
ContextManager
.
createLocalSpan
(
"CallBack/AsyncCall"
);
ContextManager
.
continued
((
ContextSnapshot
)
realCallInstance
.
getSkyWalkingDynamicField
());
Response
response
=
(
Response
)
allArguments
[
1
];
if
(
response
.
code
()
>=
400
)
{
ContextManager
.
activeSpan
().
errorOccurred
();
}
}
@Override
public
Object
afterMethod
(
EnhancedInstance
objInst
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
argumentsTypes
,
Object
ret
)
throws
Throwable
{
ContextManager
.
stopSpan
();
return
ret
;
}
...
...
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/java/org/apache/skywalking/apm/plugin/okhttp/v3/define/AsyncCallInstrumentation.java
0 → 100644
浏览文件 @
4a98bc94
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package
org.apache.skywalking.apm.plugin.okhttp.v3.define
;
import
net.bytebuddy.description.method.MethodDescription
;
import
net.bytebuddy.matcher.ElementMatcher
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.ConstructorInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.InstanceMethodsInterceptPoint
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine
;
import
org.apache.skywalking.apm.agent.core.plugin.match.ClassMatch
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
any
;
import
static
net
.
bytebuddy
.
matcher
.
ElementMatchers
.
named
;
import
static
org
.
apache
.
skywalking
.
apm
.
agent
.
core
.
plugin
.
match
.
NameMatch
.
byName
;
public
class
AsyncCallInstrumentation
extends
ClassInstanceMethodsEnhancePluginDefine
{
@Override
protected
ConstructorInterceptPoint
[]
getConstructorsInterceptPoints
()
{
return
new
ConstructorInterceptPoint
[]
{
new
ConstructorInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getConstructorMatcher
()
{
return
any
();
}
@Override
public
String
getConstructorInterceptor
()
{
return
"org.apache.skywalking.apm.plugin.okhttp.v3.AsyncCallInterceptor"
;
}
}
};
}
@Override
protected
InstanceMethodsInterceptPoint
[]
getInstanceMethodsInterceptPoints
()
{
return
new
InstanceMethodsInterceptPoint
[]
{
new
InstanceMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
"execute"
);
}
@Override
public
String
getMethodsInterceptor
()
{
return
"org.apache.skywalking.apm.plugin.okhttp.v3.AsyncCallInterceptor"
;
}
@Override
public
boolean
isOverrideArgs
()
{
return
false
;
}
}
};
}
@Override
protected
ClassMatch
enhanceClass
()
{
return
byName
(
"okhttp3.RealCall$AsyncCall"
);
}
}
apm-sniffer/apm-sdk-plugin/okhttp-3.x-plugin/src/main/resources/skywalking-plugin.def
浏览文件 @
4a98bc94
okhttp-3.x=org.apache.skywalking.apm.plugin.okhttp.v3.define.RealCallInstrumentation
okhttp-3.x=org.apache.skywalking.apm.plugin.okhttp.v3.define.CallbackInstrumentation
okhttp-3.x=org.apache.skywalking.apm.plugin.okhttp.v3.define.AsyncCallInstrumentation
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录