-
Notifications
You must be signed in to change notification settings - Fork 656
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
Adding attach/detach methods as per spec #429
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
ba7d8e9
Adding attach/detach methods as per spec
e3567c4
use a _Token for ThreadLocalRuntimeContext
6c65d62
remove instance check, ContextVar reset will do this
674658a
fix lint, catch AttributeError as well
9d94509
Merge branch 'master' into update-context-api
codeboten c0ede6c
lint fix
e6988ff
update set_current/reset to attach/detach in RuntimeContext
71256f3
catch all exceptions
a72befe
adding _load_runtime_context decorator
2e60e84
rename _Token to Token
ea4e732
fixing tests
34c5990
fixing docs
abadcac
fix mypy
fd954c7
Apply suggestions from code review
codeboten 10cd989
adding docs, removing unused method
d288715
refactoring test into a common class
2b4e958
fix mypy
ae5d8f6
adding test for out of order detach
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
# Copyright 2020, OpenTelemetry Authors | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import unittest | ||
from logging import ERROR | ||
|
||
from opentelemetry import context | ||
|
||
|
||
def do_work() -> None: | ||
context.attach(context.set_value("say", "bar")) | ||
|
||
|
||
class ContextTestCases: | ||
ocelotl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class BaseTest(unittest.TestCase): | ||
def setUp(self) -> None: | ||
self.previous_context = context.get_current() | ||
|
||
def tearDown(self) -> None: | ||
context.attach(self.previous_context) | ||
|
||
def test_context(self): | ||
self.assertIsNone(context.get_value("say")) | ||
empty = context.get_current() | ||
second = context.set_value("say", "foo") | ||
|
||
self.assertEqual(context.get_value("say", context=second), "foo") | ||
|
||
do_work() | ||
self.assertEqual(context.get_value("say"), "bar") | ||
third = context.get_current() | ||
|
||
self.assertIsNone(context.get_value("say", context=empty)) | ||
self.assertEqual(context.get_value("say", context=second), "foo") | ||
self.assertEqual(context.get_value("say", context=third), "bar") | ||
|
||
def test_set_value(self): | ||
first = context.set_value("a", "yyy") | ||
second = context.set_value("a", "zzz") | ||
third = context.set_value("a", "---", first) | ||
self.assertEqual("yyy", context.get_value("a", context=first)) | ||
self.assertEqual("zzz", context.get_value("a", context=second)) | ||
self.assertEqual("---", context.get_value("a", context=third)) | ||
self.assertEqual(None, context.get_value("a")) | ||
|
||
def test_attach(self): | ||
context.attach(context.set_value("a", "yyy")) | ||
|
||
token = context.attach(context.set_value("a", "zzz")) | ||
self.assertEqual("zzz", context.get_value("a")) | ||
|
||
context.detach(token) | ||
self.assertEqual("yyy", context.get_value("a")) | ||
|
||
with self.assertLogs(level=ERROR): | ||
context.detach("some garbage") | ||
|
||
def test_detach_out_of_order(self): | ||
t1 = context.attach(context.set_value("c", 1)) | ||
self.assertEqual(context.get_current(), {"c": 1}) | ||
t2 = context.attach(context.set_value("c", 2)) | ||
self.assertEqual(context.get_current(), {"c": 2}) | ||
context.detach(t1) | ||
self.assertEqual(context.get_current(), {}) | ||
context.detach(t2) | ||
self.assertEqual(context.get_current(), {"c": 1}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Did you try making the token type a
TypeVar
? Just curious, I don't know that it's worth the extra typing boilerplate to do so.