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

Commit cb9ce93

Browse files
authored
Replace enabled with trace_options (#43)
1 parent 38b2bec commit cb9ce93

13 files changed

Lines changed: 178 additions & 85 deletions

trace/opencensus/trace/propagation/_helpers.py

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

trace/opencensus/trace/propagation/binary_format.py

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
import logging
1818
import struct
1919

20-
from opencensus.trace.propagation import _helpers
2120
from opencensus.trace.span_context import SpanContext
21+
from opencensus.trace.trace_options import TraceOptions
2222

2323
# Used for decoding hex bytes to hex string.
2424
UTF8 = 'utf-8'
@@ -118,14 +118,12 @@ def from_header(self, binary):
118118
# with length 32, then decode it to hex string using utf-8.
119119
trace_id = str(binascii.hexlify(data.trace_id).decode(UTF8))
120120
span_id = data.span_id
121-
trace_option = data.trace_option
122-
123-
enabled = _helpers._get_enabled_trace_option(trace_option)
121+
trace_options = TraceOptions(data.trace_option)
124122

125123
span_context = SpanContext(
126124
trace_id=trace_id,
127125
span_id=span_id,
128-
enabled=enabled,
126+
trace_options=trace_options,
129127
from_header=True)
130128

131129
return span_context
@@ -142,15 +140,13 @@ def to_header(self, span_context):
142140
"""
143141
trace_id = span_context.trace_id
144142
span_id = span_context.span_id
145-
enabled = span_context.enabled
143+
trace_options = int(span_context.trace_options.trace_options_byte)
146144

147145
# If there is no span_id in this context, set it to 0, which is
148146
# considered invalid and won't be set as the downstream parent span_id.
149147
if span_id is None:
150148
span_id = 0
151149

152-
enabled_int = int(enabled)
153-
154150
# Convert trace_id to bytes with length 16, treat span_id as 64 bit
155151
# integer which is unsigned long long type and convert it to bytes with
156152
# length 8, trace_option is integer with length 1.
@@ -162,4 +158,4 @@ def to_header(self, span_context):
162158
SPAN_ID_FIELD_ID,
163159
span_id,
164160
TRACE_OPTION_FIELD_ID,
165-
enabled_int)
161+
trace_options)

trace/opencensus/trace/propagation/google_cloud_format.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,9 @@
2121
import logging
2222
import re
2323

24-
from opencensus.trace.propagation import _helpers
2524
from opencensus.trace.span_context import SpanContext
25+
from opencensus.trace.trace_options import TraceOptions
2626

27-
_ENABLED_BITMASK = 1
2827
_TRACE_CONTEXT_HEADER_FORMAT = '([0-9a-f]{32})(\/(\d+))?(;o=(\d+))?'
2928
_TRACE_CONTEXT_HEADER_RE = re.compile(_TRACE_CONTEXT_HEADER_FORMAT)
3029
_TRACE_ID_DELIMETER = '/'
@@ -59,17 +58,15 @@ def from_header(self, header):
5958
if match:
6059
trace_id = match.group(1)
6160
span_id = match.group(3)
62-
options = match.group(5)
61+
trace_options = match.group(5)
6362

64-
if options is None:
65-
options = 1
66-
67-
enabled = _helpers._get_enabled_trace_option(options)
63+
if trace_options is None:
64+
trace_options = 1
6865

6966
span_context = SpanContext(
7067
trace_id=trace_id,
7168
span_id=span_id,
72-
enabled=enabled,
69+
trace_options=TraceOptions(trace_options),
7370
from_header=True)
7471
return span_context
7572
else:
@@ -90,10 +87,10 @@ def to_header(self, span_context):
9087
"""
9188
trace_id = span_context.trace_id
9289
span_id = span_context.span_id
93-
enabled = span_context.enabled
90+
trace_options = span_context.trace_options.trace_options_byte
9491

9592
header = '{}/{};o={}'.format(
9693
trace_id,
9794
span_id,
98-
int(enabled))
95+
int(trace_options))
9996
return header

trace/opencensus/trace/propagation/text_format.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
"""
1919

2020
from opencensus.trace.span_context import SpanContext
21+
from opencensus.trace.trace_options import TraceOptions
2122

2223
_OPENCENSUS_TRACE_PREFIX = 'opencensus-trace'
2324
_TRACE_ID_KEY = '{}-traceid'.format(_OPENCENSUS_TRACE_PREFIX)
2425
_SPAN_ID_KEY = '{}-spanid'.format(_OPENCENSUS_TRACE_PREFIX)
25-
_ENABLED_TRACE_KEY = '{}-enabled'.format(_OPENCENSUS_TRACE_PREFIX)
26+
_TRACE_OPTIONS_KEY = '{}-traceoptions'.format(_OPENCENSUS_TRACE_PREFIX)
27+
28+
DEFAULT_TRACE_OPTIONS = '1'
2629

2730

2831
class TextFormatPropagator(object):
@@ -39,21 +42,24 @@ def from_carrier(self, carrier):
3942
"""
4043
trace_id = None
4144
span_id = None
42-
enabled = 1
45+
trace_options = None
4346

4447
for key in carrier:
4548
key = key.lower()
4649
if key == _TRACE_ID_KEY:
4750
trace_id = carrier[key]
4851
if key == _SPAN_ID_KEY:
4952
span_id = carrier[key]
50-
if key == _ENABLED_TRACE_KEY:
51-
enabled = bool(carrier[key])
53+
if key == _TRACE_OPTIONS_KEY:
54+
trace_options = bool(carrier[key])
55+
56+
if trace_options is None:
57+
trace_options = DEFAULT_TRACE_OPTIONS
5258

5359
return SpanContext(
5460
trace_id=trace_id,
5561
span_id=span_id,
56-
enabled=enabled,
62+
trace_options=TraceOptions(trace_options),
5763
from_header=True)
5864

5965
def to_carrier(self, span_context, carrier):
@@ -75,6 +81,7 @@ def to_carrier(self, span_context, carrier):
7581
if span_context.span_id is not None:
7682
carrier[_SPAN_ID_KEY] = str(span_context.span_id)
7783

78-
carrier[_ENABLED_TRACE_KEY] = str(span_context.enabled)
84+
carrier[_TRACE_OPTIONS_KEY] = str(
85+
span_context.trace_options.trace_options_byte)
7986

8087
return carrier

trace/opencensus/trace/request_tracer.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ def should_sample(self):
7373
:rtype: bool
7474
:returns: Whether to trace the request or not.
7575
"""
76-
return self.span_context.enabled and self.sampler.should_sample
76+
return self.span_context.trace_options.enabled \
77+
and self.sampler.should_sample
7778

7879
def get_tracer(self):
7980
"""Return a tracer according to the sampling decision."""

trace/opencensus/trace/span_context.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,22 @@
1919
import re
2020
import uuid
2121

22+
from opencensus.trace import trace_options
23+
2224
_INVALID_TRACE_ID = '0' * 32
2325
_INVALID_SPAN_ID = 0
2426
_TRACE_HEADER_KEY = 'X_CLOUD_TRACE_CONTEXT'
2527
_TRACE_ID_FORMAT = '[0-9a-f]{32}?'
2628

29+
# Default options, enable tracing
30+
DEFAULT_OPTIONS = 1
31+
32+
# Default trace options
33+
DEFAULT = trace_options.TraceOptions(DEFAULT_OPTIONS)
34+
2735

2836
class SpanContext(object):
29-
"""SpanContext includes 3 fields: traceId, spanId, and an enabled flag
37+
"""SpanContext includes 3 fields: traceId, spanId, and an trace_options flag
3038
which indicates whether or not the request is being traced. It contains the
3139
current context to be propagated to the child spans.
3240
@@ -37,8 +45,8 @@ class SpanContext(object):
3745
:type span_id: int
3846
:param span_id: (Optional) Identifier for the span, unique within a trace.
3947
40-
:type enabled: bool
41-
:param enabled: (Optional) Indicates whether the request is traced or not.
48+
:type trace_options: :class: `~opencensus.trace.trace_options.TraceOptions`
49+
:param trace_options: (Optional) TraceOptions indicates 8 trace options.
4250
4351
:type from_header: bool
4452
:param from_header: (Optional) Indicates whether the trace context is
@@ -48,14 +56,17 @@ def __init__(
4856
self,
4957
trace_id=None,
5058
span_id=None,
51-
enabled=True,
59+
trace_options=None,
5260
from_header=False):
5361
if trace_id is None:
5462
trace_id = generate_trace_id()
5563

64+
if trace_options is None:
65+
trace_options = DEFAULT
66+
5667
self.trace_id = self.check_trace_id(trace_id)
5768
self.span_id = self.check_span_id(span_id)
58-
self.enabled = enabled
69+
self.trace_options = trace_options
5970
self.from_header = from_header
6071

6172
def __str__(self):
@@ -66,10 +77,11 @@ def __str__(self):
6677
:rtype: str
6778
:returns: String form of the SpanContext.
6879
"""
80+
enabled = self.trace_options.enabled
6981
header = '{}/{};o={}'.format(
7082
self.trace_id,
7183
self.span_id,
72-
int(self.enabled))
84+
int(enabled))
7385
return header
7486

7587
def check_span_id(self, span_id):
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Copyright 2017, OpenCensus Authors
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import logging
16+
17+
# Enabled field is the least significant bit of trace options.
18+
_ENABLED_BITMASK = 1 << 0
19+
20+
# Default trace options
21+
DEFAULT = '1'
22+
23+
24+
class TraceOptions(object):
25+
"""A class that represents global trace options.
26+
27+
:type trace_options_byte: str
28+
:param trace_options_byte: 1 byte bitmap for trace options.
29+
"""
30+
31+
def __init__(self, trace_options_byte=None):
32+
if trace_options_byte is None:
33+
trace_options_byte = DEFAULT
34+
35+
self.trace_options_byte = self.check_trace_options(trace_options_byte)
36+
self.enabled = self.get_enabled
37+
38+
def check_trace_options(self, trace_options_byte):
39+
trace_options_int = int(trace_options_byte)
40+
41+
if trace_options_int < 0 or trace_options_int > 255:
42+
logging.warn("Trace options invalid, should be 1 byte.")
43+
trace_options_byte = DEFAULT
44+
45+
return trace_options_byte
46+
47+
@property
48+
def get_enabled(self):
49+
"""Get the last bit from the trace options which is the enabled field.
50+
51+
:type trace_options: byte
52+
:param trace_options: 1 byte field which indicates 8 trace options,
53+
currently only have the enabled option. 1 means
54+
enabled, 0 means not enabled.
55+
56+
:rtype: bool
57+
:returns: Enabled tracing or not.
58+
"""
59+
enabled = bool(int(self.trace_options_byte) & _ENABLED_BITMASK)
60+
61+
return enabled

trace/tests/unit/propagation/test_binary_format.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,21 @@ def test_from_header(self):
4545

4646
self.assertEqual(span_context.trace_id, expected_trace_id)
4747
self.assertEqual(span_context.span_id, expected_span_id)
48-
self.assertEqual(span_context.enabled, expected_trace_option)
48+
self.assertEqual(
49+
span_context.trace_options.enabled,
50+
expected_trace_option)
4951

5052
def test_to_header_span_id_zero(self):
5153
from opencensus.trace.span_context import SpanContext
54+
from opencensus.trace.trace_options import TraceOptions
5255

5356
span_context = mock.Mock(spec=SpanContext)
5457
trace_id = 'a0b72ca15c1a4bd18962d0ac59dc90b9'
5558
span_id = None
56-
enabled = True
59+
trace_options = '1'
5760
span_context.trace_id = trace_id
5861
span_context.span_id = span_id
59-
span_context.enabled = enabled
62+
span_context.trace_options = TraceOptions(trace_options)
6063

6164
propagator = binary_format.BinaryFormatPropagator()
6265

@@ -70,14 +73,15 @@ def test_to_header_span_id_zero(self):
7073

7174
def test_to_header(self):
7275
from opencensus.trace.span_context import SpanContext
76+
from opencensus.trace.trace_options import TraceOptions
7377

7478
span_context = mock.Mock(spec=SpanContext)
7579
trace_id = 'a0b72ca15c1a4bd18962d0ac59dc90b9'
7680
span_id = 7433567179112518326
77-
enabled = True
81+
trace_options = '1'
7882
span_context.trace_id = trace_id
7983
span_context.span_id = span_id
80-
span_context.enabled = enabled
84+
span_context.trace_options = TraceOptions(trace_options)
8185

8286
propagator = binary_format.BinaryFormatPropagator()
8387

0 commit comments

Comments
 (0)