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.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_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_int_attachment_value(self): timestamp = time.time() attachment = {"One": "one", "Two": 2} with self.assertRaisesRegexp( TypeError, 'attachment value should not be empty and should be a string'): aggregation_data_module.Exemplar(6, timestamp, attachment)
def test_exemplar_null_attachment_key(self): timestamp = time.time() attachment = {None: "one", "Two": "two"} with self.assertRaisesRegexp( TypeError, 'attachment key should not be empty and should be a string'): aggregation_data_module.Exemplar(6, timestamp, attachment)
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_add_sample_attachment(self): mean_data = 1.0 count_data = 1 _min = 0 _max = 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, min_=_min, max_=_max, 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(0, dist_agg_data.min) self.assertEqual(3, dist_agg_data.max) 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, min_=_min, max_=_max, 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_constructor_with_exemplar(self): timestamp = time.time() attachments = {"One": "one", "Two": "two"} exemplar_1 = aggregation_data_module.Exemplar(4, timestamp, attachments) exemplar_2 = aggregation_data_module.Exemplar(5, timestamp, attachments) mean_data = 1 count_data = 0 _min = 0 _max = 1 sum_of_sqd_deviations = mock.Mock() counts_per_bucket = [1, 1, 1, 1] bounds = [0, 1.0 / 2.0, 1] exemplars = [exemplar_1, exemplar_2] dist_agg_data = aggregation_data_module.DistributionAggregationData( mean_data=mean_data, count_data=count_data, min_=_min, max_=_max, sum_of_sqd_deviations=sum_of_sqd_deviations, exemplars=exemplars, 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(0, dist_agg_data.min) self.assertEqual(1, dist_agg_data.max) self.assertEqual(sum_of_sqd_deviations, dist_agg_data.sum_of_sqd_deviations) self.assertEqual([1, 1, 1, 1], dist_agg_data.counts_per_bucket) self.assertEqual([exemplar_1, exemplar_2], dist_agg_data.exemplars[3]) self.assertEqual([0, 1.0 / 2.0, 1], dist_agg_data.bounds) self.assertIsNotNone(dist_agg_data.sum) self.assertEqual(0, dist_agg_data.variance)
def test_exemplar_null_attachments(self): timestamp = time.time() with self.assertRaisesRegexp(TypeError, 'attachments should not be empty'): aggregation_data_module.Exemplar(6, timestamp, None)