You've already forked opentelemetry-go
mirror of
https://github.com/open-telemetry/opentelemetry-go.git
synced 2025-11-25 22:41:46 +02:00
Update to new stdlib apis. The new Float64 is uniform, which resolves a long comment. https://cs.opensource.google/go/go/+/refs/tags/go1.24.2:src/math/rand/v2/rand.go;l=209 > // There are exactly 1<<53 float64s in [0,1). Use Intn(1<<53) / (1<<53). return float64(r.Uint64()<<11>>11) / (1 << 53) ``` goos: linux goarch: amd64 pkg: go.opentelemetry.io/otel/sdk/trace cpu: 12th Gen Intel(R) Core(TM) i7-1260P │ old.txt │ new.txt │ │ sec/op │ sec/op vs base │ TraceStart/with_a_simple_span-16 387.1n ± 5% 306.8n ± 15% -20.73% (p=0.000 n=10) TraceStart/with_several_links-16 542.2n ± 5% 501.0n ± 2% -7.61% (p=0.000 n=10) TraceStart/with_attributes-16 521.4n ± 14% 571.6n ± 6% +9.64% (p=0.009 n=10) geomean 478.3n 444.6n -7.05% │ old.txt │ new.txt │ │ B/op │ B/op vs base │ TraceStart/with_a_simple_span-16 528.0 ± 0% 528.0 ± 0% ~ (p=1.000 n=10) ¹ TraceStart/with_several_links-16 704.0 ± 0% 704.0 ± 0% ~ (p=1.000 n=10) ¹ TraceStart/with_attributes-16 784.0 ± 0% 784.0 ± 0% ~ (p=1.000 n=10) ¹ geomean 663.0 663.0 +0.00% ¹ all samples are equal │ old.txt │ new.txt │ │ allocs/op │ allocs/op vs base │ TraceStart/with_a_simple_span-16 2.000 ± 0% 2.000 ± 0% ~ (p=1.000 n=10) ¹ TraceStart/with_several_links-16 3.000 ± 0% 3.000 ± 0% ~ (p=1.000 n=10) ¹ TraceStart/with_attributes-16 4.000 ± 0% 4.000 ± 0% ~ (p=1.000 n=10) ¹ geomean 2.884 2.884 +0.00% ¹ all samples are equal ``` --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Damien Mathieu <42@dmathieu.com>
70 lines
1.9 KiB
Go
70 lines
1.9 KiB
Go
// Copyright The OpenTelemetry Authors
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
package trace // import "go.opentelemetry.io/otel/sdk/trace"
|
|
|
|
import (
|
|
"context"
|
|
"encoding/binary"
|
|
"math/rand/v2"
|
|
|
|
"go.opentelemetry.io/otel/trace"
|
|
)
|
|
|
|
// IDGenerator allows custom generators for TraceID and SpanID.
|
|
type IDGenerator interface {
|
|
// DO NOT CHANGE: any modification will not be backwards compatible and
|
|
// must never be done outside of a new major release.
|
|
|
|
// NewIDs returns a new trace and span ID.
|
|
NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID)
|
|
// DO NOT CHANGE: any modification will not be backwards compatible and
|
|
// must never be done outside of a new major release.
|
|
|
|
// NewSpanID returns a ID for a new span in the trace with traceID.
|
|
NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID
|
|
// DO NOT CHANGE: any modification will not be backwards compatible and
|
|
// must never be done outside of a new major release.
|
|
}
|
|
|
|
type randomIDGenerator struct{}
|
|
|
|
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 {
|
|
sid := trace.SpanID{}
|
|
for {
|
|
binary.NativeEndian.PutUint64(sid[:], rand.Uint64())
|
|
if sid.IsValid() {
|
|
break
|
|
}
|
|
}
|
|
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) {
|
|
tid := trace.TraceID{}
|
|
sid := trace.SpanID{}
|
|
for {
|
|
binary.NativeEndian.PutUint64(tid[:8], rand.Uint64())
|
|
binary.NativeEndian.PutUint64(tid[8:], rand.Uint64())
|
|
if tid.IsValid() {
|
|
break
|
|
}
|
|
}
|
|
for {
|
|
binary.NativeEndian.PutUint64(sid[:], rand.Uint64())
|
|
if sid.IsValid() {
|
|
break
|
|
}
|
|
}
|
|
return tid, sid
|
|
}
|
|
|
|
func defaultIDGenerator() IDGenerator {
|
|
return &randomIDGenerator{}
|
|
}
|