Skip to content

Commit

Permalink
Merge pull request #448 from TarsCloud/per/traceid_spanid
Browse files Browse the repository at this point in the history
perf(trace): optimize traceId and spanId generation
  • Loading branch information
lbbniu authored Apr 7, 2023
2 parents e5b5fdb + 202e9da commit 15373f4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.13
require (
github.com/gin-gonic/gin v1.8.1
github.com/google/go-cmp v0.5.9 // indirect
github.com/google/uuid v1.3.0
github.com/opentracing/opentracing-go v1.1.0
github.com/openzipkin-contrib/zipkin-go-opentracing v0.4.5
github.com/openzipkin/zipkin-go v0.4.0
Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/context v1.1.1/go.mod h1:kBGZzfjB9CEq2AlWe17Uuf7NDRt0dE0s8S51q0aT7Yg=
github.com/gorilla/mux v1.6.2/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
github.com/gorilla/mux v1.7.3/go.mod h1:1lud6UwP+6orDFRuTfBEV8e9/aOM/c4fVVCaMa2zaAs=
Expand Down
50 changes: 50 additions & 0 deletions tars/util/trace/id_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package trace

import (
crand "crypto/rand"
"encoding/binary"
"encoding/hex"
"math/rand"
"sync"
)

type randomIDGenerator struct {
sync.Mutex
randSource *rand.Rand
}

func newGenerator() *randomIDGenerator {
var rngSeed int64
_ = binary.Read(crand.Reader, binary.LittleEndian, &rngSeed)
return &randomIDGenerator{randSource: rand.New(rand.NewSource(rngSeed))}
}

// NewSpanID returns a non-zero span ID from a randomly-chosen sequence.
func (gen *randomIDGenerator) NewSpanID() string {
gen.Lock()
defer gen.Unlock()
sid := [8]byte{}
_, _ = gen.randSource.Read(sid[:])
return hex.EncodeToString(sid[:])
}

// NewTraceID returns a non-zero trace ID from a randomly-chosen sequence.
func (gen *randomIDGenerator) NewTraceID() string {
gen.Lock()
defer gen.Unlock()
tid := [16]byte{}
_, _ = gen.randSource.Read(tid[:])
return hex.EncodeToString(tid[:])
}

// NewIDs returns a non-zero trace ID and a non-zero span ID from a
// randomly-chosen sequence.
func (gen *randomIDGenerator) NewIDs() (string, string) {
gen.Lock()
defer gen.Unlock()
tid := [16]byte{}
_, _ = gen.randSource.Read(tid[:])
sid := [8]byte{}
_, _ = gen.randSource.Read(sid[:])
return hex.EncodeToString(tid[:]), hex.EncodeToString(sid[:])
}
11 changes: 6 additions & 5 deletions tars/util/trace/trace.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package trace
import (
"strconv"
"strings"

"github.com/google/uuid"
)

// SpanContext 调用链追踪信息
Expand Down Expand Up @@ -46,7 +44,10 @@ const (
AnnotationSS = "ss"
)

var traceParamMaxLen uint = 1 // 默认1K
var (
idGenerator = newGenerator()
traceParamMaxLen uint = 1 // 默认1K
)

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

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

0 comments on commit 15373f4

Please sign in to comment.