Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 371eaa8

Browse files
authored
Add __contains__ to TraceState (open-telemetry#1773)
1 parent 71e3a7a commit 371eaa8

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2727
- Added ProxyTracerProvider and ProxyTracer implementations to allow fetching provider
2828
and tracer instances before a global provider is set up.
2929
([#1726](https://github.com/open-telemetry/opentelemetry-python/pull/1726))
30+
- Added `__contains__` to `opentelementry.trace.span.TraceState`.
31+
([#1773](https://github.com/open-telemetry/opentelemetry-python/pull/1773))
3032

3133

3234
## [1.0.0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.0.0) - 2021-03-26

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,8 +231,11 @@ def __init__(
231231
"Invalid key/value pair (%s, %s) found.", key, value
232232
)
233233

234-
def __getitem__(self, key: str) -> typing.Optional[str]: # type: ignore
235-
return self._dict.get(key)
234+
def __contains__(self, item: object) -> bool:
235+
return item in self._dict
236+
237+
def __getitem__(self, key: str) -> str:
238+
return self._dict[key]
236239

237240
def __iter__(self) -> typing.Iterator[str]:
238241
return iter(self._dict)

opentelemetry-api/tests/trace/test_tracestate.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,19 @@ def test_tracestate_order_changed(self):
9696
foo_place = entries.index(("foo", "bar33")) # type: ignore
9797
prev_first_place = entries.index(("1a-2f@foo", "bar1")) # type: ignore
9898
self.assertLessEqual(foo_place, prev_first_place)
99+
100+
def test_trace_contains(self):
101+
entries = [
102+
"1a-2f@foo=bar1",
103+
"1a-_*/2b@foo=bar2",
104+
"foo=bar3",
105+
"foo-_*/bar=bar4",
106+
]
107+
header_list = [",".join(entries)]
108+
state = TraceState.from_header(header_list)
109+
110+
self.assertTrue("foo" in state)
111+
self.assertFalse("bar" in state)
112+
self.assertIsNone(state.get("bar"))
113+
with self.assertRaises(KeyError):
114+
state["bar"] # pylint:disable=W0104

0 commit comments

Comments
 (0)