Skip to content

Commit

Permalink
Rename trace.TraceID & trace.TraceIDFromHex
Browse files Browse the repository at this point in the history
  • Loading branch information
tensorchen committed May 3, 2020
1 parent c6c155d commit bae2298
Show file tree
Hide file tree
Showing 31 changed files with 88 additions and 88 deletions.
2 changes: 1 addition & 1 deletion api/trace/always_off_sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type alwaysOffSampler struct{}
func (ns alwaysOffSampler) ShouldSample(
_ SpanContext,
_ bool,
_ TraceID,
_ ID,
_ SpanID,
_ string,
_ SpanKind,
Expand Down
2 changes: 1 addition & 1 deletion api/trace/always_off_sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func TestNeverSamperShouldSample(t *testing.T) {
gotD := AlwaysOffSampler().ShouldSample(
SpanContext{}, false, TraceID{}, SpanID{}, "span", SpanKindClient, []core.KeyValue{}, []Link{})
SpanContext{}, false, ID{}, SpanID{}, "span", SpanKindClient, []core.KeyValue{}, []Link{})
wantD := Decision{Sampled: false}
if diff := cmp.Diff(wantD, gotD); diff != "" {
t.Errorf("Decision: +got, -want%v", diff)
Expand Down
2 changes: 1 addition & 1 deletion api/trace/always_on_sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type alwaysOnSampler struct{}
func (as alwaysOnSampler) ShouldSample(
_ SpanContext,
_ bool,
_ TraceID,
_ ID,
_ SpanID,
_ string,
_ SpanKind,
Expand Down
2 changes: 1 addition & 1 deletion api/trace/always_on_sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

func TestAlwaysOnSamplerShouldSample(t *testing.T) {
gotD := AlwaysOnSampler().ShouldSample(
SpanContext{}, false, TraceID{}, SpanID{}, "span", SpanKindClient, []core.KeyValue{}, []Link{})
SpanContext{}, false, ID{}, SpanID{}, "span", SpanKindClient, []core.KeyValue{}, []Link{})
wantD := Decision{Sampled: true}
if diff := cmp.Diff(wantD, gotD); diff != "" {
t.Errorf("Decision: +got, -want%v", diff)
Expand Down
4 changes: 2 additions & 2 deletions api/trace/b3_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (b3 B3) Extract(ctx context.Context, supplier propagation.HTTPSupplier) con
}

func (b3 B3) extract(supplier propagation.HTTPSupplier) SpanContext {
tid, err := TraceIDFromHex(supplier.Get(B3TraceIDHeader))
tid, err := IDFromHex(supplier.Get(B3TraceIDHeader))
if err != nil {
return EmptySpanContext()
}
Expand Down Expand Up @@ -139,7 +139,7 @@ func (b3 B3) extractSingleHeader(supplier propagation.HTTPSupplier) SpanContext
}

var err error
sc.TraceID, err = TraceIDFromHex(parts[0])
sc.TraceID, err = IDFromHex(parts[0])
if err != nil {
return EmptySpanContext()
}
Expand Down
2 changes: 1 addition & 1 deletion api/trace/sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type Sampler interface {
ShouldSample(
sc SpanContext,
remote bool,
traceID TraceID,
traceID ID,
spanID SpanID,
spanName string,
spanKind SpanKind,
Expand Down
20 changes: 10 additions & 10 deletions api/trace/span_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,26 +44,26 @@ func (e errorConst) Error() string {
return string(e)
}

// TraceID is a unique identity of a trace.
type TraceID [16]byte
// ID is a unique identity of a trace.
type ID [16]byte

var nilTraceID TraceID
var nilTraceID ID
var _ json.Marshaler = nilTraceID

// IsValid checks whether the trace ID is valid. A valid trace ID does
// not consist of zeros only.
func (t TraceID) IsValid() bool {
func (t ID) IsValid() bool {
return !bytes.Equal(t[:], nilTraceID[:])
}

// MarshalJSON implements a custom marshal function to encode TraceID
// as a hex string.
func (t TraceID) MarshalJSON() ([]byte, error) {
func (t ID) MarshalJSON() ([]byte, error) {
return json.Marshal(t.String())
}

// String returns the hex string representation form of a TraceID
func (t TraceID) String() string {
func (t ID) String() string {
return hex.EncodeToString(t[:])
}

Expand All @@ -90,11 +90,11 @@ func (s SpanID) String() string {
return hex.EncodeToString(s[:])
}

// TraceIDFromHex returns a TraceID from a hex string if it is compliant
// IDFromHex returns a TraceID from a hex string if it is compliant
// with the w3c trace-context specification.
// See more at https://www.w3.org/TR/trace-context/#trace-id
func TraceIDFromHex(h string) (TraceID, error) {
t := TraceID{}
func IDFromHex(h string) (ID, error) {
t := ID{}
if len(h) != 32 {
return t, ErrInvalidTraceIDLength
}
Expand Down Expand Up @@ -152,7 +152,7 @@ func decodeHex(h string, b []byte) error {
// SpanContext contains basic information about the span - its trace
// ID, span ID and trace flags.
type SpanContext struct {
TraceID TraceID
TraceID ID
SpanID SpanID
TraceFlags byte
}
Expand Down
32 changes: 16 additions & 16 deletions api/trace/span_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
func TestIsValid(t *testing.T) {
for _, testcase := range []struct {
name string
tid trace.TraceID
tid trace.ID
sid trace.SpanID
want bool
}{
Expand All @@ -34,17 +34,17 @@ func TestIsValid(t *testing.T) {
want: true,
}, {
name: "SpanContext.IsValid() returns false if sc has neither an Trace ID nor Span ID",
tid: trace.TraceID([16]byte{}),
tid: trace.ID([16]byte{}),
sid: [8]byte{},
want: false,
}, {
name: "SpanContext.IsValid() returns false if sc has a Span ID but not a Trace ID",
tid: trace.TraceID([16]byte{}),
tid: trace.ID([16]byte{}),
sid: [8]byte{42},
want: false,
}, {
name: "SpanContext.IsValid() returns false if sc has a Trace ID but not a Span ID",
tid: trace.TraceID([16]byte{1}),
tid: trace.ID([16]byte{1}),
sid: [8]byte{},
want: false,
},
Expand All @@ -66,12 +66,12 @@ func TestIsValidFromHex(t *testing.T) {
for _, testcase := range []struct {
name string
hex string
tid trace.TraceID
tid trace.ID
valid bool
}{
{
name: "Valid TraceID",
tid: trace.TraceID([16]byte{128, 241, 152, 238, 86, 52, 59, 168, 100, 254, 139, 42, 87, 211, 239, 247}),
tid: trace.ID([16]byte{128, 241, 152, 238, 86, 52, 59, 168, 100, 254, 139, 42, 87, 211, 239, 247}),
hex: "80f198ee56343ba864fe8b2a57d3eff7",
valid: true,
}, {
Expand All @@ -89,7 +89,7 @@ func TestIsValidFromHex(t *testing.T) {
},
} {
t.Run(testcase.name, func(t *testing.T) {
tid, err := trace.TraceIDFromHex(testcase.hex)
tid, err := trace.IDFromHex(testcase.hex)

if testcase.valid && err != nil {
t.Errorf("Expected TraceID %s to be valid but end with error %s", testcase.hex, err.Error())
Expand All @@ -109,16 +109,16 @@ func TestIsValidFromHex(t *testing.T) {
func TestHasTraceID(t *testing.T) {
for _, testcase := range []struct {
name string
tid trace.TraceID
tid trace.ID
want bool
}{
{
name: "SpanContext.HasTraceID() returns true if both Low and High are nonzero",
tid: trace.TraceID([16]byte{1}),
tid: trace.ID([16]byte{1}),
want: true,
}, {
name: "SpanContext.HasTraceID() returns false if neither Low nor High are nonzero",
tid: trace.TraceID{},
tid: trace.ID{},
want: false,
},
} {
Expand Down Expand Up @@ -168,20 +168,20 @@ func TestSpanContextIsSampled(t *testing.T) {
{
name: "sampled",
sc: trace.SpanContext{
TraceID: trace.TraceID([16]byte{1}),
TraceID: trace.ID([16]byte{1}),
TraceFlags: trace.TraceFlagsSampled,
},
want: true,
}, {
name: "sampled plus unused",
sc: trace.SpanContext{
TraceID: trace.TraceID([16]byte{1}),
TraceID: trace.ID([16]byte{1}),
TraceFlags: trace.TraceFlagsSampled | trace.TraceFlagsUnused,
},
want: true,
}, {
name: "not sampled/default",
sc: trace.SpanContext{TraceID: trace.TraceID{}},
sc: trace.SpanContext{TraceID: trace.ID{}},
want: false,
},
} {
Expand All @@ -197,17 +197,17 @@ func TestSpanContextIsSampled(t *testing.T) {
func TestStringTraceID(t *testing.T) {
for _, testcase := range []struct {
name string
tid trace.TraceID
tid trace.ID
want string
}{
{
name: "TraceID.String returns string representation of self.TraceID values > 0",
tid: trace.TraceID([16]byte{255}),
tid: trace.ID([16]byte{255}),
want: "ff000000000000000000000000000000",
},
{
name: "TraceID.String returns string representation of self.TraceID values == 0",
tid: trace.TraceID([16]byte{}),
tid: trace.ID([16]byte{}),
want: "00000000000000000000000000000000",
},
} {
Expand Down
6 changes: 3 additions & 3 deletions api/trace/testtrace/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
)

type Generator interface {
TraceID() trace.TraceID
TraceID() trace.ID
SpanID() trace.SpanID
}

Expand All @@ -41,7 +41,7 @@ func NewCountGenerator() *CountGenerator {
return &CountGenerator{}
}

func (g *CountGenerator) TraceID() trace.TraceID {
func (g *CountGenerator) TraceID() trace.ID {
g.lock.Lock()
defer g.lock.Unlock()

Expand All @@ -51,7 +51,7 @@ func (g *CountGenerator) TraceID() trace.TraceID {
g.traceIDLow++
}

var traceID trace.TraceID
var traceID trace.ID

binary.BigEndian.PutUint64(traceID[0:8], g.traceIDLow)
binary.BigEndian.PutUint64(traceID[8:], g.traceIDHigh)
Expand Down
2 changes: 1 addition & 1 deletion api/trace/testtrace/propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type outOfThinAirPropagator struct {
var _ propagation.HTTPPropagator = outOfThinAirPropagator{}

func (p outOfThinAirPropagator) Extract(ctx context.Context, supplier propagation.HTTPSupplier) context.Context {
traceID, err := trace.TraceIDFromHex("938753245abe987f098c0987a9873987")
traceID, err := trace.IDFromHex("938753245abe987f098c0987a9873987")
require.NoError(p.t, err)
spanID, err := trace.SpanIDFromHex("2345f98c0987a09d")
require.NoError(p.t, err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func injectSubBenchmarks(b *testing.B, fn func(context.Context, *testing.B)) {
b.Run("SampledSpanContext", func(b *testing.B) {
var id uint64
spanID, _ := trace.SpanIDFromHex("00f067aa0ba902b7")
traceID, _ := trace.TraceIDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")
traceID, _ := trace.IDFromHex("4bf92f3577b34da6a3ce929d0e0e4736")

mockTracer := &mocktrace.MockTracer{
Sampled: false,
Expand Down
4 changes: 2 additions & 2 deletions api/trace/testtrace/trace_context_propagator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ var (
spanID = mustSpanIDFromHex("00f067aa0ba902b7")
)

func mustTraceIDFromHex(s string) (t trace.TraceID) {
t, _ = trace.TraceIDFromHex(s)
func mustTraceIDFromHex(s string) (t trace.ID) {
t, _ = trace.IDFromHex(s)
return
}

Expand Down
2 changes: 1 addition & 1 deletion api/trace/testtrace/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (t *Tracer) Start(ctx context.Context, name string, opts ...trace.StartOpti
opt(&c)
}

var traceID trace.TraceID
var traceID trace.ID
var parentSpanID trace.SpanID

parentSpanContext, _, links := parent.GetSpanContextAndLinks(ctx, c.NewRoot)
Expand Down
2 changes: 1 addition & 1 deletion api/trace/trace_context_propagator.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (TraceContext) extract(supplier propagation.HTTPSupplier) SpanContext {

var sc SpanContext

sc.TraceID, err = TraceIDFromHex(sections[1][:32])
sc.TraceID, err = IDFromHex(sections[1][:32])
if err != nil {
return EmptySpanContext()
}
Expand Down
8 changes: 4 additions & 4 deletions bridge/opentracing/internal/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type MockContextKeyValue struct {
type MockTracer struct {
Resources otelcorrelation.Map
FinishedSpans []*MockSpan
SpareTraceIDs []oteltrace.TraceID
SpareTraceIDs []oteltrace.ID
SpareSpanIDs []oteltrace.SpanID
SpareContextKeyValues []MockContextKeyValue

Expand Down Expand Up @@ -126,7 +126,7 @@ func (t *MockTracer) addSpareContextValue(ctx context.Context) context.Context {
return ctx
}

func (t *MockTracer) getTraceID(ctx context.Context, spanOpts *oteltrace.StartConfig) oteltrace.TraceID {
func (t *MockTracer) getTraceID(ctx context.Context, spanOpts *oteltrace.StartConfig) oteltrace.ID {
if parent := t.getParentSpanContext(ctx, spanOpts); parent.IsValid() {
return parent.TraceID
}
Expand Down Expand Up @@ -175,11 +175,11 @@ func (t *MockTracer) getRandSpanID() oteltrace.SpanID {
return sid
}

func (t *MockTracer) getRandTraceID() oteltrace.TraceID {
func (t *MockTracer) getRandTraceID() oteltrace.ID {
t.randLock.Lock()
defer t.randLock.Unlock()

tid := oteltrace.TraceID{}
tid := oteltrace.ID{}
t.rand.Read(tid[:])

return tid
Expand Down
8 changes: 4 additions & 4 deletions bridge/opentracing/mix_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestMixedAPIs(t *testing.T) {
// simple test

type simpleTest struct {
traceID oteltrace.TraceID
traceID oteltrace.ID
spanIDs []oteltrace.SpanID
}

Expand Down Expand Up @@ -178,7 +178,7 @@ func (st *simpleTest) noop(t *testing.T, ctx context.Context) context.Context {
// current/active span test

type currentActiveSpanTest struct {
traceID oteltrace.TraceID
traceID oteltrace.ID
spanIDs []oteltrace.SpanID

recordedCurrentOtelSpanIDs []oteltrace.SpanID
Expand Down Expand Up @@ -614,7 +614,7 @@ func generateBaggageKeys(key string) (otKey, otelKey string) {

// helpers

func checkTraceAndSpans(t *testing.T, tracer *internal.MockTracer, expectedTraceID oteltrace.TraceID, expectedSpanIDs []oteltrace.SpanID) {
func checkTraceAndSpans(t *testing.T, tracer *internal.MockTracer, expectedTraceID oteltrace.ID, expectedSpanIDs []oteltrace.SpanID) {
expectedSpanCount := len(expectedSpanIDs)

// reverse spanIDs, since first span ID belongs to root, that
Expand Down Expand Up @@ -661,7 +661,7 @@ func reverse(length int, swap func(i, j int)) {
}
}

func simpleTraceID() oteltrace.TraceID {
func simpleTraceID() oteltrace.ID {
return [16]byte{123, 42}
}

Expand Down
Loading

0 comments on commit bae2298

Please sign in to comment.