-
Notifications
You must be signed in to change notification settings - Fork 705
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 7 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 |
---|---|---|
|
@@ -420,7 +420,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 | ||
and trace_id < 2 ** 128 - 1 | ||
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. @ocelotl nit regarding the single use constant, I think it's worth having the constant as it's easier to understand what the magic number means and for the speedup of not calculating the value every time in this hot code path. (I was curious so checked and CPython is not smart enough to optimize this into a constant on its own (funny enough, it does do it for the bit shifting approach)): In [2]: def f(trace_id):
...: return trace_id < 2 ** 128 - 1
...:
In [3]: dis(f)
2 0 LOAD_FAST 0 (trace_id)
2 LOAD_CONST 1 (2)
4 LOAD_CONST 2 (128)
6 BINARY_POWER
8 LOAD_CONST 3 (1)
10 BINARY_SUBTRACT
12 COMPARE_OP 0 (<)
14 RETURN_VALUE 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. Ok, that's a good point. In that case, the constant should be added as a private attribute of the class to keep it as close as where it is being used. 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. Not sure if we have specs somewhere, but my 2 cents is that I think I can go either way on the constant though! 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. ok I put the private const variable for readabiliy. As per bit shift vs multiplication, I leave the decision. |
||
) | ||
|
||
return tuple.__new__( | ||
cls, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we add move these two checks into
_validate_trace_id()
so it's all together?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Instead, we should just put the code of
_validate_trace_id
here instead of having a single-use function. Keep in mind that the only thing that_validate_trace_id
does istrace_id < 2 ** 128
.