Skip to content
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 opentelemetry-api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

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