Skip to content

Span id encoding #719

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jul 17, 2019
2 changes: 1 addition & 1 deletion opencensus/trace/propagation/google_cloud_format.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
from opencensus.trace.trace_options import TraceOptions

_TRACE_CONTEXT_HEADER_NAME = 'X-Cloud-Trace-Context'
_TRACE_CONTEXT_HEADER_FORMAT = r'([0-9a-f]{32})(\/([0-9a-f]{16}))?(;o=(\d+))?'
_TRACE_CONTEXT_HEADER_FORMAT = r'([0-9a-f]{0,32})(\/([\d]{0,20}))?(;o=(\d+))?'
_TRACE_CONTEXT_HEADER_RE = re.compile(_TRACE_CONTEXT_HEADER_FORMAT)
_TRACE_ID_DELIMETER = '/'
_SPAN_ID_DELIMETER = ';'
Expand Down
40 changes: 24 additions & 16 deletions opencensus/trace/span_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
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, don't force sampling
DEFAULT_OPTIONS = '0'
Expand Down Expand Up @@ -85,8 +84,8 @@ def __repr__(self):
)

def _check_span_id(self, span_id):
"""Check the format of the span_id to ensure it is 16-character hex
value representing a 64-bit number. If span_id is invalid, logs a
"""Check the format of the span_id to ensure it is a
value representing a 64-bit integer. If span_id is invalid, logs a
warning message and returns None

:type span_id: str
Expand All @@ -105,16 +104,14 @@ def _check_span_id(self, span_id):
self.from_header = False
return None

match = SPAN_ID_PATTERN.match(span_id)

if match:
if is_64bit_int(span_id):
return span_id
else:
logging.warning(
'Span_id %s does not the match the '
'required format', span_id)
self.from_header = False
return None

logging.warning(
'Span_id %s does not the match the '
'required format', span_id)
self.from_header = False
return None

def _check_trace_id(self, trace_id):
"""Check the format of the trace_id to ensure it is 32-character hex
Expand Down Expand Up @@ -149,13 +146,13 @@ def _check_trace_id(self, trace_id):


def generate_span_id():
"""Return the random generated span ID for a span. Must be a 16 character
hexadecimal encoded string
"""Return the random generated span ID for a span. Must be a 64 bit
integer as string

:rtype: str
:returns: 16 digit randomly generated hex trace id.
:returns: digit randomly generated trace id.
"""
return '{:016x}'.format(random.getrandbits(64))
return str(random.getrandbits(64))


def generate_trace_id():
Expand All @@ -165,3 +162,14 @@ def generate_trace_id():
:returns: 32 digit randomly generated hex trace id.
"""
return '{:032x}'.format(random.getrandbits(128))


def is_64bit_int(span_id):
"""Return if the given string represents a 64-bit integer

:rtype: bool
"""
try:
return int(span_id, 10)>>64 == 0
except (TypeError, ValueError):
return False