Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
milvus
milvus
提交
db8067d6
M
milvus
项目概览
milvus
/
milvus
11 个月 前同步成功
通知
261
Star
22476
Fork
2472
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
milvus
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
未验证
提交
db8067d6
编写于
9月 03, 2021
作者:
G
godchen
提交者:
GitHub
9月 03, 2021
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add trace ut (#7445)
Signed-off-by:
N
godchen
<
qingxiang.chen@zilliz.com
>
上级
0897a877
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
99 addition
and
26 deletion
+99
-26
internal/util/trace/util.go
internal/util/trace/util.go
+37
-26
internal/util/trace/util_test.go
internal/util/trace/util_test.go
+62
-0
未找到文件。
internal/util/trace/util.go
浏览文件 @
db8067d6
...
...
@@ -25,26 +25,15 @@ import (
)
func
InitTracing
(
serviceName
string
)
io
.
Closer
{
if
opentracing
.
IsGlobalTracerRegistered
()
{
return
nil
cfg
:=
&
config
.
Configuration
{
ServiceName
:
serviceName
,
Sampler
:
&
config
.
SamplerConfig
{
Type
:
"const"
,
Param
:
0
,
},
}
var
cfg
*
config
.
Configuration
var
err
error
if
true
{
cfg
,
err
=
config
.
FromEnv
()
if
err
!=
nil
{
log
.
Error
(
err
)
return
nil
}
cfg
.
ServiceName
=
serviceName
}
else
{
cfg
=
&
config
.
Configuration
{
ServiceName
:
serviceName
,
Sampler
:
&
config
.
SamplerConfig
{
Type
:
"const"
,
Param
:
1
,
},
}
cfg
=
InitFromEnv
(
serviceName
)
}
tracer
,
closer
,
err
:=
cfg
.
NewTracer
()
if
err
!=
nil
{
...
...
@@ -55,13 +44,27 @@ func InitTracing(serviceName string) io.Closer {
return
closer
}
func
InitFromEnv
(
serviceName
string
)
*
config
.
Configuration
{
cfg
,
err
:=
config
.
FromEnv
()
if
err
!=
nil
{
log
.
Error
(
err
)
return
nil
}
cfg
.
ServiceName
=
serviceName
return
cfg
}
func
StartSpanFromContext
(
ctx
context
.
Context
,
opts
...
opentracing
.
StartSpanOption
)
(
opentracing
.
Span
,
context
.
Context
)
{
return
StartSpanFromContextWithSkip
(
ctx
,
2
,
opts
...
)
}
func
StartSpanFromContextWithSkip
(
ctx
context
.
Context
,
skip
int
,
opts
...
opentracing
.
StartSpanOption
)
(
opentracing
.
Span
,
context
.
Context
)
{
if
ctx
==
nil
{
return
NoopSpan
(),
ctx
}
var
pcs
[
1
]
uintptr
n
:=
runtime
.
Callers
(
2
,
pcs
[
:
])
n
:=
runtime
.
Callers
(
skip
,
pcs
[
:
])
if
n
<
1
{
span
,
ctx
:=
opentracing
.
StartSpanFromContext
(
ctx
,
"unknown"
,
opts
...
)
span
.
LogFields
(
log
.
Error
(
errors
.
New
(
"runtime.Callers failed"
)))
...
...
@@ -85,12 +88,16 @@ func StartSpanFromContext(ctx context.Context, opts ...opentracing.StartSpanOpti
}
func
StartSpanFromContextWithOperationName
(
ctx
context
.
Context
,
operationName
string
,
opts
...
opentracing
.
StartSpanOption
)
(
opentracing
.
Span
,
context
.
Context
)
{
return
StartSpanFromContextWithOperationNameWithSkip
(
ctx
,
operationName
,
2
,
opts
...
)
}
func
StartSpanFromContextWithOperationNameWithSkip
(
ctx
context
.
Context
,
operationName
string
,
skip
int
,
opts
...
opentracing
.
StartSpanOption
)
(
opentracing
.
Span
,
context
.
Context
)
{
if
ctx
==
nil
{
return
NoopSpan
(),
ctx
}
var
pcs
[
1
]
uintptr
n
:=
runtime
.
Callers
(
2
,
pcs
[
:
])
n
:=
runtime
.
Callers
(
skip
,
pcs
[
:
])
if
n
<
1
{
span
,
ctx
:=
opentracing
.
StartSpanFromContext
(
ctx
,
operationName
,
opts
...
)
span
.
LogFields
(
log
.
Error
(
errors
.
New
(
"runtime.Callers failed"
)))
...
...
@@ -130,17 +137,21 @@ func LogError(span opentracing.Span, err error) error {
}
func
InfoFromSpan
(
span
opentracing
.
Span
)
(
traceID
string
,
sampled
bool
,
found
bool
)
{
if
spanContext
,
ok
:=
span
.
Context
()
.
(
jaeger
.
SpanContext
);
ok
{
traceID
=
spanContext
.
TraceID
()
.
String
()
sampled
=
spanContext
.
IsSampled
()
return
traceID
,
sampled
,
true
if
span
!=
nil
{
if
spanContext
,
ok
:=
span
.
Context
()
.
(
jaeger
.
SpanContext
);
ok
{
traceID
=
spanContext
.
TraceID
()
.
String
()
sampled
=
spanContext
.
IsSampled
()
return
traceID
,
sampled
,
true
}
}
return
""
,
false
,
false
}
func
InfoFromContext
(
ctx
context
.
Context
)
(
traceID
string
,
sampled
bool
,
found
bool
)
{
if
span
:=
opentracing
.
SpanFromContext
(
ctx
);
span
!=
nil
{
return
InfoFromSpan
(
span
)
if
ctx
!=
nil
{
if
span
:=
opentracing
.
SpanFromContext
(
ctx
);
span
!=
nil
{
return
InfoFromSpan
(
span
)
}
}
return
""
,
false
,
false
}
...
...
internal/util/trace/util_test.go
浏览文件 @
db8067d6
...
...
@@ -18,7 +18,9 @@ import (
"errors"
"github.com/opentracing/opentracing-go"
oplog
"github.com/opentracing/opentracing-go/log"
"github.com/stretchr/testify/assert"
)
type
simpleStruct
struct
{
...
...
@@ -26,6 +28,11 @@ type simpleStruct struct {
value
string
}
func
TestInit
(
t
*
testing
.
T
)
{
cfg
:=
InitFromEnv
(
"test"
)
assert
.
NotNil
(
t
,
cfg
)
}
func
TestTracing
(
t
*
testing
.
T
)
{
//Already Init in each framework, this can be ignored in debug
closer
:=
InitTracing
(
"test"
)
...
...
@@ -37,6 +44,8 @@ func TestTracing(t *testing.T) {
//start span
//default use function name for operation name
sp
,
ctx
:=
StartSpanFromContext
(
ctx
)
id
,
sampled
,
found
:=
InfoFromContext
(
ctx
)
fmt
.
Printf
(
"traceID = %s, sampled = %t, found = %t"
,
id
,
sampled
,
found
)
sp
.
SetTag
(
"tag1"
,
"tag1"
)
// use self-defined operation name for span
// sp, ctx := StartSpanFromContextWithOperationName(ctx, "self-defined name")
...
...
@@ -77,3 +86,56 @@ func caller(ctx context.Context) error {
}
return
nil
}
func
TestInject
(
t
*
testing
.
T
)
{
//Already Init in each framework, this can be ignored in debug
closer
:=
InitTracing
(
"test"
)
defer
closer
.
Close
()
// context normally can be propagated through func params
ctx
:=
context
.
Background
()
//start span
//default use function name for operation name
sp
,
ctx
:=
StartSpanFromContext
(
ctx
)
id
,
sampled
,
found
:=
InfoFromContext
(
ctx
)
fmt
.
Printf
(
"traceID = %s, sampled = %t, found = %t"
,
id
,
sampled
,
found
)
pp
:=
PropertiesReaderWriter
{
PpMap
:
map
[
string
]
string
{}}
InjectContextToPulsarMsgProperties
(
sp
.
Context
(),
pp
.
PpMap
)
tracer
:=
opentracing
.
GlobalTracer
()
sc
,
_
:=
tracer
.
Extract
(
opentracing
.
TextMap
,
pp
)
assert
.
NotNil
(
t
,
sc
)
}
func
TestTraceError
(
t
*
testing
.
T
)
{
//Already Init in each framework, this can be ignored in debug
closer
:=
InitTracing
(
"test"
)
defer
closer
.
Close
()
// context normally can be propagated through func params
sp
,
ctx
:=
StartSpanFromContext
(
nil
)
assert
.
Nil
(
t
,
ctx
)
assert
.
NotNil
(
t
,
sp
)
sp
,
ctx
=
StartSpanFromContextWithOperationName
(
nil
,
"test"
)
assert
.
Nil
(
t
,
ctx
)
assert
.
NotNil
(
t
,
sp
)
//Will Cause span log error
StartSpanFromContextWithOperationNameWithSkip
(
context
.
Background
(),
"test"
,
10000
)
//Will Cause span log error
StartSpanFromContextWithSkip
(
context
.
Background
(),
10000
)
id
,
sampled
,
found
:=
InfoFromSpan
(
nil
)
assert
.
Equal
(
t
,
id
,
""
)
assert
.
Equal
(
t
,
sampled
,
false
)
assert
.
Equal
(
t
,
found
,
false
)
id
,
sampled
,
found
=
InfoFromContext
(
nil
)
assert
.
Equal
(
t
,
id
,
""
)
assert
.
Equal
(
t
,
sampled
,
false
)
assert
.
Equal
(
t
,
found
,
false
)
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录