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

Implement CreateKey Functionality #1853

Merged
merged 45 commits into from
Jun 11, 2021

Conversation

euniceek
Copy link
Contributor

This PR implements the CreateKey functionality such that it aligns partially with the context specification and does not make any breaking changes to the API.

Like before, the Opentelemetry-python context uses strings as input to keys for context. However, changes have been made such that it now first goes through create_key to generate a unique key for every inputted key names.

Multiple calls to CreateKey with the same name SHOULD NOT return the same value unless language constraints dictate otherwise. Different languages may impose different restrictions on the expected types, so this parameter remains an implementation detail.

The following part of the specification is not met in order to avoid breaking changes in the API.

The API MUST return an opaque object representing the newly created key.

Key implementation detail:

  • create_key method to create a new unique key string every time it is called.
  • The outputted key from create_key gets inputted into get_value and set_value methods.
  • Added unit tests for the create_key method.
  • Updated b3 and jaeger propagator unit tests to work with unique keys.

Fixes #1737

Type of change

  • New feature (non-breaking change which adds functionality)

How Has This Been Tested?

New unit tests were added to ensure that same key name does not generate the same key.
tox was ran to ensure the new and existing unit tests all pass.

Checklist:

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

cc @alolita

@euniceek euniceek requested review from a team, owais and srikanthccv and removed request for a team May 15, 2021 00:41
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

Nice! Thanks for the PR. Please update the changelog. Also are a few other places in the code where set_value is called w/ a string directly, those should probably be updated as well.

CHANGELOG.md Outdated Show resolved Hide resolved
CHANGELOG.md Outdated Show resolved Hide resolved
opentelemetry-api/src/opentelemetry/context/__init__.py Outdated Show resolved Hide resolved
@lzchen
Copy link
Contributor

lzchen commented May 17, 2021

I believe the set_value(key...)) changes need to be made in places in the contrib repo as well.

@euniceek
Copy link
Contributor Author

I believe the set_value(key...)) changes need to be made in places in the contrib repo as well.

Okay will do!
Just to confirm, does this mean creating a separate PR on the python contrib repo with the relevant changes?

@codeboten
Copy link
Contributor

I believe the set_value(key...)) changes need to be made in places in the contrib repo as well.

Okay will do!
Just to confirm, does this mean creating a separate PR on the python contrib repo with the relevant changes?

Correct. In order for the CI to pass, you'll need to ensure the git SHA is updated in the PR as per the steps in the contributing doc: https://github.com/open-telemetry/opentelemetry-python/blob/main/CONTRIBUTING.md#how-to-send-pull-requests

@euniceek euniceek force-pushed the create-context-key branch from 58fff25 to 061f591 Compare June 6, 2021 00:51
tox.ini Outdated Show resolved Hide resolved
@euniceek euniceek force-pushed the create-context-key branch from 71a1645 to ba15cdc Compare June 7, 2021 20:46
Copy link
Contributor

@codeboten codeboten left a comment

Choose a reason for hiding this comment

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

The changes look great! Please fix the lint issues and we should be good to merge soon. @owais please review

@codeboten codeboten requested a review from owais June 10, 2021 15:37
opentelemetry-api/src/opentelemetry/context/__init__.py Outdated Show resolved Hide resolved
@@ -68,6 +69,18 @@ def wrapper( # type: ignore[misc]
return typing.cast(_F, wrapper) # type: ignore[misc]


def _create_key(keyname: str) -> str:
Copy link
Member

Choose a reason for hiding this comment

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

This is just a suggestion if its easy enough to implement – the return type for the key seems like a great fit for a NewType (e.g. ContextKey = NewType('ContextKey', str)). Then we can annotate where a ContextKey is expected so its clear to users and mypy can enforce it.

Then you can add an overload for set_value(key: ContextKey, ...) and we can remove the original str key overload in the next major version.

from opentelemetry.context.context import Context
from opentelemetry.trace.span import INVALID_SPAN, Span

SPAN_KEY = "current-span"
Copy link
Member

Choose a reason for hiding this comment

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

Is this not a breaking API change? cc @ocelotl

Copy link
Contributor

Choose a reason for hiding this comment

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

Probably this was changed to avoid the public api test cases from breaking, SPAN_KEY should remain public and the tests will pass after the corresponding label is added. @eunice98k can you please fix this?

Copy link
Member

Choose a reason for hiding this comment

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

The public API check is passing on the current commit, shouldn't it have caught this? Or does it only check for additions atm?

Copy link
Contributor

Choose a reason for hiding this comment

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

It only checks for additions. The script is not meant to detect breaking changes, but to warn us when a possibly unnecessarily new public symbol is being added, to avoid our API from growing without need.

from opentelemetry.context.context import Context
from opentelemetry.trace.span import INVALID_SPAN, Span

SPAN_KEY = "current-span"
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably this was changed to avoid the public api test cases from breaking, SPAN_KEY should remain public and the tests will pass after the corresponding label is added. @eunice98k can you please fix this?

@euniceek euniceek force-pushed the create-context-key branch 2 times, most recently from 08542e1 to 092e341 Compare June 11, 2021 07:11
from opentelemetry.context.context import Context
from opentelemetry.trace.span import INVALID_SPAN, Span

SPAN_KEY = "current-span"
SPAN_KEY = create_key("current-span")
Copy link
Contributor

Choose a reason for hiding this comment

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

This is still breaking. You should leave SPAN_KEY = "current-span" add a variable _SPAN_KEY = create_key("current_span") which will be used internally.

@@ -118,6 +125,7 @@

ValueT = TypeVar("ValueT", int, float, bool, str)
logger = logging.getLogger(__name__)
SHIM_KEY = create_key("scope_shim")
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
SHIM_KEY = create_key("scope_shim")
_SHIM_KEY= create_key("scope_shim")

@ocelotl ocelotl added the Approve Public API check This label shows that the public symbols added or changed in a PR are strictly necessary label Jun 11, 2021
@lzchen lzchen merged commit 540b8a5 into open-telemetry:main Jun 11, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Approve Public API check This label shows that the public symbols added or changed in a PR are strictly necessary
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Context API is missing CreateKey functionality
7 participants