Skip to content

opentelemetry-instrumentation-pika: avoid duplicate consumer wrapping#4670

Open
popsiclelmlm wants to merge 1 commit into
open-telemetry:mainfrom
popsiclelmlm:codex/otel-pika-consumer-wrap
Open

opentelemetry-instrumentation-pika: avoid duplicate consumer wrapping#4670
popsiclelmlm wants to merge 1 commit into
open-telemetry:mainfrom
popsiclelmlm:codex/otel-pika-consumer-wrap

Conversation

@popsiclelmlm

Copy link
Copy Markdown

Description

Fixes #4667.

Summary

Avoid wrapping an already-instrumented pika consumer callback again when _instrument_channel_consumers runs more than once for the same channel.

Reproduction

A BlockingChannel can call basic_consume multiple times. The instrumentation reruns _instrument_channel_consumers after each registration. The new regression test calls _instrument_channel_consumers twice for the same channel and asserts the callback remains the first decorated function.

Root cause

_instrument_channel_consumers decorated every callback in the channel consumer map each time it ran. Previously registered callbacks could therefore accumulate nested consumer span wrappers.

Changes

  • Skip callbacks that already carry _original_callback metadata.
  • Add a regression test for repeated consumer instrumentation on the same BlockingChannel.
  • Add a changelog fragment.

Tests

  • .venv-tox/bin/tox -e py314-test-instrumentation-sio-pika-1-wrapt2
  • .venv-tox/bin/tox -e py314-test-instrumentation-sio-pika-0-wrapt2
  • .venv-tox/bin/tox -e lint-instrumentation-sio-pika
  • .tox/lint-instrumentation-sio-pika/bin/ruff check instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/pika_instrumentor.py instrumentation/opentelemetry-instrumentation-pika/tests/test_pika_instrumentation.py
  • .tox/lint-instrumentation-sio-pika/bin/ruff format --check instrumentation/opentelemetry-instrumentation-pika/src/opentelemetry/instrumentation/pika/pika_instrumentor.py instrumentation/opentelemetry-instrumentation-pika/tests/test_pika_instrumentation.py
  • git diff --check

Screenshots/Logs

N/A. This is a unit-level instrumentation fix.

Type of change

  • Bug fix (non-breaking change which fixes an issue)

Does This PR Require a Core Repo Change?

  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@popsiclelmlm popsiclelmlm requested a review from a team as a code owner June 8, 2026 01:55

@herin049 herin049 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks good to me, just a few small comments.

Comment on lines +61 to +63
if "_original_callback" in getattr(
consumer_callback, "__dict__", {}
):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why not just?

Suggested change
if "_original_callback" in getattr(
consumer_callback, "__dict__", {}
):
if hasattr(consumer_callback, "_original_callback"):

if "_original_callback" in getattr(
consumer_callback, "__dict__", {}
):
continue

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It also might be worth adding a warning log here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: No status

Development

Successfully merging this pull request may close these issues.

pika-instrumentation; Multiple basic_consume calls on one channel produce duplicate (nested) CONSUMER spans

2 participants