@@ -49,80 +49,93 @@ def json(self):
49
49
50
50
51
51
class TestAwsXRayRemoteSampler (TestCase ):
52
+ def setUp (self ):
53
+ self .rs = None
54
+
55
+ def tearDown (self ):
56
+ # Clean up timers
57
+ if self .rs is not None :
58
+ self .rs ._rules_timer .cancel ()
59
+ self .rs ._targets_timer .cancel ()
60
+
52
61
def test_create_remote_sampler_with_empty_resource (self ):
53
- rs = AwsXRayRemoteSampler (resource = Resource .get_empty ())
54
- self .assertIsNotNone (rs ._rules_timer )
55
- self .assertEqual (rs ._AwsXRayRemoteSampler__polling_interval , 300 )
56
- self .assertIsNotNone (rs ._AwsXRayRemoteSampler__xray_client )
57
- self .assertIsNotNone (rs ._AwsXRayRemoteSampler__resource )
58
- self .assertTrue (len (rs ._AwsXRayRemoteSampler__client_id ), 24 )
62
+ self . rs = AwsXRayRemoteSampler (resource = Resource .get_empty ())
63
+ self .assertIsNotNone (self . rs ._rules_timer )
64
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__polling_interval , 300 )
65
+ self .assertIsNotNone (self . rs ._AwsXRayRemoteSampler__xray_client )
66
+ self .assertIsNotNone (self . rs ._AwsXRayRemoteSampler__resource )
67
+ self .assertTrue (len (self . rs ._AwsXRayRemoteSampler__client_id ), 24 )
59
68
60
69
def test_create_remote_sampler_with_populated_resource (self ):
61
- rs = AwsXRayRemoteSampler (
70
+ self . rs = AwsXRayRemoteSampler (
62
71
resource = Resource .create ({"service.name" : "test-service-name" , "cloud.platform" : "test-cloud-platform" })
63
72
)
64
- self .assertIsNotNone (rs ._rules_timer )
65
- self .assertEqual (rs ._AwsXRayRemoteSampler__polling_interval , 300 )
66
- self .assertIsNotNone (rs ._AwsXRayRemoteSampler__xray_client )
67
- self .assertIsNotNone (rs ._AwsXRayRemoteSampler__resource )
68
- self .assertEqual (rs ._AwsXRayRemoteSampler__resource .attributes ["service.name" ], "test-service-name" )
69
- self .assertEqual (rs ._AwsXRayRemoteSampler__resource .attributes ["cloud.platform" ], "test-cloud-platform" )
73
+ self .assertIsNotNone (self . rs ._rules_timer )
74
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__polling_interval , 300 )
75
+ self .assertIsNotNone (self . rs ._AwsXRayRemoteSampler__xray_client )
76
+ self .assertIsNotNone (self . rs ._AwsXRayRemoteSampler__resource )
77
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__resource .attributes ["service.name" ], "test-service-name" )
78
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__resource .attributes ["cloud.platform" ], "test-cloud-platform" )
70
79
71
80
def test_create_remote_sampler_with_all_fields_populated (self ):
72
- rs = AwsXRayRemoteSampler (
81
+ self . rs = AwsXRayRemoteSampler (
73
82
resource = Resource .create ({"service.name" : "test-service-name" , "cloud.platform" : "test-cloud-platform" }),
74
83
endpoint = "http://abc.com" ,
75
84
polling_interval = 120 ,
76
85
log_level = DEBUG ,
77
86
)
78
- self .assertIsNotNone (rs ._rules_timer )
79
- self .assertEqual (rs ._AwsXRayRemoteSampler__polling_interval , 120 )
80
- self .assertIsNotNone (rs ._AwsXRayRemoteSampler__xray_client )
81
- self .assertIsNotNone (rs ._AwsXRayRemoteSampler__resource )
87
+ self .assertIsNotNone (self . rs ._rules_timer )
88
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__polling_interval , 120 )
89
+ self .assertIsNotNone (self . rs ._AwsXRayRemoteSampler__xray_client )
90
+ self .assertIsNotNone (self . rs ._AwsXRayRemoteSampler__resource )
82
91
self .assertEqual (
83
- rs ._AwsXRayRemoteSampler__xray_client ._AwsXRaySamplingClient__get_sampling_rules_endpoint ,
92
+ self . rs ._AwsXRayRemoteSampler__xray_client ._AwsXRaySamplingClient__get_sampling_rules_endpoint ,
84
93
"http://abc.com/GetSamplingRules" ,
85
94
)
86
- self .assertEqual (rs ._AwsXRayRemoteSampler__resource .attributes ["service.name" ], "test-service-name" )
87
- self .assertEqual (rs ._AwsXRayRemoteSampler__resource .attributes ["cloud.platform" ], "test-cloud-platform" )
95
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__resource .attributes ["service.name" ], "test-service-name" )
96
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__resource .attributes ["cloud.platform" ], "test-cloud-platform" )
88
97
89
98
@patch ("requests.Session.post" , side_effect = mocked_requests_get )
90
99
@patch ("amazon.opentelemetry.distro.sampler.aws_xray_remote_sampler.DEFAULT_TARGET_POLLING_INTERVAL_SECONDS" , 2 )
91
100
def test_update_sampling_rules_and_targets_with_pollers_and_should_sample (self , mock_post = None ):
92
- rs = AwsXRayRemoteSampler (
101
+ self . rs = AwsXRayRemoteSampler (
93
102
resource = Resource .create ({"service.name" : "test-service-name" , "cloud.platform" : "test-cloud-platform" })
94
103
)
95
- self .assertEqual (rs ._AwsXRayRemoteSampler__target_polling_interval , 2 )
104
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__target_polling_interval , 2 )
96
105
97
106
time .sleep (1.0 )
98
107
self .assertEqual (
99
- rs ._AwsXRayRemoteSampler__rule_cache ._RuleCache__rule_appliers [0 ].sampling_rule .RuleName , "test"
108
+ self .rs ._AwsXRayRemoteSampler__rule_cache ._RuleCache__rule_appliers [0 ].sampling_rule .RuleName ,
109
+ "test" ,
100
110
)
101
- self .assertEqual (rs .should_sample (None , 0 , "name" , attributes = {"abc" : "1234" }).decision , Decision .DROP )
111
+ self .assertEqual (self . rs .should_sample (None , 0 , "name" , attributes = {"abc" : "1234" }).decision , Decision .DROP )
102
112
103
113
# wait 2 more seconds since targets polling was patched to 2 seconds (rather than 10s)
104
114
time .sleep (2.0 )
105
- self .assertEqual (rs ._AwsXRayRemoteSampler__target_polling_interval , 1000 )
115
+ self .assertEqual (self . rs ._AwsXRayRemoteSampler__target_polling_interval , 1000 )
106
116
self .assertEqual (
107
- rs .should_sample (None , 0 , "name" , attributes = {"abc" : "1234" }).decision , Decision .RECORD_AND_SAMPLE
117
+ self .rs .should_sample (None , 0 , "name" , attributes = {"abc" : "1234" }).decision ,
118
+ Decision .RECORD_AND_SAMPLE ,
108
119
)
109
120
self .assertEqual (
110
- rs .should_sample (None , 0 , "name" , attributes = {"abc" : "1234" }).decision , Decision .RECORD_AND_SAMPLE
121
+ self .rs .should_sample (None , 0 , "name" , attributes = {"abc" : "1234" }).decision ,
122
+ Decision .RECORD_AND_SAMPLE ,
111
123
)
112
124
self .assertEqual (
113
- rs .should_sample (None , 0 , "name" , attributes = {"abc" : "1234" }).decision , Decision .RECORD_AND_SAMPLE
125
+ self .rs .should_sample (None , 0 , "name" , attributes = {"abc" : "1234" }).decision ,
126
+ Decision .RECORD_AND_SAMPLE ,
114
127
)
115
128
116
129
@patch ("requests.Session.post" , side_effect = mocked_requests_get )
117
130
@patch ("amazon.opentelemetry.distro.sampler.aws_xray_remote_sampler.DEFAULT_TARGET_POLLING_INTERVAL_SECONDS" , 3 )
118
131
def test_multithreading_with_large_reservoir_with_otel_sdk (self , mock_post = None ):
119
- rs = AwsXRayRemoteSampler (
132
+ self . rs = AwsXRayRemoteSampler (
120
133
resource = Resource .create ({"service.name" : "test-service-name" , "cloud.platform" : "test-cloud-platform" })
121
134
)
122
135
attributes = {"abc" : "1234" }
123
136
124
137
time .sleep (2.0 )
125
- self .assertEqual (rs .should_sample (None , 0 , "name" , attributes = attributes ).decision , Decision .DROP )
138
+ self .assertEqual (self . rs .should_sample (None , 0 , "name" , attributes = attributes ).decision , Decision .DROP )
126
139
127
140
# wait 3 more seconds since targets polling was patched to 2 seconds (rather than 10s)
128
141
time .sleep (3.0 )
@@ -139,7 +152,7 @@ def test_multithreading_with_large_reservoir_with_otel_sdk(self, mock_post=None)
139
152
target = create_spans ,
140
153
name = "thread_" + str (idx ),
141
154
daemon = True ,
142
- args = (sampled_array , idx , attributes , rs , number_of_spans ),
155
+ args = (sampled_array , idx , attributes , self . rs , number_of_spans ),
143
156
)
144
157
)
145
158
threads [idx ].start ()
@@ -149,7 +162,7 @@ def test_multithreading_with_large_reservoir_with_otel_sdk(self, mock_post=None)
149
162
threads [idx ].join ()
150
163
sum_sampled += sampled_array [idx ]
151
164
152
- test_rule_applier = rs ._AwsXRayRemoteSampler__rule_cache ._RuleCache__rule_appliers [0 ]
165
+ test_rule_applier = self . rs ._AwsXRayRemoteSampler__rule_cache ._RuleCache__rule_appliers [0 ]
153
166
self .assertEqual (
154
167
test_rule_applier ._SamplingRuleApplier__reservoir_sampler ._root ._RateLimitingSampler__reservoir ._quota ,
155
168
100000 ,
@@ -161,7 +174,7 @@ def test_multithreading_with_large_reservoir_with_otel_sdk(self, mock_post=None)
161
174
@patch ("amazon.opentelemetry.distro.sampler.aws_xray_remote_sampler.DEFAULT_TARGET_POLLING_INTERVAL_SECONDS" , 2 )
162
175
@patch ("amazon.opentelemetry.distro.sampler.aws_xray_remote_sampler._Clock" , MockClock )
163
176
def test_multithreading_with_some_reservoir_with_otel_sdk (self , mock_post = None ):
164
- rs = AwsXRayRemoteSampler (
177
+ self . rs = AwsXRayRemoteSampler (
165
178
resource = Resource .create ({"service.name" : "test-service-name" , "cloud.platform" : "test-cloud-platform" })
166
179
)
167
180
attributes = {"abc" : "non-matching attribute value, use default rule" }
@@ -170,17 +183,19 @@ def test_multithreading_with_some_reservoir_with_otel_sdk(self, mock_post=None):
170
183
# which will eat up more than 1 second of reservoir. Using MockClock we can freeze time
171
184
# and pretend all thread jobs start and end at the exact same time,
172
185
# assume and test exactly 1 second of reservoir (100 quota) only
173
- mock_clock : MockClock = rs ._clock
186
+ mock_clock : MockClock = self . rs ._clock
174
187
175
188
time .sleep (1.0 )
176
189
mock_clock .add_time (1.0 )
177
- self .assertEqual (mock_clock .now (), rs ._clock .now ())
178
- self .assertEqual (rs .should_sample (None , 0 , "name" , attributes = attributes ).decision , Decision .RECORD_AND_SAMPLE )
190
+ self .assertEqual (mock_clock .now (), self .rs ._clock .now ())
191
+ self .assertEqual (
192
+ self .rs .should_sample (None , 0 , "name" , attributes = attributes ).decision , Decision .RECORD_AND_SAMPLE
193
+ )
179
194
180
195
# wait 2 more seconds since targets polling was patched to 2 seconds (rather than 10s)
181
196
time .sleep (2.0 )
182
197
mock_clock .add_time (2.0 )
183
- self .assertEqual (mock_clock .now (), rs ._clock .now ())
198
+ self .assertEqual (mock_clock .now (), self . rs ._clock .now ())
184
199
185
200
number_of_spans = 100
186
201
thread_count = 1000
@@ -194,7 +209,7 @@ def test_multithreading_with_some_reservoir_with_otel_sdk(self, mock_post=None):
194
209
target = create_spans ,
195
210
name = "thread_" + str (idx ),
196
211
daemon = True ,
197
- args = (sampled_array , idx , attributes , rs , number_of_spans ),
212
+ args = (sampled_array , idx , attributes , self . rs , number_of_spans ),
198
213
)
199
214
)
200
215
threads [idx ].start ()
@@ -204,7 +219,7 @@ def test_multithreading_with_some_reservoir_with_otel_sdk(self, mock_post=None):
204
219
threads [idx ].join ()
205
220
sum_sampled += sampled_array [idx ]
206
221
207
- default_rule_applier = rs ._AwsXRayRemoteSampler__rule_cache ._RuleCache__rule_appliers [1 ]
222
+ default_rule_applier = self . rs ._AwsXRayRemoteSampler__rule_cache ._RuleCache__rule_appliers [1 ]
208
223
self .assertEqual (
209
224
default_rule_applier ._SamplingRuleApplier__reservoir_sampler ._root ._RateLimitingSampler__reservoir ._quota ,
210
225
100 ,
0 commit comments