Skip to content

Commit

Permalink
Rework trace.IDGenerator interface
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
Aneurysm9 committed Dec 8, 2020
1 parent 4f95b6e commit 30e6437
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 14 deletions.
17 changes: 10 additions & 7 deletions sdk/trace/id_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package trace // import "go.opentelemetry.io/otel/sdk/trace"

import (
"context"
crand "crypto/rand"
"encoding/binary"
"math/rand"
Expand All @@ -25,8 +26,8 @@ import (

// IDGenerator allows custom generators for TraceID and SpanID.
type IDGenerator interface {
NewTraceID() trace.TraceID
NewSpanID() trace.SpanID
NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID)
NewSpanID(ctx context.Context, traceID trace.TraceID) trace.SpanID
}

type randomIDGenerator struct {
Expand All @@ -37,22 +38,24 @@ type randomIDGenerator struct {
var _ IDGenerator = &randomIDGenerator{}

// NewSpanID returns a non-zero span ID from a randomly-chosen sequence.
func (gen *randomIDGenerator) NewSpanID() trace.SpanID {
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
}

// NewTraceID returns a non-zero trace ID from a randomly-chosen sequence.
// mu should be held while this function is called.
func (gen *randomIDGenerator) NewTraceID() trace.TraceID {
// NewIDs returns a non-zero trace ID and a non-zero span ID from a
// randomly-chosen sequence. mu should be held while this function is called.
func (gen *randomIDGenerator) NewIDs(ctx context.Context) (trace.TraceID, trace.SpanID) {
gen.Lock()
defer gen.Unlock()
tid := trace.TraceID{}
gen.randSource.Read(tid[:])
return tid
sid := trace.SpanID{}
gen.randSource.Read(sid[:])
return tid, sid
}

func defaultIDGenerator() IDGenerator {
Expand Down
3 changes: 2 additions & 1 deletion sdk/trace/sampling_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package trace

import (
"context"
"fmt"
"math/rand"
"testing"
Expand Down Expand Up @@ -178,7 +179,7 @@ func TestTraceIdRatioSamplesInclusively(t *testing.T) {
samplerHi := TraceIDRatioBased(ratioHi)
samplerLo := TraceIDRatioBased(ratioLo)
for j := 0; j < numTraces; j++ {
traceID := idg.NewTraceID()
traceID, _ := idg.NewIDs(context.Background())

params := SamplingParameters{TraceID: traceID}
if samplerLo.ShouldSample(params).Decision == RecordAndSample {
Expand Down
10 changes: 7 additions & 3 deletions sdk/trace/span.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package trace // import "go.opentelemetry.io/otel/sdk/trace"

import (
"context"
"errors"
"fmt"
"reflect"
Expand Down Expand Up @@ -306,18 +307,21 @@ func (s *span) addChild() {
s.mu.Unlock()
}

func startSpanInternal(tr *tracer, name string, parent trace.SpanContext, remoteParent bool, o *trace.SpanConfig) *span {
func startSpanInternal(ctx context.Context, tr *tracer, name string, parent trace.SpanContext, remoteParent bool, o *trace.SpanConfig) *span {
var noParent bool
span := &span{}
span.spanContext = parent

cfg := tr.provider.config.Load().(*Config)

if parent == emptySpanContext {
span.spanContext.TraceID = cfg.IDGenerator.NewTraceID()
// Generate both TraceID and SpanID
span.spanContext.TraceID, span.spanContext.SpanID = cfg.IDGenerator.NewIDs(ctx)
noParent = true
} else {
// TraceID already exists, just generate a SpanID
span.spanContext.SpanID = cfg.IDGenerator.NewSpanID(ctx, parent.TraceID)
}
span.spanContext.SpanID = cfg.IDGenerator.NewSpanID()
data := samplingData{
noParent: noParent,
remoteParent: remoteParent,
Expand Down
5 changes: 3 additions & 2 deletions sdk/trace/trace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,9 +263,10 @@ func TestSampling(t *testing.T) {
for i := 0; i < total; i++ {
ctx := context.Background()
if tc.parent {
tid, sid := idg.NewIDs(ctx)
psc := trace.SpanContext{
TraceID: idg.NewTraceID(),
SpanID: idg.NewSpanID(),
TraceID: tid,
SpanID: sid,
}
if tc.sampledParent {
psc.TraceFlags = trace.FlagsSampled
Expand Down
2 changes: 1 addition & 1 deletion sdk/trace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (tr *tracer) Start(ctx context.Context, name string, options ...trace.SpanO
}
}

span := startSpanInternal(tr, name, parentSpanContext, remoteParent, config)
span := startSpanInternal(ctx, tr, name, parentSpanContext, remoteParent, config)
for _, l := range links {
span.addLink(l)
}
Expand Down

0 comments on commit 30e6437

Please sign in to comment.