Skip to content

Make return value of get_correlations immutable #1024

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 5 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from 4 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 opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- Moved samplers from API to SDK
([#1023](https://github.com/open-telemetry/opentelemetry-python/pull/1023))
- Change return value type of `correlationcontext.get_correlations` to immutable `MappingProxyType`
([#1024](https://github.com/open-telemetry/opentelemetry-python/pull/1024))

## Version 0.12b0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
# limitations under the License.

import typing
from types import MappingProxyType

from opentelemetry.context import get_value, set_value
from opentelemetry.context.context import Context
Expand All @@ -22,7 +23,7 @@

def get_correlations(
context: typing.Optional[Context] = None,
) -> typing.Dict[str, object]:
) -> typing.Mapping[str, object]:
"""Returns the name/value pairs in the CorrelationContext

Args:
Expand All @@ -33,8 +34,8 @@ def get_correlations(
"""
correlations = get_value(_CORRELATION_CONTEXT_KEY, context=context)
if isinstance(correlations, dict):
return correlations.copy()
return {}
return MappingProxyType(correlations.copy())
return MappingProxyType({})


def get_correlation(
Expand Down Expand Up @@ -67,7 +68,7 @@ def set_correlation(
Returns:
A Context with the value updated
"""
correlations = get_correlations(context=context)
correlations = dict(get_correlations(context=context))
correlations[name] = value
return set_value(_CORRELATION_CONTEXT_KEY, correlations, context=context)

Expand All @@ -84,7 +85,7 @@ def remove_correlation(
Returns:
A Context with the name/value removed
"""
correlations = get_correlations(context=context)
correlations = dict(get_correlations(context=context))
correlations.pop(name, None)

return set_value(_CORRELATION_CONTEXT_KEY, correlations, context=context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def inject(
)


def _format_correlations(correlations: typing.Dict[str, object]) -> str:
def _format_correlations(correlations: typing.Mapping[str, object]) -> str:
return ",".join(
key + "=" + urllib.parse.quote_plus(str(value))
for key, value in correlations.items()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ def test_modifying_correlations(self):
ctx = cctx.set_correlation("test", "value")
self.assertEqual(cctx.get_correlation("test", context=ctx), "value")
correlations = cctx.get_correlations(context=ctx)
correlations["test"] = "mess-this-up"
with self.assertRaises(TypeError):
correlations["test"] = "mess-this-up"
self.assertEqual(cctx.get_correlation("test", context=ctx), "value")

def test_remove_correlations(self):
Expand Down