|
14 | 14 | from logging import getLogger
|
15 | 15 | from typing import Any, Callable, Collection, Dict, Optional
|
16 | 16 |
|
| 17 | +import wrapt |
17 | 18 | from pika.adapters import BlockingConnection
|
18 | 19 | from pika.channel import Channel
|
19 | 20 |
|
|
22 | 23 | from opentelemetry.instrumentation.pika import utils
|
23 | 24 | from opentelemetry.instrumentation.pika.package import _instruments
|
24 | 25 | from opentelemetry.instrumentation.pika.version import __version__
|
| 26 | +from opentelemetry.instrumentation.utils import unwrap |
25 | 27 | from opentelemetry.trace import Tracer, TracerProvider
|
26 | 28 |
|
27 | 29 | _LOG = getLogger(__name__)
|
@@ -93,25 +95,25 @@ def uninstrument_channel(channel: Channel) -> None:
|
93 | 95 | if hasattr(callback, "_original_callback"):
|
94 | 96 | channel._impl._consumers[key] = callback._original_callback
|
95 | 97 | PikaInstrumentor._uninstrument_channel_functions(channel)
|
| 98 | + if hasattr(channel, "__opentelemetry_tracer"): |
| 99 | + delattr(channel, "__opentelemetry_tracer") |
96 | 100 |
|
97 | 101 | def _decorate_channel_function(
|
98 | 102 | self, tracer_provider: Optional[TracerProvider]
|
99 | 103 | ) -> None:
|
100 |
| - self.original_channel_func = BlockingConnection.channel |
101 |
| - |
102 |
| - def _wrapper(*args, **kwargs): |
103 |
| - channel = self.original_channel_func(*args, **kwargs) |
| 104 | + def wrapper(wrapped, instance, args, kwargs): |
| 105 | + channel = wrapped(*args, **kwargs) |
104 | 106 | self.instrument_channel(channel, tracer_provider=tracer_provider)
|
105 | 107 | return channel
|
106 | 108 |
|
107 |
| - BlockingConnection.channel = _wrapper |
| 109 | + wrapt.wrap_function_wrapper(BlockingConnection, "channel", wrapper) |
108 | 110 |
|
109 | 111 | def _instrument(self, **kwargs: Dict[str, Any]) -> None:
|
110 | 112 | tracer_provider: TracerProvider = kwargs.get("tracer_provider", None)
|
111 | 113 | self._decorate_channel_function(tracer_provider)
|
112 | 114 |
|
113 | 115 | def _uninstrument(self, **kwargs: Dict[str, Any]) -> None:
|
114 |
| - BlockingConnection.channel = self.original_channel_func |
| 116 | + unwrap(BlockingConnection, "channel") |
115 | 117 |
|
116 | 118 | def instrumentation_dependencies(self) -> Collection[str]:
|
117 | 119 | return _instruments
|
0 commit comments