From c42e14616bb79630e8d3d189a62bf2acbdd7120c Mon Sep 17 00:00:00 2001 From: yore Date: Thu, 3 Jan 2019 17:02:50 +0800 Subject: [PATCH 1/2] Add get data of span method return the data of span to allow accessing all info of a span. just return the value of data instead a pointer to avoid changing by user. --- trace/trace.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/trace/trace.go b/trace/trace.go index 9e5e5f033..525a301b8 100644 --- a/trace/trace.go +++ b/trace/trace.go @@ -286,6 +286,14 @@ func (s *Span) makeSpanData() *SpanData { return &sd } +// SpanData returns the data of the span +func (s *Span) SpanData() SpanData { + if s == nil || s.data == nil { + return SpanData{} + } + return *s.data +} + // SpanContext returns the SpanContext of the span. func (s *Span) SpanContext() SpanContext { if s == nil { From a759ac1d5127875b8781b221839de7bbfca4a3a7 Mon Sep 17 00:00:00 2001 From: yore Date: Fri, 4 Jan 2019 15:07:15 +0800 Subject: [PATCH 2/2] Add flag to force generate span data span data will not init if span is not sampled and not enable local span storage. so add a flag to force generate span data. this operation will have a little more memery overhead, but it can be use to get all information when we get a span. we can use the information to do logging and rebuild the trace from logs. also change SpanData method to use makeSpanData result, because shallow copy of span.data would allow for mutating for instance the Attributes map (and get some concurrent access nasties). --- trace/trace.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/trace/trace.go b/trace/trace.go index 525a301b8..adbad4193 100644 --- a/trace/trace.go +++ b/trace/trace.go @@ -147,6 +147,14 @@ func WithSampler(sampler Sampler) StartOption { } } +// force to generate span data even span is not be sampled +var forceGenerateSpanData = false + +// SetForceGenerateSpanData set true to flag forceGenerateSpanData +func SetForceGenerateSpanData() { + forceGenerateSpanData = true +} + // StartSpan starts a new child span of the current span in the context. If // there is no span in the context, creates a new trace and span. // @@ -215,7 +223,8 @@ func startSpanInternal(name string, hasParent bool, parent SpanContext, remotePa HasRemoteParent: remoteParent}).Sample) } - if !internal.LocalSpanStoreEnabled && !span.spanContext.IsSampled() { + // will not return here if forceGenerateSpanData + if !forceGenerateSpanData && !internal.LocalSpanStoreEnabled && !span.spanContext.IsSampled() { return span } @@ -287,11 +296,10 @@ func (s *Span) makeSpanData() *SpanData { } // SpanData returns the data of the span -func (s *Span) SpanData() SpanData { - if s == nil || s.data == nil { - return SpanData{} - } - return *s.data +func (s *Span) SpanData() *SpanData { + // use makeSpanData to deep copy span.data + // to avoid concurrent operation to attributes map + return s.makeSpanData() } // SpanContext returns the SpanContext of the span.