12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
+ import sys
15
16
import unittest
16
17
17
18
from opentelemetry import trace
@@ -137,7 +138,7 @@ def test_probability_sampler(self):
137
138
trace .SpanContext (
138
139
0xDEADBEF0 , 0xDEADBEF1 , trace_options = TO_DEFAULT
139
140
),
140
- 0x8000000000000000 ,
141
+ 0x7FFFFFFFFFFFFFFF ,
141
142
0xDEADBEEF ,
142
143
"span name" ,
143
144
).sampled
@@ -147,7 +148,7 @@ def test_probability_sampler(self):
147
148
trace .SpanContext (
148
149
0xDEADBEF0 , 0xDEADBEF1 , trace_options = TO_SAMPLED
149
150
),
150
- 0x8000000000000001 ,
151
+ 0x8000000000000000 ,
151
152
0xDEADBEEF ,
152
153
"span name" ,
153
154
).sampled
@@ -189,14 +190,13 @@ def test_probability_sampler_limits(self):
189
190
sampling .ProbabilitySampler .get_bound_for_rate (2 ** - 64 ), 0x1
190
191
)
191
192
192
- # Sample every trace with (last 8 bytes of) trace ID less than
193
- # 0xffffffffffffffff. In principle this is the highest possible
194
- # sampling rate less than 1, but we can't actually express this rate as
195
- # a float!
193
+ # Sample every trace with trace ID less than 0xffffffffffffffff. In
194
+ # principle this is the highest possible sampling rate less than 1, but
195
+ # we can't actually express this rate as a float!
196
196
#
197
197
# In practice, the highest possible sampling rate is:
198
198
#
199
- # round( sys.float_info.epsilon * 2 ** 64)
199
+ # 1 - sys.float_info.epsilon
200
200
201
201
almost_always_on = sampling .ProbabilitySampler (1 - 2 ** - 64 )
202
202
self .assertTrue (
@@ -212,12 +212,29 @@ def test_probability_sampler_limits(self):
212
212
# self.assertFalse(
213
213
# almost_always_on.should_sample(
214
214
# None,
215
- # 0xffffffffffffffff ,
216
- # 0xdeadbeef ,
215
+ # 0xFFFFFFFFFFFFFFFF ,
216
+ # 0xDEADBEEF ,
217
217
# "span name",
218
218
# ).sampled
219
219
# )
220
220
# self.assertEqual(
221
221
# sampling.ProbabilitySampler.get_bound_for_rate(1 - 2 ** -64)),
222
- # 0xffffffffffffffff ,
222
+ # 0xFFFFFFFFFFFFFFFF ,
223
223
# )
224
+
225
+ # Check that a sampler with the highest effective sampling rate < 1
226
+ # refuses to sample traces with trace ID 0xffffffffffffffff.
227
+ almost_almost_always_on = sampling .ProbabilitySampler (
228
+ 1 - sys .float_info .epsilon
229
+ )
230
+ self .assertFalse (
231
+ almost_almost_always_on .should_sample (
232
+ None , 0xFFFFFFFFFFFFFFFF , 0xDEADBEEF , "span name"
233
+ ).sampled
234
+ )
235
+ # Check that the higest effective sampling rate is actually lower than
236
+ # the highest theoretical sampling rate. If this test fails the test
237
+ # above is wrong.
238
+ self .assertLess (
239
+ almost_almost_always_on .bound , 0xFFFFFFFFFFFFFFFF ,
240
+ )
0 commit comments