def test_update(self, time_mock): time_mock.return_value = 123 observer = LastValueAggregator() # test current values without any update self.assertIsNone(observer.current) # call update with some values values = (3, 50, 3, 97, 27) for val in values: observer.update(val) self.assertEqual(observer.last_update_timestamp, 123) self.assertEqual(observer.current, values[-1])
def test_merge_last_updated_none(self): observer1 = LastValueAggregator() observer2 = LastValueAggregator() observer1.checkpoint = 23 observer2.checkpoint = 47 observer1.last_update_timestamp = 0 observer2.last_update_timestamp = 100 observer1.merge(observer2) self.assertEqual(observer1.checkpoint, 47) self.assertEqual(observer1.last_update_timestamp, 100)
def test_checkpoint(self): observer = LastValueAggregator() # take checkpoint without any update observer.take_checkpoint() self.assertEqual(observer.checkpoint, None) # call update with some values values = (3, 50, 3, 97) for val in values: observer.update(val) observer.take_checkpoint() self.assertEqual(observer.checkpoint, 97)
def test_merge_with_empty(self): observer1 = LastValueAggregator() observer2 = LastValueAggregator() observer1.checkpoint = 23 observer1.last_update_timestamp = 100 observer1.merge(observer2) self.assertEqual(observer1.checkpoint, 23) self.assertEqual(observer1.last_update_timestamp, 100)
def test_convert_from_last_value(self): last_value_record = ExportRecord( Counter("testname", "testdesc", "testunit", int, None), None, LastValueAggregator(), Resource({}), ) last_value_record.aggregator.update(1) last_value_record.aggregator.update(5) last_value_record.aggregator.take_checkpoint() expected_timeseries = self.exporter._create_timeseries( last_value_record, "testname_last", 5.0) timeseries = self.exporter._convert_from_last_value(last_value_record) self.assertEqual(timeseries[0], expected_timeseries)
def aggregator_for(self, instrument_type: Type[InstrumentT]) -> Aggregator: """Returns an aggregator based on metric instrument type. Aggregators keep track of and updates values when metrics get updated. """ # pylint:disable=R0201 if issubclass(instrument_type, (Counter, UpDownCounter)): return SumAggregator() if issubclass(instrument_type, (SumObserver, UpDownSumObserver)): return LastValueAggregator() if issubclass(instrument_type, ValueRecorder): return MinMaxSumCountAggregator() if issubclass(instrument_type, ValueObserver): return ValueObserverAggregator() # TODO: Add other aggregators return SumAggregator()
def test_valid_convert_to_timeseries(self): test_records = [ ExportRecord( Counter("testname", "testdesc", "testunit", int, None), None, SumAggregator(), Resource({}), ), ExportRecord( Counter("testname", "testdesc", "testunit", int, None), None, MinMaxSumCountAggregator(), Resource({}), ), ExportRecord( Counter("testname", "testdesc", "testunit", int, None), None, HistogramAggregator(), Resource({}), ), ExportRecord( Counter("testname", "testdesc", "testunit", int, None), None, LastValueAggregator(), Resource({}), ), ExportRecord( Counter("testname", "testdesc", "testunit", int, None), None, ValueObserverAggregator(), Resource({}), ), ] for record in test_records: record.aggregator.update(5) record.aggregator.take_checkpoint() data = self.exporter._convert_to_timeseries(test_records) self.assertIsInstance(data, list) self.assertEqual(len(data), 13) for timeseries in data: self.assertIsInstance(timeseries, TimeSeries)