Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable support for externally-defined ID generators #1363

Merged
merged 6 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

### Added

- `trace.WithIDGenerator()` `TracerProviderOption`. (#1363)

### Changed

- Move the OpenCensus example into `example` directory. (#1359)
- Moved the SDK's `internal.IDGenerator` interface in to the `sdk/trace` package to enable support for externally-defined ID generators. (#1363)

## [0.14.0] - 2020-11-19

Expand Down
3 changes: 1 addition & 2 deletions sdk/trace/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package trace // import "go.opentelemetry.io/otel/sdk/trace"

import (
"go.opentelemetry.io/otel/sdk/resource"
"go.opentelemetry.io/otel/sdk/trace/internal"
)

// Config represents the global tracing configuration.
Expand All @@ -25,7 +24,7 @@ type Config struct {
DefaultSampler Sampler

// IDGenerator is for internal use only.
IDGenerator internal.IDGenerator
IDGenerator IDGenerator

// MaxEventsPerSpan is max number of message events per span
MaxEventsPerSpan int
Expand Down
26 changes: 20 additions & 6 deletions sdk/trace/id_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,29 @@
package trace // import "go.opentelemetry.io/otel/sdk/trace"

import (
crand "crypto/rand"
"encoding/binary"
"math/rand"
"sync"

"go.opentelemetry.io/otel/trace"

"go.opentelemetry.io/otel/sdk/trace/internal"
)

type defaultIDGenerator struct {
// IDGenerator allows custom generators for TraceID and SpanID.
type IDGenerator interface {
NewTraceID() trace.TraceID
NewSpanID() trace.SpanID
MrAlias marked this conversation as resolved.
Show resolved Hide resolved
}

type randomIDGenerator struct {
sync.Mutex
randSource *rand.Rand
}

var _ internal.IDGenerator = &defaultIDGenerator{}
var _ IDGenerator = &randomIDGenerator{}

// NewSpanID returns a non-zero span ID from a randomly-chosen sequence.
func (gen *defaultIDGenerator) NewSpanID() trace.SpanID {
func (gen *randomIDGenerator) NewSpanID() trace.SpanID {
gen.Lock()
defer gen.Unlock()
sid := trace.SpanID{}
Expand All @@ -41,10 +47,18 @@ func (gen *defaultIDGenerator) NewSpanID() trace.SpanID {

// 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() trace.TraceID {
func (gen *randomIDGenerator) NewTraceID() trace.TraceID {
gen.Lock()
defer gen.Unlock()
tid := trace.TraceID{}
gen.randSource.Read(tid[:])
return tid
}

func defaultIDGenerator() IDGenerator {
gen := &randomIDGenerator{}
var rngSeed int64
_ = binary.Read(crand.Reader, binary.LittleEndian, &rngSeed)
gen.randSource = rand.New(rand.NewSource(rngSeed))
return gen
}
24 changes: 0 additions & 24 deletions sdk/trace/internal/internal.go

This file was deleted.

9 changes: 8 additions & 1 deletion sdk/trace/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ func NewTracerProvider(opts ...TracerProviderOption) *TracerProvider {
}
tp.config.Store(&Config{
DefaultSampler: ParentBased(AlwaysSample()),
IDGenerator: defIDGenerator(),
IDGenerator: defaultIDGenerator(),
MaxAttributesPerSpan: DefaultMaxAttributesPerSpan,
MaxEventsPerSpan: DefaultMaxEventsPerSpan,
MaxLinksPerSpan: DefaultMaxLinksPerSpan,
Expand Down Expand Up @@ -231,3 +231,10 @@ func WithResource(r *resource.Resource) TracerProviderOption {
opts.config.Resource = r
}
}

// WithIDGenerator option registers an IDGenerator with the TracerProvider.
func WithIDGenerator(g IDGenerator) TracerProviderOption {
return func(opts *TracerProviderConfig) {
opts.config.IDGenerator = g
}
}
2 changes: 1 addition & 1 deletion sdk/trace/sampling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func TestTraceIdRatioSamplesInclusively(t *testing.T) {
numSamplers = 1000
numTraces = 100
)
idg := defIDGenerator()
idg := defaultIDGenerator()

for i := 0; i < numSamplers; i++ {
ratioLo, ratioHi := rand.Float64(), rand.Float64()
Expand Down
31 changes: 0 additions & 31 deletions sdk/trace/trace.go

This file was deleted.

2 changes: 1 addition & 1 deletion sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ func TestRecordingIsOn(t *testing.T) {
}

func TestSampling(t *testing.T) {
idg := defIDGenerator()
idg := defaultIDGenerator()
const total = 10000
for name, tc := range map[string]struct {
sampler Sampler
Expand Down