Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
SkyAPM
go2sky
提交
f10048c7
G
go2sky
项目概览
SkyAPM
/
go2sky
通知
2
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
G
go2sky
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
f10048c7
编写于
12月 24, 2019
作者:
Z
zhang-wei
提交者:
GitHub
12月 24, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add TraceID(ctx context.Context) (#41)
上级
01c5495c
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
72 addition
and
1 deletion
+72
-1
README.md
README.md
+8
-0
segment.go
segment.go
+10
-0
trace.go
trace.go
+17
-1
trace_test.go
trace_test.go
+37
-0
未找到文件。
README.md
浏览文件 @
f10048c7
...
...
@@ -41,6 +41,14 @@ passing `context.Background()`. We must also be sure to end this span, which wil
span
,
ctx
,
err
:=
tracer
.
CreateLocalSpan
(
context
.
Background
())
```
## Get Global TraceID
Get the
`TraceID`
of the
`activeSpan`
in the
`Context`
.
```
go
go2sky
.
TraceID
(
ctx
)
```
## Create a sub span
A sub span created as the children of root span links to its parent with
`Context`
.
...
...
segment.go
浏览文件 @
f10048c7
...
...
@@ -18,6 +18,8 @@
package
go2sky
import
(
"fmt"
"strings"
"sync/atomic"
"github.com/SkyAPM/go2sky/internal/idgen"
...
...
@@ -54,6 +56,14 @@ type SegmentContext struct {
spanIDGenerator
*
int32
}
func
(
ctx
SegmentContext
)
GetReadableGlobalTraceID
()
string
{
ii
:=
make
([]
string
,
len
(
ctx
.
TraceID
))
for
i
,
v
:=
range
ctx
.
TraceID
{
ii
[
i
]
=
fmt
.
Sprint
(
v
)
}
return
strings
.
Join
(
ii
,
"."
)
}
// ReportedSpan is accessed by Reporter to load reported data
type
ReportedSpan
interface
{
Context
()
*
SegmentContext
...
...
trace.go
浏览文件 @
f10048c7
...
...
@@ -30,7 +30,11 @@ import (
"github.com/SkyAPM/go2sky/propagation"
)
const
errParameter
=
tool
.
Error
(
"parameter are nil"
)
const
(
errParameter
=
tool
.
Error
(
"parameter are nil"
)
EmptyTraceID
=
"N/A"
NoopTraceID
=
"[Ignored Trace]"
)
// Tracer is go2sky tracer implementation.
type
Tracer
struct
{
...
...
@@ -235,3 +239,15 @@ type Reporter interface {
Send
(
spans
[]
ReportedSpan
)
Close
()
}
func
TraceID
(
ctx
context
.
Context
)
string
{
activeSpan
:=
ctx
.
Value
(
ctxKeyInstance
)
if
activeSpan
==
nil
{
return
EmptyTraceID
}
span
,
ok
:=
activeSpan
.
(
segmentSpan
)
if
ok
{
return
span
.
context
()
.
GetReadableGlobalTraceID
()
}
return
NoopTraceID
}
trace_test.go
浏览文件 @
f10048c7
...
...
@@ -107,6 +107,43 @@ func verifySpans(t *testing.T, span ReportedSpan, subSpan ReportedSpan) {
}
}
func
TestTrace_TraceID
(
t
*
testing
.
T
)
{
// activeSpan == nil
traceID
:=
TraceID
(
context
.
Background
())
verifyTraceID
(
t
,
EmptyTraceID
,
traceID
)
// activeSpan == NoopSpan
reporter
:=
&
mockRegisterReporter
{
success
:
false
,
}
tracer
,
_
:=
NewTracer
(
"service"
,
WithReporter
(
reporter
))
_
,
ctx
,
err
:=
tracer
.
CreateLocalSpan
(
context
.
Background
())
if
err
!=
nil
{
t
.
Error
(
err
)
}
traceID
=
TraceID
(
ctx
)
verifyTraceID
(
t
,
NoopTraceID
,
traceID
)
// activeSpan == segmentSpan
reporter
=
&
mockRegisterReporter
{
success
:
true
,
}
tracer
,
_
=
NewTracer
(
"service"
,
WithReporter
(
reporter
))
tracer
.
WaitUntilRegister
()
span
,
ctx
,
err
:=
tracer
.
CreateLocalSpan
(
context
.
Background
())
if
err
!=
nil
{
t
.
Error
(
err
)
}
traceID
=
TraceID
(
ctx
)
verifyTraceID
(
t
,
span
.
(
segmentSpan
)
.
context
()
.
GetReadableGlobalTraceID
(),
traceID
)
}
func
verifyTraceID
(
t
*
testing
.
T
,
expectTraceID
string
,
actualTraceID
string
)
{
if
expectTraceID
!=
actualTraceID
{
t
.
Errorf
(
"expectTraceID: %v, actualTraceID: %v"
,
expectTraceID
,
actualTraceID
)
}
}
type
mockRegisterReporter
struct
{
success
bool
wg
sync
.
WaitGroup
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录