gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

id_generator.go (2465B)


      1 // Copyright The OpenTelemetry Authors
      2 //
      3 // Licensed under the Apache License, Version 2.0 (the "License");
      4 // you may not use this file except in compliance with the License.
      5 // You may obtain a copy of the License at
      6 //
      7 //     http://www.apache.org/licenses/LICENSE-2.0
      8 //
      9 // Unless required by applicable law or agreed to in writing, software
     10 // distributed under the License is distributed on an "AS IS" BASIS,
     11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package trace // import "go.opentelemetry.io/otel/sdk/trace"
     16 
     17 import (
     18 	"context"
     19 	crand "crypto/rand"
     20 	"encoding/binary"
     21 	"math/rand"
     22 	"sync"
     23 
     24 	"go.opentelemetry.io/otel/trace"
     25 )
     26 
     27 // IDGenerator allows custom generators for TraceID and SpanID.
     28 type IDGenerator interface {
     29 	// DO NOT CHANGE: any modification will not be backwards compatible and
     30 	// must never be done outside of a new major release.
     31 
     32 	// NewIDs returns a new trace and span ID.
     33 	NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID)
     34 	// DO NOT CHANGE: any modification will not be backwards compatible and
     35 	// must never be done outside of a new major release.
     36 
     37 	// NewSpanID returns a ID for a new span in the trace with traceID.
     38 	NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID
     39 	// DO NOT CHANGE: any modification will not be backwards compatible and
     40 	// must never be done outside of a new major release.
     41 }
     42 
     43 type randomIDGenerator struct {
     44 	sync.Mutex
     45 	randSource *rand.Rand
     46 }
     47 
     48 var _ IDGenerator = &randomIDGenerator{}
     49 
     50 // NewSpanID returns a non-zero span ID from a randomly-chosen sequence.
     51 func (gen *randomIDGenerator) NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID {
     52 	gen.Lock()
     53 	defer gen.Unlock()
     54 	sid := trace.SpanID{}
     55 	_, _ = gen.randSource.Read(sid[:])
     56 	return sid
     57 }
     58 
     59 // NewIDs returns a non-zero trace ID and a non-zero span ID from a
     60 // randomly-chosen sequence.
     61 func (gen *randomIDGenerator) NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID) {
     62 	gen.Lock()
     63 	defer gen.Unlock()
     64 	tid := trace.TraceID{}
     65 	_, _ = gen.randSource.Read(tid[:])
     66 	sid := trace.SpanID{}
     67 	_, _ = gen.randSource.Read(sid[:])
     68 	return tid, sid
     69 }
     70 
     71 func defaultIDGenerator() IDGenerator {
     72 	gen := &randomIDGenerator{}
     73 	var rngSeed int64
     74 	_ = binary.Read(crand.Reader, binary.LittleEndian, &rngSeed)
     75 	gen.randSource = rand.New(rand.NewSource(rngSeed))
     76 	return gen
     77 }