From c7076ecacdf16a08bf1cd3e103d380207d2380d7 Mon Sep 17 00:00:00 2001 From: frrist Date: Thu, 7 Jun 2018 05:45:25 -0700 Subject: [PATCH 1/2] add SpanID & TraceID to loggable spans --- log_test.go | 21 ++++++++++++++++++++- tracer/recorder.go | 4 ++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/log_test.go b/log_test.go index 2fd15e2c5378..46ac7372592d 100644 --- a/log_test.go +++ b/log_test.go @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) } @@ -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) } @@ -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) { @@ -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) } diff --git a/tracer/recorder.go b/tracer/recorder.go index 12121f29594b..11353a2e6e9e 100644 --- a/tracer/recorder.go +++ b/tracer/recorder.go @@ -27,6 +27,8 @@ func NewLoggableRecorder() *LoggableSpanRecorder { // Loggable Representation of a span, treated as an event log type LoggableSpan struct { + TraceID uint64 `json:"TraceID"` + SpanID uint64 `json:"SpanID"` Operation string `json:"Operation"` Start time.Time `json:"Start"` Duration time.Duration `json:"Duration"` @@ -63,6 +65,8 @@ func (r *LoggableSpanRecorder) RecordSpan(span RawSpan) { } spanlog := &LoggableSpan{ + TraceID: span.Context.TraceID, + SpanID: span.Context.SpanID, Operation: span.Operation, Start: span.Start, Duration: span.Duration, From 414e675391aea0f5fced0223d87aa946da2e7296 Mon Sep 17 00:00:00 2001 From: Jeromy Date: Thu, 7 Jun 2018 08:04:16 -0700 Subject: [PATCH 2/2] small tweak, add parentspanid --- log.go | 4 ++-- tracer/recorder.go | 30 ++++++++++++++++-------------- 2 files changed, 18 insertions(+), 16 deletions(-) diff --git a/log.go b/log.go index 52cbc38bf297..7732d7db8f29 100644 --- a/log.go +++ b/log.go @@ -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) } } diff --git a/tracer/recorder.go b/tracer/recorder.go index 11353a2e6e9e..60ed1f8cb27f 100644 --- a/tracer/recorder.go +++ b/tracer/recorder.go @@ -27,13 +27,14 @@ func NewLoggableRecorder() *LoggableSpanRecorder { // Loggable Representation of a span, treated as an event log type LoggableSpan struct { - TraceID uint64 `json:"TraceID"` - SpanID uint64 `json:"SpanID"` - 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 { @@ -65,13 +66,14 @@ func (r *LoggableSpanRecorder) RecordSpan(span RawSpan) { } spanlog := &LoggableSpan{ - TraceID: span.Context.TraceID, - SpanID: span.Context.SpanID, - 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)