Skip to content

Commit aa5acef

Browse files
CopilotnikhilNava
andcommitted
Remove add_baggage() from OpenTelemetryScope
The method had an unpaired context.attach() that leaked context tokens. Users should use BaggageBuilder.build() context manager instead, which properly restores the previous context on exit. Co-authored-by: nikhilNava <211831449+nikhilNava@users.noreply.github.com>
1 parent 5c39fb2 commit aa5acef

File tree

1 file changed

+1
-30
lines changed
  • libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core

1 file changed

+1
-30
lines changed

libraries/microsoft-agents-a365-observability-core/microsoft_agents_a365/observability/core/opentelemetry_scope.py

Lines changed: 1 addition & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from threading import Lock
1010
from typing import TYPE_CHECKING, Any
1111

12-
from opentelemetry import baggage, context, trace
12+
from opentelemetry import context, trace
1313
from opentelemetry.trace import (
1414
Span,
1515
SpanKind,
@@ -126,7 +126,6 @@ def __init__(
126126
self._error_type: str | None = None
127127
self._exception: Exception | None = None
128128
self._context_token = None
129-
self._baggage_tokens: list[object] = []
130129

131130
if self._is_telemetry_enabled():
132131
tracer = self._get_tracer()
@@ -242,29 +241,6 @@ def set_tag_maybe(self, name: str, value: Any) -> None:
242241
if value is not None and self._span and self._is_telemetry_enabled():
243242
self._span.set_attribute(name, value)
244243

245-
def add_baggage(self, key: str, value: str) -> None:
246-
"""Add baggage to the current context.
247-
248-
.. warning::
249-
This method attaches a new context that cannot be detached. Prefer using
250-
:class:`~microsoft_agents_a365.observability.core.middleware.baggage_builder.BaggageBuilder`
251-
with its context-manager API (``with builder.build(): ...``) which properly
252-
restores the previous context on exit.
253-
254-
Args:
255-
key: The baggage key
256-
value: The baggage value
257-
"""
258-
# Set baggage in the current context
259-
if self._is_telemetry_enabled():
260-
# Set baggage on the current context
261-
# This will be inherited by child spans created within this context
262-
baggage_context = baggage.set_baggage(key, value)
263-
# The context needs to be made current for child spans to inherit the baggage
264-
token = context.attach(baggage_context)
265-
# Store the token so it can be detached when the scope ends
266-
self._baggage_tokens.append(token)
267-
268244
def record_attributes(self, attributes: dict[str, Any] | list[tuple[str, Any]]) -> None:
269245
"""Record multiple attribute key/value pairs for telemetry tracking.
270246
@@ -303,11 +279,6 @@ def _end(self) -> None:
303279
span_id = f"{self._span.context.span_id:016x}" if self._span.context else "unknown"
304280
logger.info(f"Span ended: '{self._span.name}' ({span_id})")
305281

306-
# Detach any baggage tokens in reverse order
307-
for token in reversed(self._baggage_tokens):
308-
context.detach(token)
309-
self._baggage_tokens.clear()
310-
311282
# Convert custom end time to OTel-compatible format (nanoseconds since epoch)
312283
otel_end_time = self._datetime_to_ns(self._custom_end_time)
313284
if otel_end_time is not None:

0 commit comments

Comments
 (0)