Skip to content

Commit 540b8a5

Browse files
authored
Implement CreateKey Functionality (open-telemetry#1853)
1 parent 3bb6bdc commit 540b8a5

File tree

14 files changed

+79
-33
lines changed

14 files changed

+79
-33
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
# Otherwise, set variable to the commit of your branch on
1111
# opentelemetry-python-contrib which is compatible with these Core repo
1212
# changes.
13-
CONTRIB_REPO_SHA: 01bb63fb8cc429a17f1fb0c93f7ac72a3fc7b4fc
13+
CONTRIB_REPO_SHA: 82c4f600872a72fedaebad758f0d5588dfb53a00
1414

1515
jobs:
1616
build:

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1818
### Added
1919
- Allow span limits to be set programatically via TracerProvider.
2020
([#1877](https://github.com/open-telemetry/opentelemetry-python/pull/1877))
21+
- Added support for CreateKey functionality.
22+
([#1853](https://github.com/open-telemetry/opentelemetry-python/pull/1853))
2123

2224
### Changed
2325
- Updated get_tracer to return an empty string when passed an invalid name

docs-requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ sphinx-jekyll-builder
88
# doesn't work for pkg_resources.
99
./opentelemetry-api
1010
./opentelemetry-semantic-conventions
11+
./opentelemetry-python-contrib/opentelemetry-instrumentation
1112
./opentelemetry-sdk
1213

1314
# Required by instrumentation and exporter packages

opentelemetry-api/src/opentelemetry/baggage/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
import typing
1616
from types import MappingProxyType
1717

18-
from opentelemetry.context import get_value, set_value
18+
from opentelemetry.context import create_key, get_value, set_value
1919
from opentelemetry.context.context import Context
2020

21-
_BAGGAGE_KEY = "baggage"
21+
_BAGGAGE_KEY = create_key("baggage")
2222

2323

2424
def get_all(

opentelemetry-api/src/opentelemetry/context/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import logging
1616
import threading
1717
import typing
18+
import uuid
1819
from functools import wraps
1920
from os import environ
2021

@@ -68,6 +69,18 @@ def wrapper( # type: ignore[misc]
6869
return typing.cast(_F, wrapper) # type: ignore[misc]
6970

7071

72+
def create_key(keyname: str) -> str:
73+
"""To allow cross-cutting concern to control access to their local state,
74+
the RuntimeContext API provides a function which takes a keyname as input,
75+
and returns a unique key.
76+
Args:
77+
keyname: The key name is for debugging purposes and is not required to be unique.
78+
Returns:
79+
A unique string representing the newly created key.
80+
"""
81+
return keyname + "-" + str(uuid.uuid4())
82+
83+
7184
def get_value(key: str, context: typing.Optional[Context] = None) -> "object":
7285
"""To access the local state of a concern, the RuntimeContext API
7386
provides a function which takes a context and a key as input,

opentelemetry-api/src/opentelemetry/trace/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
from opentelemetry.context.context import Context
8686
from opentelemetry.environment_variables import OTEL_PYTHON_TRACER_PROVIDER
8787
from opentelemetry.trace.propagation import (
88-
SPAN_KEY,
88+
_SPAN_KEY,
8989
get_current_span,
9090
set_span_in_context,
9191
)
@@ -517,7 +517,7 @@ def use_span(
517517
this mechanism if it was previously set manually.
518518
"""
519519
try:
520-
token = context_api.attach(context_api.set_value(SPAN_KEY, span))
520+
token = context_api.attach(context_api.set_value(_SPAN_KEY, span))
521521
try:
522522
yield span
523523
finally:

opentelemetry-api/src/opentelemetry/trace/propagation/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313
# limitations under the License.
1414
from typing import Optional
1515

16-
from opentelemetry.context import get_value, set_value
16+
from opentelemetry.context import create_key, get_value, set_value
1717
from opentelemetry.context.context import Context
1818
from opentelemetry.trace.span import INVALID_SPAN, Span
1919

2020
SPAN_KEY = "current-span"
21+
_SPAN_KEY = create_key("current-span")
2122

2223

2324
def set_span_in_context(
@@ -30,7 +31,7 @@ def set_span_in_context(
3031
context: a Context object. if one is not passed, the
3132
default current context is used instead.
3233
"""
33-
ctx = set_value(SPAN_KEY, span, context=context)
34+
ctx = set_value(_SPAN_KEY, span, context=context)
3435
return ctx
3536

3637

@@ -44,7 +45,7 @@ def get_current_span(context: Optional[Context] = None) -> Span:
4445
Returns:
4546
The Span set in the context if it exists. INVALID_SPAN otherwise.
4647
"""
47-
span = get_value(SPAN_KEY, context=context)
48+
span = get_value(_SPAN_KEY, context=context)
4849
if span is None or not isinstance(span, Span):
4950
return INVALID_SPAN
5051
return span

opentelemetry-api/tests/context/test_context.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,39 @@
1818
from opentelemetry.context.context import Context
1919

2020

21-
def do_work() -> None:
22-
context.attach(context.set_value("say", "bar"))
21+
def _do_work() -> str:
22+
key = context.create_key("say")
23+
context.attach(context.set_value(key, "bar"))
24+
return key
2325

2426

2527
class TestContext(unittest.TestCase):
2628
def setUp(self):
2729
context.attach(Context())
2830

31+
def test_context_key(self):
32+
key1 = context.create_key("say")
33+
key2 = context.create_key("say")
34+
self.assertNotEqual(key1, key2)
35+
first = context.set_value(key1, "foo")
36+
second = context.set_value(key2, "bar")
37+
self.assertEqual(context.get_value(key1, context=first), "foo")
38+
self.assertEqual(context.get_value(key2, context=second), "bar")
39+
2940
def test_context(self):
30-
self.assertIsNone(context.get_value("say"))
41+
key1 = context.create_key("say")
42+
self.assertIsNone(context.get_value(key1))
3143
empty = context.get_current()
32-
second = context.set_value("say", "foo")
33-
self.assertEqual(context.get_value("say", context=second), "foo")
44+
second = context.set_value(key1, "foo")
45+
self.assertEqual(context.get_value(key1, context=second), "foo")
3446

35-
do_work()
36-
self.assertEqual(context.get_value("say"), "bar")
47+
key2 = _do_work()
48+
self.assertEqual(context.get_value(key2), "bar")
3749
third = context.get_current()
3850

39-
self.assertIsNone(context.get_value("say", context=empty))
40-
self.assertEqual(context.get_value("say", context=second), "foo")
41-
self.assertEqual(context.get_value("say", context=third), "bar")
51+
self.assertIsNone(context.get_value(key1, context=empty))
52+
self.assertEqual(context.get_value(key1, context=second), "foo")
53+
self.assertEqual(context.get_value(key2, context=third), "bar")
4254

4355
def test_set_value(self):
4456
first = context.set_value("a", "yyy")

opentelemetry-sdk/setup.cfg

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ include_package_data = True
4444
install_requires =
4545
opentelemetry-api == 1.4.0.dev0
4646
opentelemetry-semantic-conventions == 0.23.dev0
47+
opentelemetry-instrumentation == 0.23.dev0
4748

4849
[options.packages.find]
4950
where = src

opentelemetry-sdk/src/opentelemetry/sdk/trace/export/__init__.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
from typing import Optional
2323

2424
from opentelemetry.context import Context, attach, detach, set_value
25+
from opentelemetry.instrumentation.utils import _SUPPRESS_INSTRUMENTATION_KEY
2526
from opentelemetry.sdk.environment_variables import (
2627
OTEL_BSP_EXPORT_TIMEOUT,
2728
OTEL_BSP_MAX_EXPORT_BATCH_SIZE,
@@ -86,7 +87,7 @@ def on_start(
8687
def on_end(self, span: ReadableSpan) -> None:
8788
if not span.context.trace_flags.sampled:
8889
return
89-
token = attach(set_value("suppress_instrumentation", True))
90+
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
9091
try:
9192
self.span_exporter.export((span,))
9293
# pylint: disable=broad-except
@@ -326,7 +327,7 @@ def _export_batch(self) -> int:
326327
while idx < self.max_export_batch_size and self.queue:
327328
self.spans_list[idx] = self.queue.pop()
328329
idx += 1
329-
token = attach(set_value("suppress_instrumentation", True))
330+
token = attach(set_value(_SUPPRESS_INSTRUMENTATION_KEY, True))
330331
try:
331332
# Ignore type b/c the Optional[None]+slicing is too "clever"
332333
# for mypy

0 commit comments

Comments
 (0)