Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
阿信在这里
SkyWalking
提交
b0cc9138
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,发现更多精彩内容 >>
未验证
提交
b0cc9138
编写于
1月 16, 2021
作者:
E
Evan
提交者:
GitHub
1月 16, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
export the segmentId and spanId in toolkit (#6213)
上级
98d0430b
变更
14
隐藏空白更改
内联
并排
Showing
14 changed file
with
258 addition
and
14 deletion
+258
-14
CHANGES.md
CHANGES.md
+1
-0
apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/TraceContext.java
...org/apache/skywalking/apm/toolkit/trace/TraceContext.java
+18
-0
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java
...walking/apm/agent/core/context/AbstractTracerContext.java
+14
-0
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
...che/skywalking/apm/agent/core/context/ContextManager.java
+20
-6
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java
...ywalking/apm/agent/core/context/IgnoredTracerContext.java
+12
-1
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
...che/skywalking/apm/agent/core/context/TracingContext.java
+10
-0
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/SegmentIDInterceptor.java
...ng/apm/toolkit/activation/trace/SegmentIDInterceptor.java
+49
-0
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/SpanIDInterceptor.java
...lking/apm/toolkit/activation/trace/SpanIDInterceptor.java
+49
-0
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceContextActivation.java
.../apm/toolkit/activation/trace/TraceContextActivation.java
+41
-4
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceIDInterceptor.java
...king/apm/toolkit/activation/trace/TraceIDInterceptor.java
+2
-2
docs/en/setup/service-agent/java-agent/Application-toolkit-trace.md
...tup/service-agent/java-agent/Application-toolkit-trace.md
+17
-0
test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml
...arios/apm-toolkit-trace-scenario/config/expectedData.yaml
+3
-0
test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/TraceContext.java
...org/apache/skywalking/apm/toolkit/trace/TraceContext.java
+18
-0
test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestController.java
...lking/apm/testcase/toolkit/controller/TestController.java
+4
-1
未找到文件。
CHANGES.md
浏览文件 @
b0cc9138
...
...
@@ -32,6 +32,7 @@ Release Notes.
*
Fix bug that rocketmq-plugin set the wrong tag.
*
Fix duplicated
`EnhancedInstance`
interface added.
*
Fix thread leaks caused by the elasticsearch-6.x-plugin plugin.
*
Support reading segmentId and spanId with toolkit.
#### OAP-Backend
...
...
apm-application-toolkit/apm-toolkit-trace/src/main/java/org/apache/skywalking/apm/toolkit/trace/TraceContext.java
浏览文件 @
b0cc9138
...
...
@@ -36,6 +36,24 @@ public class TraceContext {
return
""
;
}
/**
* Try to get the segmentId of current trace context.
*
* @return segmentId, if it exists, or empty {@link String}.
*/
public
static
String
segmentId
()
{
return
""
;
}
/**
* Try to get the spanId of current trace context.
*
* @return spanId, if it exists, or empty {@link String}.
*/
public
static
String
spanId
()
{
return
""
;
}
/**
* Try to get the custom value from trace context.
*
...
...
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/AbstractTracerContext.java
浏览文件 @
b0cc9138
...
...
@@ -62,6 +62,20 @@ public interface AbstractTracerContext {
*/
String
getReadablePrimaryTraceId
();
/**
* Get the current segment id, if needEnhance. How to build, depends on the implementation.
*
* @return the string represents the id.
*/
String
getSegmentId
();
/**
* Get the active span id, if needEnhance. How to build, depends on the implementation.
*
* @return the string represents the id.
*/
String
getSpanId
();
/**
* Create an entry span
*
...
...
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/ContextManager.java
浏览文件 @
b0cc9138
...
...
@@ -18,6 +18,7 @@
package
org.apache.skywalking.apm.agent.core.context
;
import
java.util.Objects
;
import
org.apache.skywalking.apm.agent.core.boot.BootService
;
import
org.apache.skywalking.apm.agent.core.boot.ServiceManager
;
import
org.apache.skywalking.apm.agent.core.context.trace.AbstractSpan
;
...
...
@@ -38,6 +39,7 @@ import static org.apache.skywalking.apm.agent.core.conf.Config.Agent.OPERATION_N
* <p> Also, {@link ContextManager} delegates to all {@link AbstractTracerContext}'s major methods.
*/
public
class
ContextManager
implements
BootService
{
private
static
final
String
EMPTY_TRACE_CONTEXT_ID
=
"N/A"
;
private
static
final
ILog
LOGGER
=
LogManager
.
getLogger
(
ContextManager
.
class
);
private
static
ThreadLocal
<
AbstractTracerContext
>
CONTEXT
=
new
ThreadLocal
<
AbstractTracerContext
>();
private
static
ThreadLocal
<
RuntimeContext
>
RUNTIME_CONTEXT
=
new
ThreadLocal
<
RuntimeContext
>();
...
...
@@ -71,12 +73,24 @@ public class ContextManager implements BootService {
* @return the first global trace id if needEnhance. Otherwise, "N/A".
*/
public
static
String
getGlobalTraceId
()
{
AbstractTracerContext
segment
=
CONTEXT
.
get
();
if
(
segment
==
null
)
{
return
"N/A"
;
}
else
{
return
segment
.
getReadablePrimaryTraceId
();
}
AbstractTracerContext
context
=
CONTEXT
.
get
();
return
Objects
.
nonNull
(
context
)
?
context
.
getReadablePrimaryTraceId
()
:
EMPTY_TRACE_CONTEXT_ID
;
}
/**
* @return the current segment id if needEnhance. Otherwise, "N/A".
*/
public
static
String
getSegmentId
()
{
AbstractTracerContext
context
=
CONTEXT
.
get
();
return
Objects
.
nonNull
(
context
)
?
context
.
getSegmentId
()
:
EMPTY_TRACE_CONTEXT_ID
;
}
/**
* @return the current span id if needEnhance. Otherwise, "N/A".
*/
public
static
String
getSpanId
()
{
AbstractTracerContext
context
=
CONTEXT
.
get
();
return
Objects
.
nonNull
(
context
)
?
context
.
getSpanId
()
:
EMPTY_TRACE_CONTEXT_ID
;
}
public
static
AbstractSpan
createEntrySpan
(
String
operationName
,
ContextCarrier
carrier
)
{
...
...
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/IgnoredTracerContext.java
浏览文件 @
b0cc9138
...
...
@@ -31,6 +31,7 @@ import org.apache.skywalking.apm.agent.core.context.trace.NoopSpan;
*/
public
class
IgnoredTracerContext
implements
AbstractTracerContext
{
private
static
final
NoopSpan
NOOP_SPAN
=
new
NoopSpan
();
private
static
final
String
IGNORE_TRACE
=
"Ignored_Trace"
;
private
final
CorrelationContext
correlationContext
;
private
final
ExtensionContext
extensionContext
;
...
...
@@ -65,7 +66,17 @@ public class IgnoredTracerContext implements AbstractTracerContext {
@Override
public
String
getReadablePrimaryTraceId
()
{
return
"Ignored_Trace"
;
return
IGNORE_TRACE
;
}
@Override
public
String
getSegmentId
()
{
return
IGNORE_TRACE
;
}
@Override
public
String
getSpanId
()
{
return
IGNORE_TRACE
;
}
@Override
...
...
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/context/TracingContext.java
浏览文件 @
b0cc9138
...
...
@@ -243,6 +243,16 @@ public class TracingContext implements AbstractTracerContext {
return
segment
.
getRelatedGlobalTraces
().
get
(
0
);
}
@Override
public
String
getSegmentId
()
{
return
segment
.
getTraceSegmentId
();
}
@Override
public
String
getSpanId
()
{
return
String
.
valueOf
(
activeSpan
().
getSpanId
());
}
/**
* Create an entry span
*
...
...
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/SegmentIDInterceptor.java
0 → 100644
浏览文件 @
b0cc9138
/*
* 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.toolkit.activation.trace
;
import
java.lang.reflect.Method
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.logging.api.ILog
;
import
org.apache.skywalking.apm.agent.core.logging.api.LogManager
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor
;
public
class
SegmentIDInterceptor
implements
StaticMethodsAroundInterceptor
{
private
static
final
ILog
LOGGER
=
LogManager
.
getLogger
(
SegmentIDInterceptor
.
class
);
@Override
public
void
beforeMethod
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
MethodInterceptResult
result
)
{
result
.
defineReturnValue
(
ContextManager
.
getSegmentId
());
}
@Override
public
Object
afterMethod
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
Object
ret
)
{
return
ret
;
}
@Override
public
void
handleMethodException
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
Throwable
t
)
{
LOGGER
.
error
(
"Failed to getDefault segment Id."
,
t
);
}
}
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/SpanIDInterceptor.java
0 → 100644
浏览文件 @
b0cc9138
/*
* 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.toolkit.activation.trace
;
import
java.lang.reflect.Method
;
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.logging.api.ILog
;
import
org.apache.skywalking.apm.agent.core.logging.api.LogManager
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMethodsAroundInterceptor
;
public
class
SpanIDInterceptor
implements
StaticMethodsAroundInterceptor
{
private
static
final
ILog
LOGGER
=
LogManager
.
getLogger
(
SpanIDInterceptor
.
class
);
@Override
public
void
beforeMethod
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
MethodInterceptResult
result
)
{
result
.
defineReturnValue
(
ContextManager
.
getSpanId
());
}
@Override
public
Object
afterMethod
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
Object
ret
)
{
return
ret
;
}
@Override
public
void
handleMethodException
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
Throwable
t
)
{
LOGGER
.
error
(
"Failed to getDefault span Id."
,
t
);
}
}
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/TraceContextActivation.java
浏览文件 @
b0cc9138
...
...
@@ -35,9 +35,13 @@ import static net.bytebuddy.matcher.ElementMatchers.named;
*/
public
class
TraceContextActivation
extends
ClassStaticMethodsEnhancePluginDefine
{
public
static
final
String
INTERCEPT_CLASS
=
"org.apache.skywalking.apm.toolkit.activation.trace.TraceContextInterceptor"
;
public
static
final
String
TRACE_ID_INTERCEPT_CLASS
=
"org.apache.skywalking.apm.toolkit.activation.trace.TraceIDInterceptor"
;
public
static
final
String
SEGMENT_ID_INTERCEPT_CLASS
=
"org.apache.skywalking.apm.toolkit.activation.trace.SegmentIDInterceptor"
;
public
static
final
String
SPAN_ID_INTERCEPT_CLASS
=
"org.apache.skywalking.apm.toolkit.activation.trace.SpanIDInterceptor"
;
public
static
final
String
ENHANCE_CLASS
=
"org.apache.skywalking.apm.toolkit.trace.TraceContext"
;
public
static
final
String
ENHANCE_METHOD
=
"traceId"
;
public
static
final
String
ENHANCE_TRACE_ID_METHOD
=
"traceId"
;
public
static
final
String
ENHANCE_SEGMENT_ID_METHOD
=
"segmentId"
;
public
static
final
String
ENHANCE_SPAN_ID_METHOD
=
"spanId"
;
public
static
final
String
ENHANCE_GET_CORRELATION_METHOD
=
"getCorrelation"
;
public
static
final
String
INTERCEPT_GET_CORRELATION_CLASS
=
"org.apache.skywalking.apm.toolkit.activation.trace.CorrelationContextGetInterceptor"
;
public
static
final
String
ENHANCE_PUT_CORRELATION_METHOD
=
"putCorrelation"
;
...
...
@@ -61,12 +65,12 @@ public class TraceContextActivation extends ClassStaticMethodsEnhancePluginDefin
new
StaticMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
ENHANCE_METHOD
);
return
named
(
ENHANCE_
TRACE_ID_
METHOD
);
}
@Override
public
String
getMethodsInterceptor
()
{
return
INTERCEPT_CLASS
;
return
TRACE_ID_
INTERCEPT_CLASS
;
}
@Override
...
...
@@ -74,6 +78,39 @@ public class TraceContextActivation extends ClassStaticMethodsEnhancePluginDefin
return
false
;
}
},
new
StaticMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
ENHANCE_SEGMENT_ID_METHOD
);
}
@Override
public
String
getMethodsInterceptor
()
{
return
SEGMENT_ID_INTERCEPT_CLASS
;
}
@Override
public
boolean
isOverrideArgs
()
{
return
false
;
}
},
new
StaticMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
return
named
(
ENHANCE_SPAN_ID_METHOD
);
}
@Override
public
String
getMethodsInterceptor
()
{
return
SPAN_ID_INTERCEPT_CLASS
;
}
@Override
public
boolean
isOverrideArgs
()
{
return
false
;
}
},
new
StaticMethodsInterceptPoint
()
{
@Override
public
ElementMatcher
<
MethodDescription
>
getMethodsMatcher
()
{
...
...
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/Trace
Context
Interceptor.java
→
apm-sniffer/apm-toolkit-activation/apm-toolkit-trace-activation/src/main/java/org/apache/skywalking/apm/toolkit/activation/trace/Trace
ID
Interceptor.java
浏览文件 @
b0cc9138
...
...
@@ -25,9 +25,9 @@ import org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.StaticMet
import
org.apache.skywalking.apm.agent.core.context.ContextManager
;
import
org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.MethodInterceptResult
;
public
class
Trace
Context
Interceptor
implements
StaticMethodsAroundInterceptor
{
public
class
Trace
ID
Interceptor
implements
StaticMethodsAroundInterceptor
{
private
static
final
ILog
LOGGER
=
LogManager
.
getLogger
(
Trace
Context
Interceptor
.
class
);
private
static
final
ILog
LOGGER
=
LogManager
.
getLogger
(
Trace
ID
Interceptor
.
class
);
@Override
public
void
beforeMethod
(
Class
clazz
,
Method
method
,
Object
[]
allArguments
,
Class
<?>[]
parameterTypes
,
...
...
docs/en/setup/service-agent/java-agent/Application-toolkit-trace.md
浏览文件 @
b0cc9138
...
...
@@ -14,6 +14,23 @@ import TraceContext;
modelAndView
.
addObject
(
"traceId"
,
TraceContext
.
traceId
());
```
*
Use
`TraceContext.segmentId()`
API to obtain segmentId.
```
java
import
TraceContext
;
...
modelAndView
.
addObject
(
"segmentId"
,
TraceContext
.
segmentId
());
```
*
Use
`TraceContext.spanId()`
API to obtain spanId.
```
java
import
TraceContext
;
...
modelAndView
.
addObject
(
"spanId"
,
TraceContext
.
spanId
());
```
_Sample codes only_
*
Add
`@Trace`
to any method you want to trace. After that, you can see the span in the Stack.
...
...
test/plugin/scenarios/apm-toolkit-trace-scenario/config/expectedData.yaml
浏览文件 @
b0cc9138
...
...
@@ -143,6 +143,9 @@ segmentItems:
tags
:
-
{
key
:
url
,
value
:
'
http://localhost:8080/apm-toolkit-trace-scenario/case/tool-kit'
}
-
{
key
:
http.method
,
value
:
GET
}
-
{
key
:
traceID
,
value
:
not null
}
-
{
key
:
segmentID
,
value
:
not null
}
-
{
key
:
spanID
,
value
:
not null
}
skipAnalysis
:
'
true'
refs
:
-
{
parentEndpoint
:
/app
,
networkAddress
:
'
127.0.0.1:8080'
,
refType
:
CrossProcess
,
...
...
test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/org/apache/skywalking/apm/toolkit/trace/TraceContext.java
浏览文件 @
b0cc9138
...
...
@@ -36,6 +36,24 @@ public class TraceContext {
return
""
;
}
/**
* Try to get the segmentId of current trace context.
*
* @return segmentId, if it exists, or empty {@link String}.
*/
public
static
String
segmentId
()
{
return
""
;
}
/**
* Try to get the spanId of current trace context.
*
* @return spanId, if it exists, or empty {@link String}.
*/
public
static
String
spanId
()
{
return
""
;
}
/**
* Try to get the custom value from trace context.
*
...
...
test/plugin/scenarios/apm-toolkit-trace-scenario/src/main/java/test/apache/skywalking/apm/testcase/toolkit/controller/TestController.java
浏览文件 @
b0cc9138
...
...
@@ -59,6 +59,9 @@ public class TestController {
testService
.
testTagAnnotation
(
"testTagAnnotationParam1"
,
"testTagAnnotationParam2"
);
testService
.
testTagAnnotationReturnInfo
(
"zhangsan"
,
15
);
TraceContext
.
putCorrelation
(
CORRELATION_CONTEXT_KEY
,
CORRELATION_CONTEXT_VALUE
);
ActiveSpan
.
tag
(
"traceID"
,
TraceContext
.
traceId
());
ActiveSpan
.
tag
(
"segmentID"
,
TraceContext
.
segmentId
());
ActiveSpan
.
tag
(
"spanID"
,
TraceContext
.
spanId
());
testService
.
asyncCallable
(()
->
{
visit
(
"http://localhost:8080/apm-toolkit-trace-scenario/case/asyncVisit/callable"
);
return
true
;
...
...
@@ -83,7 +86,7 @@ public class TestController {
MeterFactory
.
counter
(
"test_counter"
).
tag
(
"ck1"
,
"cv1"
).
build
().
increment
(
2
d
);
MeterFactory
.
gauge
(
"test_gauge"
,
()
->
1
d
).
tag
(
"gk1"
,
"gv1"
).
build
();
MeterFactory
.
histogram
(
"test_histogram"
).
tag
(
"hk1"
,
"hv1"
).
steps
(
Arrays
.
asList
(
1
d
,
5
d
,
10
d
))
.
build
().
addValue
(
4
d
);
.
build
().
addValue
(
4
d
);
return
SUCCESS
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录