Skip to content

Add __contains__ to TraceState #1773

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
- Added ProxyTracerProvider and ProxyTracer implementations to allow fetching provider
and tracer instances before a global provider is set up.
([#1726](https://github.com/open-telemetry/opentelemetry-python/pull/1726))
- Added `__contains__` to `opentelementry.trace.span.TraceState`.
([#1773](https://github.com/open-telemetry/opentelemetry-python/pull/1773))


## [1.0.0](https://github.com/open-telemetry/opentelemetry-python/releases/tag/v1.0.0) - 2021-03-26
Expand Down
7 changes: 5 additions & 2 deletions opentelemetry-api/src/opentelemetry/trace/span.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,8 +231,11 @@ def __init__(
"Invalid key/value pair (%s, %s) found.", key, value
)

def __getitem__(self, key: str) -> typing.Optional[str]: # type: ignore
return self._dict.get(key)
def __contains__(self, item: object) -> bool:
return item in self._dict

def __getitem__(self, key: str) -> str:
return self._dict[key]

def __iter__(self) -> typing.Iterator[str]:
return iter(self._dict)
Expand Down
16 changes: 16 additions & 0 deletions opentelemetry-api/tests/trace/test_tracestate.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,19 @@ def test_tracestate_order_changed(self):
foo_place = entries.index(("foo", "bar33")) # type: ignore
prev_first_place = entries.index(("1a-2f@foo", "bar1")) # type: ignore
self.assertLessEqual(foo_place, prev_first_place)

def test_trace_contains(self):
entries = [
"1a-2f@foo=bar1",
"1a-_*/2b@foo=bar2",
"foo=bar3",
"foo-_*/bar=bar4",
]
header_list = [",".join(entries)]
state = TraceState.from_header(header_list)

self.assertTrue("foo" in state)
self.assertFalse("bar" in state)
self.assertIsNone(state.get("bar"))
with self.assertRaises(KeyError):
state["bar"] # pylint:disable=W0104