Skip to content
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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## Unreleased

### Fixed

- `opentelemetry-instrumentation`: Avoid calls to `context.detach` with `None` token.
([#3673](https://github.com/open-telemetry/opentelemetry-python-contrib/pull/3673))

## Version 1.36.0/0.57b0 (2025-07-29)

### Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ async def decorated(message: AbstractIncomingMessage):
with trace.use_span(span, end_on_exit=True):
return_value = await callback(message)
finally:
context.detach(token)
if token:
context.detach(token)
return return_value

return decorated
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,8 @@ def create_trace_config(

def _end_trace(trace_config_ctx: types.SimpleNamespace):
elapsed_time = max(default_timer() - trace_config_ctx.start_time, 0)
context_api.detach(trace_config_ctx.token)
if trace_config_ctx.token:
context_api.detach(trace_config_ctx.token)
trace_config_ctx.span.end()

if trace_config_ctx.duration_histogram_old is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,8 @@ async def _create_consumer_span(
await async_consume_hook(span, record, args, kwargs)
except Exception as hook_exception: # pylint: disable=W0703
_LOG.exception(hook_exception)
context.detach(token)
if token:
context.detach(token)

return span

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,8 @@ def _instrumented_lambda_handler_call( # noqa pylint: disable=too-many-branches
result.get("statusCode"),
)
finally:
context_api.detach(token)
if token:
context_api.detach(token)

now = time.time()
_tracer_provider = tracer_provider or get_tracer_provider()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,9 @@ def _safe_end_processing_span(receipt_handle: str) -> None:
Boto3SQSInstrumentor.current_span_related_to_token
== started_span
):
context.detach(Boto3SQSInstrumentor.current_context_token)
Boto3SQSInstrumentor.current_context_token = None
if Boto3SQSInstrumentor.current_context_token:
context.detach(Boto3SQSInstrumentor.current_context_token)
Boto3SQSInstrumentor.current_context_token = None
started_span.end()

@staticmethod
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ def set(self, carrier: textmap.CarrierT, key: str, value: str) -> None:


def _end_current_consume_span(instance):
context.detach(instance._current_context_token)
if instance._current_context_token:
context.detach(instance._current_context_token)
instance._current_context_token = None
instance._current_consume_span.end()
instance._current_consume_span = None
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,8 @@ def _set_remote_context(self, servicer_context):
try:
yield
finally:
detach(token)
if token:
detach(token)
else:
yield

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,8 @@ def _create_consumer_span(
consume_hook(span, record, args, kwargs)
except Exception as hook_exception: # pylint: disable=W0703
_LOG.exception(hook_exception)
context.detach(token)
if token:
context.detach(token)


def _wrap_next(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ def decorated_callback(
_LOG.exception(hook_exception)
retval = callback(channel, method, properties, body)
finally:
context.detach(token)
if token:
context.detach(token)
return retval

return decorated_callback
Expand Down Expand Up @@ -252,7 +253,8 @@ def popleft(self, *args, **kwargs):
operation=MessagingOperationValues.RECEIVE,
)
try:
context.detach(message_ctx_token)
if message_ctx_token:
context.detach(message_ctx_token)
self._self_active_token = context.attach(
trace.set_span_in_context(span)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,8 @@ def _suppress_instrumentation(*keys: str) -> Generator[None]:
try:
yield
finally:
context.detach(token)
if token:
context.detach(token)


@contextmanager
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,8 @@ def set_ip_on_next_http_connection(span: Span):
try:
yield
finally:
context.detach(token)
if token:
context.detach(token)
else:
spans = state["need_ip"]
spans.append(span)
Expand Down