示例#1
0
 def test_init_invalid(self):
     time_series.TimeSeries(LABEL_VALUES, POINTS, None)
     with self.assertRaises(ValueError):
         time_series.TimeSeries(None, POINTS, START_TIMESTAMP)
     with self.assertRaises(ValueError):
         time_series.TimeSeries(LABEL_VALUES, None, START_TIMESTAMP)
     with self.assertRaises(ValueError):
         time_series.TimeSeries(LABEL_VALUES, [], START_TIMESTAMP)
示例#2
0
    def test_check_points_type(self):
        ts = time_series.TimeSeries(LABEL_VALUES, POINTS, START_TIMESTAMP)
        self.assertTrue(ts.check_points_type(value.ValueLong))

        bad_points = POINTS + (point.Point(value.ValueDouble(6.0),
                                           "2018-10-10T04:33:44.012345Z"), )
        bad_time_series = time_series.TimeSeries(LABEL_VALUES, bad_points,
                                                 START_TIMESTAMP)

        self.assertFalse(bad_time_series.check_points_type(value.ValueLong))
        self.assertFalse(bad_time_series.check_points_type(value.ValueLong))
def view_data_to_metric(view_data, timestamp):
    """Convert a ViewData to a Metric at time `timestamp`.

    :type view_data: :class: `opencensus.stats.view_data.ViewData`
    :param view_data: The ViewData to convert.

    :type timestamp: :class: `datetime.datetime`
    :param timestamp: The time to set on the metric's point's aggregation,
    usually the current time.

    :rtype: :class: `opencensus.metrics.export.metric.Metric`
    :return: A converted Metric.
    """
    md = view_data.view.get_metric_descriptor()

    # TODO: implement gauges
    if is_gauge(md.type):
        ts_start = None  # pragma: NO COVER
    else:
        ts_start = view_data.start_time

    ts_list = []
    for tag_vals, agg_data in view_data.tag_value_aggregation_data_map.items():
        label_values = get_label_values(tag_vals)
        point = agg_data.to_point(timestamp)
        ts_list.append(time_series.TimeSeries(label_values, [point], ts_start))
    return metric.Metric(md, ts_list)
    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)
        ])
示例#6
0
    def test_check_points_type(self):
        ts = time_series.TimeSeries(LABEL_VALUES, POINTS, START_TIMESTAMP)
        self.assertTrue(
            ts.check_points_type(
                metric_descriptor.MetricDescriptorType.GAUGE_INT64))

        bad_points = POINTS + (point.Point(value.Value.double_value(6.0),
                                           "2018-10-10T04:33:44.012345Z"), )
        bad_time_series = time_series.TimeSeries(LABEL_VALUES, bad_points,
                                                 START_TIMESTAMP)

        self.assertFalse(
            bad_time_series.check_points_type(
                metric_descriptor.MetricDescriptorType.GAUGE_INT64))
        self.assertFalse(
            bad_time_series.check_points_type(
                metric_descriptor.MetricDescriptorType.GAUGE_DOUBLE))
    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 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)
示例#9
0
def get_timeseries_list(points, timestamp):
    """Convert a list of `GaugePoint`s into a list of `TimeSeries`.

    Get a :class:`opencensus.metrics.export.time_series.TimeSeries` for each
    measurement in `points`. Each series contains a single
    :class:`opencensus.metrics.export.point.Point` that represents the last
    recorded value of the measurement.

    :type points: list(:class:`GaugePoint`)
    :param points: The list of measurements to convert.

    :type timestamp: :class:`datetime.datetime`
    :param timestamp: Recording time to report, usually the current time.

    :rtype: list(:class:`opencensus.metrics.export.time_series.TimeSeries`)
    :return: A list of one `TimeSeries` for each point in `points`.
    """
    ts_list = []
    for lv, gp in points.items():
        point = point_module.Point(gp.to_point_value(), timestamp)
        ts_list.append(time_series.TimeSeries(lv, [point], timestamp))
    return ts_list
示例#10
0
    def get_metric(self, timestamp):
        """Get a metric including all current time series.

        Get a :class:`opencensus.metrics.export.metric.Metric` with one
        :class:`opencensus.metrics.export.time_series.TimeSeries` for each
        set of label values with a recorded measurement. Each `TimeSeries`
        has a single point that represents the last recorded value.

        :type timestamp: :class:`datetime.datetime`
        :param timestamp: Recording time to report, usually the current time.

        :rtype: :class:`opencensus.metrics.export.metric.Metric` or None
        :return: A converted metric for all current measurements.
        """
        if not self.points:
            return None

        ts_list = []
        with self._points_lock:
            for lv, gp in self.points.items():
                point = point_module.Point(
                    self.value_type(gp.value), timestamp)
                ts_list.append(time_series.TimeSeries(lv, [point], timestamp))
        return metric.Metric(self.descriptor, ts_list)
示例#11
0
    def test_init(self):
        ts = time_series.TimeSeries(LABEL_VALUES, POINTS, START_TIMESTAMP)

        self.assertEqual(ts.start_timestamp, START_TIMESTAMP)
        self.assertEqual(ts.label_values, LABEL_VALUES)
        self.assertEqual(ts.points, POINTS)