16
16
17
17
import wrapt
18
18
from pika .adapters import BlockingConnection
19
- from pika .channel import Channel
19
+ from pika .adapters . blocking_connection import BlockingChannel
20
20
21
21
from opentelemetry import trace
22
22
from opentelemetry .instrumentation .instrumentor import BaseInstrumentor
35
35
class PikaInstrumentor (BaseInstrumentor ): # type: ignore
36
36
# pylint: disable=attribute-defined-outside-init
37
37
@staticmethod
38
- def _instrument_consumers (
39
- consumers_dict : Dict [ str , Callable [..., Any ]] , tracer : Tracer
38
+ def _instrument_blocking_channel_consumers (
39
+ channel : BlockingChannel , tracer : Tracer
40
40
) -> Any :
41
- for key , callback in consumers_dict .items ():
41
+ for consumer_tag , consumer_info in channel . _consumer_infos .items ():
42
42
decorated_callback = utils ._decorate_callback (
43
- callback , tracer , key
43
+ consumer_info . on_message_callback , tracer , consumer_tag
44
44
)
45
- setattr (decorated_callback , "_original_callback" , callback )
46
- consumers_dict [key ] = decorated_callback
45
+
46
+ setattr (
47
+ decorated_callback ,
48
+ "_original_callback" ,
49
+ consumer_info .on_message_callback ,
50
+ )
51
+ consumer_info .on_message_callback = decorated_callback
47
52
48
53
@staticmethod
49
- def _instrument_basic_publish (channel : Channel , tracer : Tracer ) -> None :
54
+ def _instrument_basic_publish (
55
+ channel : BlockingChannel , tracer : Tracer
56
+ ) -> None :
50
57
original_function = getattr (channel , "basic_publish" )
51
58
decorated_function = utils ._decorate_basic_publish (
52
59
original_function , channel , tracer
@@ -57,13 +64,13 @@ def _instrument_basic_publish(channel: Channel, tracer: Tracer) -> None:
57
64
58
65
@staticmethod
59
66
def _instrument_channel_functions (
60
- channel : Channel , tracer : Tracer
67
+ channel : BlockingChannel , tracer : Tracer
61
68
) -> None :
62
69
if hasattr (channel , "basic_publish" ):
63
70
PikaInstrumentor ._instrument_basic_publish (channel , tracer )
64
71
65
72
@staticmethod
66
- def _uninstrument_channel_functions (channel : Channel ) -> None :
73
+ def _uninstrument_channel_functions (channel : BlockingChannel ) -> None :
67
74
for function_name in _FUNCTIONS_TO_UNINSTRUMENT :
68
75
if not hasattr (channel , function_name ):
69
76
continue
@@ -73,8 +80,10 @@ def _uninstrument_channel_functions(channel: Channel) -> None:
73
80
unwrap (channel , "basic_consume" )
74
81
75
82
@staticmethod
83
+ # Make sure that the spans are created inside hash them set as parent and not as brothers
76
84
def instrument_channel (
77
- channel : Channel , tracer_provider : Optional [TracerProvider ] = None ,
85
+ channel : BlockingChannel ,
86
+ tracer_provider : Optional [TracerProvider ] = None ,
78
87
) -> None :
79
88
if not hasattr (channel , "_is_instrumented_by_opentelemetry" ):
80
89
channel ._is_instrumented_by_opentelemetry = False
@@ -84,18 +93,14 @@ def instrument_channel(
84
93
)
85
94
return
86
95
tracer = trace .get_tracer (__name__ , __version__ , tracer_provider )
87
- if not hasattr (channel , "_impl" ):
88
- _LOG .error ("Could not find implementation for provided channel!" )
89
- return
90
- if channel ._impl ._consumers :
91
- PikaInstrumentor ._instrument_consumers (
92
- channel ._impl ._consumers , tracer
93
- )
96
+ PikaInstrumentor ._instrument_blocking_channel_consumers (
97
+ channel , tracer
98
+ )
94
99
PikaInstrumentor ._decorate_basic_consume (channel , tracer )
95
100
PikaInstrumentor ._instrument_channel_functions (channel , tracer )
96
101
97
102
@staticmethod
98
- def uninstrument_channel (channel : Channel ) -> None :
103
+ def uninstrument_channel (channel : BlockingChannel ) -> None :
99
104
if (
100
105
not hasattr (channel , "_is_instrumented_by_opentelemetry" )
101
106
or not channel ._is_instrumented_by_opentelemetry
@@ -104,12 +109,12 @@ def uninstrument_channel(channel: Channel) -> None:
104
109
"Attempting to uninstrument Pika channel while already uninstrumented!"
105
110
)
106
111
return
107
- if not hasattr ( channel , "_impl" ):
108
- _LOG . error ( "Could not find implementation for provided channel!" )
109
- return
110
- for key , callback in channel ._impl . _consumers . items ():
111
- if hasattr ( callback , "_original_callback" ):
112
- channel . _impl . _consumers [ key ] = callback ._original_callback
112
+
113
+ for consumers_tag , client_info in channel . _consumer_infos . items ():
114
+ if hasattr ( client_info . on_message_callback , "_original_callback" ):
115
+ channel ._consumer_infos [
116
+ consumers_tag
117
+ ] = client_info . on_message_callback ._original_callback
113
118
PikaInstrumentor ._uninstrument_channel_functions (channel )
114
119
115
120
def _decorate_channel_function (
@@ -123,28 +128,15 @@ def wrapper(wrapped, instance, args, kwargs):
123
128
wrapt .wrap_function_wrapper (BlockingConnection , "channel" , wrapper )
124
129
125
130
@staticmethod
126
- def _decorate_basic_consume (channel , tracer : Optional [Tracer ]) -> None :
131
+ def _decorate_basic_consume (
132
+ channel : BlockingChannel , tracer : Optional [Tracer ]
133
+ ) -> None :
127
134
def wrapper (wrapped , instance , args , kwargs ):
128
- if not hasattr (channel , "_impl" ):
129
- _LOG .error (
130
- "Could not find implementation for provided channel!"
131
- )
132
- return wrapped (* args , ** kwargs )
133
- current_keys = set (channel ._impl ._consumers .keys ())
134
135
return_value = wrapped (* args , ** kwargs )
135
- new_key_list = list (
136
- set (channel ._impl ._consumers .keys ()) - current_keys
137
- )
138
- if not new_key_list :
139
- _LOG .error ("Could not find added callback" )
140
- return return_value
141
- new_key = new_key_list [0 ]
142
- callback = channel ._impl ._consumers [new_key ]
143
- decorated_callback = utils ._decorate_callback (
144
- callback , tracer , new_key
136
+
137
+ PikaInstrumentor ._instrument_blocking_channel_consumers (
138
+ channel , tracer
145
139
)
146
- setattr (decorated_callback , "_original_callback" , callback )
147
- channel ._impl ._consumers [new_key ] = decorated_callback
148
140
return return_value
149
141
150
142
wrapt .wrap_function_wrapper (channel , "basic_consume" , wrapper )
0 commit comments