1717from opencensus .trace .span_context import SpanContext
1818from opencensus .trace import labels_helper
1919from opencensus .trace import span as trace_span
20- from opencensus .trace .trace import Trace
2120from opencensus .trace .tracer import base
2221
2322
@@ -34,24 +33,16 @@ def __init__(self, span_context=None):
3433
3534 self .span_context = span_context
3635 self .trace_id = span_context .trace_id
37- self .cur_trace = self .trace ()
38- self ._span_stack = []
3936 self .root_span_id = span_context .span_id
4037
41- def trace (self ):
42- """Create a trace using the context information.
43-
44- :rtype: :class:`~opencensus.trace.trace.Trace`
45- :returns: The Trace object.
46- """
47- return Trace (trace_id = self .trace_id )
38+ # Stack which maintains nested spans
39+ self ._span_stack = []
4840
49- def start_trace (self ):
50- """Start a trace."""
51- self .cur_trace .start ()
41+ # List of spans to report
42+ self ._spans_list = []
5243
53- def end_trace (self ):
54- """End a trace.
44+ def finish (self ):
45+ """Finish all spans
5546
5647 :rtype: dict
5748 :returns: JSON format trace.
@@ -61,7 +52,6 @@ def end_trace(self):
6152 helper .set_labels ()
6253
6354 trace = self ._get_trace_json ()
64- self .cur_trace .finish ()
6555
6656 return trace
6757
@@ -91,7 +81,7 @@ def start_span(self, name='span'):
9181 name ,
9282 parent_span_id = parent_span_id ,
9383 context_tracer = self )
94- self .cur_trace . spans .append (span )
84+ self ._spans_list .append (span )
9585 self ._span_stack .append (span )
9686 self .span_context .span_id = span .span_id
9787 span .start ()
@@ -123,7 +113,7 @@ def current_span(self):
123113 return cur_span
124114
125115 def list_collected_spans (self ):
126- return self .cur_trace . spans
116+ return self ._spans_list
127117
128118 def add_label_to_current_span (self , label_key , label_value ):
129119 """Add label to current span.
@@ -146,13 +136,13 @@ def add_label_to_spans(self, label_key, label_value):
146136 :type label_value:str
147137 :param label_value: Label value.
148138 """
149- for span in self .cur_trace . spans :
139+ for span in self ._spans_list :
150140 span .add_label (label_key , label_value )
151141
152142 def _get_trace_json (self ):
153143 """Get the JSON format trace."""
154144 spans_list = []
155- for root_span in self .cur_trace . spans :
145+ for root_span in self ._spans_list :
156146 span_tree = list (iter (root_span ))
157147 span_tree_json = [trace_span .format_span_json (span )
158148 for span in span_tree ]
@@ -162,7 +152,7 @@ def _get_trace_json(self):
162152 return
163153
164154 trace = {
165- 'traceId' : self .cur_trace . trace_id ,
155+ 'traceId' : self .trace_id ,
166156 'spans' : spans_list ,
167157 }
168158
0 commit comments