def __init__(self, name, description, unit, label_keys): self._len_label_keys = len(label_keys) self.default_label_values = [None] * self._len_label_keys self.descriptor = metric_descriptor.MetricDescriptor( name, description, unit, self.descriptor_type, label_keys) self.points = OrderedDict() self._points_lock = threading.Lock()
def test_export_metrics(self): lv = label_value.LabelValue('val') val = value.ValueLong(value=123) dt = datetime(2019, 3, 20, 21, 34, 0, 537954) pp = point.Point(value=val, timestamp=dt) ts = [ time_series.TimeSeries(label_values=[lv], points=[pp], start_timestamp=utils.to_iso_str(dt)) ] desc = metric_descriptor.MetricDescriptor( name='name', description='description', unit='unit', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('key', 'description')]) mm = metric.Metric(descriptor=desc, time_series=ts) exporter = stackdriver.StackdriverStatsExporter(client=mock.Mock()) exporter.export_metrics([mm]) self.assertEqual(exporter.client.create_time_series.call_count, 1) sd_args = exporter.client.create_time_series.call_args[0][1] self.assertEqual(len(sd_args), 1) [sd_arg] = exporter.client.create_time_series.call_args[0][1] self.assertEqual(sd_arg.points[0].value.int64_value, 123)
def _create_metric(descriptor_type=metric_descriptor.MetricDescriptorType. CUMULATIVE_INT64, points=[]): return metric.Metric( metric_descriptor.MetricDescriptor('', '', '', descriptor_type, []), [ time_series.TimeSeries([label_value.LabelValue()], points, TEST_TIME_STR) ])
def test_init(self): md = metric_descriptor.MetricDescriptor( NAME, DESCRIPTION, UNIT, metric_descriptor.MetricDescriptorType.GAUGE_DOUBLE, (LABEL_KEY1, LABEL_KEY2)) self.assertEqual(md.name, NAME) self.assertEqual(md.description, DESCRIPTION) self.assertEqual(md.unit, UNIT) self.assertEqual(md.type, metric_descriptor.MetricDescriptorType.GAUGE_DOUBLE) self.assertEqual(md.label_keys, LABEL_KEYS)
def view_to_metric_descriptor(view): """Get a MetricDescriptor for given view data. :type view: (:class: '~opencensus.stats.view.View') :param view: the view data to for which to build a metric descriptor """ return metric_descriptor.MetricDescriptor( view.name, view.description, view.measure.unit, get_metric_type(view.measure, view.aggregation), # TODO: add label key description [label_key.LabelKey(tk, "") for tk in view.columns])
def test_get_metric_descriptor_bad_type(self): exporter = stackdriver.StackdriverStatsExporter( options=stackdriver.Options(project_id='project_id'), client=mock.Mock()) bad_type_oc_md = metric_descriptor.MetricDescriptor( name='name', description='description', unit='unit', # Need a valid type to create the descriptor type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('key', 'description')]) bad_type_oc_md._type = 100 with self.assertRaises(TypeError): exporter.get_metric_descriptor(bad_type_oc_md)
def test_export_single_metric(self, mock_stats, mock_client): """Check that we can export a set of a single metric.""" lv = label_value.LabelValue('val') val = value.ValueLong(value=123) dt = datetime(2019, 3, 20, 21, 34, 0, 537954) pp = point.Point(value=val, timestamp=dt) ts = [ time_series.TimeSeries(label_values=[lv], points=[pp], start_timestamp=utils.to_iso_str(dt)) ] desc = metric_descriptor.MetricDescriptor( name='name2', description='description2', unit='unit2', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('key', 'description')]) mm = metric.Metric(descriptor=desc, time_series=ts) mock_stats.get_metrics.return_value = [mm] with MockGetExporterThread() as mget: exporter = stackdriver.new_stats_exporter( stackdriver.Options(project_id=1)) mget.transport.step() exporter.client.create_metric_descriptor.assert_called() self.assertEqual(exporter.client.create_metric_descriptor.call_count, 1) md_call_arg =\ exporter.client.create_metric_descriptor.call_args[0][1] self.assertEqual(md_call_arg.metric_kind, monitoring_v3.enums.MetricDescriptor.MetricKind.GAUGE) self.assertEqual(md_call_arg.value_type, monitoring_v3.enums.MetricDescriptor.ValueType.INT64) exporter.client.create_time_series.assert_called() self.assertEqual(exporter.client.create_time_series.call_count, 1) ts_call_arg = exporter.client.create_time_series.call_args[0][1] self.assertEqual(len(ts_call_arg), 1) self.assertEqual(len(ts_call_arg[0].points), 1) self.assertEqual(ts_call_arg[0].points[0].value.int64_value, 123)
def test_get_metric_descriptor_custom_prefix(self): exporter = stackdriver.StackdriverStatsExporter( options=stackdriver.Options(default_monitoring_labels={'dk': 'dd'}, metric_prefix='metric_prefix', project_id='project_id'), client=mock.Mock()) oc_md = metric_descriptor.MetricDescriptor( name='name', description='description', unit='unit', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('ck', 'cd')]) sd_md = exporter.get_metric_descriptor(oc_md) self.assertIn('metric_prefix', sd_md.type) self.assertIn('metric_prefix', sd_md.name)
def get_metric_descriptor(self): """Get a MetricDescriptor for this view. Lazily creates a MetricDescriptor for metrics conversion. :rtype: :class: `opencensus.metrics.export.metric_descriptor.MetricDescriptor` :return: A converted Metric. """ # noqa with self._md_cache_lock: if self._metric_descriptor is None: self._metric_descriptor = metric_descriptor.MetricDescriptor( self.name, self.description, self.measure.unit, self.aggregation.get_metric_type(self.measure), # TODO: add label key description [label_key.LabelKey(tk, "") for tk in self.columns]) return self._metric_descriptor
def test_register_metric_descriptor(self): exporter = stackdriver.StackdriverStatsExporter( options=stackdriver.Options(metric_prefix='metric_prefix', project_id='project_id'), client=mock.Mock()) oc_md = metric_descriptor.MetricDescriptor( name='name', description='description', unit='unit', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('key', 'description')]) exporter.register_metric_descriptor(oc_md) self.assertEqual(exporter.client.create_metric_descriptor.call_count, 1) exporter.register_metric_descriptor(oc_md) self.assertEqual(exporter.client.create_metric_descriptor.call_count, 1)
def create_metric(): lv = label_value.LabelValue('val') val = value.ValueLong(value=123) dt = datetime(2019, 3, 20, 21, 34, 0, 537954) pp = point.Point(value=val, timestamp=dt) ts = [ time_series.TimeSeries(label_values=[lv], points=[pp], start_timestamp=utils.to_iso_str(dt)) ] desc = metric_descriptor.MetricDescriptor( name='name', description='description', unit='unit', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('key', 'description')]) return metric.Metric(descriptor=desc, time_series=ts)
def test_get_metric_descriptor(self): exporter = stackdriver.StackdriverStatsExporter( options=stackdriver.Options(default_monitoring_labels={'dk': 'dd'}, project_id='project_id'), client=mock.Mock()) oc_md = metric_descriptor.MetricDescriptor( name='name', description='description', unit='unit', type_=metric_descriptor.MetricDescriptorType.GAUGE_INT64, label_keys=[label_key.LabelKey('ck', 'cd')]) sd_md = exporter.get_metric_descriptor(oc_md) self.assertEqual(sd_md.metric_kind, monitoring_v3.enums.MetricDescriptor.MetricKind.GAUGE) self.assertEqual(sd_md.value_type, monitoring_v3.enums.MetricDescriptor.ValueType.INT64) self.assertIsInstance(sd_md, monitoring_v3.types.MetricDescriptor) exporter.client.create_metric_descriptor.assert_not_called()
def test_null_label_key_values(self): with self.assertRaises(ValueError): metric_descriptor.MetricDescriptor( NAME, DESCRIPTION, UNIT, metric_descriptor.MetricDescriptorType.GAUGE_DOUBLE, (None, ))
def test_bogus_type(self): with self.assertRaises(ValueError): metric_descriptor.MetricDescriptor(NAME, DESCRIPTION, UNIT, 0, (LABEL_KEY1, ))
def test_empty_label_keys(self): metric_descriptor.MetricDescriptor( NAME, DESCRIPTION, UNIT, metric_descriptor.MetricDescriptorType.GAUGE_DOUBLE, [])