-
Notifications
You must be signed in to change notification settings - Fork 703
Add validation for Trace ID #1992
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
Changes from 3 commits
cab7290
9dedda0
aa86fcd
8ec5d80
54ddfb5
75b5f4a
9b086d8
b41b2d2
53e2cf1
6b6674f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -390,6 +390,26 @@ def values(self) -> typing.ValuesView[str]: | |||||||||||||||
|
||||||||||||||||
DEFAULT_TRACE_STATE = TraceState.get_default() | ||||||||||||||||
|
||||||||||||||||
_TRACE_ID_BYTES = 16 | ||||||||||||||||
_TRACE_ID_HEX_LENGTH = 2 * _TRACE_ID_BYTES | ||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
def _validate_trace_id(trace_id: int) -> bool: | ||||||||||||||||
"""Validates a trace_id. | ||||||||||||||||
|
||||||||||||||||
Args: | ||||||||||||||||
trace_id: An int that is expected to corresponds to 16-bytes hexdecimal value | ||||||||||||||||
|
||||||||||||||||
Returns: | ||||||||||||||||
True if trace_id is valid, False otherwise. | ||||||||||||||||
""" | ||||||||||||||||
if not trace_id: | ||||||||||||||||
return False | ||||||||||||||||
if len(format(trace_id, "032x")) != _TRACE_ID_HEX_LENGTH: | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this work and be a bit faster? # constant somewhere
_MAX_TRACE_ID = (1 << 128) - 1
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I also prefer to make sure the trace id value is lesser than a certain value. Also, instead of |
||||||||||||||||
return False | ||||||||||||||||
|
||||||||||||||||
return True | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Building off of what @aabmass said. I'm okay with constant/no constant since it's only used once but having a
Suggested change
|
||||||||||||||||
|
||||||||||||||||
|
||||||||||||||||
class SpanContext( | ||||||||||||||||
typing.Tuple[int, int, bool, "TraceFlags", "TraceState", bool] | ||||||||||||||||
|
@@ -420,7 +440,11 @@ def __new__( | |||||||||||||||
if trace_state is None: | ||||||||||||||||
trace_state = DEFAULT_TRACE_STATE | ||||||||||||||||
|
||||||||||||||||
is_valid = trace_id != INVALID_TRACE_ID and span_id != INVALID_SPAN_ID | ||||||||||||||||
is_valid = ( | ||||||||||||||||
trace_id != INVALID_TRACE_ID | ||||||||||||||||
and span_id != INVALID_SPAN_ID | ||||||||||||||||
Comment on lines
+425
to
+426
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add move these two checks into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead, we should just put the code of |
||||||||||||||||
and _validate_trace_id(trace_id) | ||||||||||||||||
) | ||||||||||||||||
|
||||||||||||||||
return tuple.__new__( | ||||||||||||||||
cls, | ||||||||||||||||
|
Uh oh!
There was an error while loading. Please reload this page.