Skip to content
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

Fix TraceState to adhere to specs #1502

Merged
merged 46 commits into from Jan 20, 2021

Conversation

srikanthccv
Copy link
Member

@srikanthccv srikanthccv commented Dec 24, 2020

Description

Fixes #1487

Type of change

Please delete options that are not relevant.

  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • tox -e test-core-api
  • tox -e test-core-sdk

Does This PR Require a Contrib Repo Change?

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@srikanthccv srikanthccv requested a review from a team as a code owner December 24, 2020 09:22
@srikanthccv srikanthccv requested review from aabmass and hectorhdzg and removed request for a team December 24, 2020 09:22
@srikanthccv srikanthccv marked this pull request as ready for review December 24, 2020 10:35
typing.Sequence[typing.Tuple[str, str]]
] = None,
) -> None:
self._dict = collections.OrderedDict()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should keep the previous behaviour and have TraceState be the actual dictionary, instead of having to construct a TraceState object?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would make the TraceState to be mutable by anyone and insertion order remembering is not a language feature until python3.7. Specs says it should be immutable list of string key/value pairs, and all mutable operations (through add, update, delete) should validate the inputs and return a new TraceState and order of pairs should always be preserved.

API should provide the add, update, delete operations. To achieve this we would still need to inherit from mapping and implement them. In case if we consider to drop support for lower python versions we should override __setitem__ and __delitem__ to not allow mutating directly. Keeping all this in mind, I think we should have TraceState object and it wouldn't be feasible to follow spec with actual dictionary.

Copy link
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice, just a few nits

opentelemetry-api/src/opentelemetry/trace/span.py Outdated Show resolved Hide resolved
opentelemetry-api/src/opentelemetry/trace/span.py Outdated Show resolved Hide resolved
A string that adheres to the w3c tracestate
header format.
"""
return ",".join(key + "=" + value for key, value in self._dict.items())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Im not super familiar with the tracestate spec; do we need some escaping in the values here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From W3C Trace Context spec

The value is an opaque string containing up to 256 printable ASCII characters (i.e., the range 0x20 to 0x7E) except comma (,) and (=).

If I understand it correctly we don't need to do any escaping as this header will be ASCII only which is totally fine with HTTP Headers.

opentelemetry-api/src/opentelemetry/trace/span.py Outdated Show resolved Hide resolved
# empty members are valid, but no need to process further.
if not member:
continue
match = _MEMBER_PATTERN.fullmatch(member)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have util like _is_valid_pair(), why not have a is_valid_member()?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the mutation operations on trace state should validate the key/value. The member would be in format of key=value. If we go with is_valid_member() we will have construct member from key/value params. If we stick with is_valid_pair we need to split the member from header to give pair. Either way would be fine.

Comment on lines +11 to +14
_DELIMITER_PATTERN,
_MEMBER_PATTERN,
_TRACECONTEXT_MAXIMUM_TRACESTATE_KEYS,
_is_valid_pair,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These imports shouldn't be named with underscore prefixes right?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think these are for internal use and should have leading underscore. Ideally users should only rely on TraceState and inject/extract API.

@lzchen
Copy link
Contributor

lzchen commented Jan 19, 2021

@lonewolf3739
Can you fix merge conflicts?

@srikanthccv
Copy link
Member Author

@lonewolf3739
Can you fix merge conflicts?

Done.

@lzchen lzchen merged commit 1d39f7f into open-telemetry:master Jan 20, 2021
@srikanthccv srikanthccv deleted the trace-state-fix branch September 24, 2021 08:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Fix TraceState to adhere to specs
3 participants