-
Notifications
You must be signed in to change notification settings - Fork 650
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
Conversation
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.
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.
I believe the |
Okay will do! |
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 |
58fff25
to
061f591
Compare
71a1645
to
ba15cdc
Compare
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.
The changes look great! Please fix the lint issues and we should be good to merge soon. @owais please review
@@ -68,6 +69,18 @@ def wrapper( # type: ignore[misc] | |||
return typing.cast(_F, wrapper) # type: ignore[misc] | |||
|
|||
|
|||
def _create_key(keyname: str) -> str: |
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.
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" |
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.
Is this not a breaking API change? cc @ocelotl
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.
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?
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.
The public API check is passing on the current commit, shouldn't it have caught this? Or does it only check for additions atm?
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.
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" |
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.
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?
08542e1
to
092e341
Compare
from opentelemetry.context.context import Context | ||
from opentelemetry.trace.span import INVALID_SPAN, Span | ||
|
||
SPAN_KEY = "current-span" | ||
SPAN_KEY = create_key("current-span") |
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.
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") |
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.
SHIM_KEY = create_key("scope_shim") | |
_SHIM_KEY= create_key("scope_shim") |
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.The following part of the specification is not met in order to avoid breaking changes in the API.
Key implementation detail:
create_key
method to create a new unique key string every time it is called.create_key
gets inputted intoget_value
andset_value methods
.create_key
method.Fixes #1737
Type of change
How Has This Been Tested?
New unit tests were added to ensure that same
key name
does not generate the samekey
.tox
was ran to ensure the new and existing unit tests all pass.Checklist:
cc @alolita