Avoid calls to context.detach with None token (#3673)

* fixes open-telemetry/opentelemetry-python-contrib#3351

* Add changelog entry about open-telemetry/opentelemetry-python-contrib#3673

---------

Co-authored-by: Emídio Neto <9735060+emdneto@users.noreply.github.com>
Co-authored-by: Riccardo Magliocchetti <riccardo.magliocchetti@gmail.com>
This commit is contained in:
Andre Murbach Maidl 2025-08-18 09:39:16 -03:00 committed by GitHub
parent bbcdf8b737
commit f11f03ce6e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 30 additions and 13 deletions

View File

@ -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

View File

@ -53,7 +53,8 @@ class CallbackDecorator:
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

View File

@ -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:

View File

@ -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

View File

@ -384,7 +384,8 @@ def _instrument(
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()

View File

@ -180,8 +180,9 @@ class Boto3SQSInstrumentor(BaseInstrumentor):
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

View File

@ -82,7 +82,8 @@ _kafka_getter = KafkaContextGetter()
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

View File

@ -209,7 +209,8 @@ class OpenTelemetryServerInterceptor(grpc.ServerInterceptor):
try:
yield
finally:
detach(token)
if token:
detach(token)
else:
yield

View File

@ -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(

View File

@ -80,7 +80,8 @@ def _decorate_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
@ -252,7 +253,8 @@ class ReadyMessagesDequeProxy(ObjectProxy):
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)
)

View File

@ -207,7 +207,8 @@ def _suppress_instrumentation(*keys: str) -> Generator[None]:
try:
yield
finally:
context.detach(token)
if token:
context.detach(token)
@contextmanager

View File

@ -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)