Skip to content

Commit

Permalink
Merge pull request #34 from ipfs/feat/loggable-spanId
Browse files Browse the repository at this point in the history
log spanID info to json output
  • Loading branch information
whyrusleeping committed Jun 8, 2018
2 parents b9df188 + 414e675 commit cb3cb25
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 13 deletions.
4 changes: 2 additions & 2 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,8 +223,8 @@ func (el *eventLogger) SetTags(ctx context.Context, tags map[string]interface{})
log.Errorf("SetTags with no Span in context called on %s:%d", path.Base(file), line)
return
}
for k := range tags {
span.SetTag(k, tags[k])
for k, v := range tags {
span.SetTag(k, v)
}
}

Expand Down
21 changes: 20 additions & 1 deletion log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestSingleEvent(t *testing.T) {
// greater than zero should work for now
assertNotZero(t, ls.Duration)
assertNotZero(t, ls.Start)
assertNotZero(t, ls.TraceID)
assertNotZero(t, ls.SpanID)
}

func TestSingleEventWithErr(t *testing.T) {
Expand Down Expand Up @@ -88,6 +90,8 @@ func TestSingleEventWithErr(t *testing.T) {
// greater than zero should work for now
assertNotZero(t, ls.Duration)
assertNotZero(t, ls.Start)
assertNotZero(t, ls.TraceID)
assertNotZero(t, ls.SpanID)
}

func TestEventWithTag(t *testing.T) {
Expand Down Expand Up @@ -122,6 +126,8 @@ func TestEventWithTag(t *testing.T) {
// greater than zero should work for now
assertNotZero(t, ls.Duration)
assertNotZero(t, ls.Start)
assertNotZero(t, ls.TraceID)
assertNotZero(t, ls.SpanID)
}

func TestEventWithTags(t *testing.T) {
Expand Down Expand Up @@ -160,6 +166,8 @@ func TestEventWithTags(t *testing.T) {
// greater than zero should work for now
assertNotZero(t, ls.Duration)
assertNotZero(t, ls.Start)
assertNotZero(t, ls.TraceID)
assertNotZero(t, ls.SpanID)
}

func TestEventWithLogs(t *testing.T) {
Expand Down Expand Up @@ -204,6 +212,8 @@ func TestEventWithLogs(t *testing.T) {
// greater than zero should work for now
assertNotZero(t, ls.Duration)
assertNotZero(t, ls.Start)
assertNotZero(t, ls.TraceID)
assertNotZero(t, ls.SpanID)
}

func TestMultiEvent(t *testing.T) {
Expand Down Expand Up @@ -238,11 +248,14 @@ func TestMultiEvent(t *testing.T) {
assertEqual(t, "e2", e2.Operation)
assertEqual(t, "test", e2.Tags["system"])
assertNotZero(t, e2.Duration)
assertEqual(t, e1.TraceID, e2.TraceID)

er := getEvent(evtDecoder)
assertEqual(t, "root", er.Operation)
assertEqual(t, "test", er.Tags["system"])
assertNotZero(t, er.Start)
assertNotZero(t, er.TraceID)
assertNotZero(t, er.SpanID)

}

Expand Down Expand Up @@ -288,7 +301,9 @@ func TestEventSerialization(t *testing.T) {
assertEqual(t, "recv", e.Operation)
assertEqual(t, "test", e.Tags["system"])
assertNotZero(t, e.Start)
assertNotZero(t, e.Start)
assertNotZero(t, e.Duration)
assertNotZero(t, e.TraceID)
assertNotZero(t, e.SpanID)

}

Expand Down Expand Up @@ -347,6 +362,8 @@ func TestEventBegin(t *testing.T) {
// greater than zero should work for now
assertNotZero(t, ls.Duration)
assertNotZero(t, ls.Start)
assertNotZero(t, ls.SpanID)
assertNotZero(t, ls.TraceID)
}

func TestEventBeginWithErr(t *testing.T) {
Expand Down Expand Up @@ -385,4 +402,6 @@ func TestEventBeginWithErr(t *testing.T) {
// greater than zero should work for now
assertNotZero(t, ls.Duration)
assertNotZero(t, ls.Start)
assertNotZero(t, ls.SpanID)
assertNotZero(t, ls.TraceID)
}
26 changes: 16 additions & 10 deletions tracer/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ func NewLoggableRecorder() *LoggableSpanRecorder {

// Loggable Representation of a span, treated as an event log
type LoggableSpan struct {
Operation string `json:"Operation"`
Start time.Time `json:"Start"`
Duration time.Duration `json:"Duration"`
Tags opentrace.Tags `json:"Tags"`
Logs []SpanLog `json:"Logs"`
TraceID uint64 `json:"TraceID"`
SpanID uint64 `json:"SpanID"`
ParentSpanID uint64 `json:"ParentSpanID"`
Operation string `json:"Operation"`
Start time.Time `json:"Start"`
Duration time.Duration `json:"Duration"`
Tags opentrace.Tags `json:"Tags"`
Logs []SpanLog `json:"Logs"`
}

type SpanLog struct {
Expand Down Expand Up @@ -63,11 +66,14 @@ func (r *LoggableSpanRecorder) RecordSpan(span RawSpan) {
}

spanlog := &LoggableSpan{
Operation: span.Operation,
Start: span.Start,
Duration: span.Duration,
Tags: span.Tags,
Logs: sl,
TraceID: span.Context.TraceID,
SpanID: span.Context.SpanID,
ParentSpanID: span.ParentSpanID,
Operation: span.Operation,
Start: span.Start,
Duration: span.Duration,
Tags: span.Tags,
Logs: sl,
}

out, err := json.Marshal(spanlog)
Expand Down

0 comments on commit cb3cb25

Please sign in to comment.