Skip to content

Commit 766f12f

Browse files
authored
Merge pull request #6 from fuyibing/2.x
child context support for tracing
2 parents c6e26a6 + c23fd08 commit 766f12f

File tree

6 files changed

+73
-0
lines changed

6 files changed

+73
-0
lines changed

client.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ package log
66
import (
77
"fmt"
88
"os"
9+
"runtime"
10+
"strings"
911

1012
"github.com/fuyibing/log/v2/interfaces"
1113
)
@@ -103,6 +105,26 @@ func (o *client) Errorfc(ctx interface{}, text string, args ...interface{}) {
103105
}
104106
}
105107

108+
func (o *client) Panic(text string) {
109+
o.Panicfc(nil, text)
110+
}
111+
112+
func (o *client) Panicf(text string, args ...interface{}) {
113+
o.Panicfc(nil, text, args...)
114+
}
115+
116+
func (o *client) Panicfc(ctx interface{}, text string, args ...interface{}) {
117+
str := fmt.Sprintf(text, args...)
118+
for i := 1; ; i++ {
119+
_, f, l, got := runtime.Caller(i)
120+
if !got {
121+
break
122+
}
123+
str += fmt.Sprintf("\n%s:%d", strings.TrimSpace(f), l)
124+
}
125+
o.Errorfc(ctx, str)
126+
}
127+
106128
// 日志处理逻辑.
107129
func (o *client) log(ctx interface{}, level interfaces.Level, text string, args ...interface{}) {
108130
if handler := Config.GetHandler(); handler != nil {

context.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,18 @@ func IrisBind(ctx iris.Context) {
2626
func NewContext() context.Context {
2727
return context.WithValue(context.TODO(), interfaces.OpenTracingKey, NewTracing().UseDefault())
2828
}
29+
30+
// 子级上下文.
31+
func ChildContext(ctx interface{}) context.Context {
32+
// 1. return NewContext() if param ctx nil.
33+
if ctx == nil {
34+
return NewContext()
35+
}
36+
// 2. parse bound tracing.
37+
t := ParseTracing(ctx)
38+
if t == nil {
39+
return NewContext()
40+
}
41+
// 3. generate new tracing.
42+
return context.WithValue(context.TODO(), interfaces.OpenTracingKey, NewTracing().Use(t.GetTraceId(), t.GenPreviewVersion()))
43+
}

func.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,21 @@ func Errorfc(ctx interface{}, text string, args ...interface{}) {
8686
Client.Errorfc(ctx, text, args...)
8787
}
8888
}
89+
90+
func Panic(text string) {
91+
if Config.ErrorOn() {
92+
Client.Panic(text)
93+
}
94+
}
95+
96+
func Panicf(text string, args ...interface{}) {
97+
if Config.ErrorOn() {
98+
Client.Panicf(text, args...)
99+
}
100+
}
101+
102+
func Panicfc(ctx interface{}, text string, args ...interface{}) {
103+
if Config.ErrorOn() {
104+
Client.Panicfc(ctx, text, args...)
105+
}
106+
}

interfaces/client_interface.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,4 +40,8 @@ type ClientInterface interface {
4040

4141
// 添加Error日志, 支持格式化和请求链.
4242
Errorfc(ctx interface{}, text string, args ...interface{})
43+
44+
Panic(text string)
45+
Panicf(text string, args ...interface{})
46+
Panicfc(ctx interface{}, text string, args ...interface{})
4347
}

interfaces/trace_interface.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,16 @@ import (
88
)
99

1010
type TraceInterface interface {
11+
GenCurrentVersion() string
12+
GenPreviewVersion() string
1113
GenVersion(i int32) string
1214
GetParentSpanId() string
1315
GetSpanId() string
1416
GetSpanVersion() string
1517
GetTraceId() string
1618
IncrOffset() (before int32, after int32)
1719
RequestInfo() (method string, url string)
20+
Use(traceId, spanVersion string) TraceInterface
1821
UseDefault() TraceInterface
1922
UseRequest(req *http.Request) TraceInterface
2023
}

tracing.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type tracing struct {
3232

3333
// 创建OpenTracing.
3434
func NewTracing() interfaces.TraceInterface { return &tracing{spanVersion: "0"} }
35+
func (o *tracing) GenCurrentVersion() string { return o.GenVersion(o.offset) }
36+
func (o *tracing) GenPreviewVersion() string { return o.GenVersion(o.offset-1) }
3537
func (o *tracing) GenVersion(i int32) string { return fmt.Sprintf("%s.%d", o.spanVersion, i) }
3638
func (o *tracing) GetParentSpanId() string { return o.parentSpanId }
3739
func (o *tracing) GetSpanId() string { return o.spanId }
@@ -48,6 +50,15 @@ func (o *tracing) IncrOffset() (before int32, after int32) {
4850
// Http请求参数.
4951
func (o *tracing) RequestInfo() (method string, url string) { return o.method, o.url }
5052

53+
// Use specified.
54+
func (o *tracing) Use(traceId, spanVersion string) interfaces.TraceInterface {
55+
o.offset = 0
56+
o.traceId = traceId
57+
o.spanId = o.generateUniqId()
58+
o.spanVersion = spanVersion
59+
return o
60+
}
61+
5162
// 使用默认模式.
5263
func (o *tracing) UseDefault() interfaces.TraceInterface {
5364
o.offset = 0

0 commit comments

Comments
 (0)