Skip to content
This repository was archived by the owner on Aug 17, 2020. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ func NewAgent(options ...Option) (*Agent, error) {

agent.tracer = tracer.NewWithOptions(tracer.Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool {
ShouldSample: func(traceID uuid.UUID) bool {
return true
},
MaxLogsPerSpan: 10000,
Expand Down
4 changes: 2 additions & 2 deletions agent/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (r *SpanRecorder) getPayloadComponents(span tracer.RawSpan) (PayloadSpan, [
}
payloadSpan := PayloadSpan{
"context": map[string]interface{}{
"trace_id": fmt.Sprintf("%x", span.Context.TraceID),
"trace_id": tracer.UUIDToString(span.Context.TraceID),
"span_id": fmt.Sprintf("%x", span.Context.SpanID),
"baggage": span.Context.Baggage,
},
Expand All @@ -345,7 +345,7 @@ func (r *SpanRecorder) getPayloadComponents(span tracer.RawSpan) (PayloadSpan, [
}
events = append(events, PayloadEvent{
"context": map[string]interface{}{
"trace_id": fmt.Sprintf("%x", span.Context.TraceID),
"trace_id": tracer.UUIDToString(span.Context.TraceID),
"span_id": fmt.Sprintf("%x", span.Context.SpanID),
"event_id": eventId.String(),
},
Expand Down
3 changes: 2 additions & 1 deletion tracer/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package tracer
import (
"testing"

"github.com/google/uuid"
ot "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/harness"
)
Expand All @@ -11,7 +12,7 @@ import (
func newTracer() (tracer ot.Tracer, closer func()) {
tracer = NewWithOptions(Options{
Recorder: NewInMemoryRecorder(),
ShouldSample: func(traceID uint64) bool { return true }, // always sample
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
})
return tracer, nil
}
Expand Down
3 changes: 2 additions & 1 deletion tracer/bench_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"testing"

"github.com/google/uuid"
"github.com/opentracing/opentracing-go"
)

Expand Down Expand Up @@ -80,7 +81,7 @@ func BenchmarkTrimmedSpan_100Events_100Tags_100BaggageItems(b *testing.B) {
var r CountingRecorder
opts := DefaultOptions()
opts.TrimUnsampledSpans = true
opts.ShouldSample = func(_ uint64) bool { return false }
opts.ShouldSample = func(_ uuid.UUID) bool { return false }
opts.Recorder = &r
t := NewWithOptions(opts)
benchmarkWithOpsAndCB(b, func() opentracing.Span {
Expand Down
4 changes: 3 additions & 1 deletion tracer/context.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package tracer

import "github.com/google/uuid"

// SpanContext holds the basic Span metadata.
type SpanContext struct {
// A probabilistically unique identifier for a [multi-span] trace.
TraceID uint64
TraceID uuid.UUID

// A probabilistically unique identifier for a span.
SpanID uint64
Expand Down
9 changes: 6 additions & 3 deletions tracer/propagation.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package tracer

import opentracing "github.com/opentracing/opentracing-go"
import (
"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
)

type accessorPropagator struct {
tracer *tracerImpl
Expand All @@ -10,8 +13,8 @@ type accessorPropagator struct {
// by types which have a means of storing the trace metadata and already know
// how to serialize themselves (for example, protocol buffers).
type DelegatingCarrier interface {
SetState(traceID, spanID uint64, sampled bool)
State() (traceID, spanID uint64, sampled bool)
SetState(traceID uuid.UUID, spanID uint64, sampled bool)
State() (traceID uuid.UUID, spanID uint64, sampled bool)
SetBaggageItem(key, value string)
GetBaggage(func(key, value string))
}
Expand Down
12 changes: 8 additions & 4 deletions tracer/propagation_env_var.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@ package tracer

import (
"fmt"
"github.com/opentracing/opentracing-go"
"os"
"strconv"
"strings"

"github.com/google/uuid"
"github.com/opentracing/opentracing-go"
)

const (
Expand All @@ -30,7 +32,8 @@ func (p *envVarPropagator) Inject(
if carrier == nil {
return opentracing.ErrInvalidCarrier
}
carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16))

carrier.Set(fieldNameTraceID, UUIDToString(sc.TraceID))
carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16))
carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled))
for k, v := range sc.Baggage {
Expand All @@ -50,14 +53,15 @@ func (p *envVarPropagator) Extract(
return nil, opentracing.ErrInvalidCarrier
}
requiredFieldCount := 0
var traceID, spanID uint64
var traceID uuid.UUID
var spanID uint64
var sampled bool
var err error
decodedBaggage := make(map[string]string)
err = carrier.ForeachKey(func(k, v string) error {
switch strings.ToLower(k) {
case fieldNameTraceID:
traceID, err = strconv.ParseUint(v, 16, 64)
traceID, err = StringToUUID(v)
if err != nil {
return opentracing.ErrSpanContextCorrupted
}
Expand Down
17 changes: 12 additions & 5 deletions tracer/propagation_ot.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/gogo/protobuf/proto"
"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"go.undefinedlabs.com/scopeagent/tracer/wire"
)
Expand Down Expand Up @@ -40,7 +41,7 @@ func (p *textMapPropagator) Inject(
if !ok {
return opentracing.ErrInvalidCarrier
}
carrier.Set(fieldNameTraceID, strconv.FormatUint(sc.TraceID, 16))
carrier.Set(fieldNameTraceID, UUIDToString(sc.TraceID))
carrier.Set(fieldNameSpanID, strconv.FormatUint(sc.SpanID, 16))
carrier.Set(fieldNameSampled, strconv.FormatBool(sc.Sampled))

Expand All @@ -58,14 +59,15 @@ func (p *textMapPropagator) Extract(
return nil, opentracing.ErrInvalidCarrier
}
requiredFieldCount := 0
var traceID, spanID uint64
var traceID uuid.UUID
var spanID uint64
var sampled bool
var err error
decodedBaggage := make(map[string]string)
err = carrier.ForeachKey(func(k, v string) error {
switch strings.ToLower(k) {
case fieldNameTraceID:
traceID, err = strconv.ParseUint(v, 16, 64)
traceID, err = StringToUUID(v)
if err != nil {
return opentracing.ErrSpanContextCorrupted
}
Expand Down Expand Up @@ -122,7 +124,8 @@ func (p *binaryPropagator) Inject(
}

state := wire.TracerState{}
state.TraceId = sc.TraceID
state.TraceIdHi = binary.LittleEndian.Uint64(sc.TraceID[:8])
state.TraceIdLo = binary.LittleEndian.Uint64(sc.TraceID[8:])
state.SpanId = sc.SpanID
state.Sampled = sc.Sampled
state.BaggageItems = sc.Baggage
Expand Down Expand Up @@ -171,8 +174,12 @@ func (p *binaryPropagator) Extract(
return nil, opentracing.ErrSpanContextCorrupted
}

traceId := uuid.UUID{}
binary.LittleEndian.PutUint64(traceId[:8], ctx.TraceIdHi)
binary.LittleEndian.PutUint64(traceId[8:], ctx.TraceIdLo)

return SpanContext{
TraceID: ctx.TraceId,
TraceID: traceId,
SpanID: ctx.SpanId,
Sampled: ctx.Sampled,
Baggage: ctx.BaggageItems,
Expand Down
5 changes: 3 additions & 2 deletions tracer/propagation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

"github.com/davecgh/go-spew/spew"
"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"go.undefinedlabs.com/scopeagent/tracer"
)
Expand All @@ -29,11 +30,11 @@ func (vc *verbatimCarrier) GetBaggage(f func(string, string)) {
}
}

func (vc *verbatimCarrier) SetState(tID, sID uint64, sampled bool) {
func (vc *verbatimCarrier) SetState(tID uuid.UUID, sID uint64, sampled bool) {
vc.SpanContext = tracer.SpanContext{TraceID: tID, SpanID: sID, Sampled: sampled}
}

func (vc *verbatimCarrier) State() (traceID, spanID uint64, sampled bool) {
func (vc *verbatimCarrier) State() (traceID uuid.UUID, spanID uint64, sampled bool) {
return vc.SpanContext.TraceID, vc.SpanContext.SpanID, vc.SpanContext.Sampled
}

Expand Down
17 changes: 9 additions & 8 deletions tracer/span_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strconv"
"testing"

"github.com/google/uuid"
opentracing "github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"
"github.com/opentracing/opentracing-go/log"
Expand All @@ -15,7 +16,7 @@ func TestSpan_Baggage(t *testing.T) {
recorder := NewInMemoryRecorder()
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
})
span := tracer.StartSpan("x")
span.SetBaggageItem("x", "y")
Expand Down Expand Up @@ -52,7 +53,7 @@ func TestSpan_Sampling(t *testing.T) {
recorder := NewInMemoryRecorder()
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true },
ShouldSample: func(traceID uuid.UUID) bool { return true },
})
span := tracer.StartSpan("x")
span.Finish()
Expand All @@ -66,7 +67,7 @@ func TestSpan_Sampling(t *testing.T) {

tracer = NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return false },
ShouldSample: func(traceID uuid.UUID) bool { return false },
})

recorder.Reset()
Expand All @@ -85,7 +86,7 @@ func TestSpan_SingleLoggedTaggedSpan(t *testing.T) {
recorder := NewInMemoryRecorder()
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
})
span := tracer.StartSpan("x")
span.LogEventWithPayload("event", "payload")
Expand All @@ -112,7 +113,7 @@ func TestSpan_TrimUnsampledSpans(t *testing.T) {
// Tracer that trims only unsampled but always samples
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
TrimUnsampledSpans: true,
})

Expand All @@ -133,7 +134,7 @@ func TestSpan_TrimUnsampledSpans(t *testing.T) {
// Tracer that trims only unsampled and never samples
tracer = NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return false }, // never sample
ShouldSample: func(traceID uuid.UUID) bool { return false }, // never sample
TrimUnsampledSpans: true,
})

Expand All @@ -152,7 +153,7 @@ func TestSpan_DropAllLogs(t *testing.T) {
// Tracer that drops logs
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
DropAllLogs: true,
})

Expand All @@ -175,7 +176,7 @@ func TestSpan_MaxLogSperSpan(t *testing.T) {
// Tracer that only retains the last <limit> logs.
tracer := NewWithOptions(Options{
Recorder: recorder,
ShouldSample: func(traceID uint64) bool { return true }, // always sample
ShouldSample: func(traceID uuid.UUID) bool { return true }, // always sample
MaxLogsPerSpan: limit,
})

Expand Down
14 changes: 8 additions & 6 deletions tracer/tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import (
"time"

"github.com/go-errors/errors"
"github.com/google/uuid"
"github.com/opentracing/opentracing-go"
)

const emptyUUID = "00000000-0000-0000-0000-000000000000"

// Tracer extends the opentracing.Tracer interface with methods to
// probe implementation state, for use by basictracer consumers.
type Tracer interface {
Expand All @@ -24,10 +27,9 @@ type Options struct {
// to allow deterministic sampling decisions to be made across different nodes.
// For example,
//
// func(traceID uint64) { return traceID % 64 == 0 }
// func(traceID uuid.UUID) { return true }
//
// samples every 64th trace on average.
ShouldSample func(traceID uint64) bool
ShouldSample func(traceID uuid.UUID) bool
// TrimUnsampledSpans turns potentially expensive operations on unsampled
// Spans into no-ops. More precisely, tags and log events are silently
// discarded. If NewSpanEventListener is set, the callbacks will still fire.
Expand Down Expand Up @@ -100,7 +102,7 @@ type Options struct {
// returned object with a Tracer.
func DefaultOptions() Options {
return Options{
ShouldSample: func(traceID uint64) bool { return traceID%64 == 0 },
ShouldSample: func(traceID uuid.UUID) bool { return true },
MaxLogsPerSpan: 100,
}
}
Expand Down Expand Up @@ -195,10 +197,10 @@ ReferencesLoop:
break ReferencesLoop
}
}
if sp.raw.Context.TraceID == 0 {
if sp.raw.Context.TraceID.String() == emptyUUID {
// No parent Span found; allocate new trace and span ids and determine
// the Sampled status.
sp.raw.Context.TraceID = getRandomId()
sp.raw.Context.TraceID = getRandomUUID()
sp.raw.Context.SpanID = getRandomId()
sp.raw.Context.Sampled = t.options.ShouldSample(sp.raw.Context.TraceID)
}
Expand Down
Loading