Example #1
0
    def test_send_modifies_metric_values(self):
        interface.state.global_monitor = stubs.MockMonitor()
        interface.state.target = stubs.MockTarget()

        # pylint: disable=unused-argument
        def serialize_to(pb, start_time, fields, value, target):
            pb.data.add().name = 'foo'

        fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
        fake_metric.name = 'fake'
        fake_metric.serialize_to.side_effect = serialize_to
        interface.register(fake_metric)

        # Setting this will modify store._values in the middle of iteration.
        delayed_metric = metrics.CounterMetric('foo')

        def send(proto):
            delayed_metric.increment_by(1)

        interface.state.global_monitor.send.side_effect = send

        for i in xrange(1001):
            interface.state.store.set('fake', ('field', i), None, 123)

        # Shouldn't raise an exception.
        interface.flush()
  def test_flush(self):
    interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
    interface.state.target = mock.create_autospec(targets.Target)
    interface.state.global_monitor.send.return_value = None
    interface.state.global_monitor.failed.return_value = False

    # pylint: disable=unused-argument
    def populate_data_set(pb):
      pb.metric_name = 'foo'

    fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
    fake_metric.name = 'fake'
    fake_metric.populate_data_set.side_effect = populate_data_set
    interface.register(fake_metric)
    interface.state.store.set('fake', (), None, 123)

    interface.flush()
    self.assertEqual(1, interface.state.global_monitor.send.call_count)
    self.assertEqual(1, interface.state.global_monitor.failed.call_count)
    proto = interface.state.global_monitor.send.call_args[0][0]
    self.assertEqual(1,
        len(proto.metrics_collection[0].metrics_data_set[0].data))
    self.assertEqual('foo',
        proto.metrics_collection[0].metrics_data_set[0].metric_name)
    self.assertFalse(interface.state.global_monitor.wait.called)
Example #3
0
    def test_flush_many(self):
        interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
        interface.state.target = mock.create_autospec(targets.Target)
        interface.state.target.__hash__.return_value = 42

        # pylint: disable=unused-argument
        def populate_data_set(pb):
            pb.metric_name = 'foo'

        # We can't use the mock's call_args_list here because the same object is
        # reused as the argument to both calls and cleared inbetween.
        data_lengths = []

        def send(proto):
            data_lengths.append(
                len(proto.metrics_collection[0].metrics_data_set[0].data))

        interface.state.global_monitor.send.side_effect = send

        fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
        fake_metric.name = 'fake'
        fake_metric.populate_data_set.side_effect = populate_data_set
        interface.register(fake_metric)

        for i in range(501):
            interface.state.store.set('fake', ('field', i), None, 123)

        interface.flush()
        self.assertEquals(2, interface.state.global_monitor.send.call_count)
        self.assertListEqual([500, 1], data_lengths)
Example #4
0
def _flush_metrics(time_now):
  """Return True if metrics were actually sent."""
  datetime_now = datetime.datetime.utcfromtimestamp(time_now)
  entity = shared.get_instance_entity()
  if entity.task_num < 0:
    if interface.state.target.task_num >= 0:
      _reset_cumulative_metrics()
    interface.state.target.task_num = -1
    interface.state.last_flushed = entity.last_updated
    updated_sec_ago = (datetime_now - entity.last_updated).total_seconds()
    if updated_sec_ago > shared.INSTANCE_EXPECTED_TO_HAVE_TASK_NUM_SEC:
      logging.warning('Instance %s is %d seconds old with no task_num.',
                      shared.instance_key_id(), updated_sec_ago)
    return False
  interface.state.target.task_num = entity.task_num

  entity.last_updated = datetime_now
  entity_deferred = entity.put_async()

  interface.flush()

  for metric in shared.global_metrics.itervalues():
    metric.reset()

  entity_deferred.get_result()
  return True
Example #5
0
def _flush_metrics(time_now):
    """Return True if metrics were actually sent."""
    if interface.state.target is None:
        # ts_mon is not configured.
        return False

    datetime_now = datetime.datetime.utcfromtimestamp(time_now)
    entity = shared.get_instance_entity()
    if entity.task_num < 0:
        if interface.state.target.task_num >= 0:
            _reset_cumulative_metrics()
        interface.state.target.task_num = -1
        interface.state.last_flushed = entity.last_updated
        updated_sec_ago = (datetime_now - entity.last_updated).total_seconds()
        if updated_sec_ago > shared.INSTANCE_EXPECTED_TO_HAVE_TASK_NUM_SEC:
            logging.warning('Instance %s is %d seconds old with no task_num.',
                            shared.instance_key_id(), updated_sec_ago)
        return False
    interface.state.target.task_num = entity.task_num

    entity.last_updated = datetime_now
    entity_deferred = entity.put_async()

    interface.flush()

    for metric in interface.state.global_metrics.itervalues():
        metric.reset()

    entity_deferred.get_result()
    return True
  def test_flush_many(self):
    interface.state.global_monitor = stubs.MockMonitor()
    interface.state.target = stubs.MockTarget()

    # pylint: disable=unused-argument
    def serialize_to(pb, start_time, fields, value, target):
      pb.data.add().name = 'foo'

    # We can't use the mock's call_args_list here because the same object is
    # reused as the argument to both calls and cleared inbetween.
    data_lengths = []
    def send(proto):
      data_lengths.append(len(proto.data))
    interface.state.global_monitor.send.side_effect = send

    fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
    fake_metric.name = 'fake'
    fake_metric.serialize_to.side_effect = serialize_to
    interface.register(fake_metric)

    for i in xrange(1001):
      interface.state.store.set('fake', ('field', i), 123)

    interface.flush()
    self.assertEquals(2, interface.state.global_monitor.send.call_count)
    self.assertEqual(1000, data_lengths[0])
    self.assertEqual(1, data_lengths[1])
Example #7
0
    def test_flush_many_new(self):
        interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
        interface.state.target = targets.TaskTarget('a', 'b', 'c', 'd', 1)

        # We can't use the mock's call_args_list here because the same object is
        # reused as the argument to both calls and cleared inbetween.
        data_lengths = []

        def send(proto):
            count = 0
            for coll in proto.metrics_collection:
                for data_set in coll.metrics_data_set:
                    for _ in data_set.data:
                        count += 1
            data_lengths.append(count)

        interface.state.global_monitor.send.side_effect = send

        counter = metrics.CounterMetric('counter', 'desc',
                                        [metrics.IntegerField('field')])
        interface.register(counter)

        for i in range(interface.METRICS_DATA_LENGTH_LIMIT + 1):
            counter.increment_by(i, {'field': i})

        interface.flush()
        self.assertEquals(2, interface.state.global_monitor.send.call_count)
        self.assertListEqual([500, 1], data_lengths)
Example #8
0
    def test_send_modifies_metric_values(self):
        interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
        interface.state.target = mock.create_autospec(targets.Target)
        interface.state.target.__hash__.return_value = 42

        # pylint: disable=unused-argument
        def populate_data_set(pb):
            pb.metric_name = 'foo'

        fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
        fake_metric.name = 'fake'
        fake_metric.populate_data_set.side_effect = populate_data_set
        interface.register(fake_metric)

        # Setting this will modify store._values in the middle of iteration.
        delayed_metric = metrics.CounterMetric('foo', 'desc', None)

        def send(proto):
            delayed_metric.increment_by(1)

        interface.state.global_monitor.send.side_effect = send

        for i in range(1001):
            interface.state.store.set('fake', (i, ), None, 123)

        # Shouldn't raise an exception.
        interface.flush()
Example #9
0
    def test_flush_many(self):
        interface.state.global_monitor = stubs.MockMonitor()
        interface.state.target = stubs.MockTarget()

        # pylint: disable=unused-argument
        def serialize_to(pb, start_time, fields, value, target):
            pb.data.add().name = 'foo'

        # We can't use the mock's call_args_list here because the same object is
        # reused as the argument to both calls and cleared inbetween.
        data_lengths = []

        def send(proto):
            data_lengths.append(len(proto.data))

        interface.state.global_monitor.send.side_effect = send

        fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
        fake_metric.name = 'fake'
        fake_metric.serialize_to.side_effect = serialize_to
        interface.register(fake_metric)

        for i in xrange(1001):
            interface.state.store.set('fake', ('field', i), None, 123)

        interface.flush()
        self.assertEquals(2, interface.state.global_monitor.send.call_count)
        self.assertEqual(1000, data_lengths[0])
        self.assertEqual(1, data_lengths[1])
Example #10
0
    def test_flush_empty_new(self):
        interface.state.metric_name_prefix = '/infra/test/'
        interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
        interface.state.target = targets.TaskTarget('a', 'b', 'c', 'd', 1)

        interface.flush()
        self.assertFalse(interface.state.global_monitor.send.called)
Example #11
0
    def test_flush_new(self):
        interface.state.metric_name_prefix = '/infra/test/'
        interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
        interface.state.target = targets.TaskTarget('a', 'b', 'c', 'd', 1)

        counter = metrics.CounterMetric('counter', 'desc', None)
        interface.register(counter)
        counter.increment_by(3)

        interface.flush()
        self.assertEqual(1, interface.state.global_monitor.send.call_count)

        proto = interface.state.global_monitor.send.call_args[0][0]
        self.assertEqual(1, len(proto.metrics_collection))
        self.assertEqual(1, len(proto.metrics_collection[0].metrics_data_set))

        data_set = proto.metrics_collection[0].metrics_data_set[0]
        self.assertEqual('/infra/test/counter', data_set.metric_name)
Example #12
0
    def test_flush_different_target_fields_new(self):
        interface.state.metric_name_prefix = '/infra/test/'
        interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
        interface.state.target = targets.TaskTarget('s', 'j', 'r', 'h')
        metric = metrics.GaugeMetric('m', 'desc', None)

        metric.set(123)
        metric.set(456, target_fields={'service_name': 'foo'})
        interface.flush()

        self.assertEqual(1, interface.state.global_monitor.send.call_count)
        proto = interface.state.global_monitor.send.call_args[0][0]
        col = proto.metrics_collection
        self.assertEqual(2, len(col))
        self.assertEqual(123, col[0].metrics_data_set[0].data[0].int64_value)
        self.assertEqual(456, col[1].metrics_data_set[0].data[0].int64_value)
        self.assertEqual('s', col[0].task.service_name)
        self.assertEqual('foo', col[1].task.service_name)
  def test_flush(self):
    interface.state.global_monitor = stubs.MockMonitor()
    interface.state.target = stubs.MockTarget()

    # pylint: disable=unused-argument
    def serialize_to(pb, start_time, fields, value, target):
      pb.data.add().name = 'foo'

    fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
    fake_metric.name = 'fake'
    fake_metric.serialize_to.side_effect = serialize_to
    interface.register(fake_metric)
    interface.state.store.set('fake', (), 123)

    interface.flush()
    interface.state.global_monitor.send.assert_called_once()
    proto = interface.state.global_monitor.send.call_args[0][0]
    self.assertEqual(1, len(proto.data))
    self.assertEqual('foo', proto.data[0].name)
Example #14
0
    def test_flush(self):
        interface.state.global_monitor = stubs.MockMonitor()
        interface.state.target = stubs.MockTarget()

        # pylint: disable=unused-argument
        def serialize_to(pb, start_time, fields, value, target):
            pb.data.add().name = 'foo'

        fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
        fake_metric.name = 'fake'
        fake_metric.serialize_to.side_effect = serialize_to
        interface.register(fake_metric)
        interface.state.store.set('fake', (), None, 123)

        interface.flush()
        interface.state.global_monitor.send.assert_called_once()
        proto = interface.state.global_monitor.send.call_args[0][0]
        self.assertEqual(1, len(proto.data))
        self.assertEqual('foo', proto.data[0].name)
Example #15
0
  def test_send_modifies_metric_values(self):
    interface.state.global_monitor = stubs.MockMonitor()
    interface.state.target = stubs.MockTarget()

    # pylint: disable=unused-argument
    def serialize_to(pb, start_time, fields, value, target):
      pb.data.add().name = 'foo'

    fake_metric = mock.create_autospec(metrics.Metric, spec_set=True)
    fake_metric.name = 'fake'
    fake_metric.serialize_to.side_effect = serialize_to
    interface.register(fake_metric)

    # Setting this will modify store._values in the middle of iteration.
    delayed_metric = metrics.CounterMetric('foo')
    def send(proto):
      delayed_metric.increment_by(1)
    interface.state.global_monitor.send.side_effect = send

    for i in xrange(1001):
      interface.state.store.set('fake', ('field', i), None, 123)

    # Shouldn't raise an exception.
    interface.flush()
Example #16
0
 def test_flush_disabled(self):
   interface.reset_for_unittest(disable=True)
   interface.state.global_monitor = stubs.MockMonitor()
   interface.state.target = stubs.MockTarget()
   interface.flush()
   self.assertFalse(interface.state.global_monitor.send.called)
Example #17
0
    def test_flush_empty(self):
        interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
        interface.state.target = mock.create_autospec(targets.Target)

        interface.flush()
        self.assertFalse(interface.state.global_monitor.send.called)
Example #18
0
 def test_callbacks_not_called_if_disabled(self):
     interface.state.invoke_global_callbacks_on_flush = False
     cb = mock.Mock()
     interface.register_global_metrics_callback('test', cb)
     interface.flush()
     self.assertFalse(cb.called)
Example #19
0
 def test_flush_continues_after_exception(self):
     cb = mock.Mock(side_effect=[Exception, None])
     interface.register_global_metrics_callback('cb1', cb)
     interface.register_global_metrics_callback('cb2', cb)
     interface.flush()
     self.assertEqual(2, cb.call_count)
Example #20
0
 def test_callbacks_called_on_flush(self):
     cb = mock.Mock()
     interface.register_global_metrics_callback('test', cb)
     interface.flush()
     cb.assert_called_once_with()
 def test_flush_raises(self):
   self.assertIsNone(interface.state.global_monitor)
   with self.assertRaises(errors.MonitoringNoConfiguredMonitorError):
     interface.flush()
Example #22
0
 def test_flush_raises(self):
     self.assertIsNone(interface.state.global_monitor)
     with self.assertRaises(errors.MonitoringNoConfiguredMonitorError):
         interface.flush()
Example #23
0
 def test_flush_disabled(self):
     interface.reset_for_unittest(disable=True)
     interface.state.global_monitor = mock.create_autospec(monitors.Monitor)
     interface.state.target = mock.create_autospec(targets.Target)
     interface.flush()
     self.assertFalse(interface.state.global_monitor.send.called)
Example #24
0
 def test_flush_disabled(self):
     interface.reset_for_unittest(disable=True)
     interface.state.global_monitor = stubs.MockMonitor()
     interface.state.target = stubs.MockTarget()
     interface.flush()
     self.assertFalse(interface.state.global_monitor.send.called)