def test_flush(self): self.mock_now(datetime.datetime(2015, 1, 2, 3, 4, 5)) calls = [] self.mock(metrics, '_enqueue_flush', lambda *args: calls.append(args)) d1 = metrics.Descriptor('name1', 'Desc 1', labels={'A': 'a', 'B': 'b'}) d2 = metrics.Descriptor('name2', 'Desc 2', labels={'C': 'c', 'D': 'd'}) d3 = metrics.Descriptor('name3', 'Desc 3', value_type='double') buf = metrics.Buffer() # Overwrite. buf.set_gauge(d1, 123, labels={'A': '1', 'B': '2'}) buf.set_gauge(d1, 456, labels={'A': '1', 'B': '2'}) # Another label set. buf.set_gauge(d1, 789, labels={'A': '3', 'B': '4'}) # Another descriptor. buf.set_gauge(d2, 555, labels={'C': 'x', 'D': 'z'}) # Another metric type. buf.set_gauge(d3, 3.0) buf.flush(task_queue_name='task-queue') self.assertEqual(1, len(calls)) self.assertEqual('task-queue', calls[0][1]) self.assertEqual(self.EXPECTED_FLUSH_TASK, calls[0][0])
def test_conflicting_descriptors(self): d1 = metrics.Descriptor('name', 'Description', labels={ 'A': 'a', 'B': 'b' }) d2 = metrics.Descriptor('name', 'Description', labels={ 'A': 'a', 'B': 'b' }) d3 = metrics.Descriptor('name', 'Description', labels={ 'C': 'c', 'D': 'd' }) buf = metrics.Buffer() # No conflict. buf.set_gauge(d1, 0, {'A': 'blah', 'B': 'blah'}) buf.set_gauge(d1, 0, {'A': 'blah 2', 'B': 'blah 2'}) buf.set_gauge(d2, 0, {'A': 'blah 3', 'B': 'blah 3'}) # Conflict: same metric name, different set of labels. with self.assertRaises(ValueError): buf.set_gauge(d3, 0, {'C': 'blah', 'D': 'blah'})
def test_validate_value_int(self): d = metrics.Descriptor(name='abc', description='Blah', value_type='int64') d.validate_value(1) d.validate_value(1L) with self.assertRaises(TypeError): d.validate_value(None) with self.assertRaises(TypeError): d.validate_value(1.0) with self.assertRaises(TypeError): d.validate_value('boo')
def test_validate_labels(self): d = metrics.Descriptor(name='abc', description='Blah', labels={ 'A': 'a', 'B': 'b' }) d.validate_labels({'A': 'a value', 'B': 'b value'}) with self.assertRaises(TypeError): d.validate_labels(None) with self.assertRaises(TypeError): d.validate_labels({'A': 'a value'}) with self.assertRaises(TypeError): d.validate_labels({'A': 'a value', 'B': 123}) with self.assertRaises(TypeError): d.validate_labels({'A': 'a value', 'B': ''})
def test_to_from_dict(self): d1 = metrics.Descriptor(name='abc', description='Blah', labels={ 'A': '1', 'B': '2' }, metric_type='gauge', value_type='int64') self.assertEqual([('A', '1'), ('B', '2')], d1.labels) self.assertEqual( { 'description': 'Blah', 'labels': { 'A': '1', 'B': '2' }, 'metric_type': 'gauge', 'name': 'abc', 'value_type': 'int64' }, d1.to_dict()) d2 = metrics.Descriptor.from_dict(d1.to_dict()) self.assertEqual(vars(d1), vars(d2))