Skip to content

Commit

Permalink
Add validation for Trace ID (#1992)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymotongpoo committed Aug 2, 2021
1 parent 13f09db commit 65670cf
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 1 deletion.
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
7 changes: 6 additions & 1 deletion opentelemetry-api/src/opentelemetry/trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ def values(self) -> typing.ValuesView[str]:


DEFAULT_TRACE_STATE = TraceState.get_default()
_TRACE_ID_HEX_LENGTH = 2 ** 128 - 1


class SpanContext(
Expand Down Expand Up @@ -420,7 +421,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 < _TRACE_ID_HEX_LENGTH
)

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)

0 comments on commit 65670cf

Please sign in to comment.