def test_generate_every_type_of_field(self): counter = metrics.CounterMetric('counter', 'desc', [ metrics.IntegerField('a'), metrics.BooleanField('b'), metrics.StringField('c'), ]) interface.register(counter) counter.increment({'a': 1, 'b': True, 'c': 'test'}) proto = list(interface._generate_proto())[0] data_set = proto.metrics_collection[0].metrics_data_set[0] field_type = metrics_pb2.MetricsDataSet.MetricFieldDescriptor self.assertEqual('a', data_set.field_descriptor[0].name) self.assertEqual(field_type.INT64, data_set.field_descriptor[0].field_type) self.assertEqual('b', data_set.field_descriptor[1].name) self.assertEqual(field_type.BOOL, data_set.field_descriptor[1].field_type) self.assertEqual('c', data_set.field_descriptor[2].name) self.assertEqual(field_type.STRING, data_set.field_descriptor[2].field_type) self.assertEqual(1, data_set.data[0].int64_value) self.assertEqual('a', data_set.data[0].field[0].name) self.assertEqual(1, data_set.data[0].field[0].int64_value) self.assertEqual('b', data_set.data[0].field[1].name) self.assertTrue(data_set.data[0].field[1].bool_value) self.assertEqual('c', data_set.data[0].field[2].name) self.assertEqual('test', data_set.data[0].field[2].string_value)
def test_generate_proto_with_dangerously_set_start_time(self): counter0 = metrics.CounterMetric('counter0', 'desc0', [metrics.IntegerField('test')]) interface.register(counter0) counter0.increment_by(3, {'test': 123}) counter0.increment_by(3, {'test': 456}) counter0.dangerously_set_start_time(1000) proto = list(interface._generate_proto())[0] data_set = proto.metrics_collection[0].metrics_data_set[0] for d in data_set.data: self.assertEqual(1000, d.start_timestamp.seconds) counter0.dangerously_set_start_time(2000) proto = list(interface._generate_proto())[0] data_set = proto.metrics_collection[0].metrics_data_set[0] for d in data_set.data: self.assertEqual(2000, d.start_timestamp.seconds)
def _test_proto(self, metric, set_fn, value_type, stream_kind): self.time_fn.return_value = 100.3 interface.register(metric) set_fn(metric) self.time_fn.return_value = 1000.6 proto = list(interface._generate_proto())[0] data_set = proto.metrics_collection[0].metrics_data_set[0] data = data_set.data[0] self.assertEqual(stream_kind, data_set.stream_kind) self.assertEqual(value_type, data_set.value_type) self.assertEqual(100, data.start_timestamp.seconds) self.assertEqual(1000, data.end_timestamp.seconds) self.assertFalse(data_set.annotations.HasField('unit')) return data
def test_grouping(self): counter0 = metrics.CounterMetric('counter0', 'desc0', [metrics.IntegerField('test')]) counter1 = metrics.CounterMetric('counter1', 'desc1', None) counter2 = metrics.CounterMetric('counter2', 'desc2', None) interface.register(counter0) interface.register(counter1) interface.register(counter2) counter0.increment_by(3, {'test': 123}) counter0.increment_by(5, {'test': 999}) counter1.increment() counter2.increment_by(4, target_fields={'task_num': 1}) protos = list(interface._generate_proto()) self.assertEqual(1, len(protos)) proto = protos[0] self.assertEqual(2, len(proto.metrics_collection)) for coll in proto.metrics_collection: self.assertEqual('service', coll.task.service_name) self.assertEqual('job', coll.task.job_name) self.assertEqual('region', coll.task.data_center) self.assertEqual('hostname', coll.task.host_name) first_coll = proto.metrics_collection[0] second_coll = proto.metrics_collection[1] self.assertEqual(0, first_coll.task.task_num) self.assertEqual(1, second_coll.task.task_num) self.assertEqual(2, len(first_coll.metrics_data_set)) self.assertEqual(1, len(second_coll.metrics_data_set)) data_sets = [ first_coll.metrics_data_set[0], first_coll.metrics_data_set[1], second_coll.metrics_data_set[0] ] for i, data_set in enumerate(data_sets): self.assertEqual('/infra/test/counter%d' % i, data_set.metric_name)
def _test_distribution_proto(self, dist): interface.register(dist) self.time_fn.return_value = 100.3 for num in [0, 1, 5, 5.5, 9, 10, 10000]: dist.add(num) self.time_fn.return_value = 1000.6 proto = list(interface._generate_proto())[0] data_set = proto.metrics_collection[0].metrics_data_set[0] data = data_set.data[0] self.assertAlmostEqual(1432.928571428, data.distribution_value.mean) self.assertEqual(metrics_pb2.DISTRIBUTION, data_set.value_type) self.assertEqual(100, data.start_timestamp.seconds) self.assertEqual(1000, data.end_timestamp.seconds) self.assertFalse(data_set.annotations.HasField('unit')) return data_set, data