Skip to content

Commit

Permalink
Add __contains__ to TraceState (open-telemetry#1773)
Browse files Browse the repository at this point in the history
  • Loading branch information
alertedsnake authored and Alex Boten committed Apr 16, 2021
1 parent ab80934 commit 9f6f1ba
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
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))
- OTLP Exporter now uses the scheme in the endpoint to determine whether to establish
a secure connection or not.
([#1771](https://github.com/open-telemetry/opentelemetry-python/pull/1771))
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

0 comments on commit 9f6f1ba

Please sign in to comment.