From 202e9dad502330a6fd0721e980032dedc2046376 Mon Sep 17 00:00:00 2001 From: lbbniu Date: Fri, 7 Apr 2023 18:20:21 +0800 Subject: [PATCH] perf(trace): optimize traceId and spanId generation --- go.mod | 1 - go.sum | 2 -- tars/util/trace/id_generator.go | 50 +++++++++++++++++++++++++++++++++ tars/util/trace/trace.go | 11 ++++---- 4 files changed, 56 insertions(+), 8 deletions(-) create mode 100644 tars/util/trace/id_generator.go diff --git a/go.mod b/go.mod index 0642cce8..7cb1dc50 100644 --- a/go.mod +++ b/go.mod @@ -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 diff --git a/go.sum b/go.sum index 384b77fe..2965ed58 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/tars/util/trace/id_generator.go b/tars/util/trace/id_generator.go new file mode 100644 index 00000000..97d64823 --- /dev/null +++ b/tars/util/trace/id_generator.go @@ -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[:]) +} diff --git a/tars/util/trace/trace.go b/tars/util/trace/trace.go index 1a542344..11fae4d7 100644 --- a/tars/util/trace/trace.go +++ b/tars/util/trace/trace.go @@ -3,8 +3,6 @@ package trace import ( "strconv" "strings" - - "github.com/google/uuid" ) // SpanContext 调用链追踪信息 @@ -46,7 +44,10 @@ const ( AnnotationSS = "ss" ) -var traceParamMaxLen uint = 1 // 默认1K +var ( + idGenerator = newGenerator() + traceParamMaxLen uint = 1 // 默认1K +) // SetTraceParamMaxLen 设置控制参数长度 func SetTraceParamMaxLen(len uint) { @@ -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 } @@ -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 {