Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
apache
SkyWalking
提交
2a660950
S
SkyWalking
项目概览
apache
/
SkyWalking
上一次同步 1 年多
通知
302
Star
21345
Fork
6091
代码
文件
提交
分支
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,发现更多精彩内容 >>
提交
2a660950
编写于
2月 17, 2017
作者:
wu-sheng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Initialize TraceSegment, TraceSegmentRef, Span, Context, Carrier Class.
上级
00127403
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
285 addition
and
14 deletion
+285
-14
skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/Span.java
...-trace/src/main/java/com/a/eye/skywalking/trace/Span.java
+16
-1
skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java
...rc/main/java/com/a/eye/skywalking/trace/TraceSegment.java
+4
-0
skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java
...main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java
+32
-1
skywalking-commons/skywalking-util/src/main/java/com/a/eye/skywalking/util/StringUtil.java
...l/src/main/java/com/a/eye/skywalking/util/StringUtil.java
+21
-0
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/conf/Constants.java
...pi/src/main/java/com/a/eye/skywalking/conf/Constants.java
+1
-3
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/trace/ContextCarrier.java
.../main/java/com/a/eye/skywalking/trace/ContextCarrier.java
+44
-0
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/trace/ContextManager.java
.../main/java/com/a/eye/skywalking/trace/ContextManager.java
+26
-0
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/trace/TracerContext.java
...c/main/java/com/a/eye/skywalking/trace/TracerContext.java
+135
-0
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/util/TraceIdGenerator.java
...main/java/com/a/eye/skywalking/util/TraceIdGenerator.java
+6
-9
未找到文件。
skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/Span.java
浏览文件 @
2a660950
...
...
@@ -71,7 +71,7 @@ public class Span {
* Value -1 means no parent span if this {@link TraceSegment}.
* @param operationName {@link #operationName}
*/
p
ublic
Span
(
int
spanId
,
int
parentSpanId
,
String
operationName
){
p
rivate
Span
(
int
spanId
,
int
parentSpanId
,
String
operationName
){
this
.
spanId
=
spanId
;
this
.
parentSpanId
=
parentSpanId
;
this
.
startTime
=
System
.
currentTimeMillis
();
...
...
@@ -91,6 +91,17 @@ public class Span {
this
(
spanId
,
-
1
,
operationName
);
}
/**
* Create a new span, by given span id and given parent {@link Span}.
*
* @param spanId given by the creator, and must be unique id in the {@link TraceSegment}
* @param parentSpan {@link Span}
* @param operationName {@link #operationName}
*/
public
Span
(
int
spanId
,
Span
parentSpan
,
String
operationName
){
this
(
spanId
,
parentSpan
.
spanId
,
operationName
);
}
/**
* Finish the active Span.
* When it is finished, it will be archived by the given {@link TraceSegment}, which owners it.
...
...
@@ -177,4 +188,8 @@ public class Span {
log
(
Collections
.
singletonMap
(
"event"
,
event
));
return
this
;
}
public
int
getSpanId
()
{
return
spanId
;
}
}
skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegment.java
浏览文件 @
2a660950
...
...
@@ -96,4 +96,8 @@ public class TraceSegment {
public
void
finish
(){
this
.
endTime
=
System
.
currentTimeMillis
();
}
public
String
getTraceSegmentId
()
{
return
traceSegmentId
;
}
}
skywalking-commons/skywalking-trace/src/main/java/com/a/eye/skywalking/trace/TraceSegmentRef.java
浏览文件 @
2a660950
package
com.a.eye.skywalking.trace
;
/**
*
*
{@link TraceSegmentRef} is like a pointer, which ref to another {@link TraceSegment}.
*
* Created by wusheng on 2017/2/17.
*/
public
class
TraceSegmentRef
{
/**
* {@link TraceSegment#traceSegmentId}
*/
private
String
traceSegmentId
;
/**
* {@link Span#spanId}
*/
private
int
spanId
;
/**
* Create a {@link TraceSegmentRef} instance, without any data.
*/
public
TraceSegmentRef
()
{
}
public
String
getTraceSegmentId
()
{
return
traceSegmentId
;
}
public
void
setTraceSegmentId
(
String
traceSegmentId
)
{
this
.
traceSegmentId
=
traceSegmentId
;
}
public
int
getSpanId
()
{
return
spanId
;
}
public
void
setSpanId
(
int
spanId
)
{
this
.
spanId
=
spanId
;
}
}
skywalking-commons/skywalking-util/src/main/java/com/a/eye/skywalking/util/StringUtil.java
浏览文件 @
2a660950
...
...
@@ -7,4 +7,25 @@ public final class StringUtil {
}
return
false
;
}
public
static
String
join
(
final
char
delimiter
,
final
String
...
strings
)
{
if
(
strings
.
length
==
0
)
{
return
null
;
}
if
(
strings
.
length
==
1
)
{
return
strings
[
0
];
}
int
length
=
strings
.
length
-
1
;
for
(
final
String
s
:
strings
)
{
length
+=
s
.
length
();
}
final
StringBuilder
sb
=
new
StringBuilder
(
length
);
sb
.
append
(
strings
[
0
]);
for
(
int
i
=
1
;
i
<
strings
.
length
;
++
i
)
{
if
(!
isEmpty
(
strings
[
i
]))
{
sb
.
append
(
delimiter
).
append
(
strings
[
i
]);
}
}
return
sb
.
toString
();
}
}
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/conf/Constants.java
浏览文件 @
2a660950
...
...
@@ -7,7 +7,5 @@ public class Constants {
* This is the version, which will be the first segment of traceid.
* Ref {@link TraceIdGenerator#generate()}
*/
public
static
int
SDK_VERSION
=
212017
;
public
static
final
String
CONTEXT_DATA_SEGMENT_SPILT_CHAR
=
"#&"
;
public
static
String
SDK_VERSION
=
"302017"
;
}
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/trace/ContextCarrier.java
0 → 100644
浏览文件 @
2a660950
package
com.a.eye.skywalking.trace
;
import
com.a.eye.skywalking.util.StringUtil
;
import
java.io.Serializable
;
/**
* {@link ContextCarrier} is a data carrier of {@link TracerContext}.
* It holds the snapshot (current state) of {@link TracerContext}.
*
* Created by wusheng on 2017/2/17.
*/
public
class
ContextCarrier
extends
TraceSegmentRef
implements
Serializable
{
/**
* Serialize this {@link ContextCarrier} to a {@link String},
* with '|' split.
*
* @return the serialization string.
*/
public
String
serialize
()
{
return
StringUtil
.
join
(
'|'
,
this
.
getTraceSegmentId
(),
this
.
getSpanId
()
+
""
);
}
/**
* Initialize fields with the given text.
*
* @param text carries {@link #traceSegmentId} and {@link #spanId}, with '|' split.
*/
public
ContextCarrier
deserialize
(
String
text
)
{
if
(
text
!=
null
){
String
[]
parts
=
text
.
split
(
"|"
);
if
(
parts
.
length
==
2
){
try
{
setSpanId
(
Integer
.
parseInt
(
parts
[
1
]));
setTraceSegmentId
(
parts
[
0
]);
}
catch
(
NumberFormatException
e
){
}
}
}
return
this
;
}
}
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/trace/ContextManager.java
0 → 100644
浏览文件 @
2a660950
package
com.a.eye.skywalking.trace
;
/**
* {@link TracerContext} controls the whole context of {@link TraceSegment}. Any {@link TraceSegment} relates to
* single-thread, so this context use {@link ThreadLocal} to maintain the context, and make sure, since a {@link
* TraceSegment} starts, all ChildOf spans are in the same context.
*
* What is 'ChildOf'? {@see https://github.com/opentracing/specification/blob/master/specification.md#references-between-spans}
*
*
* Created by wusheng on 2017/2/17.
*/
public
enum
ContextManager
{
INSTANCE
;
private
static
ThreadLocal
<
TracerContext
>
CONTEXT
=
new
ThreadLocal
<>();
public
TracerContext
get
()
{
TracerContext
segment
=
CONTEXT
.
get
();
if
(
segment
==
null
)
{
segment
=
new
TracerContext
();
CONTEXT
.
set
(
segment
);
}
return
segment
;
}
}
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/trace/TracerContext.java
0 → 100644
浏览文件 @
2a660950
package
com.a.eye.skywalking.trace
;
import
com.a.eye.skywalking.util.TraceIdGenerator
;
import
java.util.ArrayList
;
import
java.util.List
;
/**
* {@link TracerContext} maintains the context.
* You manipulate (create/finish/get) spans and (inject/extract) context.
*
* Created by wusheng on 2017/2/17.
*/
public
final
class
TracerContext
{
private
TraceSegment
segment
;
/**
* Active spans stored in a Stack, usually called 'ActiveSpanStack'.
* This {@link ArrayList} is the in-memory storage-structure.
*
* I use {@link ArrayList#size()} as a pointer, to the top element of 'ActiveSpanStack'.
* And provide the top 3 important methods of stack:
* {@link #pop()}, {@link #push(Span)}, {@link #peek()}
*/
private
List
<
Span
>
activeSpanStack
=
new
ArrayList
<
Span
>(
20
);
private
int
spanIdGenerator
;
TracerContext
()
{
this
.
segment
=
new
TraceSegment
(
TraceIdGenerator
.
generate
());
this
.
spanIdGenerator
=
0
;
}
/**
* Create a new span, as an active span, by the given operationName
*
* @param operationName {@link Span#operationName}
* @return the new active span.
*/
public
Span
createSpan
(
String
operationName
)
{
Span
parentSpan
=
peek
();
Span
span
;
if
(
parentSpan
==
null
)
{
span
=
new
Span
(
spanIdGenerator
++,
operationName
);
}
else
{
span
=
new
Span
(
spanIdGenerator
++,
parentSpan
,
operationName
);
}
push
(
span
);
return
span
;
}
/**
* @return the active span of current context.
*/
public
Span
activeSpan
()
{
Span
span
=
peek
();
if
(
span
==
null
)
{
throw
new
IllegalStateException
(
"No active span."
);
}
return
span
;
}
/**
* Stop the span. And finish the {@link #segment} if all {@link #activeSpanStack} elements are finished.
*
* @param span to finish. It must the the top element of {@link #activeSpanStack}.
*/
public
void
stopSpan
(
Span
span
)
{
Span
lastSpan
=
peek
();
if
(
lastSpan
==
span
)
{
segment
.
archive
(
pop
());
}
else
{
throw
new
IllegalStateException
(
"Stopping the unexpected span = "
+
span
);
}
if
(
activeSpanStack
.
isEmpty
())
{
segment
.
finish
();
}
}
/**
* Give a snapshot of this {@link TracerContext},
* and save current state to the given {@link ContextCarrier}.
*
* @param carrier holds the snapshot
*/
private
void
inject
(
ContextCarrier
carrier
)
{
carrier
.
setTraceSegmentId
(
this
.
segment
.
getTraceSegmentId
());
carrier
.
setSpanId
(
this
.
activeSpan
().
getSpanId
());
}
/**
* Ref this {@link ContextCarrier} to this {@link TraceSegment}
*
* @param carrier holds the snapshot, if get this {@link ContextCarrier} from remote, make sure {@link
* ContextCarrier#deserialize(String)} called.
*/
private
void
extract
(
ContextCarrier
carrier
)
{
this
.
segment
.
ref
(
carrier
);
}
/**
* @return the top element of 'ActiveSpanStack', and remove it.
*/
private
Span
pop
()
{
return
activeSpanStack
.
remove
(
getTopElementIdx
());
}
/**
* Add a new Span at the top of 'ActiveSpanStack'
*
* @param span
*/
private
void
push
(
Span
span
)
{
activeSpanStack
.
add
(
activeSpanStack
.
size
(),
span
);
}
/**
* @return the top element of 'ActiveSpanStack' only.
*/
private
Span
peek
()
{
if
(
activeSpanStack
.
isEmpty
())
{
return
null
;
}
return
activeSpanStack
.
get
(
getTopElementIdx
());
}
/**
* Get the index of 'ActiveSpanStack'
*
* @return the index
*/
private
int
getTopElementIdx
()
{
return
activeSpanStack
.
size
()
-
1
;
}
}
skywalking-sniffer/skywalking-api/src/main/java/com/a/eye/skywalking/util/TraceIdGenerator.java
浏览文件 @
2a660950
package
com.a.eye.skywalking.util
;
import
java.util.UUID
;
import
com.a.eye.skywalking.conf.Constants
;
import
com.a.eye.skywalking.network.grpc.TraceId
;
import
java.util.UUID
;
public
final
class
TraceIdGenerator
{
private
static
final
ThreadLocal
<
Integer
>
ThreadTraceIdSequence
=
new
ThreadLocal
<
Integer
>(){
private
static
final
ThreadLocal
<
Integer
>
ThreadTraceIdSequence
=
new
ThreadLocal
<
Integer
>()
{
@Override
protected
Integer
initialValue
()
{
return
0
;
...
...
@@ -32,14 +30,13 @@ public final class TraceIdGenerator {
*
* @return
*/
public
static
TraceId
generate
()
{
public
static
String
generate
()
{
Integer
seq
=
ThreadTraceIdSequence
.
get
();
seq
++;
ThreadTraceIdSequence
.
set
(
seq
);
return
TraceId
.
newBuilder
().
addSegments
(
Constants
.
SDK_VERSION
)
.
addSegments
(
System
.
currentTimeMillis
()).
addSegments
(
PROCESS_UUID
)
.
addSegments
(
BuriedPointMachineUtil
.
getProcessNo
())
.
addSegments
(
Thread
.
currentThread
().
getId
()).
addSegments
(
seq
).
build
();
return
StringUtil
.
join
(
'.'
,
Constants
.
SDK_VERSION
+
""
,
System
.
currentTimeMillis
()
+
""
,
PROCESS_UUID
+
""
,
BuriedPointMachineUtil
.
getProcessNo
()
+
""
,
Thread
.
currentThread
().
getId
()
+
""
,
seq
+
""
);
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录