Skip to content

Commit 55a0fda

Browse files
committed
Move DistributedContext implementation into api level.
1 parent 0c5e803 commit 55a0fda

File tree

4 files changed

+42
-62
lines changed

4 files changed

+42
-62
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
# limitations under the License.
1414

1515
from contextlib import contextmanager
16-
import abc
1716
import typing
1817

1918
PRINTABLE = set(chr(num) for num in range(32, 127))
@@ -75,20 +74,23 @@ def __init__(
7574
self.value = value
7675

7776

78-
class DistributedContext(abc.ABC):
77+
class DistributedContext(dict):
7978
"""A container for distributed context entries"""
8079

81-
@abc.abstractmethod
8280
def get_entries(self) -> typing.Iterable[Entry]:
8381
"""Returns an immutable iterator to entries."""
82+
return self.values()
8483

85-
@abc.abstractmethod
86-
def get_entry_value(self, key: EntryKey) -> typing.Optional[EntryValue]:
84+
def get_entry_value(
85+
self,
86+
key: EntryKey
87+
) -> typing.Optional[EntryValue]:
8788
"""Returns the entry associated with a key or None
8889
8990
Args:
9091
key: the key with which to perform a lookup
9192
"""
93+
return self.get(key)
9294

9395

9496
class DistributedContextManager:

opentelemetry-api/tests/distributedcontext/test_distributed_context.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,33 @@ def test_key_new(self):
6363
self.assertEqual(key, "ok")
6464

6565

66+
class TestDistributedContext(unittest.TestCase):
67+
def setUp(self):
68+
entry = self.entry = distributedcontext.Entry(
69+
distributedcontext.EntryMetadata(
70+
distributedcontext.EntryMetadata.NO_PROPAGATION,
71+
),
72+
distributedcontext.EntryKey("key"),
73+
distributedcontext.EntryValue("value"),
74+
)
75+
context = self.context = distributedcontext.DistributedContext()
76+
context[entry.key] = entry
77+
78+
def test_get_entries(self):
79+
self.assertIn(self.entry, self.context.get_entries())
80+
81+
def test_get_entry_value_present(self):
82+
value = self.context.get_entry_value(
83+
self.entry.key,
84+
)
85+
self.assertIs(value, self.entry)
86+
87+
def test_get_entry_value_missing(self):
88+
key = distributedcontext.EntryKey("missing")
89+
value = self.context.get_entry_value(key)
90+
self.assertIsNone(value)
91+
92+
6693
class TestDistributedContextManager(unittest.TestCase):
6794
def setUp(self):
6895
self.manager = distributedcontext.DistributedContextManager()

opentelemetry-sdk/src/opentelemetry/sdk/distributedcontext/__init__.py

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,6 @@
1919
from opentelemetry.context import Context
2020

2121

22-
class DistributedContext(dict, dctx_api.DistributedContext):
23-
"""A container for distributed context entries"""
24-
25-
def get_entries(self) -> typing.Iterable[dctx_api.Entry]:
26-
"""Returns an immutable iterator to entries."""
27-
return self.values()
28-
29-
def get_entry_value(
30-
self,
31-
key: dctx_api.EntryKey
32-
) -> typing.Optional[dctx_api.EntryValue]:
33-
"""Returns the entry associated with a key or None
34-
35-
Args:
36-
key: the key with which to perform a lookup
37-
"""
38-
return self.get(key)
39-
40-
4122
class DistributedContextManager(dctx_api.DistributedContextManager):
4223
"""See `opentelemetry.distributedcontext.DistributedContextManager`
4324
@@ -53,7 +34,9 @@ def __init__(self, name: str = "") -> None:
5334

5435
self._current_context = Context.register_slot(slot_name)
5536

56-
def get_current_context(self) -> typing.Optional[DistributedContext]:
37+
def get_current_context(
38+
self,
39+
) -> typing.Optional[dctx_api.DistributedContext]:
5740
"""Gets the current DistributedContext.
5841
5942
Returns:
@@ -64,8 +47,8 @@ def get_current_context(self) -> typing.Optional[DistributedContext]:
6447
@contextmanager
6548
def use_context(
6649
self,
67-
context: DistributedContext,
68-
) -> typing.Iterator[DistributedContext]:
50+
context: dctx_api.DistributedContext,
51+
) -> typing.Iterator[dctx_api.DistributedContext]:
6952
"""Context manager for controlling a DistributedContext lifetime.
7053
7154
Set the context as the active DistributedContext.

opentelemetry-sdk/tests/distributedcontext/test_distributed_context.py

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,42 +14,10 @@
1414

1515
import unittest
1616

17-
18-
from opentelemetry.distributedcontext import (
19-
Entry,
20-
EntryMetadata,
21-
EntryKey,
22-
EntryValue,
23-
)
24-
17+
from opentelemetry import distributedcontext as dctx_api
2518
from opentelemetry.sdk import distributedcontext
2619

2720

28-
class TestDistributedContext(unittest.TestCase):
29-
def setUp(self):
30-
entry = self.entry = Entry(
31-
EntryMetadata(EntryMetadata.NO_PROPAGATION),
32-
EntryKey("key"),
33-
EntryValue("value"),
34-
)
35-
context = self.context = distributedcontext.DistributedContext()
36-
context[entry.key] = entry
37-
38-
def test_get_entries(self):
39-
self.assertIn(self.entry, self.context.get_entries())
40-
41-
def test_get_entry_value_present(self):
42-
value = self.context.get_entry_value(
43-
self.entry.key,
44-
)
45-
self.assertIs(value, self.entry)
46-
47-
def test_get_entry_value_missing(self):
48-
key = EntryKey("missing")
49-
value = self.context.get_entry_value(key)
50-
self.assertIsNone(value)
51-
52-
5321
class TestDistributedContextManager(unittest.TestCase):
5422
def setUp(self):
5523
self.manager = distributedcontext.DistributedContextManager()
@@ -59,7 +27,7 @@ def test_use_context(self):
5927
self.assertIsNone(self.manager.get_current_context())
6028

6129
# Start initial context
62-
dctx = distributedcontext.DistributedContext()
30+
dctx = dctx_api.DistributedContext()
6331
with self.manager.use_context(dctx) as current:
6432
self.assertIs(current, dctx)
6533
self.assertIs(
@@ -68,7 +36,7 @@ def test_use_context(self):
6836
)
6937

7038
# Context is overridden
71-
nested_dctx = distributedcontext.DistributedContext()
39+
nested_dctx = dctx_api.DistributedContext()
7240
with self.manager.use_context(nested_dctx) as current:
7341
self.assertIs(current, nested_dctx)
7442
self.assertIs(

0 commit comments

Comments
 (0)