Skip to content

Commit

Permalink
Don't trace by default, don't share trace context
Browse files Browse the repository at this point in the history
  • Loading branch information
c24t committed Feb 5, 2019
1 parent dc67db2 commit 035a6dc
Show file tree
Hide file tree
Showing 11 changed files with 64 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def to_headers(self, span_context):
"""
trace_id = span_context.trace_id
span_id = span_context.span_id
trace_options = span_context.trace_options.enabled
trace_options = span_context.trace_options.get_enabled()

# Convert the trace options
trace_options = '01' if trace_options else '00'
Expand Down
10 changes: 2 additions & 8 deletions opencensus/trace/span_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,14 @@
import re
import uuid

from opencensus.trace import trace_options
from opencensus.trace import trace_options as trace_options_module

_INVALID_TRACE_ID = '0' * 32
INVALID_SPAN_ID = '0' * 16

TRACE_ID_PATTERN = re.compile('[0-9a-f]{32}?')
SPAN_ID_PATTERN = re.compile('[0-9a-f]{16}?')

# Default options, enable tracing
DEFAULT_OPTIONS = 1

# Default trace options
DEFAULT = trace_options.TraceOptions(DEFAULT_OPTIONS)


class SpanContext(object):
"""SpanContext includes 3 fields: traceId, spanId, and an trace_options flag
Expand Down Expand Up @@ -64,7 +58,7 @@ def __init__(
trace_id = generate_trace_id()

if trace_options is None:
trace_options = DEFAULT
trace_options = trace_options_module.TraceOptions()

self.from_header = from_header
self.trace_id = self._check_trace_id(trace_id)
Expand Down
49 changes: 24 additions & 25 deletions opencensus/trace/trace_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,43 @@
_ENABLED_BITMASK = 1 << 0

# Default trace options
DEFAULT = '0'
DEFAULT = 0x0


class TraceOptions(object):
"""A class that represents global trace options.
:type trace_options_byte: str
:type trace_options_byte: int
:param trace_options_byte: 1 byte bitmap for trace options.
"""

def __init__(self, trace_options_byte=None):
if trace_options_byte is None:
def __init__(self, trace_options_byte=DEFAULT):
if not isinstance(trace_options_byte, int):
logging.warning("trace_options_byte must be an int, this will be "
"an error in future versions")
try:
trace_options_byte = int(trace_options_byte)
except ValueError: # pragma: NO COVER
trace_options_byte = DEFAULT

if not 0x0 <= trace_options_byte <= 0xff:
logging.warning("Trace options invalid, should be 1 byte.")
trace_options_byte = DEFAULT

self.trace_options_byte = self.check_trace_options(trace_options_byte)
self.enabled = self.get_enabled

def check_trace_options(self, trace_options_byte):
trace_options_int = int(trace_options_byte)

if trace_options_int < 0 or trace_options_int > 255:
logging.warn("Trace options invalid, should be 1 byte.")
trace_options_byte = DEFAULT

return trace_options_byte
self.trace_options_byte = trace_options_byte

def __repr__(self):
fmt = '{}(enabled={})'
return fmt.format(
type(self).__name__,
self.get_enabled,
self.get_enabled(),
)

# TODO: deprecate, replace with is_sampled()
@property
def enabled(self):
return self.get_enabled()

def get_enabled(self):
"""Get the last bit from the trace options which is the enabled field.
Expand All @@ -61,19 +64,15 @@ def get_enabled(self):
enabled, 0 means not enabled.
:rtype: bool
:returns: Enabled tracing or not.
:returns: Whether tracing is enabled in this span context.
"""
enabled = bool(int(self.trace_options_byte) & _ENABLED_BITMASK)

return enabled
return bool(self.trace_options_byte & _ENABLED_BITMASK)

def set_enabled(self, enabled):
"""Update the last bit of the trace options byte str.
:type enabled: bool
:param enabled: Whether enable tracing in this span context or not.
:param enabled: Whether to enable tracing in this span context.
"""
enabled_bit = '1' if enabled else '0'
self.trace_options_byte = str(
self.trace_options_byte)[:-1] + enabled_bit
self.enabled = self.get_enabled
if not enabled == self.get_enabled():
self.trace_options_byte ^= _ENABLED_BITMASK
2 changes: 1 addition & 1 deletion opencensus/trace/tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def should_sample(self):
:rtype: bool
:returns: Whether to trace the request or not.
"""
return self.span_context.trace_options.enabled \
return self.span_context.trace_options.get_enabled() \
or self.sampler.should_sample(self.span_context.trace_id)

def get_tracer(self):
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/trace/exporters/test_jaeger_exporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ def test_translate_to_jaeger(self):
operationName='test1',
startTime=1502820146071158,
duration=10000000,
flags=1,
flags=0,
tags=[
jaeger.Tag(
key='key_bool', vType=jaeger.TagType.BOOL,
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/trace/propagation/test_binary_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_from_header(self):

self.assertEqual(span_context.trace_id, expected_trace_id)
self.assertEqual(span_context.span_id, expected_span_id)
self.assertEqual(span_context.trace_options.enabled,
self.assertEqual(span_context.trace_options.get_enabled(),
expected_trace_option)

def test_to_header_span_id_zero(self):
Expand Down
8 changes: 4 additions & 4 deletions tests/unit/trace/propagation/test_google_cloud_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_header_match(self):

self.assertEqual(span_context.trace_id, expected_trace_id)
self.assertEqual(span_context.span_id, expected_span_id)
self.assertFalse(span_context.trace_options.enabled)
self.assertFalse(span_context.trace_options.get_enabled())

# Trace option is enabled.
header = '6e0c63257de34c92bf9efcd03927272e/00f067aa0ba902b7;o=1'
Expand All @@ -74,7 +74,7 @@ def test_header_match(self):

self.assertEqual(span_context.trace_id, expected_trace_id)
self.assertEqual(span_context.span_id, expected_span_id)
self.assertTrue(span_context.trace_options.enabled)
self.assertTrue(span_context.trace_options.get_enabled())

def test_header_match_no_option(self):
header = '6e0c63257de34c92bf9efcd03927272e/00f067aa0ba902b7'
Expand All @@ -86,7 +86,7 @@ def test_header_match_no_option(self):

self.assertEqual(span_context.trace_id, expected_trace_id)
self.assertEqual(span_context.span_id, expected_span_id)
self.assertTrue(span_context.trace_options.enabled)
self.assertTrue(span_context.trace_options.get_enabled())

def test_header_not_match(self):
header = 'invalid_trace_id/66666;o=1'
Expand All @@ -111,7 +111,7 @@ def test_headers_match(self):

self.assertEqual(span_context.trace_id, expected_trace_id)
self.assertEqual(span_context.span_id, expected_span_id)
self.assertTrue(span_context.trace_options.enabled)
self.assertTrue(span_context.trace_options.get_enabled())

def test_to_header(self):
from opencensus.trace import span_context
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/trace/propagation/test_text_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def test_from_carrier_keys_exist(self):

self.assertEqual(span_context.trace_id, test_trace_id)
self.assertEqual(span_context.span_id, test_span_id)
self.assertEqual(span_context.trace_options.enabled,
self.assertEqual(span_context.trace_options.get_enabled(),
bool(test_options))

def test_from_carrier_keys_not_exist(self):
Expand All @@ -50,7 +50,7 @@ def test_from_carrier_keys_not_exist(self):
# Span_id should be None here which indicates no parent span_id for
# the child spans
self.assertIsNone(span_context.span_id)
self.assertTrue(span_context.trace_options.enabled)
self.assertTrue(span_context.trace_options.get_enabled())

def test_to_carrier_has_span_id(self):
test_trace_id = '6e0c63257de34c92bf9efcd03927272e'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ def test_header_match(self):

self.assertEqual(span_context.trace_id, trace_id)
self.assertEqual(span_context.span_id, span_id)
self.assertFalse(span_context.trace_options.enabled)
self.assertFalse(span_context.trace_options.get_enabled())

# Trace option is enabled.
span_context = propagator.from_headers({
Expand All @@ -149,7 +149,7 @@ def test_header_match(self):

self.assertEqual(span_context.trace_id, trace_id)
self.assertEqual(span_context.span_id, span_id)
self.assertTrue(span_context.trace_options.enabled)
self.assertTrue(span_context.trace_options.get_enabled())

def test_header_not_match(self):
propagator = trace_context_http_header_format.\
Expand Down
18 changes: 15 additions & 3 deletions tests/unit/trace/test_trace_options.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,32 @@ def test_constructor_default(self):
self.assertFalse(trace_options.enabled)

def test_constructor_explicit(self):
trace_options_byte = '0'
trace_options_byte = 0x0
trace_options = trace_opt.TraceOptions(trace_options_byte)

self.assertEqual(trace_options.trace_options_byte, trace_options_byte)
self.assertFalse(trace_options.enabled)

def test_check_trace_options_valid(self):
trace_options_byte = '10'
trace_options_byte = 0xa
trace_options = trace_opt.TraceOptions(trace_options_byte)

self.assertEqual(trace_options.trace_options_byte, trace_options_byte)

def test_check_trace_options_invalid(self):
trace_options_byte = '256'
trace_options_byte = 0x100
trace_options = trace_opt.TraceOptions(trace_options_byte)

self.assertEqual(trace_options.trace_options_byte, trace_opt.DEFAULT)

def test_enable_tracing(self):
trace_options = trace_opt.TraceOptions()
self.assertFalse(trace_options.get_enabled())
trace_options.set_enabled(True)
self.assertTrue(trace_options.get_enabled())
trace_options.set_enabled(True)
self.assertTrue(trace_options.get_enabled())
trace_options.set_enabled(False)
self.assertFalse(trace_options.get_enabled())
trace_options.set_enabled(False)
self.assertFalse(trace_options.get_enabled())
22 changes: 11 additions & 11 deletions tests/unit/trace/test_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_constructor_explicit(self):
exporter = mock.Mock()
propagator = mock.Mock()
span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False

tracer = tracer_module.Tracer(
span_context=span_context,
Expand All @@ -63,7 +63,7 @@ def test_constructor_explicit(self):
def test_should_sample_force_not_trace(self):

span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False
sampler = mock.Mock()
sampler.should_sample.return_value = False
tracer = tracer_module.Tracer(
Expand All @@ -84,14 +84,14 @@ def test_should_sample_not_sampled(self):
sampler = mock.Mock()
sampler.should_sample.return_value = False
span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False
tracer = tracer_module.Tracer(
span_context=span_context, sampler=sampler)
sampled = tracer.should_sample()

self.assertFalse(sampled)

def get_tracer_noop_tracer(self):
def test_get_tracer_noop_tracer(self):
from opencensus.trace.tracers import noop_tracer

sampler = mock.Mock()
Expand All @@ -102,7 +102,7 @@ def get_tracer_noop_tracer(self):

assert isinstance(result, noop_tracer.NoopTracer)

def get_tracer_context_tracer(self):
def test_get_tracer_context_tracer(self):
from opencensus.trace.tracers import context_tracer

sampler = mock.Mock()
Expand All @@ -120,7 +120,7 @@ def test_finish_not_sampled(self):
sampler = mock.Mock()
sampler.should_sample.return_value = False
span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False
tracer = tracer_module.Tracer(
span_context=span_context, sampler=sampler)
assert isinstance(tracer.tracer, noop_tracer.NoopTracer)
Expand All @@ -147,7 +147,7 @@ def test_span_not_sampled(self):
sampler = mock.Mock()
sampler.should_sample.return_value = False
span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False
tracer = tracer_module.Tracer(
span_context=span_context, sampler=sampler)

Expand Down Expand Up @@ -177,7 +177,7 @@ def test_start_span_not_sampled(self):
sampler = mock.Mock()
sampler.should_sample.return_value = False
span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False
tracer = tracer_module.Tracer(
span_context=span_context, sampler=sampler)

Expand All @@ -199,7 +199,7 @@ def test_end_span_not_sampled(self):
sampler = mock.Mock()
sampler.should_sample.return_value = False
span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False
tracer = tracer_module.Tracer(
sampler=sampler, span_context=span_context)

Expand Down Expand Up @@ -235,7 +235,7 @@ def test_current_span_not_sampled(self):
sampler = mock.Mock()
sampler.should_sample.return_value = False
span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False
tracer = tracer_module.Tracer(
sampler=sampler, span_context=span_context)

Expand All @@ -262,7 +262,7 @@ def test_add_attribute_to_current_span_not_sampled(self):
sampler = mock.Mock()
sampler.should_sample.return_value = False
span_context = mock.Mock()
span_context.trace_options.enabled = False
span_context.trace_options.get_enabled.return_value = False
tracer = tracer_module.Tracer(
span_context=span_context, sampler=sampler)
tracer.add_attribute_to_current_span('key', 'value')
Expand Down

0 comments on commit 035a6dc

Please sign in to comment.