Skip to content

Commit 15373f4

Browse files
authored
Merge pull request #448 from TarsCloud/per/traceid_spanid
perf(trace): optimize traceId and spanId generation
2 parents e5b5fdb + 202e9da commit 15373f4

File tree

4 files changed

+56
-8
lines changed

4 files changed

+56
-8
lines changed

go.mod

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ go 1.13
55
require (
66
github.com/gin-gonic/gin v1.8.1
77
github.com/google/go-cmp v0.5.9 // indirect
8-
github.com/google/uuid v1.3.0
98
github.com/opentracing/opentracing-go v1.1.0
109
github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5
1110
github.com/openzipkin/zipkin-go v0.4.0

go.sum

-2
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,6 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
8787
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
8888
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
8989
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
90-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
91-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
9290
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
9391
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
9492
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=

tars/util/trace/id_generator.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package trace
2+
3+
import (
4+
crand "crypto/rand"
5+
"encoding/binary"
6+
"encoding/hex"
7+
"math/rand"
8+
"sync"
9+
)
10+
11+
type randomIDGenerator struct {
12+
sync.Mutex
13+
randSource *rand.Rand
14+
}
15+
16+
func newGenerator() *randomIDGenerator {
17+
var rngSeed int64
18+
_ = binary.Read(crand.Reader, binary.LittleEndian, &rngSeed)
19+
return &randomIDGenerator{randSource: rand.New(rand.NewSource(rngSeed))}
20+
}
21+
22+
// NewSpanID returns a non-zero span ID from a randomly-chosen sequence.
23+
func (gen *randomIDGenerator) NewSpanID() string {
24+
gen.Lock()
25+
defer gen.Unlock()
26+
sid := [8]byte{}
27+
_, _ = gen.randSource.Read(sid[:])
28+
return hex.EncodeToString(sid[:])
29+
}
30+
31+
// NewTraceID returns a non-zero trace ID from a randomly-chosen sequence.
32+
func (gen *randomIDGenerator) NewTraceID() string {
33+
gen.Lock()
34+
defer gen.Unlock()
35+
tid := [16]byte{}
36+
_, _ = gen.randSource.Read(tid[:])
37+
return hex.EncodeToString(tid[:])
38+
}
39+
40+
// NewIDs returns a non-zero trace ID and a non-zero span ID from a
41+
// randomly-chosen sequence.
42+
func (gen *randomIDGenerator) NewIDs() (string, string) {
43+
gen.Lock()
44+
defer gen.Unlock()
45+
tid := [16]byte{}
46+
_, _ = gen.randSource.Read(tid[:])
47+
sid := [8]byte{}
48+
_, _ = gen.randSource.Read(sid[:])
49+
return hex.EncodeToString(tid[:]), hex.EncodeToString(sid[:])
50+
}

tars/util/trace/trace.go

+6-5
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@ package trace
33
import (
44
"strconv"
55
"strings"
6-
7-
"github.com/google/uuid"
86
)
97

108
// SpanContext 调用链追踪信息
@@ -46,7 +44,10 @@ const (
4644
AnnotationSS = "ss"
4745
)
4846

49-
var traceParamMaxLen uint = 1 // 默认1K
47+
var (
48+
idGenerator = newGenerator()
49+
traceParamMaxLen uint = 1 // 默认1K
50+
)
5051

5152
// SetTraceParamMaxLen 设置控制参数长度
5253
func SetTraceParamMaxLen(len uint) {
@@ -121,7 +122,7 @@ func (c *SpanContext) Reset() {
121122

122123
// NewSpan 生成spanId
123124
func (c *SpanContext) NewSpan() {
124-
c.spanID = uuid.NewString()
125+
c.spanID = idGenerator.NewSpanID()
125126
if len(c.parentSpanID) == 0 {
126127
c.parentSpanID = c.spanID
127128
}
@@ -215,7 +216,7 @@ func (t *Trace) NeedTraceParam(es SpanType, len uint) NeedParam {
215216
// @param traceFlag: 调用链日志输出参数控制,取值范围0-15, 0 不用打参数, 其他情况按位做控制开关,从低位到高位分别控制CS、CR、SR、SS,为1则输出对应参数
216217
// @param maxLen: 参数输出最大长度, 不传或者默认0, 则按服务模板默认取值
217218
func (t *Trace) OpenTrace(traceFlag int, maxLen uint) bool {
218-
traceID := uuid.NewString()
219+
traceID := idGenerator.NewTraceID()
219220
if maxLen > 0 {
220221
traceID = strconv.FormatInt(int64(traceFlag), 16) + "." + strconv.Itoa(int(maxLen)) + "-" + traceID
221222
} else {

0 commit comments

Comments
 (0)