Skip to content

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

Merged
merged 10 commits into from
Aug 2, 2021
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- `opentelemetry-distro` & `opentelemetry-sdk` Moved Auto Instrumentation Configurator code to SDK
to let distros use its default implementation
([#1937](https://github.com/open-telemetry/opentelemetry-python/pull/1937))
- Add Trace ID validation to meet [TraceID spec](https://github.com/open-telemetry/opentelemetry-specification/blob/main/specification/overview.md#spancontext) ([#1992](https://github.com/open-telemetry/opentelemetry-python/pull/1992))

## [0.23.1](https://github.com/open-telemetry/opentelemetry-python/pull/1987) - 2021-07-26

Expand Down
6 changes: 5 additions & 1 deletion opentelemetry-api/src/opentelemetry/trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +425 to +426
Copy link
Member

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?

Copy link
Contributor

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 is trace_id < 2 ** 128.

and trace_id < 2 ** 128 - 1
Copy link
Member

@aabmass aabmass Jul 30, 2021

Choose a reason for hiding this comment

The 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

Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 (1 << 128) - 1 is easier to read.

I can go either way on the constant though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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,
Expand Down
9 changes: 9 additions & 0 deletions opentelemetry-api/tests/trace/test_span_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,12 @@ def test_span_context_pickle(self):
pickle_sc = pickle.loads(pickle.dumps(sc))
self.assertEqual(sc.trace_id, pickle_sc.trace_id)
self.assertEqual(sc.span_id, pickle_sc.span_id)

invalid_sc = trace.SpanContext(
9999999999999999999999999999999999999999999999999999999999999999999999999999,
9,
is_remote=False,
trace_flags=trace.DEFAULT_TRACE_OPTIONS,
trace_state=trace.DEFAULT_TRACE_STATE,
)
self.assertFalse(invalid_sc.is_valid)