1
- import pika
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.
2
14
from logging import getLogger
15
+ from typing import Any , Callable , Collection , Dict , Optional
16
+
3
17
from pika .channel import Channel
4
- from pika .adapters import BaseConnection
5
- from typing import Dict , Callable , Optional , Collection , Any
18
+
6
19
from opentelemetry import trace
7
- from opentelemetry .propagate import inject
20
+ from opentelemetry .instrumentation . instrumentor import BaseInstrumentor
8
21
from opentelemetry .instrumentation .pika import utils
9
- from opentelemetry .trace import Tracer , TracerProvider
10
- from opentelemetry .instrumentation .pika import __version__
11
- from opentelemetry .semconv .trace import MessagingOperationValues
12
22
from opentelemetry .instrumentation .pika .package import _instruments
13
- from opentelemetry .instrumentation .instrumentor import BaseInstrumentor
14
-
23
+ from opentelemetry .instrumentation .pika . version import __version__
24
+ from opentelemetry . trace import Tracer , TracerProvider
15
25
16
26
_LOG = getLogger (__name__ )
17
27
CTX_KEY = "__otel_task_span"
@@ -25,58 +35,17 @@ def _instrument_consumers(
25
35
consumers_dict : Dict [str , Callable [..., Any ]], tracer : Tracer
26
36
) -> Any :
27
37
for key , callback in consumers_dict .items ():
28
-
29
- def decorated_callback (
30
- channel : Channel ,
31
- method : pika .spec .Basic .Deliver ,
32
- properties : pika .spec .BasicProperties ,
33
- body : bytes ,
34
- ) -> Any :
35
- if not properties :
36
- properties = pika .spec .BasicProperties ()
37
- span = utils .get_span (
38
- tracer ,
39
- channel ,
40
- properties ,
41
- task_name = key ,
42
- operation = MessagingOperationValues .RECEIVE ,
43
- )
44
- with trace .use_span (span , end_on_exit = True ):
45
- inject (properties .headers )
46
- retval = callback (channel , method , properties , body )
47
- return retval
48
-
49
- decorated_callback .__setattr__ ("_original_callback" , callback )
38
+ decorated_callback = utils .decorate_callback (callback , tracer , key )
39
+ setattr (decorated_callback , "_original_callback" , callback )
50
40
consumers_dict [key ] = decorated_callback
51
41
52
42
@staticmethod
53
43
def _instrument_basic_publish (channel : Channel , tracer : Tracer ) -> None :
54
44
original_function = getattr (channel , "basic_publish" )
55
-
56
- def decorated_function (
57
- exchange : str ,
58
- routing_key : str ,
59
- body : bytes ,
60
- properties : pika .spec .BasicProperties = None ,
61
- mandatory : bool = False ,
62
- ) -> Any :
63
- if not properties :
64
- properties = pika .spec .BasicProperties ()
65
- span = utils .get_span (
66
- tracer ,
67
- channel ,
68
- properties ,
69
- task_name = "(temporary)" ,
70
- operation = None ,
71
- )
72
- with trace .use_span (span , end_on_exit = True ):
73
- inject (properties .headers )
74
- retval = original_function (
75
- exchange , routing_key , body , properties , mandatory
76
- )
77
- return retval
78
-
79
- decorated_function .__setattr__ ("_original_function" , original_function )
45
+ decorated_function = utils .decorate_basic_publish (
46
+ original_function , channel , tracer
47
+ )
48
+ setattr (decorated_function , "_original_function" , original_function )
80
49
channel .__setattr__ ("basic_publish" , decorated_function )
81
50
channel .basic_publish = decorated_function
82
51
@@ -98,12 +67,9 @@ def _uninstrument_channel_functions(channel: Channel) -> None:
98
67
99
68
@staticmethod
100
69
def instrument_channel (
101
- channel : Channel ,
102
- tracer_provider : Optional [TracerProvider ] = None ,
70
+ channel : Channel , tracer_provider : Optional [TracerProvider ] = None ,
103
71
) -> None :
104
- if not hasattr (channel , "_impl" ) or not isinstance (
105
- channel ._impl , Channel
106
- ):
72
+ if not hasattr (channel , "_impl" ):
107
73
_LOG .error ("Could not find implementation for provided channel!" )
108
74
return
109
75
tracer = trace .get_tracer (__name__ , __version__ , tracer_provider )
@@ -119,20 +85,20 @@ def _instrument(self, **kwargs: Dict[str, Any]) -> None:
119
85
if not channel or not isinstance (channel , Channel ):
120
86
return
121
87
tracer_provider : TracerProvider = kwargs .get ("tracer_provider" , None )
122
- PikaInstrumentation .instrument_channel (channel , tracer_provider = tracer_provider )
88
+ PikaInstrumentation .instrument_channel (
89
+ channel , tracer_provider = tracer_provider
90
+ )
123
91
124
92
def _uninstrument (self , ** kwargs : Dict [str , Any ]) -> None :
125
93
channel : Channel = kwargs .get ("channel" , None )
126
94
if not channel or not isinstance (channel , Channel ):
127
95
return
128
- if not hasattr (channel , "_impl" ) or not isinstance (
129
- channel ._impl , BaseConnection
130
- ):
96
+ if not hasattr (channel , "_impl" ):
131
97
_LOG .error ("Could not find implementation for provided channel!" )
132
98
return
133
- for key , callback in channel ._impl ._consumers :
99
+ for key , callback in channel ._impl ._consumers . items () :
134
100
if hasattr (callback , "_original_callback" ):
135
- channel ._consumers [key ] = callback ._original_callback
101
+ channel ._impl . _consumers [key ] = callback ._original_callback
136
102
PikaInstrumentation ._uninstrument_channel_functions (channel )
137
103
138
104
def instrumentation_dependencies (self ) -> Collection [str ]:
0 commit comments