-
Notifications
You must be signed in to change notification settings - Fork 624
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
Moving contextvars and threadlocal context implementations to the API #419
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import logging | ||
import typing | ||
from os import environ | ||
from sys import version_info | ||
|
||
from pkg_resources import iter_entry_points | ||
|
||
|
@@ -84,9 +85,14 @@ def get_current() -> Context: | |
if _RUNTIME_CONTEXT is None: | ||
# FIXME use a better implementation of a configuration manager to avoid having | ||
# to get configuration values straight from environment variables | ||
if version_info < (3, 5): | ||
# contextvars are not supported in 3.4, use thread-local storage | ||
default_context = "threadlocal_context" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does this mean that async context won't work for python3.4? |
||
else: | ||
default_context = "contextvars_context" | ||
|
||
configured_context = environ.get( | ||
"OPENTELEMETRY_CONTEXT", "default_context" | ||
"OPENTELEMETRY_CONTEXT", default_context | ||
) # type: str | ||
try: | ||
_RUNTIME_CONTEXT = next( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,13 @@ | |
from contextvars import ContextVar | ||
from sys import version_info | ||
|
||
from opentelemetry.context import Context | ||
from opentelemetry.context.context import RuntimeContext | ||
from opentelemetry.context.context import Context, RuntimeContext | ||
|
||
if (3, 5, 3) <= version_info < (3, 7): | ||
import aiocontextvars # type: ignore # pylint:disable=unused-import | ||
import aiocontextvars # type: ignore # pylint:disable=unused-import,import-error | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please note that without this patch in aiocontextvars fantix/aiocontextvars#66 behavior of async code in python<3.7 differs from original contextvars in python 3.7. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hello @condorcet What specific behavior difference are you referring to? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @ocelotl !
In python 3.7 (proper behavior):
In python 3.6 (with aiocontextvars):
Here you can see that first callback changed context var, and next callback used modified value rather than initial. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for clarifying @condorcet, I'll open an issue to track this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Opened #436 |
||
|
||
elif (3, 4) < version_info <= (3, 5, 2): | ||
import opentelemetry.sdk.context.aiocontextvarsfix # pylint:disable=unused-import | ||
import opentelemetry.context.aiocontextvarsfix # pylint:disable=unused-import | ||
|
||
|
||
class ContextVarsRuntimeContext(RuntimeContext): | ||
|
This file was deleted.
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why the CLI There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It could be line length isn't being honored. I see it in the pyproject.toml, but IIRC my editor wasn't always honoring that configuration either. |
||
) | ||
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")) |
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.
Good call on the conditional requirements.