Skip to content
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

improve baggage performance #4466

Merged
merged 6 commits into from
Mar 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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 @@ -32,6 +32,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
([#4406](https://github.com/open-telemetry/opentelemetry-python/pull/4406))
- Fix env var error message for TraceLimits/SpanLimits
([#4458](https://github.com/open-telemetry/opentelemetry-python/pull/4458))
- Improve performance of baggage operations
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrong release 😢

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, sorry 🙈

([#4466](https://github.com/open-telemetry/opentelemetry-python/pull/4466))

## Version 1.30.0/0.51b0 (2025-02-03)

Expand Down
20 changes: 12 additions & 8 deletions opentelemetry-api/src/opentelemetry/baggage/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from logging import getLogger
from re import compile
from types import MappingProxyType
from typing import Mapping, Optional
from typing import Dict, Mapping, Optional

from opentelemetry.context import create_key, get_value, set_value
from opentelemetry.context.context import Context
Expand Down Expand Up @@ -44,10 +44,7 @@ def get_all(
Returns:
The name/value pairs in the Baggage
"""
baggage = get_value(_BAGGAGE_KEY, context=context)
if isinstance(baggage, dict):
return MappingProxyType(baggage)
return MappingProxyType({})
return MappingProxyType(_get_baggage_value(context=context))


def get_baggage(
Expand All @@ -64,7 +61,7 @@ def get_baggage(
The value associated with the given name, or null if the given name is
not present.
"""
return get_all(context=context).get(name)
return _get_baggage_value(context=context).get(name)


def set_baggage(
Expand All @@ -80,7 +77,7 @@ def set_baggage(
Returns:
A Context with the value updated
"""
baggage = dict(get_all(context=context))
baggage = _get_baggage_value(context=context).copy()
baggage[name] = value
return set_value(_BAGGAGE_KEY, baggage, context=context)

Expand All @@ -95,7 +92,7 @@ def remove_baggage(name: str, context: Optional[Context] = None) -> Context:
Returns:
A Context with the name/value removed
"""
baggage = dict(get_all(context=context))
baggage = _get_baggage_value(context=context).copy()
baggage.pop(name, None)

return set_value(_BAGGAGE_KEY, baggage, context=context)
Expand All @@ -113,6 +110,13 @@ def clear(context: Optional[Context] = None) -> Context:
return set_value(_BAGGAGE_KEY, {}, context=context)


def _get_baggage_value(context: Optional[Context] = None) -> Dict[str, object]:
baggage = get_value(_BAGGAGE_KEY, context=context)
if isinstance(baggage, dict):
return baggage
return {}


def _is_valid_key(name: str) -> bool:
return _KEY_PATTERN.fullmatch(str(name)) is not None

Expand Down