opentelemetry-python-contrib
opentelemetry-python-contrib copied to clipboard
Support aio-pika 9.1
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
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"
Any ETA on this?
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.
In the meantime, here is a fix that will work to override the method.
First create this
patch_spanbuilder_set_channel()
functionfrom 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
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 the fix is not yet apart of a release but will be in 0.46b0. I’m unsure of the release schedule.
Should this issue not be closed now that https://github.com/open-telemetry/opentelemetry-python-contrib/pull/2450 has been merged?
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.
@phillipuniverse Can you close this?
hi, with the autoinstrumentation using the image ghcr.io/open-telemetry/opentelemetry-operator/autoinstrumentation-python:0.46b0 the problem persists
@gchadid could you please open a new issue for this?