Skip to content
This repository has been archived by the owner on May 23, 2023. It is now read-only.

Commit

Permalink
delint dapperish
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben Sigelman committed Dec 4, 2015
1 parent e71aba1 commit 54edabe
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
70 changes: 43 additions & 27 deletions examples/dapperish/dapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ import (
"github.com/opentracing/api-golang/opentracing/standardtracer"
)

// NewTracer returns a new dapperish OpenTracer instance.
func NewTracer(processName string) opentracing.OpenTracer {
return standardtracer.New(
NewTrivialRecorder(processName),
NewDapperishTraceContextSource())
NewTraceContextSource())
}

// An implementation of opentracing.TraceContext.
// DapperishTraceContext is an implementation of opentracing.TraceContext.
type DapperishTraceContext struct {
// A probabilistically unique identifier for a [multi-span] trace.
TraceID int64
Expand All @@ -38,12 +39,13 @@ type DapperishTraceContext struct {
const (
// Note that these strings are designed to be unchanged by the conversion
// into standard HTTP headers (which messes with capitalization).
fieldNameTraceId = "Traceid"
fieldNameSpanId = "Spanid"
fieldNameTraceID = "Traceid"
fieldNameSpanID = "Spanid"
fieldNameSampled = "Sampled"
fieldNameTagPrefix = "Tag-"
)

// NewChild complies with the opentracing.TraceContext interface.
func (d *DapperishTraceContext) NewChild() (opentracing.TraceContext, opentracing.Tags) {
d.tagLock.RLock()
newTags := make(map[string]string, len(d.traceTags))
Expand All @@ -60,22 +62,7 @@ func (d *DapperishTraceContext) NewChild() (opentracing.TraceContext, opentracin
}, opentracing.Tags{"parent_span_id": d.SpanID}
}

// An implementation of opentracing.TraceContextSource.
type DapperishTraceContextSource struct{}

func NewDapperishTraceContextSource() *DapperishTraceContextSource {
return &DapperishTraceContextSource{}
}

func (m *DapperishTraceContextSource) NewRootTraceContext() opentracing.TraceContext {
return &DapperishTraceContext{
TraceID: randomID(),
SpanID: randomID(),
Sampled: randomID()%1024 == 0,
traceTags: make(map[string]string),
}
}

// SetTraceTag complies with the opentracing.TraceContext interface.
func (d *DapperishTraceContext) SetTraceTag(key, val string) opentracing.TraceContext {
d.tagLock.Lock()
defer d.tagLock.Unlock()
Expand All @@ -84,20 +71,43 @@ func (d *DapperishTraceContext) SetTraceTag(key, val string) opentracing.TraceCo
return d
}

// TraceTag complies with the opentracing.TraceContext interface.
func (d *DapperishTraceContext) TraceTag(key string) string {
d.tagLock.RLock()
defer d.tagLock.RUnlock()

return d.traceTags[key]
}

// DapperishTraceContextSource is an implementation of
// opentracing.TraceContextSource.
type DapperishTraceContextSource struct{}

// NewTraceContextSource returns a dapperish opentracing.TraceContextSource
// implementation.
func NewTraceContextSource() *DapperishTraceContextSource {
return &DapperishTraceContextSource{}
}

// NewRootTraceContext complies with the opentracing.TraceContextSource interface.
func (d *DapperishTraceContextSource) NewRootTraceContext() opentracing.TraceContext {
return &DapperishTraceContext{
TraceID: randomID(),
SpanID: randomID(),
Sampled: randomID()%1024 == 0,
traceTags: make(map[string]string),
}
}

// MarshalTraceContextStringMap complies with the
// opentracing.TraceContextSource interface.
func (d *DapperishTraceContextSource) MarshalTraceContextStringMap(
ctx opentracing.TraceContext,
) map[string]string {
dctx := ctx.(*DapperishTraceContext)
rval := map[string]string{
fieldNameTraceId: strconv.FormatInt(dctx.TraceID, 10),
fieldNameSpanId: strconv.FormatInt(dctx.SpanID, 10),
fieldNameTraceID: strconv.FormatInt(dctx.TraceID, 10),
fieldNameSpanID: strconv.FormatInt(dctx.SpanID, 10),
fieldNameSampled: strconv.FormatBool(dctx.Sampled),
}
dctx.tagLock.RLock()
Expand All @@ -108,6 +118,8 @@ func (d *DapperishTraceContextSource) MarshalTraceContextStringMap(
return rval
}

// UnmarshalTraceContextStringMap complies with the
// opentracing.TraceContextSource interface.
func (d *DapperishTraceContextSource) UnmarshalTraceContextStringMap(
encoded map[string]string,
) (opentracing.TraceContext, error) {
Expand All @@ -118,14 +130,14 @@ func (d *DapperishTraceContextSource) UnmarshalTraceContextStringMap(
var err error
for k, v := range encoded {
switch k {
case fieldNameTraceId:
traceID, err = strconv.ParseInt(encoded[fieldNameTraceId], 10, 64)
case fieldNameTraceID:
traceID, err = strconv.ParseInt(encoded[fieldNameTraceID], 10, 64)
if err != nil {
return nil, err
}
requiredFieldCount++
case fieldNameSpanId:
spanID, err = strconv.ParseInt(encoded[fieldNameSpanId], 10, 64)
case fieldNameSpanID:
spanID, err = strconv.ParseInt(encoded[fieldNameSpanID], 10, 64)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -156,6 +168,8 @@ func (d *DapperishTraceContextSource) UnmarshalTraceContextStringMap(
}, nil
}

// MarshalTraceContextBinary complies with the opentracing.TraceContextSource
// interface.
func (d *DapperishTraceContextSource) MarshalTraceContextBinary(ctx opentracing.TraceContext) []byte {
dtc := ctx.(*DapperishTraceContext)
// XXX: support tags
Expand All @@ -169,7 +183,7 @@ func (d *DapperishTraceContextSource) MarshalTraceContextBinary(ctx opentracing.
if err != nil {
panic(err)
}
var sampledByte byte = 0
var sampledByte byte
if dtc.Sampled {
sampledByte = 1
}
Expand All @@ -180,6 +194,8 @@ func (d *DapperishTraceContextSource) MarshalTraceContextBinary(ctx opentracing.
return buf.Bytes()
}

// UnmarshalTraceContextBinary complies with the opentracing.TraceContextSource
// interface.
func (d *DapperishTraceContextSource) UnmarshalTraceContextBinary(
encoded []byte,
) (opentracing.TraceContext, error) {
Expand Down
2 changes: 1 addition & 1 deletion examples/dapperish/random.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
)

var (
seededIDGen *rand.Rand = rand.New(rand.NewSource(time.Now().UnixNano()))
seededIDGen = rand.New(rand.NewSource(time.Now().UnixNano()))
seededIDLock sync.Mutex
)

Expand Down
5 changes: 5 additions & 0 deletions examples/dapperish/trivialrecorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,30 @@ import (
"github.com/opentracing/api-golang/opentracing"
)

// TrivialRecorder implements the opentracing.Recorder interface.
type TrivialRecorder struct {
processName string
tags map[string]string
}

// NewTrivialRecorder returns a TrivialRecorder for the given `processName`.
func NewTrivialRecorder(processName string) *TrivialRecorder {
return &TrivialRecorder{
processName: processName,
tags: make(map[string]string),
}
}

// ProcessName complies with the opentracing.ProcessIdentifier interface.
func (t *TrivialRecorder) ProcessName() string { return t.processName }

// SetTag complies with the opentracing.ProcessIdentifier interface.
func (t *TrivialRecorder) SetTag(key string, val interface{}) opentracing.ProcessIdentifier {
t.tags[key] = fmt.Sprint(val)
return t
}

// RecordSpan complies with the opentracing.Recorder interface.
func (t *TrivialRecorder) RecordSpan(span *opentracing.RawSpan) {
fmt.Printf(
"RecordSpan: %v[%v, %v us] --> %v logs. trace context: %v\n",
Expand Down

0 comments on commit 54edabe

Please sign in to comment.