From 6afb10c31b06df3da687ccc74c21a23a2f469a06 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 14 May 2021 14:49:28 -0700 Subject: [PATCH 01/32] Add create key functionality --- CHANGELOG.md | 2 ++ .../src/opentelemetry/baggage/__init__.py | 4 ++-- .../src/opentelemetry/context/__init__.py | 13 ++++++++++ .../trace/propagation/__init__.py | 4 ++-- .../tests/context/test_context.py | 24 +++++++++++++------ .../tests/test_b3_format.py | 8 ++++--- .../tests/test_jaeger_propagator.py | 13 +++++----- .../shim/opentracing_shim/__init__.py | 12 ++++++++-- 8 files changed, 58 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e4fb4c5b200..ab83f6f12e9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1823](https://github.com/open-telemetry/opentelemetry-python/pull/1823)) - Added support for OTEL_SERVICE_NAME. ([#1829](https://github.com/open-telemetry/opentelemetry-python/pull/1829)) +- Added support for CreateKey functionality. + (TDB) ### Changed - Fixed OTLP gRPC exporter silently failing if scheme is not specified in endpoint. diff --git a/opentelemetry-api/src/opentelemetry/baggage/__init__.py b/opentelemetry-api/src/opentelemetry/baggage/__init__.py index da78d92417c..2368c5a325f 100644 --- a/opentelemetry-api/src/opentelemetry/baggage/__init__.py +++ b/opentelemetry-api/src/opentelemetry/baggage/__init__.py @@ -15,10 +15,10 @@ import typing from types import MappingProxyType -from opentelemetry.context import get_value, set_value +from opentelemetry.context import create_key, get_value, set_value from opentelemetry.context.context import Context -_BAGGAGE_KEY = "baggage" +_BAGGAGE_KEY = create_key("baggage") def get_all( diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 0f60d86a0c7..82a2c7a645e 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -15,6 +15,7 @@ import logging import threading import typing +import uuid from functools import wraps from os import environ @@ -68,6 +69,18 @@ def wrapper( # type: ignore[misc] return typing.cast(_F, wrapper) # type: ignore[misc] +def create_key(keyname: str) -> str: + """To allow cross-cutting concern to control access to their local state, + the RuntimeContext API provides a function which takes a keyname as input, + and returns a unique key string. + Args: + keyname: The key name for debugging purposes and is not unique. + Returns: + A new unique string representing the newly created key. + """ + return str(uuid.uuid4()) + + def get_value(key: str, context: typing.Optional[Context] = None) -> "object": """To access the local state of a concern, the RuntimeContext API provides a function which takes a context and a key as input, diff --git a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py index 51146adeb31..ef241f6c427 100644 --- a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py @@ -13,11 +13,11 @@ # limitations under the License. from typing import Optional -from opentelemetry.context import get_value, set_value +from opentelemetry.context import create_key, get_value, set_value from opentelemetry.context.context import Context from opentelemetry.trace.span import INVALID_SPAN, Span -SPAN_KEY = "current-span" +SPAN_KEY = create_key("current-span") def set_span_in_context( diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py index 7dad1191c6c..2fc43cab636 100644 --- a/opentelemetry-api/tests/context/test_context.py +++ b/opentelemetry-api/tests/context/test_context.py @@ -26,19 +26,29 @@ class TestContext(unittest.TestCase): def setUp(self): context.attach(Context()) + def test_context_key(self): + key1 = context.create_key("say") + key2 = context.create_key("say") + self.assertNotEqual(key1, key2) + first = context.set_value(key1, "foo") + second = context.set_value(key2, "bar") + self.assertEqual(context.get_value(key1, context=first), "foo") + self.assertEqual(context.get_value(key2, context=second), "bar") + def test_context(self): - self.assertIsNone(context.get_value("say")) + key = context.create_key("say") + self.assertIsNone(context.get_value(key)) empty = context.get_current() - second = context.set_value("say", "foo") - self.assertEqual(context.get_value("say", context=second), "foo") + second = context.set_value(key, "foo") + self.assertEqual(context.get_value(key, context=second), "foo") do_work() - self.assertEqual(context.get_value("say"), "bar") + self.assertEqual(context.get_value(key), "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") + self.assertIsNone(context.get_value(key, context=empty)) + self.assertEqual(context.get_value(key, context=second), "foo") + self.assertEqual(context.get_value(key, context=third), "bar") def test_set_value(self): first = context.set_value("a", "yyy") diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index 87a059f57e2..7941bb7c896 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -19,12 +19,14 @@ import opentelemetry.sdk.trace as trace import opentelemetry.sdk.trace.id_generator as id_generator import opentelemetry.trace as trace_api -from opentelemetry.context import Context, get_current +from opentelemetry.context import Context, create_key, get_current from opentelemetry.propagators.b3 import ( # pylint: disable=no-name-in-module,import-error B3MultiFormat, B3SingleFormat, ) from opentelemetry.propagators.textmap import DefaultGetter +from opentelemetry.trace.propagation import SPAN_KEY + def get_child_parent_new_carrier(old_carrier, propagator): @@ -247,7 +249,7 @@ def test_derived_ctx_is_returned_for_success(self): }, old_ctx, ) - self.assertIn("current-span", new_ctx) + self.assertIn(SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object @@ -257,7 +259,7 @@ def test_derived_ctx_is_returned_for_failure(self): """Ensure returned context is derived from the given context.""" old_ctx = Context({"k2": "v2"}) new_ctx = self.get_propagator().extract({}, old_ctx) - self.assertNotIn("current-span", new_ctx) + self.assertNotIn(SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object diff --git a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py index 55e096b0954..d884e6c3b9c 100644 --- a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py +++ b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py @@ -19,7 +19,8 @@ import opentelemetry.sdk.trace.id_generator as id_generator import opentelemetry.trace as trace_api from opentelemetry import baggage -from opentelemetry.context import Context +from opentelemetry.baggage import _BAGGAGE_KEY +from opentelemetry.context import Context, create_key from opentelemetry.propagators import ( # pylint: disable=no-name-in-module jaeger, ) @@ -134,7 +135,7 @@ def test_baggage(self): input_baggage = {"key1": "value1"} _, new_carrier = get_context_new_carrier(old_carrier, input_baggage) ctx = FORMAT.extract(new_carrier) - self.assertDictEqual(input_baggage, ctx["baggage"]) + self.assertDictEqual(input_baggage, ctx[_BAGGAGE_KEY]) def test_non_string_baggage(self): old_carrier = {FORMAT.TRACE_ID_KEY: self.serialized_uber_trace_id} @@ -142,7 +143,7 @@ def test_non_string_baggage(self): formatted_baggage = {"key1": "1", "key2": "True"} _, new_carrier = get_context_new_carrier(old_carrier, input_baggage) ctx = FORMAT.extract(new_carrier) - self.assertDictEqual(formatted_baggage, ctx["baggage"]) + self.assertDictEqual(formatted_baggage, ctx[_BAGGAGE_KEY]) def test_extract_invalid_uber_trace_id(self): old_carrier = { @@ -153,7 +154,7 @@ def test_extract_invalid_uber_trace_id(self): context = FORMAT.extract(old_carrier) span_context = trace_api.get_current_span(context).get_span_context() self.assertEqual(span_context.span_id, trace_api.INVALID_SPAN_ID) - self.assertDictEqual(formatted_baggage, context["baggage"]) + self.assertDictEqual(formatted_baggage, context[_BAGGAGE_KEY]) def test_extract_invalid_trace_id(self): old_carrier = { @@ -164,7 +165,7 @@ def test_extract_invalid_trace_id(self): context = FORMAT.extract(old_carrier) span_context = trace_api.get_current_span(context).get_span_context() self.assertEqual(span_context.trace_id, trace_api.INVALID_TRACE_ID) - self.assertDictEqual(formatted_baggage, context["baggage"]) + self.assertDictEqual(formatted_baggage, context[_BAGGAGE_KEY]) def test_extract_invalid_span_id(self): old_carrier = { @@ -175,7 +176,7 @@ def test_extract_invalid_span_id(self): context = FORMAT.extract(old_carrier) span_context = trace_api.get_current_span(context).get_span_context() self.assertEqual(span_context.span_id, trace_api.INVALID_SPAN_ID) - self.assertDictEqual(formatted_baggage, context["baggage"]) + self.assertDictEqual(formatted_baggage, context[_BAGGAGE_KEY]) def test_fields(self): tracer = trace.TracerProvider().get_tracer("sdk_tracer_provider") diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 4cddf2a57ae..8d7924f0628 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -100,7 +100,14 @@ ) from opentelemetry.baggage import get_baggage, set_baggage -from opentelemetry.context import Context, attach, detach, get_value, set_value +from opentelemetry.context import ( + Context, + attach, + create_key, + detach, + get_value, + set_value, +) from opentelemetry.propagate import get_global_textmap from opentelemetry.shim.opentracing_shim import util from opentelemetry.shim.opentracing_shim.version import __version__ @@ -117,6 +124,7 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) +KEY = create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -348,7 +356,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value("scope_shim", self)) + self._token = attach(set_value(KEY, self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. From edbe22bab8bf6aa6fc60b12f19e04142669667c1 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 14 May 2021 15:25:17 -0700 Subject: [PATCH 02/32] Update unit tests --- .../tests/context/test_context.py | 24 ++++++++++--------- .../tests/test_b3_format.py | 1 - .../shim/opentracing_shim/__init__.py | 12 ++-------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py index 2fc43cab636..61c887326ec 100644 --- a/opentelemetry-api/tests/context/test_context.py +++ b/opentelemetry-api/tests/context/test_context.py @@ -18,8 +18,10 @@ from opentelemetry.context.context import Context -def do_work() -> None: - context.attach(context.set_value("say", "bar")) +def do_work() -> str: + key = context.create_key("say") + context.attach(context.set_value(key, "bar")) + return key class TestContext(unittest.TestCase): @@ -36,19 +38,19 @@ def test_context_key(self): self.assertEqual(context.get_value(key2, context=second), "bar") def test_context(self): - key = context.create_key("say") - self.assertIsNone(context.get_value(key)) + key1 = context.create_key("say") + self.assertIsNone(context.get_value(key1)) empty = context.get_current() - second = context.set_value(key, "foo") - self.assertEqual(context.get_value(key, context=second), "foo") + second = context.set_value(key1, "foo") + self.assertEqual(context.get_value(key1, context=second), "foo") - do_work() - self.assertEqual(context.get_value(key), "bar") + key2 = do_work() + self.assertEqual(context.get_value(key2), "bar") third = context.get_current() - self.assertIsNone(context.get_value(key, context=empty)) - self.assertEqual(context.get_value(key, context=second), "foo") - self.assertEqual(context.get_value(key, context=third), "bar") + self.assertIsNone(context.get_value(key1, context=empty)) + self.assertEqual(context.get_value(key1, context=second), "foo") + self.assertEqual(context.get_value(key2, context=third), "bar") def test_set_value(self): first = context.set_value("a", "yyy") diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index 7941bb7c896..83bd47678ca 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -28,7 +28,6 @@ from opentelemetry.trace.propagation import SPAN_KEY - def get_child_parent_new_carrier(old_carrier, propagator): ctx = propagator.extract(old_carrier) diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 8d7924f0628..4cddf2a57ae 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -100,14 +100,7 @@ ) from opentelemetry.baggage import get_baggage, set_baggage -from opentelemetry.context import ( - Context, - attach, - create_key, - detach, - get_value, - set_value, -) +from opentelemetry.context import Context, attach, detach, get_value, set_value from opentelemetry.propagate import get_global_textmap from opentelemetry.shim.opentracing_shim import util from opentelemetry.shim.opentracing_shim.version import __version__ @@ -124,7 +117,6 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) -KEY = create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -356,7 +348,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value(KEY, self)) + self._token = attach(set_value("scope_shim", self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. From 1ec001ddaf80be79d00d003184bd92197082c85e Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 14 May 2021 15:35:05 -0700 Subject: [PATCH 03/32] Update create_key comment --- opentelemetry-api/src/opentelemetry/context/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 82a2c7a645e..c804a7a2314 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -72,11 +72,11 @@ def wrapper( # type: ignore[misc] def create_key(keyname: str) -> str: """To allow cross-cutting concern to control access to their local state, the RuntimeContext API provides a function which takes a keyname as input, - and returns a unique key string. + and returns a unique key. Args: - keyname: The key name for debugging purposes and is not unique. + keyname: The key name is for debugging purposes and is not unique. Returns: - A new unique string representing the newly created key. + A unique string representing the newly created key. """ return str(uuid.uuid4()) From 9010487019c3a667ac7185e0d04a0df6d8fa2a4b Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 14 May 2021 19:54:19 -0700 Subject: [PATCH 04/32] Change key to reflect key name --- opentelemetry-api/src/opentelemetry/context/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index c804a7a2314..7ccb2f10a1f 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -78,7 +78,7 @@ def create_key(keyname: str) -> str: Returns: A unique string representing the newly created key. """ - return str(uuid.uuid4()) + return keyname + '-' + str(uuid.uuid4()) def get_value(key: str, context: typing.Optional[Context] = None) -> "object": From 95af93dcbc7d74ee879b645d562cce36208cd079 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Mon, 17 May 2021 09:29:52 -0700 Subject: [PATCH 05/32] Update CHANGELOG --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ab83f6f12e9..e03716f9932 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.2.0-0.21b0...HEAD) +- Added support for CreateKey functionality. + ([#1853](https://github.com/open-telemetry/opentelemetry-python/pull/1853)) ## [1.2.0, 0.21b0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.2.0-0.21b0) - 2021-05-11 @@ -15,8 +17,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1823](https://github.com/open-telemetry/opentelemetry-python/pull/1823)) - Added support for OTEL_SERVICE_NAME. ([#1829](https://github.com/open-telemetry/opentelemetry-python/pull/1829)) -- Added support for CreateKey functionality. - (TDB) ### Changed - Fixed OTLP gRPC exporter silently failing if scheme is not specified in endpoint. From fb86f6f7d35a410fd3bc4d6e383a99feca560210 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Mon, 17 May 2021 10:35:42 -0700 Subject: [PATCH 06/32] Update set_value inputs to key --- .../src/opentelemetry/context/__init__.py | 4 ++-- .../src/opentelemetry/sdk/trace/export/__init__.py | 13 ++++++++++--- .../shim/opentracing_shim/__init__.py | 14 +++++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 7ccb2f10a1f..a8dd39c53ab 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -74,11 +74,11 @@ def create_key(keyname: str) -> str: the RuntimeContext API provides a function which takes a keyname as input, and returns a unique key. Args: - keyname: The key name is for debugging purposes and is not unique. + keyname: The key name is for debugging purposes and is not required to be unique. Returns: A unique string representing the newly created key. """ - return keyname + '-' + str(uuid.uuid4()) + return keyname + "-" + str(uuid.uuid4()) def get_value(key: str, context: typing.Optional[Context] = None) -> "object": diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index 457a1ef56ca..d5638305c37 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -21,7 +21,13 @@ from os import environ, linesep from typing import Optional -from opentelemetry.context import Context, attach, detach, set_value +from opentelemetry.context import ( + Context, + attach, + create_key, + detach, + set_value, +) from opentelemetry.sdk.environment_variables import ( OTEL_BSP_EXPORT_TIMEOUT, OTEL_BSP_MAX_EXPORT_BATCH_SIZE, @@ -32,6 +38,7 @@ from opentelemetry.util._time import _time_ns logger = logging.getLogger(__name__) +EXPORT_KEY = create_key("suppress_instrumentation") class SpanExportResult(Enum): @@ -86,7 +93,7 @@ def on_start( def on_end(self, span: ReadableSpan) -> None: if not span.context.trace_flags.sampled: return - token = attach(set_value("suppress_instrumentation", True)) + token = attach(set_value(EXPORT_KEY, True)) try: self.span_exporter.export((span,)) # pylint: disable=broad-except @@ -326,7 +333,7 @@ def _export_batch(self) -> int: while idx < self.max_export_batch_size and self.queue: self.spans_list[idx] = self.queue.pop() idx += 1 - token = attach(set_value("suppress_instrumentation", True)) + token = attach(set_value(EXPORT_KEY, True)) try: # Ignore type b/c the Optional[None]+slicing is too "clever" # for mypy diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 4cddf2a57ae..55e758083f2 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -100,7 +100,14 @@ ) from opentelemetry.baggage import get_baggage, set_baggage -from opentelemetry.context import Context, attach, detach, get_value, set_value +from opentelemetry.context import ( + Context, + attach, + create_key, + detach, + get_value, + set_value, +) from opentelemetry.propagate import get_global_textmap from opentelemetry.shim.opentracing_shim import util from opentelemetry.shim.opentracing_shim.version import __version__ @@ -117,6 +124,7 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) +SHIM_KEY = create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -348,7 +356,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value("scope_shim", self)) + self._token = attach(set_value(SHIM_KEY, self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. @@ -477,7 +485,7 @@ def active(self) -> "ScopeShim": return None try: - return get_value("scope_shim") + return get_value(SHIM_KEY) except KeyError: span_context = SpanContextShim(span.get_span_context()) wrapped_span = SpanShim(self._tracer, span_context, span) From 1f59f5b85dac7f67b4bc0af775e283aad31a2abd Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Mon, 17 May 2021 12:22:22 -0700 Subject: [PATCH 07/32] Fix lint errors --- propagator/opentelemetry-propagator-b3/tests/test_b3_format.py | 2 +- .../tests/test_jaeger_propagator.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index 83bd47678ca..3ac97851ba7 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -19,7 +19,7 @@ import opentelemetry.sdk.trace as trace import opentelemetry.sdk.trace.id_generator as id_generator import opentelemetry.trace as trace_api -from opentelemetry.context import Context, create_key, get_current +from opentelemetry.context import Context, get_current from opentelemetry.propagators.b3 import ( # pylint: disable=no-name-in-module,import-error B3MultiFormat, B3SingleFormat, diff --git a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py index d884e6c3b9c..90265584700 100644 --- a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py +++ b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py @@ -20,7 +20,7 @@ import opentelemetry.trace as trace_api from opentelemetry import baggage from opentelemetry.baggage import _BAGGAGE_KEY -from opentelemetry.context import Context, create_key +from opentelemetry.context import Context from opentelemetry.propagators import ( # pylint: disable=no-name-in-module jaeger, ) From a9393276304d8b0dceba5341a81d14f671a2b767 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 14 May 2021 14:49:28 -0700 Subject: [PATCH 08/32] Add create key functionality --- CHANGELOG.md | 2 ++ .../src/opentelemetry/baggage/__init__.py | 4 ++-- .../src/opentelemetry/context/__init__.py | 13 ++++++++++ .../trace/propagation/__init__.py | 4 ++-- .../tests/context/test_context.py | 24 +++++++++++++------ .../tests/test_b3_format.py | 8 ++++--- .../tests/test_jaeger_propagator.py | 13 +++++----- .../shim/opentracing_shim/__init__.py | 12 ++++++++-- 8 files changed, 58 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b9022d5f7d..bbd6aca7f3f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1823](https://github.com/open-telemetry/opentelemetry-python/pull/1823)) - Added support for OTEL_SERVICE_NAME. ([#1829](https://github.com/open-telemetry/opentelemetry-python/pull/1829)) +- Added support for CreateKey functionality. + (TDB) ### Changed - Fixed OTLP gRPC exporter silently failing if scheme is not specified in endpoint. diff --git a/opentelemetry-api/src/opentelemetry/baggage/__init__.py b/opentelemetry-api/src/opentelemetry/baggage/__init__.py index da78d92417c..2368c5a325f 100644 --- a/opentelemetry-api/src/opentelemetry/baggage/__init__.py +++ b/opentelemetry-api/src/opentelemetry/baggage/__init__.py @@ -15,10 +15,10 @@ import typing from types import MappingProxyType -from opentelemetry.context import get_value, set_value +from opentelemetry.context import create_key, get_value, set_value from opentelemetry.context.context import Context -_BAGGAGE_KEY = "baggage" +_BAGGAGE_KEY = create_key("baggage") def get_all( diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 0f60d86a0c7..82a2c7a645e 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -15,6 +15,7 @@ import logging import threading import typing +import uuid from functools import wraps from os import environ @@ -68,6 +69,18 @@ def wrapper( # type: ignore[misc] return typing.cast(_F, wrapper) # type: ignore[misc] +def create_key(keyname: str) -> str: + """To allow cross-cutting concern to control access to their local state, + the RuntimeContext API provides a function which takes a keyname as input, + and returns a unique key string. + Args: + keyname: The key name for debugging purposes and is not unique. + Returns: + A new unique string representing the newly created key. + """ + return str(uuid.uuid4()) + + def get_value(key: str, context: typing.Optional[Context] = None) -> "object": """To access the local state of a concern, the RuntimeContext API provides a function which takes a context and a key as input, diff --git a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py index 51146adeb31..ef241f6c427 100644 --- a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py @@ -13,11 +13,11 @@ # limitations under the License. from typing import Optional -from opentelemetry.context import get_value, set_value +from opentelemetry.context import create_key, get_value, set_value from opentelemetry.context.context import Context from opentelemetry.trace.span import INVALID_SPAN, Span -SPAN_KEY = "current-span" +SPAN_KEY = create_key("current-span") def set_span_in_context( diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py index 7dad1191c6c..2fc43cab636 100644 --- a/opentelemetry-api/tests/context/test_context.py +++ b/opentelemetry-api/tests/context/test_context.py @@ -26,19 +26,29 @@ class TestContext(unittest.TestCase): def setUp(self): context.attach(Context()) + def test_context_key(self): + key1 = context.create_key("say") + key2 = context.create_key("say") + self.assertNotEqual(key1, key2) + first = context.set_value(key1, "foo") + second = context.set_value(key2, "bar") + self.assertEqual(context.get_value(key1, context=first), "foo") + self.assertEqual(context.get_value(key2, context=second), "bar") + def test_context(self): - self.assertIsNone(context.get_value("say")) + key = context.create_key("say") + self.assertIsNone(context.get_value(key)) empty = context.get_current() - second = context.set_value("say", "foo") - self.assertEqual(context.get_value("say", context=second), "foo") + second = context.set_value(key, "foo") + self.assertEqual(context.get_value(key, context=second), "foo") do_work() - self.assertEqual(context.get_value("say"), "bar") + self.assertEqual(context.get_value(key), "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") + self.assertIsNone(context.get_value(key, context=empty)) + self.assertEqual(context.get_value(key, context=second), "foo") + self.assertEqual(context.get_value(key, context=third), "bar") def test_set_value(self): first = context.set_value("a", "yyy") diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index 87a059f57e2..7941bb7c896 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -19,12 +19,14 @@ import opentelemetry.sdk.trace as trace import opentelemetry.sdk.trace.id_generator as id_generator import opentelemetry.trace as trace_api -from opentelemetry.context import Context, get_current +from opentelemetry.context import Context, create_key, get_current from opentelemetry.propagators.b3 import ( # pylint: disable=no-name-in-module,import-error B3MultiFormat, B3SingleFormat, ) from opentelemetry.propagators.textmap import DefaultGetter +from opentelemetry.trace.propagation import SPAN_KEY + def get_child_parent_new_carrier(old_carrier, propagator): @@ -247,7 +249,7 @@ def test_derived_ctx_is_returned_for_success(self): }, old_ctx, ) - self.assertIn("current-span", new_ctx) + self.assertIn(SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object @@ -257,7 +259,7 @@ def test_derived_ctx_is_returned_for_failure(self): """Ensure returned context is derived from the given context.""" old_ctx = Context({"k2": "v2"}) new_ctx = self.get_propagator().extract({}, old_ctx) - self.assertNotIn("current-span", new_ctx) + self.assertNotIn(SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object diff --git a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py index 55e096b0954..d884e6c3b9c 100644 --- a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py +++ b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py @@ -19,7 +19,8 @@ import opentelemetry.sdk.trace.id_generator as id_generator import opentelemetry.trace as trace_api from opentelemetry import baggage -from opentelemetry.context import Context +from opentelemetry.baggage import _BAGGAGE_KEY +from opentelemetry.context import Context, create_key from opentelemetry.propagators import ( # pylint: disable=no-name-in-module jaeger, ) @@ -134,7 +135,7 @@ def test_baggage(self): input_baggage = {"key1": "value1"} _, new_carrier = get_context_new_carrier(old_carrier, input_baggage) ctx = FORMAT.extract(new_carrier) - self.assertDictEqual(input_baggage, ctx["baggage"]) + self.assertDictEqual(input_baggage, ctx[_BAGGAGE_KEY]) def test_non_string_baggage(self): old_carrier = {FORMAT.TRACE_ID_KEY: self.serialized_uber_trace_id} @@ -142,7 +143,7 @@ def test_non_string_baggage(self): formatted_baggage = {"key1": "1", "key2": "True"} _, new_carrier = get_context_new_carrier(old_carrier, input_baggage) ctx = FORMAT.extract(new_carrier) - self.assertDictEqual(formatted_baggage, ctx["baggage"]) + self.assertDictEqual(formatted_baggage, ctx[_BAGGAGE_KEY]) def test_extract_invalid_uber_trace_id(self): old_carrier = { @@ -153,7 +154,7 @@ def test_extract_invalid_uber_trace_id(self): context = FORMAT.extract(old_carrier) span_context = trace_api.get_current_span(context).get_span_context() self.assertEqual(span_context.span_id, trace_api.INVALID_SPAN_ID) - self.assertDictEqual(formatted_baggage, context["baggage"]) + self.assertDictEqual(formatted_baggage, context[_BAGGAGE_KEY]) def test_extract_invalid_trace_id(self): old_carrier = { @@ -164,7 +165,7 @@ def test_extract_invalid_trace_id(self): context = FORMAT.extract(old_carrier) span_context = trace_api.get_current_span(context).get_span_context() self.assertEqual(span_context.trace_id, trace_api.INVALID_TRACE_ID) - self.assertDictEqual(formatted_baggage, context["baggage"]) + self.assertDictEqual(formatted_baggage, context[_BAGGAGE_KEY]) def test_extract_invalid_span_id(self): old_carrier = { @@ -175,7 +176,7 @@ def test_extract_invalid_span_id(self): context = FORMAT.extract(old_carrier) span_context = trace_api.get_current_span(context).get_span_context() self.assertEqual(span_context.span_id, trace_api.INVALID_SPAN_ID) - self.assertDictEqual(formatted_baggage, context["baggage"]) + self.assertDictEqual(formatted_baggage, context[_BAGGAGE_KEY]) def test_fields(self): tracer = trace.TracerProvider().get_tracer("sdk_tracer_provider") diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 4cddf2a57ae..8d7924f0628 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -100,7 +100,14 @@ ) from opentelemetry.baggage import get_baggage, set_baggage -from opentelemetry.context import Context, attach, detach, get_value, set_value +from opentelemetry.context import ( + Context, + attach, + create_key, + detach, + get_value, + set_value, +) from opentelemetry.propagate import get_global_textmap from opentelemetry.shim.opentracing_shim import util from opentelemetry.shim.opentracing_shim.version import __version__ @@ -117,6 +124,7 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) +KEY = create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -348,7 +356,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value("scope_shim", self)) + self._token = attach(set_value(KEY, self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. From 20257df3ca1c3bfb57fd9d6e22f646b79cee9723 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 14 May 2021 15:25:17 -0700 Subject: [PATCH 09/32] Update unit tests --- .../tests/context/test_context.py | 24 ++++++++++--------- .../tests/test_b3_format.py | 1 - .../shim/opentracing_shim/__init__.py | 12 ++-------- 3 files changed, 15 insertions(+), 22 deletions(-) diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py index 2fc43cab636..61c887326ec 100644 --- a/opentelemetry-api/tests/context/test_context.py +++ b/opentelemetry-api/tests/context/test_context.py @@ -18,8 +18,10 @@ from opentelemetry.context.context import Context -def do_work() -> None: - context.attach(context.set_value("say", "bar")) +def do_work() -> str: + key = context.create_key("say") + context.attach(context.set_value(key, "bar")) + return key class TestContext(unittest.TestCase): @@ -36,19 +38,19 @@ def test_context_key(self): self.assertEqual(context.get_value(key2, context=second), "bar") def test_context(self): - key = context.create_key("say") - self.assertIsNone(context.get_value(key)) + key1 = context.create_key("say") + self.assertIsNone(context.get_value(key1)) empty = context.get_current() - second = context.set_value(key, "foo") - self.assertEqual(context.get_value(key, context=second), "foo") + second = context.set_value(key1, "foo") + self.assertEqual(context.get_value(key1, context=second), "foo") - do_work() - self.assertEqual(context.get_value(key), "bar") + key2 = do_work() + self.assertEqual(context.get_value(key2), "bar") third = context.get_current() - self.assertIsNone(context.get_value(key, context=empty)) - self.assertEqual(context.get_value(key, context=second), "foo") - self.assertEqual(context.get_value(key, context=third), "bar") + self.assertIsNone(context.get_value(key1, context=empty)) + self.assertEqual(context.get_value(key1, context=second), "foo") + self.assertEqual(context.get_value(key2, context=third), "bar") def test_set_value(self): first = context.set_value("a", "yyy") diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index 7941bb7c896..83bd47678ca 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -28,7 +28,6 @@ from opentelemetry.trace.propagation import SPAN_KEY - def get_child_parent_new_carrier(old_carrier, propagator): ctx = propagator.extract(old_carrier) diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 8d7924f0628..4cddf2a57ae 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -100,14 +100,7 @@ ) from opentelemetry.baggage import get_baggage, set_baggage -from opentelemetry.context import ( - Context, - attach, - create_key, - detach, - get_value, - set_value, -) +from opentelemetry.context import Context, attach, detach, get_value, set_value from opentelemetry.propagate import get_global_textmap from opentelemetry.shim.opentracing_shim import util from opentelemetry.shim.opentracing_shim.version import __version__ @@ -124,7 +117,6 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) -KEY = create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -356,7 +348,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value(KEY, self)) + self._token = attach(set_value("scope_shim", self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. From a8741ababa32988ccbbdac6437ef3f650d8fa957 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 14 May 2021 15:35:05 -0700 Subject: [PATCH 10/32] Update create_key comment --- opentelemetry-api/src/opentelemetry/context/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 82a2c7a645e..c804a7a2314 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -72,11 +72,11 @@ def wrapper( # type: ignore[misc] def create_key(keyname: str) -> str: """To allow cross-cutting concern to control access to their local state, the RuntimeContext API provides a function which takes a keyname as input, - and returns a unique key string. + and returns a unique key. Args: - keyname: The key name for debugging purposes and is not unique. + keyname: The key name is for debugging purposes and is not unique. Returns: - A new unique string representing the newly created key. + A unique string representing the newly created key. """ return str(uuid.uuid4()) From 657e21902760f6b396b65c9a51d08e3c4f68a7e1 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 14 May 2021 19:54:19 -0700 Subject: [PATCH 11/32] Change key to reflect key name --- opentelemetry-api/src/opentelemetry/context/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index c804a7a2314..7ccb2f10a1f 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -78,7 +78,7 @@ def create_key(keyname: str) -> str: Returns: A unique string representing the newly created key. """ - return str(uuid.uuid4()) + return keyname + '-' + str(uuid.uuid4()) def get_value(key: str, context: typing.Optional[Context] = None) -> "object": From 065a3c571187ee19f2934b6ae5039cb18d5da9d2 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Mon, 17 May 2021 09:29:52 -0700 Subject: [PATCH 12/32] Update CHANGELOG --- CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bbd6aca7f3f..347776452b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## [Unreleased](https://github.com/open-telemetry/opentelemetry-python/compare/v1.2.0-0.21b0...HEAD) +- Added support for CreateKey functionality. + ([#1853](https://github.com/open-telemetry/opentelemetry-python/pull/1853)) ### Changed - Updated get_tracer to return an empty string when passed an invalid name @@ -21,8 +23,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ([#1823](https://github.com/open-telemetry/opentelemetry-python/pull/1823)) - Added support for OTEL_SERVICE_NAME. ([#1829](https://github.com/open-telemetry/opentelemetry-python/pull/1829)) -- Added support for CreateKey functionality. - (TDB) ### Changed - Fixed OTLP gRPC exporter silently failing if scheme is not specified in endpoint. From 669544b9b88ab830cf9fac314d6e212399c59936 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Mon, 17 May 2021 10:35:42 -0700 Subject: [PATCH 13/32] Update set_value inputs to key --- .../src/opentelemetry/context/__init__.py | 4 ++-- .../src/opentelemetry/sdk/trace/export/__init__.py | 13 ++++++++++--- .../shim/opentracing_shim/__init__.py | 14 +++++++++++--- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 7ccb2f10a1f..a8dd39c53ab 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -74,11 +74,11 @@ def create_key(keyname: str) -> str: the RuntimeContext API provides a function which takes a keyname as input, and returns a unique key. Args: - keyname: The key name is for debugging purposes and is not unique. + keyname: The key name is for debugging purposes and is not required to be unique. Returns: A unique string representing the newly created key. """ - return keyname + '-' + str(uuid.uuid4()) + return keyname + "-" + str(uuid.uuid4()) def get_value(key: str, context: typing.Optional[Context] = None) -> "object": diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index 457a1ef56ca..d5638305c37 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -21,7 +21,13 @@ from os import environ, linesep from typing import Optional -from opentelemetry.context import Context, attach, detach, set_value +from opentelemetry.context import ( + Context, + attach, + create_key, + detach, + set_value, +) from opentelemetry.sdk.environment_variables import ( OTEL_BSP_EXPORT_TIMEOUT, OTEL_BSP_MAX_EXPORT_BATCH_SIZE, @@ -32,6 +38,7 @@ from opentelemetry.util._time import _time_ns logger = logging.getLogger(__name__) +EXPORT_KEY = create_key("suppress_instrumentation") class SpanExportResult(Enum): @@ -86,7 +93,7 @@ def on_start( def on_end(self, span: ReadableSpan) -> None: if not span.context.trace_flags.sampled: return - token = attach(set_value("suppress_instrumentation", True)) + token = attach(set_value(EXPORT_KEY, True)) try: self.span_exporter.export((span,)) # pylint: disable=broad-except @@ -326,7 +333,7 @@ def _export_batch(self) -> int: while idx < self.max_export_batch_size and self.queue: self.spans_list[idx] = self.queue.pop() idx += 1 - token = attach(set_value("suppress_instrumentation", True)) + token = attach(set_value(EXPORT_KEY, True)) try: # Ignore type b/c the Optional[None]+slicing is too "clever" # for mypy diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 4cddf2a57ae..55e758083f2 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -100,7 +100,14 @@ ) from opentelemetry.baggage import get_baggage, set_baggage -from opentelemetry.context import Context, attach, detach, get_value, set_value +from opentelemetry.context import ( + Context, + attach, + create_key, + detach, + get_value, + set_value, +) from opentelemetry.propagate import get_global_textmap from opentelemetry.shim.opentracing_shim import util from opentelemetry.shim.opentracing_shim.version import __version__ @@ -117,6 +124,7 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) +SHIM_KEY = create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -348,7 +356,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value("scope_shim", self)) + self._token = attach(set_value(SHIM_KEY, self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. @@ -477,7 +485,7 @@ def active(self) -> "ScopeShim": return None try: - return get_value("scope_shim") + return get_value(SHIM_KEY) except KeyError: span_context = SpanContextShim(span.get_span_context()) wrapped_span = SpanShim(self._tracer, span_context, span) From 43b60d327813e51696b266fa95c19a7b552a7dd4 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Mon, 17 May 2021 12:22:22 -0700 Subject: [PATCH 14/32] Fix lint errors --- propagator/opentelemetry-propagator-b3/tests/test_b3_format.py | 2 +- .../tests/test_jaeger_propagator.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index 83bd47678ca..3ac97851ba7 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -19,7 +19,7 @@ import opentelemetry.sdk.trace as trace import opentelemetry.sdk.trace.id_generator as id_generator import opentelemetry.trace as trace_api -from opentelemetry.context import Context, create_key, get_current +from opentelemetry.context import Context, get_current from opentelemetry.propagators.b3 import ( # pylint: disable=no-name-in-module,import-error B3MultiFormat, B3SingleFormat, diff --git a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py index d884e6c3b9c..90265584700 100644 --- a/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py +++ b/propagator/opentelemetry-propagator-jaeger/tests/test_jaeger_propagator.py @@ -20,7 +20,7 @@ import opentelemetry.trace as trace_api from opentelemetry import baggage from opentelemetry.baggage import _BAGGAGE_KEY -from opentelemetry.context import Context, create_key +from opentelemetry.context import Context from opentelemetry.propagators import ( # pylint: disable=no-name-in-module jaeger, ) From 5ceae8ea3797fd1374d080c85035b862b3611562 Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 21 May 2021 15:45:14 -0700 Subject: [PATCH 15/32] Update contrib repo git SHA --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 33413f32136..013c60e1cb5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: c46b39e4e94af5ae8f97e684960f1aec0595c33e + CONTRIB_REPO_SHA: a6caae01648f09ef5c536c52e739a166e812f838 jobs: build: From df7c79c6c40675386b350369ed096b82b359d87a Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Fri, 21 May 2021 15:51:26 -0700 Subject: [PATCH 16/32] Update contrib repo SHA --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 33413f32136..013c60e1cb5 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: c46b39e4e94af5ae8f97e684960f1aec0595c33e + CONTRIB_REPO_SHA: a6caae01648f09ef5c536c52e739a166e812f838 jobs: build: From daa0de2cf3f2ca2d1037b52013d08227443aa2ce Mon Sep 17 00:00:00 2001 From: Eunice Kim Date: Tue, 25 May 2021 11:05:22 -0700 Subject: [PATCH 17/32] Rename export key to suppress instrumentation key --- .../src/opentelemetry/sdk/trace/export/__init__.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index d5638305c37..e0a273cd828 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -38,7 +38,7 @@ from opentelemetry.util._time import _time_ns logger = logging.getLogger(__name__) -EXPORT_KEY = create_key("suppress_instrumentation") +_SUPPRESS_INSTRUMENTATION_KEY = create_key("suppress_instrumentation") class SpanExportResult(Enum): @@ -93,7 +93,7 @@ def on_start( def on_end(self, span: ReadableSpan) -> None: if not span.context.trace_flags.sampled: return - token = attach(set_value(EXPORT_KEY, True)) + token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) try: self.span_exporter.export((span,)) # pylint: disable=broad-except @@ -333,7 +333,7 @@ def _export_batch(self) -> int: while idx < self.max_export_batch_size and self.queue: self.spans_list[idx] = self.queue.pop() idx += 1 - token = attach(set_value(EXPORT_KEY, True)) + token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True)) try: # Ignore type b/c the Optional[None]+slicing is too "clever" # for mypy From 3a1956f9f214e84fb5dbffb803384f7137346a5b Mon Sep 17 00:00:00 2001 From: eunice98k Date: Mon, 31 May 2021 00:38:40 -0700 Subject: [PATCH 18/32] Move suppress key to instrumentation package --- .github/workflows/test.yml | 2 +- .../src/opentelemetry/sdk/trace/export/__init__.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index d5dbffef6be..13f1cd8cf88 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: a6caae01648f09ef5c536c52e739a166e812f838 + CONTRIB_REPO_SHA: b4c0f0eb281c0b04bd7a6d7244fe741642b356fa jobs: build: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index e0a273cd828..9b37ed67937 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -28,6 +28,7 @@ detach, set_value, ) +from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY from opentelemetry.sdk.environment_variables import ( OTEL_BSP_EXPORT_TIMEOUT, OTEL_BSP_MAX_EXPORT_BATCH_SIZE, @@ -38,7 +39,6 @@ from opentelemetry.util._time import _time_ns logger = logging.getLogger(__name__) -_SUPPRESS_INSTRUMENTATION_KEY = create_key("suppress_instrumentation") class SpanExportResult(Enum): From 88c430e8e362ae28087a5e60b9a1cee81c6ae863 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Mon, 31 May 2021 16:01:21 -0700 Subject: [PATCH 19/32] Update SHA and fix lint error --- .github/workflows/test.yml | 2 +- .../src/opentelemetry/sdk/trace/export/__init__.py | 8 +------- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 13f1cd8cf88..235e617d3f1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: b4c0f0eb281c0b04bd7a6d7244fe741642b356fa + CONTRIB_REPO_SHA: 212dcb96a6c34f1f58cfe39b681506d6e85bf178 jobs: build: diff --git a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py index 9b37ed67937..d5d84df045a 100644 --- a/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py +++ b/opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py @@ -21,13 +21,7 @@ from os import environ, linesep from typing import Optional -from opentelemetry.context import ( - Context, - attach, - create_key, - detach, - set_value, -) +from opentelemetry.context import Context, attach, detach, set_value from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY from opentelemetry.sdk.environment_variables import ( OTEL_BSP_EXPORT_TIMEOUT, From c9df01f32aeca9276c1adb4dab5f4bb94c29996c Mon Sep 17 00:00:00 2001 From: eunice98k Date: Mon, 31 May 2021 17:06:41 -0700 Subject: [PATCH 20/32] Update tox.ini --- tox.ini | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/tox.ini b/tox.ini index 635c3c2df92..ebe347958c4 100644 --- a/tox.ini +++ b/tox.ini @@ -120,6 +120,9 @@ commands_pre = ; cases but it saves a lot of boilerplate in this file. test: pip install {toxinidir}/opentelemetry-api {toxinidir}/opentelemetry-semantic-conventions {toxinidir}/opentelemetry-sdk {toxinidir}/tests/util + test-core-sdk: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation + test-core-opentracing-shim: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation + test-core-proto: pip install {toxinidir}/opentelemetry-proto distro: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation {toxinidir}/opentelemetry-distro @@ -130,13 +133,18 @@ commands_pre = exporter-otlp-combined: pip install {toxinidir}/opentelemetry-proto exporter-otlp-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-grpc exporter-otlp-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-otlp + exporter-otlp-combined: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-otlp-proto-grpc: pip install {toxinidir}/opentelemetry-proto exporter-otlp-proto-grpc: pip install {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-grpc + exporter-otlp-proto-grpc: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-jaeger-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-jaeger-proto-grpc {toxinidir}/exporter/opentelemetry-exporter-jaeger-thrift {toxinidir}/exporter/opentelemetry-exporter-jaeger + exporter-jaeger-combined: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-jaeger-proto-grpc: pip install {toxinidir}/exporter/opentelemetry-exporter-jaeger-proto-grpc + exporter-jaeger-proto-grpc: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-jaeger-thrift: pip install {toxinidir}/exporter/opentelemetry-exporter-jaeger-thrift + exporter-jaeger-thrift: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation opentracing-shim: pip install {toxinidir}/opentelemetry-sdk opentracing-shim: pip install {toxinidir}/shim/opentelemetry-opentracing-shim @@ -144,11 +152,14 @@ commands_pre = exporter-zipkin-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-json exporter-zipkin-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-proto-http exporter-zipkin-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin + exporter-zipkin-combined: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-zipkin-proto-http: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-json exporter-zipkin-proto-http: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-proto-http + exporter-zipkin-proto-http: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-zipkin-json: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-json + exporter-zipkin-json: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation b3: pip install {toxinidir}/propagator/opentelemetry-propagator-b3 From f0d5437d75d0108a6c4ed89483dfed3517cf54b9 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Mon, 31 May 2021 17:12:50 -0700 Subject: [PATCH 21/32] Update tox.ini for opencensus --- tox.ini | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tox.ini b/tox.ini index ebe347958c4..b09830f5635 100644 --- a/tox.ini +++ b/tox.ini @@ -128,7 +128,7 @@ commands_pre = getting-started: pip install -e {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation -e {toxinidir}/opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-requests {toxinidir}/opentelemetry-python-contrib/util/opentelemetry-util-http -e {toxinidir}/opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-wsgi -e {toxinidir}/opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-flask - opencensus: pip install {toxinidir}/exporter/opentelemetry-exporter-opencensus + opencensus: pip install {toxinidir}/exporter/opentelemetry-exporter-opencensus {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-otlp-combined: pip install {toxinidir}/opentelemetry-proto exporter-otlp-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-grpc From a14315b1f44e13eb2d8bab86db52ee51be04cb9c Mon Sep 17 00:00:00 2001 From: eunice98k Date: Wed, 2 Jun 2021 22:19:46 -0700 Subject: [PATCH 22/32] Add opentelemtry-instrumentation to sdk setup.cfg --- .github/workflows/test.yml | 2 +- opentelemetry-sdk/setup.cfg | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 235e617d3f1..a6b95a0f691 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: 212dcb96a6c34f1f58cfe39b681506d6e85bf178 + CONTRIB_REPO_SHA: 7f6b89337ca60acd6db44f12cdf4c33e9866c6b3 jobs: build: diff --git a/opentelemetry-sdk/setup.cfg b/opentelemetry-sdk/setup.cfg index 6c258c9796a..3df5e31ed4d 100644 --- a/opentelemetry-sdk/setup.cfg +++ b/opentelemetry-sdk/setup.cfg @@ -44,6 +44,7 @@ include_package_data = True install_requires = opentelemetry-api == 1.4.0.dev0 opentelemetry-semantic-conventions == 0.23.dev0 + opentelemetry-instrumentation == 0.23.dev0 [options.packages.find] where = src From 061f5910aeb3c617eb941a424926b6977fd9b0e1 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Sat, 5 Jun 2021 17:51:06 -0700 Subject: [PATCH 23/32] Fix tox.ini --- tox.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index b09830f5635..bc3e1833bb8 100644 --- a/tox.ini +++ b/tox.ini @@ -118,7 +118,7 @@ commands_pre = py3{6,7,8,9}: python -m pip install -U pip setuptools wheel ; Install common packages for all the tests. These are not needed in all the ; cases but it saves a lot of boilerplate in this file. - test: pip install {toxinidir}/opentelemetry-api {toxinidir}/opentelemetry-semantic-conventions {toxinidir}/opentelemetry-sdk {toxinidir}/tests/util + test: pip install {toxinidir}/opentelemetry-api {toxinidir}/opentelemetry-semantic-conventions {toxinidir}/opentelemetry-sdk {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation {toxinidir}/tests/util test-core-sdk: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation test-core-opentracing-shim: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation @@ -203,6 +203,7 @@ deps = commands_pre = python -m pip install -e {toxinidir}/opentelemetry-api[test] python -m pip install -e {toxinidir}/opentelemetry-semantic-conventions[test] + python -m pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation python -m pip install -e {toxinidir}/opentelemetry-sdk[test] python -m pip install -e {toxinidir}/opentelemetry-proto[test] python -m pip install -e {toxinidir}/tests/util[test] @@ -218,7 +219,6 @@ commands_pre = python -m pip install -e {toxinidir}/exporter/opentelemetry-exporter-zipkin[test] python -m pip install -e {toxinidir}/propagator/opentelemetry-propagator-b3[test] python -m pip install -e {toxinidir}/propagator/opentelemetry-propagator-jaeger[test] - python -m pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation python -m pip install -e {toxinidir}/opentelemetry-distro[test] commands = From 8e2d3a3ac9f73617b391ee8c391fcaacb8ceb892 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Sat, 5 Jun 2021 22:08:48 -0700 Subject: [PATCH 24/32] Change instrumentation order in tox.ini --- tox.ini | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tox.ini b/tox.ini index bc3e1833bb8..e7914b6303f 100644 --- a/tox.ini +++ b/tox.ini @@ -118,7 +118,7 @@ commands_pre = py3{6,7,8,9}: python -m pip install -U pip setuptools wheel ; Install common packages for all the tests. These are not needed in all the ; cases but it saves a lot of boilerplate in this file. - test: pip install {toxinidir}/opentelemetry-api {toxinidir}/opentelemetry-semantic-conventions {toxinidir}/opentelemetry-sdk {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation {toxinidir}/tests/util + test: pip install {toxinidir}/opentelemetry-api {toxinidir}/opentelemetry-semantic-conventions {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation {toxinidir}/opentelemetry-sdk {toxinidir}/tests/util test-core-sdk: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation test-core-opentracing-shim: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation @@ -250,8 +250,8 @@ deps = commands_pre = pip install -e {toxinidir}/opentelemetry-api \ -e {toxinidir}/opentelemetry-semantic-conventions \ - -e {toxinidir}/opentelemetry-sdk \ -e {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation \ + -e {toxinidir}/opentelemetry-sdk \ -e {toxinidir}/opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-requests \ -e {toxinidir}/opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-wsgi \ -e {toxinidir}/opentelemetry-python-contrib/util/opentelemetry-util-http From c4cdffd0c8bd47b2e5c4f4a823722ca514f10db3 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Sat, 5 Jun 2021 22:28:43 -0700 Subject: [PATCH 25/32] Add instrumentation to docs-requirements --- docs-requirements.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/docs-requirements.txt b/docs-requirements.txt index f60325f00b1..03a3ebcda6c 100644 --- a/docs-requirements.txt +++ b/docs-requirements.txt @@ -8,6 +8,7 @@ sphinx-jekyll-builder # doesn't work for pkg_resources. ./opentelemetry-api ./opentelemetry-semantic-conventions +./opentelemetry-python-contrib/opentelemetry-instrumentation ./opentelemetry-sdk # Required by instrumentation and exporter packages From 268f2683ab283565ffde44a174ed6020e8e32b99 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Sun, 6 Jun 2021 00:36:34 -0700 Subject: [PATCH 26/32] Update SHA --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a6b95a0f691..b974777387e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: 7f6b89337ca60acd6db44f12cdf4c33e9866c6b3 + CONTRIB_REPO_SHA: 73c67be0741b49fcd49ab8544dd6614b5dd813f4 jobs: build: From 38ca88e9f58104d70cd36b188b75a49866910b22 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Mon, 7 Jun 2021 11:46:15 -0700 Subject: [PATCH 27/32] Fix public API errors --- docs-requirements.txt | 1 + .../src/opentelemetry/baggage/__init__.py | 4 ++-- .../src/opentelemetry/context/__init__.py | 2 +- .../src/opentelemetry/trace/__init__.py | 4 ++-- .../src/opentelemetry/trace/propagation/__init__.py | 8 ++++---- opentelemetry-api/tests/context/test_context.py | 12 ++++++------ .../tests/test_b3_format.py | 6 +++--- .../opentelemetry/shim/opentracing_shim/__init__.py | 8 ++++---- 8 files changed, 23 insertions(+), 22 deletions(-) diff --git a/docs-requirements.txt b/docs-requirements.txt index 03a3ebcda6c..5cf8d396adc 100644 --- a/docs-requirements.txt +++ b/docs-requirements.txt @@ -14,6 +14,7 @@ sphinx-jekyll-builder # Required by instrumentation and exporter packages asgiref~=3.0 asyncpg>=0.12.0 +requests ~= 2.0 ddtrace>=0.34.0 aiohttp~= 3.0 aiopg>=0.13.0 diff --git a/opentelemetry-api/src/opentelemetry/baggage/__init__.py b/opentelemetry-api/src/opentelemetry/baggage/__init__.py index 2368c5a325f..ae2e01a18c9 100644 --- a/opentelemetry-api/src/opentelemetry/baggage/__init__.py +++ b/opentelemetry-api/src/opentelemetry/baggage/__init__.py @@ -15,10 +15,10 @@ import typing from types import MappingProxyType -from opentelemetry.context import create_key, get_value, set_value +from opentelemetry.context import _create_key, get_value, set_value from opentelemetry.context.context import Context -_BAGGAGE_KEY = create_key("baggage") +_BAGGAGE_KEY = _create_key("baggage") def get_all( diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index a8dd39c53ab..0aa938bc550 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -69,7 +69,7 @@ def wrapper( # type: ignore[misc] return typing.cast(_F, wrapper) # type: ignore[misc] -def create_key(keyname: str) -> str: +def _create_key(keyname: str) -> str: """To allow cross-cutting concern to control access to their local state, the RuntimeContext API provides a function which takes a keyname as input, and returns a unique key. diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index cae50a5b472..e0aa9e32d71 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -85,7 +85,7 @@ from opentelemetry.context.context import Context from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER from opentelemetry.trace.propagation import ( - SPAN_KEY, + _SPAN_KEY, get_current_span, set_span_in_context, ) @@ -517,7 +517,7 @@ def use_span( this mechanism if it was previously set manually. """ try: - token = context_api.attach(context_api.set_value(SPAN_KEY, span)) + token = context_api.attach(context_api.set_value(_SPAN_KEY, span)) try: yield span finally: diff --git a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py index ef241f6c427..5062b9167f8 100644 --- a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py @@ -13,11 +13,11 @@ # limitations under the License. from typing import Optional -from opentelemetry.context import create_key, get_value, set_value +from opentelemetry.context import _create_key, get_value, set_value from opentelemetry.context.context import Context from opentelemetry.trace.span import INVALID_SPAN, Span -SPAN_KEY = create_key("current-span") +_SPAN_KEY = _create_key("current-span") def set_span_in_context( @@ -30,7 +30,7 @@ def set_span_in_context( context: a Context object. if one is not passed, the default current context is used instead. """ - ctx = set_value(SPAN_KEY, span, context=context) + ctx = set_value(_SPAN_KEY, span, context=context) return ctx @@ -44,7 +44,7 @@ def get_current_span(context: Optional[Context] = None) -> Span: Returns: The Span set in the context if it exists. INVALID_SPAN otherwise. """ - span = get_value(SPAN_KEY, context=context) + span = get_value(_SPAN_KEY, context=context) if span is None or not isinstance(span, Span): return INVALID_SPAN return span diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py index 61c887326ec..f6fd2ee0341 100644 --- a/opentelemetry-api/tests/context/test_context.py +++ b/opentelemetry-api/tests/context/test_context.py @@ -18,8 +18,8 @@ from opentelemetry.context.context import Context -def do_work() -> str: - key = context.create_key("say") +def _do_work() -> str: + key = context._create_key("say") context.attach(context.set_value(key, "bar")) return key @@ -29,8 +29,8 @@ def setUp(self): context.attach(Context()) def test_context_key(self): - key1 = context.create_key("say") - key2 = context.create_key("say") + key1 = context._create_key("say") + key2 = context._create_key("say") self.assertNotEqual(key1, key2) first = context.set_value(key1, "foo") second = context.set_value(key2, "bar") @@ -38,13 +38,13 @@ def test_context_key(self): self.assertEqual(context.get_value(key2, context=second), "bar") def test_context(self): - key1 = context.create_key("say") + key1 = context._create_key("say") self.assertIsNone(context.get_value(key1)) empty = context.get_current() second = context.set_value(key1, "foo") self.assertEqual(context.get_value(key1, context=second), "foo") - key2 = do_work() + key2 = _do_work() self.assertEqual(context.get_value(key2), "bar") third = context.get_current() diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index 3ac97851ba7..ebefebaf4fb 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -25,7 +25,7 @@ B3SingleFormat, ) from opentelemetry.propagators.textmap import DefaultGetter -from opentelemetry.trace.propagation import SPAN_KEY +from opentelemetry.trace.propagation import _SPAN_KEY def get_child_parent_new_carrier(old_carrier, propagator): @@ -248,7 +248,7 @@ def test_derived_ctx_is_returned_for_success(self): }, old_ctx, ) - self.assertIn(SPAN_KEY, new_ctx) + self.assertIn(_SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object @@ -258,7 +258,7 @@ def test_derived_ctx_is_returned_for_failure(self): """Ensure returned context is derived from the given context.""" old_ctx = Context({"k2": "v2"}) new_ctx = self.get_propagator().extract({}, old_ctx) - self.assertNotIn(SPAN_KEY, new_ctx) + self.assertNotIn(_SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 55e758083f2..ffd487bffe1 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -103,7 +103,7 @@ from opentelemetry.context import ( Context, attach, - create_key, + _create_key, detach, get_value, set_value, @@ -124,7 +124,7 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) -SHIM_KEY = create_key("scope_shim") +_SHIM_KEY = _create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -356,7 +356,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value(SHIM_KEY, self)) + self._token = attach(set_value(_SHIM_KEY, self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. @@ -485,7 +485,7 @@ def active(self) -> "ScopeShim": return None try: - return get_value(SHIM_KEY) + return get_value(_SHIM_KEY) except KeyError: span_context = SpanContextShim(span.get_span_context()) wrapped_span = SpanShim(self._tracer, span_context, span) From 4cf1bf55ab7d2d79e57791f2ca00c539998c82f7 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Mon, 7 Jun 2021 12:18:52 -0700 Subject: [PATCH 28/32] Update contrib sha and tox.ini --- .github/workflows/test.yml | 2 +- docs-requirements.txt | 1 - tox.ini | 8 +------- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index b974777387e..a25d2d9446d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: 73c67be0741b49fcd49ab8544dd6614b5dd813f4 + CONTRIB_REPO_SHA: ef05e54a3c188b9d313f74ec2196ba3337e6cbed jobs: build: diff --git a/docs-requirements.txt b/docs-requirements.txt index 5cf8d396adc..03a3ebcda6c 100644 --- a/docs-requirements.txt +++ b/docs-requirements.txt @@ -14,7 +14,6 @@ sphinx-jekyll-builder # Required by instrumentation and exporter packages asgiref~=3.0 asyncpg>=0.12.0 -requests ~= 2.0 ddtrace>=0.34.0 aiohttp~= 3.0 aiopg>=0.13.0 diff --git a/tox.ini b/tox.ini index 6963350a9b2..49b62e1e274 100644 --- a/tox.ini +++ b/tox.ini @@ -128,23 +128,20 @@ commands_pre = getting-started: pip install requests flask -e {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation -e {toxinidir}/opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-requests {toxinidir}/opentelemetry-python-contrib/util/opentelemetry-util-http -e {toxinidir}/opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-wsgi -e {toxinidir}/opentelemetry-python-contrib/instrumentation/opentelemetry-instrumentation-flask - opencensus: pip install {toxinidir}/exporter/opentelemetry-exporter-opencensus {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation + opencensus: pip install {toxinidir}/exporter/opentelemetry-exporter-opencensus exporter-otlp-combined: pip install {toxinidir}/opentelemetry-proto exporter-otlp-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-grpc exporter-otlp-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-otlp - exporter-otlp-combined: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-otlp-proto-grpc: pip install {toxinidir}/opentelemetry-proto exporter-otlp-proto-grpc: pip install {toxinidir}/exporter/opentelemetry-exporter-otlp-proto-grpc - exporter-otlp-proto-grpc: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-jaeger-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-jaeger-proto-grpc {toxinidir}/exporter/opentelemetry-exporter-jaeger-thrift {toxinidir}/exporter/opentelemetry-exporter-jaeger exporter-jaeger-combined: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-jaeger-proto-grpc: pip install {toxinidir}/exporter/opentelemetry-exporter-jaeger-proto-grpc exporter-jaeger-proto-grpc: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-jaeger-thrift: pip install {toxinidir}/exporter/opentelemetry-exporter-jaeger-thrift - exporter-jaeger-thrift: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation opentracing-shim: pip install {toxinidir}/opentelemetry-sdk opentracing-shim: pip install {toxinidir}/shim/opentelemetry-opentracing-shim @@ -152,14 +149,11 @@ commands_pre = exporter-zipkin-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-json exporter-zipkin-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-proto-http exporter-zipkin-combined: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin - exporter-zipkin-combined: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-zipkin-proto-http: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-json exporter-zipkin-proto-http: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-proto-http - exporter-zipkin-proto-http: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation exporter-zipkin-json: pip install {toxinidir}/exporter/opentelemetry-exporter-zipkin-json - exporter-zipkin-json: pip install {toxinidir}/opentelemetry-python-contrib/opentelemetry-instrumentation b3: pip install {toxinidir}/propagator/opentelemetry-propagator-b3 From ba15cdc46f946f075d1be67be5694fcea73e0838 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Mon, 7 Jun 2021 13:46:44 -0700 Subject: [PATCH 29/32] Change context import order --- .../src/opentelemetry/shim/opentracing_shim/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index ffd487bffe1..7ac163ade7b 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -102,8 +102,8 @@ from opentelemetry.baggage import get_baggage, set_baggage from opentelemetry.context import ( Context, - attach, _create_key, + attach, detach, get_value, set_value, From 092e3418cfcdbee5a70fd2f846462ff6481a700b Mon Sep 17 00:00:00 2001 From: eunice98k Date: Thu, 10 Jun 2021 10:44:36 -0700 Subject: [PATCH 30/32] Fix lint errors --- opentelemetry-api/tests/context/test_context.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py index f6fd2ee0341..89c4bc136ff 100644 --- a/opentelemetry-api/tests/context/test_context.py +++ b/opentelemetry-api/tests/context/test_context.py @@ -19,6 +19,7 @@ def _do_work() -> str: + # pylint: disable=protected-access key = context._create_key("say") context.attach(context.set_value(key, "bar")) return key @@ -29,6 +30,7 @@ def setUp(self): context.attach(Context()) def test_context_key(self): + # pylint: disable=protected-access key1 = context._create_key("say") key2 = context._create_key("say") self.assertNotEqual(key1, key2) @@ -38,6 +40,7 @@ def test_context_key(self): self.assertEqual(context.get_value(key2, context=second), "bar") def test_context(self): + # pylint: disable=protected-access key1 = context._create_key("say") self.assertIsNone(context.get_value(key1)) empty = context.get_current() From eaff5b2a276da9e600d15d826fed9d90005f8ce4 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Fri, 11 Jun 2021 00:13:02 -0700 Subject: [PATCH 31/32] Revert public API errors --- .github/workflows/test.yml | 2 +- .../src/opentelemetry/baggage/__init__.py | 4 ++-- .../src/opentelemetry/context/__init__.py | 2 +- opentelemetry-api/src/opentelemetry/trace/__init__.py | 4 ++-- .../src/opentelemetry/trace/propagation/__init__.py | 8 ++++---- opentelemetry-api/tests/context/test_context.py | 11 ++++------- .../tests/test_b3_format.py | 6 +++--- .../opentelemetry/shim/opentracing_shim/__init__.py | 8 ++++---- 8 files changed, 21 insertions(+), 24 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a25d2d9446d..c88b7313327 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -10,7 +10,7 @@ env: # Otherwise, set variable to the commit of your branch on # opentelemetry-python-contrib which is compatible with these Core repo # changes. - CONTRIB_REPO_SHA: ef05e54a3c188b9d313f74ec2196ba3337e6cbed + CONTRIB_REPO_SHA: 82c4f600872a72fedaebad758f0d5588dfb53a00 jobs: build: diff --git a/opentelemetry-api/src/opentelemetry/baggage/__init__.py b/opentelemetry-api/src/opentelemetry/baggage/__init__.py index ae2e01a18c9..2368c5a325f 100644 --- a/opentelemetry-api/src/opentelemetry/baggage/__init__.py +++ b/opentelemetry-api/src/opentelemetry/baggage/__init__.py @@ -15,10 +15,10 @@ import typing from types import MappingProxyType -from opentelemetry.context import _create_key, get_value, set_value +from opentelemetry.context import create_key, get_value, set_value from opentelemetry.context.context import Context -_BAGGAGE_KEY = _create_key("baggage") +_BAGGAGE_KEY = create_key("baggage") def get_all( diff --git a/opentelemetry-api/src/opentelemetry/context/__init__.py b/opentelemetry-api/src/opentelemetry/context/__init__.py index 0aa938bc550..a8dd39c53ab 100644 --- a/opentelemetry-api/src/opentelemetry/context/__init__.py +++ b/opentelemetry-api/src/opentelemetry/context/__init__.py @@ -69,7 +69,7 @@ def wrapper( # type: ignore[misc] return typing.cast(_F, wrapper) # type: ignore[misc] -def _create_key(keyname: str) -> str: +def create_key(keyname: str) -> str: """To allow cross-cutting concern to control access to their local state, the RuntimeContext API provides a function which takes a keyname as input, and returns a unique key. diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index e0aa9e32d71..cae50a5b472 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -85,7 +85,7 @@ from opentelemetry.context.context import Context from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER from opentelemetry.trace.propagation import ( - _SPAN_KEY, + SPAN_KEY, get_current_span, set_span_in_context, ) @@ -517,7 +517,7 @@ def use_span( this mechanism if it was previously set manually. """ try: - token = context_api.attach(context_api.set_value(_SPAN_KEY, span)) + token = context_api.attach(context_api.set_value(SPAN_KEY, span)) try: yield span finally: diff --git a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py index 5062b9167f8..ef241f6c427 100644 --- a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py @@ -13,11 +13,11 @@ # limitations under the License. from typing import Optional -from opentelemetry.context import _create_key, get_value, set_value +from opentelemetry.context import create_key, get_value, set_value from opentelemetry.context.context import Context from opentelemetry.trace.span import INVALID_SPAN, Span -_SPAN_KEY = _create_key("current-span") +SPAN_KEY = create_key("current-span") def set_span_in_context( @@ -30,7 +30,7 @@ def set_span_in_context( context: a Context object. if one is not passed, the default current context is used instead. """ - ctx = set_value(_SPAN_KEY, span, context=context) + ctx = set_value(SPAN_KEY, span, context=context) return ctx @@ -44,7 +44,7 @@ def get_current_span(context: Optional[Context] = None) -> Span: Returns: The Span set in the context if it exists. INVALID_SPAN otherwise. """ - span = get_value(_SPAN_KEY, context=context) + span = get_value(SPAN_KEY, context=context) if span is None or not isinstance(span, Span): return INVALID_SPAN return span diff --git a/opentelemetry-api/tests/context/test_context.py b/opentelemetry-api/tests/context/test_context.py index 89c4bc136ff..b90c011b992 100644 --- a/opentelemetry-api/tests/context/test_context.py +++ b/opentelemetry-api/tests/context/test_context.py @@ -19,8 +19,7 @@ def _do_work() -> str: - # pylint: disable=protected-access - key = context._create_key("say") + key = context.create_key("say") context.attach(context.set_value(key, "bar")) return key @@ -30,9 +29,8 @@ def setUp(self): context.attach(Context()) def test_context_key(self): - # pylint: disable=protected-access - key1 = context._create_key("say") - key2 = context._create_key("say") + key1 = context.create_key("say") + key2 = context.create_key("say") self.assertNotEqual(key1, key2) first = context.set_value(key1, "foo") second = context.set_value(key2, "bar") @@ -40,8 +38,7 @@ def test_context_key(self): self.assertEqual(context.get_value(key2, context=second), "bar") def test_context(self): - # pylint: disable=protected-access - key1 = context._create_key("say") + key1 = context.create_key("say") self.assertIsNone(context.get_value(key1)) empty = context.get_current() second = context.set_value(key1, "foo") diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index ebefebaf4fb..3ac97851ba7 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -25,7 +25,7 @@ B3SingleFormat, ) from opentelemetry.propagators.textmap import DefaultGetter -from opentelemetry.trace.propagation import _SPAN_KEY +from opentelemetry.trace.propagation import SPAN_KEY def get_child_parent_new_carrier(old_carrier, propagator): @@ -248,7 +248,7 @@ def test_derived_ctx_is_returned_for_success(self): }, old_ctx, ) - self.assertIn(_SPAN_KEY, new_ctx) + self.assertIn(SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object @@ -258,7 +258,7 @@ def test_derived_ctx_is_returned_for_failure(self): """Ensure returned context is derived from the given context.""" old_ctx = Context({"k2": "v2"}) new_ctx = self.get_propagator().extract({}, old_ctx) - self.assertNotIn(_SPAN_KEY, new_ctx) + self.assertNotIn(SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 7ac163ade7b..55e758083f2 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -102,8 +102,8 @@ from opentelemetry.baggage import get_baggage, set_baggage from opentelemetry.context import ( Context, - _create_key, attach, + create_key, detach, get_value, set_value, @@ -124,7 +124,7 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) -_SHIM_KEY = _create_key("scope_shim") +SHIM_KEY = create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -356,7 +356,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value(_SHIM_KEY, self)) + self._token = attach(set_value(SHIM_KEY, self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. @@ -485,7 +485,7 @@ def active(self) -> "ScopeShim": return None try: - return get_value(_SHIM_KEY) + return get_value(SHIM_KEY) except KeyError: span_context = SpanContextShim(span.get_span_context()) wrapped_span = SpanShim(self._tracer, span_context, span) From 4ed8eea617b7fd09fe052dba29cb71f052450962 Mon Sep 17 00:00:00 2001 From: eunice98k Date: Fri, 11 Jun 2021 09:26:14 -0700 Subject: [PATCH 32/32] Fix span_key and shim_key --- opentelemetry-api/src/opentelemetry/trace/__init__.py | 4 ++-- .../src/opentelemetry/trace/propagation/__init__.py | 7 ++++--- .../opentelemetry-propagator-b3/tests/test_b3_format.py | 6 +++--- .../src/opentelemetry/shim/opentracing_shim/__init__.py | 6 +++--- 4 files changed, 12 insertions(+), 11 deletions(-) diff --git a/opentelemetry-api/src/opentelemetry/trace/__init__.py b/opentelemetry-api/src/opentelemetry/trace/__init__.py index cae50a5b472..e0aa9e32d71 100644 --- a/opentelemetry-api/src/opentelemetry/trace/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/__init__.py @@ -85,7 +85,7 @@ from opentelemetry.context.context import Context from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER from opentelemetry.trace.propagation import ( - SPAN_KEY, + _SPAN_KEY, get_current_span, set_span_in_context, ) @@ -517,7 +517,7 @@ def use_span( this mechanism if it was previously set manually. """ try: - token = context_api.attach(context_api.set_value(SPAN_KEY, span)) + token = context_api.attach(context_api.set_value(_SPAN_KEY, span)) try: yield span finally: diff --git a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py index ef241f6c427..d3529e1779e 100644 --- a/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py @@ -17,7 +17,8 @@ from opentelemetry.context.context import Context from opentelemetry.trace.span import INVALID_SPAN, Span -SPAN_KEY = create_key("current-span") +SPAN_KEY = "current-span" +_SPAN_KEY = create_key("current-span") def set_span_in_context( @@ -30,7 +31,7 @@ def set_span_in_context( context: a Context object. if one is not passed, the default current context is used instead. """ - ctx = set_value(SPAN_KEY, span, context=context) + ctx = set_value(_SPAN_KEY, span, context=context) return ctx @@ -44,7 +45,7 @@ def get_current_span(context: Optional[Context] = None) -> Span: Returns: The Span set in the context if it exists. INVALID_SPAN otherwise. """ - span = get_value(SPAN_KEY, context=context) + span = get_value(_SPAN_KEY, context=context) if span is None or not isinstance(span, Span): return INVALID_SPAN return span diff --git a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py index 3ac97851ba7..ebefebaf4fb 100644 --- a/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py +++ b/propagator/opentelemetry-propagator-b3/tests/test_b3_format.py @@ -25,7 +25,7 @@ B3SingleFormat, ) from opentelemetry.propagators.textmap import DefaultGetter -from opentelemetry.trace.propagation import SPAN_KEY +from opentelemetry.trace.propagation import _SPAN_KEY def get_child_parent_new_carrier(old_carrier, propagator): @@ -248,7 +248,7 @@ def test_derived_ctx_is_returned_for_success(self): }, old_ctx, ) - self.assertIn(SPAN_KEY, new_ctx) + self.assertIn(_SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object @@ -258,7 +258,7 @@ def test_derived_ctx_is_returned_for_failure(self): """Ensure returned context is derived from the given context.""" old_ctx = Context({"k2": "v2"}) new_ctx = self.get_propagator().extract({}, old_ctx) - self.assertNotIn(SPAN_KEY, new_ctx) + self.assertNotIn(_SPAN_KEY, new_ctx) for key, value in old_ctx.items(): # pylint:disable=no-member self.assertIn(key, new_ctx) # pylint:disable=unsubscriptable-object diff --git a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py index 6044569693d..5777bd0edc9 100644 --- a/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py +++ b/shim/opentelemetry-opentracing-shim/src/opentelemetry/shim/opentracing_shim/__init__.py @@ -125,7 +125,7 @@ ValueT = TypeVar("ValueT", int, float, bool, str) logger = logging.getLogger(__name__) -SHIM_KEY = create_key("scope_shim") +_SHIM_KEY = create_key("scope_shim") def create_tracer(otel_tracer_provider: TracerProvider) -> "TracerShim": @@ -357,7 +357,7 @@ def __init__( ): super().__init__(manager, span) self._span_cm = span_cm - self._token = attach(set_value(SHIM_KEY, self)) + self._token = attach(set_value(_SHIM_KEY, self)) # TODO: Change type of `manager` argument to `opentracing.ScopeManager`? We # need to get rid of `manager.tracer` for this. @@ -494,7 +494,7 @@ def active(self) -> "ScopeShim": return None try: - return get_value(SHIM_KEY) + return get_value(_SHIM_KEY) except KeyError: span_context = SpanContextShim(span.get_span_context()) wrapped_span = SpanShim(self._tracer, span_context, span)