opentelemetry-python-contrib icon indicating copy to clipboard operation
opentelemetry-python-contrib copied to clipboard

Support aio-pika 9.1

Open phillipuniverse opened this issue 1 year ago • 4 comments

Is your feature request related to a problem?

The aio-pika instrumentation is currently broken for publish and consume spans in aio-pika 9.1+. This is due to a refactoring of connection properties on a channel in the 9.1.0 release.

Here's the exception:

self = <opentelemetry.instrumentation.aio_pika.span_builder.SpanBuilder object at 0x10fe952d0>
channel = <aio_pika.robust_channel.RobustChannel object at 0x10fed1900>

    def set_channel(self, channel: AbstractChannel):
>       connection = channel.connection
E       AttributeError: 'RobustChannel' object has no attribute 'connection'. Did you mean: '_connection'?

Describe the solution you'd like

Otel working with aio-pika 9.1

Describe alternatives you've considered

N/A

Additional context

I asked for clarification on the aio-pika maintainer's opinion of the correct fix at https://github.com/mosquito/aio-pika/pull/533#issuecomment-1575363432

phillipuniverse avatar Jun 04 '23 03:06 phillipuniverse

In the meantime, here is a fix that will work to override the method.

First create this patch_spanbuilder_set_channel() function

from aio_pika.abc import AbstractChannel
from opentelemetry.semconv.trace import SpanAttributes


def patch_spanbuilder_set_channel() -> None:
    """
    The default SpanBuilder.set_channel does not work with aio_pika 9.1 and the refactored connection
    attribute
    """
    import opentelemetry.instrumentation.aio_pika.span_builder
    from opentelemetry.instrumentation.aio_pika.span_builder import SpanBuilder

    def set_channel(self: SpanBuilder, channel: AbstractChannel) -> None:
        if hasattr(channel, "_connection"):
            url = channel._connection.url
            self._attributes.update(
                {
                    SpanAttributes.NET_PEER_NAME: url.host,
                    SpanAttributes.NET_PEER_PORT: url.port,
                }
            )

    opentelemetry.instrumentation.aio_pika.span_builder.SpanBuilder.set_channel = set_channel  # type: ignore[misc]

Add an ENTRYPOINT in your project to pre_instrument. In Poetry it looks like this in pyproject.toml:

[tool.poetry.plugins."opentelemetry_pre_instrument"]
aio_pika = "package.path.to.patch_spanbuilder_set_channel"

phillipuniverse avatar Jun 04 '23 03:06 phillipuniverse

Any ETA on this?

LockedThread avatar Dec 27 '23 18:12 LockedThread

I'm also interested in this being resolved. Would it be appropriate to bump the comment at https://github.com/mosquito/aio-pika/pull/533#issuecomment-1575363432 since it seems that we're close to a resolution but have yet to get a reply from the aio-pika maintainers.

ericstengard avatar Jan 24 '24 14:01 ericstengard

In the meantime, here is a fix that will work to override the method.

First create this patch_spanbuilder_set_channel() function

from aio_pika.abc import AbstractChannel
from opentelemetry.semconv.trace import SpanAttributes


def patch_spanbuilder_set_channel() -> None:
    """
    The default SpanBuilder.set_channel does not work with aio_pika 9.1 and the refactored connection
    attribute
    """
    import opentelemetry.instrumentation.aio_pika.span_builder
    from opentelemetry.instrumentation.aio_pika.span_builder import SpanBuilder

    def set_channel(self: SpanBuilder, channel: AbstractChannel) -> None:
        if hasattr(channel, "_connection"):
            url = channel._connection.url
            self._attributes.update(
                {
                    SpanAttributes.NET_PEER_NAME: url.host,
                    SpanAttributes.NET_PEER_PORT: url.port,
                }
            )

    opentelemetry.instrumentation.aio_pika.span_builder.SpanBuilder.set_channel = set_channel  # type: ignore[misc]

Add an ENTRYPOINT in your project to pre_instrument. In Poetry it looks like this in pyproject.toml:

[tool.poetry.plugins."opentelemetry_pre_instrument"]
aio_pika = "package.path.to.patch_spanbuilder_set_channel"

This should be:

from aio_pika.abc import AbstractChannel
from opentelemetry.semconv.trace import SpanAttributes


def patch_spanbuilder_set_channel() -> None:
    """
    The default SpanBuilder.set_channel does not work with aio_pika 9.1 and the refactored connection
    attribute
    """
    import opentelemetry.instrumentation.aio_pika.span_builder
    from opentelemetry.instrumentation.aio_pika.span_builder import SpanBuilder

    def set_channel(self: SpanBuilder, channel: AbstractChannel) -> None:
        if hasattr(channel, "_connection"):
            url = channel._connection.url
            port = url.port or 5672
            self._attributes.update(
                {
                    SpanAttributes.NET_PEER_NAME: url.host,
                    SpanAttributes.NET_PEER_PORT: port,
                }
            )

    opentelemetry.instrumentation.aio_pika.span_builder.SpanBuilder.set_channel = set_channel  # type: ignore[misc]

thank you for your valuable fix

the pyproject.toml change can be omitted for users who directly invoke instrumentation:

patch_spanbuilder_set_channel()
AioPikaInstrumentor().instrument()

is also sufficient

doctorpangloss avatar Apr 16 '24 22:04 doctorpangloss

While there is the PR, somehow it still seems to be an issue with opentelemetry-instrumentation-aio-pika = "^0.45b0" and aio-pika = "^9.4.1"

RedLine89 avatar May 23 '24 19:05 RedLine89

@RedLine89 the fix is not yet apart of a release but will be in 0.46b0. I’m unsure of the release schedule.

phillipuniverse avatar May 23 '24 19:05 phillipuniverse

Should this issue not be closed now that https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2450 has been merged?

emil-vdw avatar Aug 01 '24 13:08 emil-vdw

Should this issue not be closed now that https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2450 has been merged?

Yes, it should be.

LockedThread avatar Aug 24 '24 16:08 LockedThread

@phillipuniverse Can you close this?

LockedThread avatar Aug 24 '24 16:08 LockedThread

hi, with the autoinstrumentation using the image ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-python:0.46b0 the problem persists

cbeddd6e-63f5-4fd0-8bc2-da81e1770231

gchadid avatar Sep 02 '24 20:09 gchadid

@gchadid could you please open a new issue for this?

emdneto avatar Sep 03 '24 00:09 emdneto