-
Notifications
You must be signed in to change notification settings - Fork 648
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Moving contextvars and threadlocal context implementations to the API (…
…#419) While keeping the flexibility of the interface to support additional implementations of the RuntimeContext, moving the ContextVarsRuntimeContext and ThreadLocalRuntimeContext into the API ensures a useful context implementation for users without requiring additional configuration. This changes checks the version number to determine which implementation to use, unless it is overridden by the OPENTELEMETRY_CONTEXT environment variable. Removing the DefaultRuntimeContext as part of this change. Signed-off-by: Alex Boten <[email protected]> Co-authored-by: Chris Kleinknecht <[email protected]>
- Loading branch information
Showing
11 changed files
with
99 additions
and
192 deletions.
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
34 changes: 0 additions & 34 deletions
34
opentelemetry-api/src/opentelemetry/context/default_context.py
This file was deleted.
Oops, something went wrong.
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
68 changes: 68 additions & 0 deletions
68
opentelemetry-api/tests/context/test_contextvars_context.py
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,68 @@ | ||
# 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 unittest.mock import patch | ||
|
||
from opentelemetry import context | ||
|
||
try: | ||
import contextvars # pylint: disable=unused-import | ||
from opentelemetry.context.contextvars_context import ( | ||
ContextVarsRuntimeContext, | ||
) | ||
except ImportError: | ||
raise unittest.SkipTest("contextvars not available") | ||
|
||
|
||
def do_work() -> None: | ||
context.set_current(context.set_value("say", "bar")) | ||
|
||
|
||
class TestContextVarsContext(unittest.TestCase): | ||
def setUp(self): | ||
self.previous_context = context.get_current() | ||
|
||
def tearDown(self): | ||
context.set_current(self.previous_context) | ||
|
||
@patch( | ||
"opentelemetry.context._RUNTIME_CONTEXT", ContextVarsRuntimeContext() # type: ignore | ||
) | ||
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") | ||
|
||
@patch( | ||
"opentelemetry.context._RUNTIME_CONTEXT", ContextVarsRuntimeContext() # type: ignore | ||
) | ||
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")) |
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
Oops, something went wrong.