|
| 1 | +# Copyright The OpenTelemetry Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +from typing import Any, Callable, Collection |
| 15 | + |
| 16 | +import wrapt |
| 17 | +from aio_pika import Exchange, Queue |
| 18 | +from aio_pika.abc import AbstractIncomingMessage |
| 19 | + |
| 20 | +from opentelemetry import trace |
| 21 | +from opentelemetry.instrumentation.aio_pika.callback_decorator import ( |
| 22 | + CallbackDecorator, |
| 23 | +) |
| 24 | +from opentelemetry.instrumentation.aio_pika.package import _instruments |
| 25 | +from opentelemetry.instrumentation.aio_pika.publish_decorator import ( |
| 26 | + PublishDecorator, |
| 27 | +) |
| 28 | +from opentelemetry.instrumentation.aio_pika.version import __version__ |
| 29 | +from opentelemetry.instrumentation.instrumentor import BaseInstrumentor |
| 30 | +from opentelemetry.instrumentation.utils import unwrap |
| 31 | +from opentelemetry.trace import Tracer |
| 32 | + |
| 33 | +_INSTRUMENTATION_MODULE_NAME = "opentelemetry.instrumentation.aio_pika" |
| 34 | + |
| 35 | + |
| 36 | +class AioPikaInstrumentor(BaseInstrumentor): |
| 37 | + @staticmethod |
| 38 | + def _instrument_queue(tracer: Tracer): |
| 39 | + async def wrapper(wrapped, instance, args, kwargs): |
| 40 | + async def consume( |
| 41 | + callback: Callable[[AbstractIncomingMessage], Any], |
| 42 | + *fargs, |
| 43 | + **fkwargs |
| 44 | + ): |
| 45 | + decorated_callback = CallbackDecorator( |
| 46 | + tracer, instance |
| 47 | + ).decorate(callback) |
| 48 | + return await wrapped(decorated_callback, *fargs, **fkwargs) |
| 49 | + |
| 50 | + return await consume(*args, **kwargs) |
| 51 | + |
| 52 | + wrapt.wrap_function_wrapper(Queue, "consume", wrapper) |
| 53 | + |
| 54 | + @staticmethod |
| 55 | + def _instrument_exchange(tracer: Tracer): |
| 56 | + async def wrapper(wrapped, instance, args, kwargs): |
| 57 | + decorated_publish = PublishDecorator(tracer, instance).decorate( |
| 58 | + wrapped |
| 59 | + ) |
| 60 | + return await decorated_publish(*args, **kwargs) |
| 61 | + |
| 62 | + wrapt.wrap_function_wrapper(Exchange, "publish", wrapper) |
| 63 | + |
| 64 | + def _instrument(self, **kwargs): |
| 65 | + tracer_provider = kwargs.get("tracer_provider", None) |
| 66 | + tracer = trace.get_tracer( |
| 67 | + _INSTRUMENTATION_MODULE_NAME, __version__, tracer_provider |
| 68 | + ) |
| 69 | + self._instrument_queue(tracer) |
| 70 | + self._instrument_exchange(tracer) |
| 71 | + |
| 72 | + @staticmethod |
| 73 | + def _uninstrument_queue(): |
| 74 | + unwrap(Queue, "consume") |
| 75 | + |
| 76 | + @staticmethod |
| 77 | + def _uninstrument_exchange(): |
| 78 | + unwrap(Exchange, "publish") |
| 79 | + |
| 80 | + def _uninstrument(self, **kwargs): |
| 81 | + self._uninstrument_queue() |
| 82 | + self._uninstrument_exchange() |
| 83 | + |
| 84 | + def instrumentation_dependencies(self) -> Collection[str]: |
| 85 | + return _instruments |
0 commit comments