github.com/wangyougui/gf/v2@v2.6.5/internal/tracing/tracing.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 // Package tracing provides some utility functions for tracing functionality. 8 package tracing 9 10 import ( 11 "math" 12 "time" 13 14 "go.opentelemetry.io/otel/trace" 15 16 "github.com/wangyougui/gf/v2/container/gtype" 17 "github.com/wangyougui/gf/v2/encoding/gbinary" 18 "github.com/wangyougui/gf/v2/util/grand" 19 ) 20 21 var ( 22 randomInitSequence = int32(grand.Intn(math.MaxInt32)) 23 sequence = gtype.NewInt32(randomInitSequence) 24 ) 25 26 // NewIDs creates and returns a new trace and span ID. 27 func NewIDs() (traceID trace.TraceID, spanID trace.SpanID) { 28 return NewTraceID(), NewSpanID() 29 } 30 31 // NewTraceID creates and returns a trace ID. 32 func NewTraceID() (traceID trace.TraceID) { 33 var ( 34 timestampNanoBytes = gbinary.EncodeInt64(time.Now().UnixNano()) 35 sequenceBytes = gbinary.EncodeInt32(sequence.Add(1)) 36 randomBytes = grand.B(4) 37 ) 38 copy(traceID[:], timestampNanoBytes) 39 copy(traceID[8:], sequenceBytes) 40 copy(traceID[12:], randomBytes) 41 return 42 } 43 44 // NewSpanID creates and returns a span ID. 45 func NewSpanID() (spanID trace.SpanID) { 46 copy(spanID[:], gbinary.EncodeInt64(time.Now().UnixNano()/1e3)) 47 copy(spanID[4:], grand.B(4)) 48 return 49 }