// Copyright 2019, OpenTelemetry Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package trace import ( "math/rand" "sync" "sync/atomic" "go.opentelemetry.io/api/core" "go.opentelemetry.io/sdk/trace/internal" ) type defaultIDGenerator struct { sync.Mutex // Please keep these as the first fields // so that these 8 byte fields will be aligned on addresses // divisible by 8, on both 32-bit and 64-bit machines when // performing atomic increments and accesses. // See: // * https://github.com/census-instrumentation/opencensus-go/issues/587 // * https://github.com/census-instrumentation/opencensus-go/issues/865 // * https://golang.org/pkg/sync/atomic/#pkg-note-BUG nextSpanID uint64 spanIDInc uint64 traceIDAdd [2]uint64 traceIDRand *rand.Rand } var _ internal.IDGenerator = &defaultIDGenerator{} // NewSpanID returns a non-zero span ID from a randomly-chosen sequence. func (gen *defaultIDGenerator) NewSpanID() uint64 { var id uint64 for id == 0 { id = atomic.AddUint64(&gen.nextSpanID, gen.spanIDInc) } return id } // NewTraceID returns a non-zero trace ID from a randomly-chosen sequence. // mu should be held while this function is called. func (gen *defaultIDGenerator) NewTraceID() core.TraceID { gen.Lock() defer gen.Unlock() // Construct the trace ID from two outputs of traceIDRand, with a constant // added to each half for additional entropy. tid := core.TraceID{ High: gen.traceIDRand.Uint64() + gen.traceIDAdd[0], Low: gen.traceIDRand.Uint64() + gen.traceIDAdd[1], } return tid }