diff --git a/opentelemetry-api/CHANGELOG.md b/opentelemetry-api/CHANGELOG.md index fc39c9ef643..048da1f1667 100644 --- a/opentelemetry-api/CHANGELOG.md +++ b/opentelemetry-api/CHANGELOG.md @@ -6,6 +6,8 @@ ([#1005](https://github.com/open-telemetry/opentelemetry-python/pull/1005)) - 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)) - Remove lazy Event and Link API from Span interface ([#1045](https://github.com/open-telemetry/opentelemetry-python/pull/1045)) diff --git a/opentelemetry-api/src/opentelemetry/correlationcontext/__init__.py b/opentelemetry-api/src/opentelemetry/correlationcontext/__init__.py index c16d75162ad..8dbb357495a 100644 --- a/opentelemetry-api/src/opentelemetry/correlationcontext/__init__.py +++ b/opentelemetry-api/src/opentelemetry/correlationcontext/__init__.py @@ -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 @@ -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: @@ -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( @@ -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) @@ -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) diff --git a/opentelemetry-api/src/opentelemetry/correlationcontext/propagation/__init__.py b/opentelemetry-api/src/opentelemetry/correlationcontext/propagation/__init__.py index fca9465fbb3..4032394ce71 100644 --- a/opentelemetry-api/src/opentelemetry/correlationcontext/propagation/__init__.py +++ b/opentelemetry-api/src/opentelemetry/correlationcontext/propagation/__init__.py @@ -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() diff --git a/opentelemetry-api/tests/correlationcontext/test_correlation_context.py b/opentelemetry-api/tests/correlationcontext/test_correlation_context.py index 31996c6913a..aaa5d9fa925 100644 --- a/opentelemetry-api/tests/correlationcontext/test_correlation_context.py +++ b/opentelemetry-api/tests/correlationcontext/test_correlation_context.py @@ -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):