20
20
21
21
import boto3
22
22
from botocore .awsrequest import AWSResponse
23
- from wrapt import BoundFunctionWrapper , FunctionWrapper
23
+ from wrapt import BoundFunctionWrapper
24
24
25
25
from opentelemetry .instrumentation .boto3sqs import (
26
26
Boto3SQSGetter ,
37
37
from opentelemetry .trace .span import Span , format_span_id , format_trace_id
38
38
39
39
40
- def _make_sqs_client ():
41
- return boto3 .client (
40
+ def _make_sqs_client (* , session = False ):
41
+ return (boto3 .Session () if session else boto3 ).client (
42
+ "sqs" ,
43
+ region_name = "us-east-1" ,
44
+ aws_access_key_id = "dummy" ,
45
+ aws_secret_access_key = "dummy" ,
46
+ )
47
+
48
+
49
+ def _make_sqs_resource (* , session = False ):
50
+ return (boto3 .Session () if session else boto3 ).resource (
42
51
"sqs" ,
43
52
region_name = "us-east-1" ,
44
53
aws_access_key_id = "dummy" ,
@@ -48,7 +57,6 @@ def _make_sqs_client():
48
57
49
58
class TestBoto3SQSInstrumentor (TestCase ):
50
59
def _assert_instrumented (self , client ):
51
- self .assertIsInstance (boto3 .client , FunctionWrapper )
52
60
self .assertIsInstance (client .send_message , BoundFunctionWrapper )
53
61
self .assertIsInstance (client .send_message_batch , BoundFunctionWrapper )
54
62
self .assertIsInstance (client .receive_message , BoundFunctionWrapper )
@@ -57,6 +65,17 @@ def _assert_instrumented(self, client):
57
65
client .delete_message_batch , BoundFunctionWrapper
58
66
)
59
67
68
+ def _assert_uninstrumented (self , client ):
69
+ self .assertNotIsInstance (client .send_message , BoundFunctionWrapper )
70
+ self .assertNotIsInstance (
71
+ client .send_message_batch , BoundFunctionWrapper
72
+ )
73
+ self .assertNotIsInstance (client .receive_message , BoundFunctionWrapper )
74
+ self .assertNotIsInstance (client .delete_message , BoundFunctionWrapper )
75
+ self .assertNotIsInstance (
76
+ client .delete_message_batch , BoundFunctionWrapper
77
+ )
78
+
60
79
@staticmethod
61
80
@contextmanager
62
81
def _active_instrumentor ():
@@ -67,19 +86,48 @@ def _active_instrumentor():
67
86
Boto3SQSInstrumentor ().uninstrument ()
68
87
69
88
def test_instrument_api_before_client_init (self ) -> None :
70
- with self ._active_instrumentor ():
71
- client = _make_sqs_client ()
72
- self ._assert_instrumented (client )
89
+ for session in (False , True ):
90
+ with self ._active_instrumentor ():
91
+ client = _make_sqs_client (session = session )
92
+ self ._assert_instrumented (client )
93
+ self ._assert_uninstrumented (client )
73
94
74
95
def test_instrument_api_after_client_init (self ) -> None :
75
- client = _make_sqs_client ()
76
- with self ._active_instrumentor ():
77
- self ._assert_instrumented (client )
96
+ for session in (False , True ):
97
+ client = _make_sqs_client (session = session )
98
+ with self ._active_instrumentor ():
99
+ self ._assert_instrumented (client )
100
+ self ._assert_uninstrumented (client )
78
101
79
102
def test_instrument_multiple_clients (self ):
80
- with self ._active_instrumentor ():
81
- self ._assert_instrumented (_make_sqs_client ())
82
- self ._assert_instrumented (_make_sqs_client ())
103
+ for session in (False , True ):
104
+ with self ._active_instrumentor ():
105
+ self ._assert_instrumented (_make_sqs_client (session = session ))
106
+ self ._assert_instrumented (_make_sqs_client (session = session ))
107
+
108
+ def test_instrument_api_before_resource_init (self ) -> None :
109
+ for session in (False , True ):
110
+ with self ._active_instrumentor ():
111
+ sqs = _make_sqs_resource (session = session )
112
+ self ._assert_instrumented (sqs .meta .client )
113
+ self ._assert_uninstrumented (sqs .meta .client )
114
+
115
+ def test_instrument_api_after_resource_init (self ) -> None :
116
+ for session in (False , True ):
117
+ sqs = _make_sqs_resource (session = session )
118
+ with self ._active_instrumentor ():
119
+ self ._assert_instrumented (sqs .meta .client )
120
+ self ._assert_uninstrumented (sqs .meta .client )
121
+
122
+ def test_instrument_multiple_resources (self ):
123
+ for session in (False , True ):
124
+ with self ._active_instrumentor ():
125
+ self ._assert_instrumented (
126
+ _make_sqs_resource (session = session ).meta .client
127
+ )
128
+ self ._assert_instrumented (
129
+ _make_sqs_resource (session = session ).meta .client
130
+ )
83
131
84
132
85
133
class TestBoto3SQSGetter (TestCase ):
0 commit comments