Skip to content
This repository was archived by the owner on Sep 17, 2025. It is now read-only.

Commit 38b2bec

Browse files
authored
Remove trace class (#39)
1 parent 8370e6b commit 38b2bec

16 files changed

Lines changed: 81 additions & 358 deletions

trace/opencensus/trace/__init__.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from opencensus.trace.trace import Trace
1615
from opencensus.trace.span import Span
1716

1817

19-
__all__ = ['Trace', 'Span']
18+
__all__ = ['Span']

trace/opencensus/trace/ext/django/middleware.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,6 @@ def process_request(self, request):
146146
exporter=self.exporter,
147147
propagator=self.propagator)
148148

149-
tracer.start_trace()
150-
151149
# Span name is being set at process_view
152150
tracer.start_span()
153151
tracer.add_label_to_spans(
@@ -182,7 +180,7 @@ def process_response(self, request, response):
182180
_set_django_labels(tracer, request)
183181

184182
tracer.end_span()
185-
tracer.end_trace()
183+
tracer.finish()
186184
except Exception: # pragma: NO COVER
187185
log.error('Failed to trace request', exc_info=True)
188186
finally:

trace/opencensus/trace/ext/flask/flask_middleware.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,6 @@ def _before_request(self):
8484
exporter=self.exporter,
8585
propagator=self.propagator)
8686

87-
tracer.start_trace()
88-
8987
span = tracer.start_span()
9088

9189
# Set the span name as the name of the current module name
@@ -109,7 +107,7 @@ def _after_request(self, response):
109107
str(response.status_code))
110108

111109
tracer.end_span()
112-
tracer.end_trace()
110+
tracer.finish()
113111
except Exception: # pragma: NO COVER
114112
log.error('Failed to trace request', exc_info=True)
115113
finally:

trace/opencensus/trace/request_tracer.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,9 @@ def store_tracer(self):
8888
"""Add the current tracer to thread_local"""
8989
execution_context.set_opencensus_tracer(self)
9090

91-
def start_trace(self):
92-
"""Start a trace."""
93-
self.tracer.start_trace()
94-
95-
def end_trace(self):
96-
"""End a trace and send trace using exporter."""
97-
trace = self.tracer.end_trace()
91+
def finish(self):
92+
"""End all spans and send spans using reporter."""
93+
trace = self.tracer.finish()
9894

9995
if trace is not None:
10096
self.exporter.export(trace)

trace/opencensus/trace/span.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import random
16-
15+
from datetime import datetime
1716
from itertools import chain
1817

19-
from datetime import datetime
2018
from opencensus.trace.enums import Enum
19+
from opencensus.trace.span_context import generate_span_id
2120

2221

2322
class Span(object):
@@ -152,17 +151,6 @@ def __exit__(self, exception_type, exception_value, traceback):
152151
self.finish()
153152

154153

155-
def generate_span_id():
156-
"""Return the random generated span ID for a span.
157-
158-
:rtype: int
159-
:returns: Identifier for the span. Must be a 64-bit integer other
160-
than 0 and unique within a trace.
161-
"""
162-
span_id = random.getrandbits(64)
163-
return span_id
164-
165-
166154
def format_span_json(span):
167155
"""Helper to format a Span in JSON format.
168156

trace/opencensus/trace/span_context.py

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@
1515
"""SpanContext encapsulates the current context within the request's trace."""
1616

1717
import logging
18+
import random
1819
import re
19-
20-
from opencensus.trace import trace
20+
import uuid
2121

2222
_INVALID_TRACE_ID = '0' * 32
2323
_INVALID_SPAN_ID = 0
@@ -51,7 +51,7 @@ def __init__(
5151
enabled=True,
5252
from_header=False):
5353
if trace_id is None:
54-
trace_id = trace.generate_trace_id()
54+
trace_id = generate_trace_id()
5555

5656
self.trace_id = self.check_trace_id(trace_id)
5757
self.span_id = self.check_span_id(span_id)
@@ -121,7 +121,7 @@ def check_trace_id(self, trace_id):
121121
'Trace_id {} is invalid (cannot be all zero), '
122122
'generate a new one.'.format(trace_id))
123123
self.from_header = False
124-
return trace.generate_trace_id()
124+
return generate_trace_id()
125125

126126
trace_id_pattern = re.compile(_TRACE_ID_FORMAT)
127127

@@ -134,4 +134,25 @@ def check_trace_id(self, trace_id):
134134
'Trace_id {} does not the match the required format,'
135135
'generate a new one instead.'.format(trace_id))
136136
self.from_header = False
137-
return trace.generate_trace_id()
137+
return generate_trace_id()
138+
139+
140+
def generate_span_id():
141+
"""Return the random generated span ID for a span.
142+
143+
:rtype: int
144+
:returns: Identifier for the span. Must be a 64-bit integer other
145+
than 0 and unique within a trace.
146+
"""
147+
span_id = random.getrandbits(64)
148+
return span_id
149+
150+
151+
def generate_trace_id():
152+
"""Generate a trace_id randomly.
153+
154+
:rtype: str
155+
:returns: 32 digit randomly generated trace ID.
156+
"""
157+
trace_id = uuid.uuid4().hex
158+
return trace_id

trace/opencensus/trace/trace.py

Lines changed: 0 additions & 79 deletions
This file was deleted.

trace/opencensus/trace/tracer/base.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,8 @@ class Tracer(object):
1818
1919
Subclasses of :class:`Tracer` must implement the below methods.
2020
"""
21-
def trace(self):
22-
"""Create a trace using the context information.
23-
24-
:rtype: :class:`~opencensus.trace.trace.Trace`
25-
:returns: The Trace object.
26-
"""
27-
raise NotImplementedError
28-
29-
def start_trace(self):
30-
"""Start a trace."""
31-
raise NotImplementedError
32-
33-
def end_trace(self):
34-
"""End a trace."""
21+
def finish(self):
22+
"""End the spans and send to reporters."""
3523
raise NotImplementedError
3624

3725
def span(self, name='span'):

trace/opencensus/trace/tracer/context_tracer.py

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
from opencensus.trace.span_context import SpanContext
1818
from opencensus.trace import labels_helper
1919
from opencensus.trace import span as trace_span
20-
from opencensus.trace.trace import Trace
2120
from 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

trace/opencensus/trace/tracer/noop_tracer.py

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,8 @@ class NoopTracer(base.Tracer):
1919
"""No-op implementation of the :class:`Tracer` interface, all methods are
2020
no-ops. Should be used when tracing is not enabled or not sampled.
2121
"""
22-
def trace(self):
23-
"""Create a trace using the context information.
24-
25-
:rtype: :class:`~opencensus.trace.trace.Trace`
26-
:returns: The Trace object.
27-
"""
28-
return base.NullContextManager()
29-
30-
def start_trace(self):
31-
"""Start a trace."""
32-
return
33-
34-
def end_trace(self):
35-
"""End a trace."""
22+
def finish(self):
23+
"""End spans and send to reporter."""
3624
return None
3725

3826
def span(self, name='span'):

0 commit comments

Comments
 (0)