mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2024-12-12 10:04:29 +02:00
970755bd08
* Enable support for externally-defined ID generators * Moved the SDK's `internal.IDGenerator` interface to the `sdk/trace` package. * Added `trace.WithIDGenerator()` `TracerProviderOption`. Signed-off-by: Anthony J Mirabella <a9@aneurysm9.com> * Update CHANGELOG.md with PR info * Address PR feedback: * Fix IDGenerator godoc comment * rename type defaultIDGenerator to randomIDGenerator * rename defIDGenerator() to defaultIDGenerator() Signed-off-by: Anthony J Mirabella <a9@aneurysm9.com> * Rework trace.IDGenerator interface * NewTraceID() -> NewIDs(ctx) ** Returns both TraceID and SpanID * NewSpanID() -> NewSpanID(ctx, traceID) ** Returns only SpanID, has access to TraceID * Both methods now receive a context, from which they may extract information * startSpanInternal() updated to receive a context to pass to the ID generator * Drop outdated comment from docblock Co-authored-by: Krzesimir Nowak <qdlacz@gmail.com> Co-authored-by: Krzesimir Nowak <qdlacz@gmail.com>
68 lines
1.9 KiB
Go
68 lines
1.9 KiB
Go
// Copyright The 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 "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
import (
|
|
"context"
|
|
crand "crypto/rand"
|
|
"encoding/binary"
|
|
"math/rand"
|
|
"sync"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// IDGenerator allows custom generators for TraceID and SpanID.
|
|
type IDGenerator interface {
|
|
NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID)
|
|
NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID
|
|
}
|
|
|
|
type randomIDGenerator struct {
|
|
sync.Mutex
|
|
randSource *rand.Rand
|
|
}
|
|
|
|
var _ IDGenerator = &randomIDGenerator{}
|
|
|
|
// NewSpanID returns a non-zero span ID from a randomly-chosen sequence.
|
|
func (gen *randomIDGenerator) NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID {
|
|
gen.Lock()
|
|
defer gen.Unlock()
|
|
sid := trace.SpanID{}
|
|
gen.randSource.Read(sid[:])
|
|
return sid
|
|
}
|
|
|
|
// NewIDs returns a non-zero trace ID and a non-zero span ID from a
|
|
// randomly-chosen sequence.
|
|
func (gen *randomIDGenerator) NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID) {
|
|
gen.Lock()
|
|
defer gen.Unlock()
|
|
tid := trace.TraceID{}
|
|
gen.randSource.Read(tid[:])
|
|
sid := trace.SpanID{}
|
|
gen.randSource.Read(sid[:])
|
|
return tid, sid
|
|
}
|
|
|
|
func defaultIDGenerator() IDGenerator {
|
|
gen := &randomIDGenerator{}
|
|
var rngSeed int64
|
|
_ = binary.Read(crand.Reader, binary.LittleEndian, &rngSeed)
|
|
gen.randSource = rand.New(rand.NewSource(rngSeed))
|
|
return gen
|
|
}
|