This repository was archived by the owner on Sep 17, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 247
Expand file tree
/
Copy pathtest_aggregation_data.py
More file actions
537 lines (457 loc) · 21.3 KB
/
test_aggregation_data.py
File metadata and controls
537 lines (457 loc) · 21.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
# Copyright 2018, OpenCensus Authors
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import time
import unittest
from datetime import datetime
import mock
from opencensus.metrics.export import point
from opencensus.metrics.export import value as value_module
from opencensus.stats import aggregation_data as aggregation_data_module
class TestSumAggregationData(unittest.TestCase):
def test_constructor_float(self):
sum_data = 1.0
sum_aggregation_data = aggregation_data_module.SumAggregationData(
value_type=value_module.ValueDouble, sum_data=sum_data)
self.assertEqual(1.0, sum_aggregation_data.sum_data)
def test_constructor_int(self):
sum_data = 1
sum_aggregation_data = aggregation_data_module.SumAggregationData(
value_type=value_module.ValueLong, sum_data=sum_data)
self.assertEqual(1, sum_aggregation_data.sum_data)
def test_add_sample_float(self):
sum_data = 1
value = 3.5
sum_aggregation_data = aggregation_data_module.SumAggregationData(
value_type=value_module.ValueDouble, sum_data=sum_data)
sum_aggregation_data.add_sample(value, None, None)
self.assertEqual(4.5, sum_aggregation_data.sum_data)
def test_add_sample_int(self):
sum_data = 1
value = 3
sum_aggregation_data = aggregation_data_module.SumAggregationData(
value_type=value_module.ValueLong, sum_data=sum_data)
sum_aggregation_data.add_sample(value, None, None)
self.assertEqual(4, sum_aggregation_data.sum_data)
def test_to_point_float(self):
sum_data = 12.345
timestamp = datetime(1970, 1, 1)
agg = aggregation_data_module.SumAggregationData(
value_type=value_module.ValueDouble, sum_data=sum_data)
converted_point = agg.to_point(timestamp)
self.assertTrue(isinstance(converted_point, point.Point))
self.assertTrue(isinstance(converted_point.value,
value_module.ValueDouble))
self.assertEqual(converted_point.value.value, sum_data)
self.assertEqual(converted_point.timestamp, timestamp)
def test_to_point_int(self):
sum_data = 12
timestamp = datetime(1970, 1, 1)
agg = aggregation_data_module.SumAggregationData(
value_type=value_module.ValueLong, sum_data=sum_data)
converted_point = agg.to_point(timestamp)
self.assertTrue(isinstance(converted_point, point.Point))
self.assertTrue(isinstance(converted_point.value,
value_module.ValueLong))
self.assertEqual(converted_point.value.value, sum_data)
self.assertEqual(converted_point.timestamp, timestamp)
class TestLastValueAggregationData(unittest.TestCase):
def test_constructor_float(self):
value_data = 0.0
last_value_aggregation_data =\
aggregation_data_module.LastValueAggregationData(
value_type=value_module.ValueDouble, value=value_data)
self.assertEqual(0.0, last_value_aggregation_data.value)
def test_constructor_int(self):
value_data = 0
last_value_aggregation_data =\
aggregation_data_module.LastValueAggregationData(
value_type=value_module.ValueLong, value=value_data)
self.assertEqual(0, last_value_aggregation_data.value)
def test_overwrite_sample_float(self):
first_data = 0
last_value_aggregation_data =\
aggregation_data_module.LastValueAggregationData(
value_type=value_module.ValueDouble, value=first_data)
self.assertEqual(0, last_value_aggregation_data.value)
last_value_aggregation_data.add_sample(1.2, None, None)
self.assertEqual(1.2, last_value_aggregation_data.value)
def test_overwrite_sample_int(self):
first_data = 0
last_value_aggregation_data =\
aggregation_data_module.LastValueAggregationData(
value_type=value_module.ValueLong, value=first_data)
self.assertEqual(0, last_value_aggregation_data.value)
last_value_aggregation_data.add_sample(1, None, None)
self.assertEqual(1, last_value_aggregation_data.value)
def test_to_point_float(self):
val = 1.2
timestamp = datetime(1970, 1, 1)
agg = aggregation_data_module.LastValueAggregationData(
value_type=value_module.ValueDouble, value=val)
converted_point = agg.to_point(timestamp)
self.assertTrue(isinstance(converted_point, point.Point))
self.assertTrue(isinstance(converted_point.value,
value_module.ValueDouble))
self.assertEqual(converted_point.value.value, val)
self.assertEqual(converted_point.timestamp, timestamp)
def test_to_pointInt(self):
val = 1
timestamp = datetime(1970, 1, 1)
agg = aggregation_data_module.LastValueAggregationData(
value_type=value_module.ValueLong, value=val)
converted_point = agg.to_point(timestamp)
self.assertTrue(isinstance(converted_point, point.Point))
self.assertTrue(isinstance(converted_point.value,
value_module.ValueLong))
self.assertEqual(converted_point.value.value, val)
self.assertEqual(converted_point.timestamp, timestamp)
def exemplars_equal(stats_ex, metrics_ex):
"""Compare a stats exemplar to a metrics exemplar."""
assert isinstance(stats_ex, aggregation_data_module.Exemplar)
assert isinstance(metrics_ex, value_module.Exemplar)
return (stats_ex.value == metrics_ex.value and
stats_ex.timestamp == metrics_ex.timestamp and
stats_ex.attachments == metrics_ex.attachments)
class TestDistributionAggregationData(unittest.TestCase):
def test_constructor(self):
mean_data = 1
count_data = 0
sum_of_sqd_deviations = mock.Mock()
counts_per_bucket = [1, 1, 1]
bounds = [1.0 / 2.0, 1]
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
self.assertEqual(1, dist_agg_data.mean_data)
self.assertEqual(0, dist_agg_data.count_data)
self.assertEqual(sum_of_sqd_deviations,
dist_agg_data.sum_of_sqd_deviations)
self.assertEqual([1, 1, 1], dist_agg_data.counts_per_bucket)
self.assertEqual([1.0 / 2.0, 1], dist_agg_data.bounds)
self.assertIsNotNone(dist_agg_data.sum)
self.assertEqual(0, dist_agg_data.variance)
def test_init_bad_bucket_counts(self):
# Check that len(counts_per_bucket) == len(bounds) + 1
with self.assertRaises(AssertionError):
aggregation_data_module.DistributionAggregationData(
mean_data=mock.Mock(),
count_data=mock.Mock(),
sum_of_sqd_deviations=mock.Mock(),
counts_per_bucket=[0, 0, 0],
bounds=[1, 2, 3])
# Check that counts aren't negative
with self.assertRaises(AssertionError):
aggregation_data_module.DistributionAggregationData(
mean_data=mock.Mock(),
count_data=mock.Mock(),
sum_of_sqd_deviations=mock.Mock(),
counts_per_bucket=[0, 2, -2, 0],
bounds=[1, 2, 3])
# And check that we don't throw given the right args
aggregation_data_module.DistributionAggregationData(
mean_data=mock.Mock(),
count_data=mock.Mock(),
sum_of_sqd_deviations=mock.Mock(),
counts_per_bucket=[0, 0, 0, 0],
bounds=[1, 2, 3])
def test_init_bad_bounds(self):
# Check that bounds are unique
with self.assertRaises(AssertionError):
aggregation_data_module.DistributionAggregationData(
mean_data=mock.Mock(),
count_data=mock.Mock(),
sum_of_sqd_deviations=mock.Mock(),
counts_per_bucket=[0, 0, 0, 0],
bounds=[1, 2, 2])
# Check that bounds are sorted
with self.assertRaises(AssertionError):
aggregation_data_module.DistributionAggregationData(
mean_data=mock.Mock(),
count_data=mock.Mock(),
sum_of_sqd_deviations=mock.Mock(),
counts_per_bucket=[0, 0, 0, 0],
bounds=[1, 3, 2])
# Check that all bounds are positive
with self.assertRaises(AssertionError):
aggregation_data_module.DistributionAggregationData(
mean_data=mock.Mock(),
count_data=mock.Mock(),
sum_of_sqd_deviations=mock.Mock(),
counts_per_bucket=[0, 0, 0, 0],
bounds=[-1, 1, 2])
def test_init_bad_exemplars(self):
# Check that we don't allow exemplars without bounds
with self.assertRaises(ValueError):
aggregation_data_module.DistributionAggregationData(
mean_data=mock.Mock(),
count_data=mock.Mock(),
sum_of_sqd_deviations=mock.Mock(),
counts_per_bucket=mock.Mock(),
bounds=None,
exemplars=[mock.Mock()])
# Check that the exemplar count matches the bucket count
with self.assertRaises(ValueError):
aggregation_data_module.DistributionAggregationData(
mean_data=mock.Mock(),
count_data=mock.Mock(),
sum_of_sqd_deviations=mock.Mock(),
counts_per_bucket=mock.Mock(),
bounds=[0, 1],
exemplars=[mock.Mock(), mock.Mock()])
def test_constructor_with_exemplar(self):
timestamp = time.time()
attachments = {"One": "one", "Two": "two"}
exemplars = [
aggregation_data_module.Exemplar(.07, timestamp, attachments),
aggregation_data_module.Exemplar(.7, timestamp, attachments),
aggregation_data_module.Exemplar(7, timestamp, attachments)
]
mean_data = 2.59
count_data = 3
sum_of_sqd_deviations = mock.Mock()
counts_per_bucket = [1, 1, 1]
bounds = [1.0 / 2.0, 1]
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
exemplars=exemplars,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
self.assertEqual(dist_agg_data.mean_data, mean_data)
self.assertEqual(dist_agg_data.count_data, count_data)
self.assertEqual(dist_agg_data.sum_of_sqd_deviations,
sum_of_sqd_deviations)
self.assertEqual(dist_agg_data.counts_per_bucket, counts_per_bucket)
self.assertEqual(dist_agg_data.bounds, bounds)
self.assertEqual(dist_agg_data.sum, mean_data * count_data)
for ii, ex in enumerate(exemplars):
self.assertEqual(dist_agg_data.exemplars[ii], ex)
def test_exemplar(self):
timestamp = time.time()
attachments = {"One": "one", "Two": "two"}
exemplar = aggregation_data_module.Exemplar(4, timestamp, attachments)
self.assertEqual(4, exemplar.value)
self.assertEqual(timestamp, exemplar.timestamp)
self.assertEqual(attachments, exemplar.attachments)
def test_exemplar_null_attachments(self):
timestamp = time.time()
with self.assertRaisesRegex(TypeError,
'attachments should not be empty'):
aggregation_data_module.Exemplar(6, timestamp, None)
def test_exemplar_null_attachment_key(self):
timestamp = time.time()
attachment = {None: "one", "Two": "two"}
with self.assertRaisesRegex(
TypeError,
'attachment key should not be empty and should be a string'):
aggregation_data_module.Exemplar(6, timestamp, attachment)
def test_exemplar_null_attachment_value(self):
timestamp = time.time()
attachment = {"One": "one", "Two": None}
with self.assertRaisesRegex(
TypeError,
'attachment value should not be empty and should be a string'):
aggregation_data_module.Exemplar(6, timestamp, attachment)
def test_exemplar_int_attachment_key(self):
timestamp = time.time()
attachment = {1: "one", "Two": "two"}
with self.assertRaisesRegex(
TypeError,
'attachment key should not be empty and should be a string'):
aggregation_data_module.Exemplar(6, timestamp, attachment)
def test_exemplar_int_attachment_value(self):
timestamp = time.time()
attachment = {"One": "one", "Two": 2}
with self.assertRaisesRegex(
TypeError,
'attachment value should not be empty and should be a string'):
aggregation_data_module.Exemplar(6, timestamp, attachment)
def test_variance(self):
mean_data = mock.Mock()
count_data = 0
sum_of_sqd_deviations = mock.Mock()
counts_per_bucket = [1, 1, 1]
bounds = [1.0 / 2.0, 1]
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
self.assertEqual(0, dist_agg_data.variance)
count_data = 2
sum_of_sqd_deviations = 2
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
self.assertEqual(2.0, dist_agg_data.variance)
def test_add_sample(self):
mean_data = 1.0
count_data = 0
sum_of_sqd_deviations = 2
counts_per_bucket = [1, 1, 1, 1]
bounds = [0.5, 1, 1.5]
value = 3
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
dist_agg_data.add_sample(value, None, None)
self.assertEqual(1, dist_agg_data.count_data)
self.assertEqual(value, dist_agg_data.mean_data)
count_data = 1
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
dist_agg_data.add_sample(value, None, None)
self.assertEqual(2, dist_agg_data.count_data)
self.assertEqual(2.0, dist_agg_data.mean_data)
self.assertEqual(4.0, dist_agg_data.sum_of_sqd_deviations)
self.assertIsNot(0, dist_agg_data.count_data)
def test_add_sample_attachment(self):
mean_data = 1.0
count_data = 1
sum_of_sqd_deviations = 2
counts_per_bucket = [1, 1, 1, 1]
bounds = [0.5, 1, 1.5]
value = 3
timestamp = time.time()
attachments = {"One": "one", "Two": "two"}
exemplar_1 = aggregation_data_module.Exemplar(4, timestamp,
attachments)
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds,
exemplars=[None, None, None, exemplar_1])
self.assertEqual(dist_agg_data.exemplars[3], exemplar_1)
dist_agg_data.add_sample(value, timestamp, attachments)
self.assertEqual(2, dist_agg_data.count_data)
self.assertEqual(2.0, dist_agg_data.mean_data)
# Check that adding a sample overwrites the bucket's exemplar
self.assertNotEqual(dist_agg_data.exemplars[3], exemplar_1)
self.assertEqual(dist_agg_data.exemplars[3].value, 3)
self.assertEqual(dist_agg_data.exemplars[3].attachments, attachments)
count_data = 4
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=[2, 1, 2, 1, 1, 1],
bounds=[1, 2, 3, 4, 5])
dist_agg_data.add_sample(value, timestamp, attachments)
self.assertEqual(5, dist_agg_data.count_data)
self.assertEqual(1.4, dist_agg_data.mean_data)
self.assertEqual(5.2, dist_agg_data.sum_of_sqd_deviations)
self.assertIsNot(0, dist_agg_data.count_data)
self.assertEqual(3, dist_agg_data.exemplars[3].value)
def test_increment_bucket_count(self):
mean_data = mock.Mock()
count_data = mock.Mock()
sum_of_sqd_deviations = mock.Mock()
counts_per_bucket = [0]
bounds = []
value = 1
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
dist_agg_data.increment_bucket_count(value=value)
self.assertEqual([1], dist_agg_data.counts_per_bucket)
counts_per_bucket = [1, 1, 1]
bounds = [1.0 / 4.0, 3.0 / 2.0]
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
dist_agg_data.increment_bucket_count(value=value)
self.assertEqual([1, 2, 1], dist_agg_data.counts_per_bucket)
bounds = [1.0 / 4.0, 1.0 / 2.0]
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=mean_data,
count_data=count_data,
sum_of_sqd_deviations=sum_of_sqd_deviations,
counts_per_bucket=counts_per_bucket,
bounds=bounds)
dist_agg_data.increment_bucket_count(value=value)
self.assertEqual([1, 2, 2], dist_agg_data.counts_per_bucket)
def test_to_point(self):
timestamp = datetime(1970, 1, 1)
ex_9 = aggregation_data_module.Exemplar(
9, timestamp, {'trace_id': 'dead', 'span_id': 'beef'}
)
ex_99 = aggregation_data_module.Exemplar(
99, timestamp, {'trace_id': 'dead', 'span_id': 'bef0'}
)
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=50,
count_data=99,
sum_of_sqd_deviations=80850.0,
counts_per_bucket=[0, 9, 90, 0],
bounds=[1, 10, 100],
exemplars=[None, ex_9, ex_99, None],
)
converted_point = dist_agg_data.to_point(timestamp)
self.assertTrue(isinstance(converted_point.value,
value_module.ValueDistribution))
self.assertEqual(converted_point.value.count, 99)
self.assertEqual(converted_point.value.sum, 4950)
self.assertEqual(converted_point.value.sum_of_squared_deviation,
80850.0)
self.assertEqual([bb.count for bb in converted_point.value.buckets],
[0, 9, 90, 0])
self.assertEqual(converted_point.value.bucket_options.type_.bounds,
[1, 10, 100])
self.assertTrue(
exemplars_equal(
ex_9,
converted_point.value.buckets[1].exemplar))
self.assertTrue(
exemplars_equal(
ex_99,
converted_point.value.buckets[2].exemplar))
def test_to_point_no_histogram(self):
timestamp = datetime(1970, 1, 1)
dist_agg_data = aggregation_data_module.DistributionAggregationData(
mean_data=50,
count_data=99,
sum_of_sqd_deviations=80850.0,
)
converted_point = dist_agg_data.to_point(timestamp)
self.assertTrue(isinstance(converted_point.value,
value_module.ValueDistribution))
self.assertEqual(converted_point.value.count, 99)
self.assertEqual(converted_point.value.sum, 4950)
self.assertEqual(converted_point.value.sum_of_squared_deviation,
80850.0)
self.assertIsNone(converted_point.value.buckets)
self.assertIsNone(converted_point.value.bucket_options._type)