Exemple #1
0
    def test_iteration_headers_only(self):
        T0 = datetime.datetime(2016, 4, 6, 22, 5, 0)
        T1 = datetime.datetime(2016, 4, 6, 22, 10, 0)

        SERIES1 = {
            "metric": {
                "type": METRIC_TYPE,
                "labels": METRIC_LABELS
            },
            "resource": {
                "type": RESOURCE_TYPE,
                "labels": RESOURCE_LABELS
            },
            "metric_kind": METRIC_KIND,
            "value_type": VALUE_TYPE,
        }
        SERIES2 = {
            "metric": {
                "type": METRIC_TYPE,
                "labels": METRIC_LABELS2
            },
            "resource": {
                "type": RESOURCE_TYPE,
                "labels": RESOURCE_LABELS2
            },
            "metric_kind": METRIC_KIND,
            "value_type": VALUE_TYPE,
        }

        RESPONSE = {"time_series": [SERIES1, SERIES2], "next_page_token": ""}

        channel = ChannelStub(responses=[RESPONSE])
        client = self._create_client(channel)
        query = self._make_one(client, PROJECT, METRIC_TYPE)
        query = query.select_interval(start_time=T0, end_time=T1)
        response = list(query.iter(headers_only=True))

        self.assertEqual(len(response), 2)
        series1, series2 = response

        self.assertEqual(series1.metric.labels, METRIC_LABELS)
        self.assertEqual(series2.metric.labels, METRIC_LABELS2)
        self.assertEqual(series1.resource.labels, RESOURCE_LABELS)
        self.assertEqual(series2.resource.labels, RESOURCE_LABELS2)

        self.assertFalse(len(series1.points))
        self.assertFalse(len(series2.points))

        expected_request = monitoring_v3.ListTimeSeriesRequest(
            name="projects/" + PROJECT,
            filter='metric.type = "{type}"'.format(type=METRIC_TYPE),
            interval=self._make_interval(T1, T0),
            view=monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.HEADERS,
        )
        request = channel.requests[0][1]
        self.assertEqual(request, expected_request)
Exemple #2
0
    def iter(self, headers_only=False, page_size=None):
        """Yield all time series objects selected by the query.

        The generator returned iterates over
        :class:`~google.cloud.monitoring_v3.types.TimeSeries` objects
        containing points ordered from oldest to newest.

        Note that the :class:`Query` object itself is an iterable, such that
        the following are equivalent::

            for timeseries in query:
                ...

            for timeseries in query.iter():
                ...

        :type headers_only: bool
        :param headers_only:
             Whether to omit the point data from the time series objects.

        :type page_size: int
        :param page_size:
            (Optional) The maximum number of points in each page of results
            from this request. Non-positive values are ignored. Defaults
            to a sensible value set by the API.

        :raises: :exc:`ValueError` if the query time interval has not been
            specified.
        """
        if self._end_time is None:
            raise ValueError("Query time interval not specified.")

        params = self._build_query_params(headers_only, page_size)

        request = monitoring_v3.ListTimeSeriesRequest(**params)
        for ts in self._client.list_time_series(request):
            yield ts
Exemple #3
0
    def test_iteration(self):
        T0 = datetime.datetime(2016, 4, 6, 22, 5, 0)
        T1 = datetime.datetime(2016, 4, 6, 22, 10, 0)

        INTERVAL1 = self._make_interval(TS1, TS0)
        INTERVAL2 = self._make_interval(TS2, TS1)

        VALUE1 = 60  # seconds
        VALUE2 = 60.001  # seconds

        # Currently cannot create from a list of dict for repeated fields due to
        # https://github.com/googleapis/proto-plus-python/issues/135
        POINT1 = monitoring_v3.Point({
            "interval": INTERVAL2,
            "value": {
                "double_value": VALUE1
            }
        })
        POINT2 = monitoring_v3.Point({
            "interval": INTERVAL1,
            "value": {
                "double_value": VALUE1
            }
        })
        POINT3 = monitoring_v3.Point({
            "interval": INTERVAL2,
            "value": {
                "double_value": VALUE2
            }
        })
        POINT4 = monitoring_v3.Point({
            "interval": INTERVAL1,
            "value": {
                "double_value": VALUE2
            }
        })
        SERIES1 = monitoring_v3.TimeSeries({
            "metric": {
                "type": METRIC_TYPE,
                "labels": METRIC_LABELS
            },
            "resource": {
                "type": RESOURCE_TYPE,
                "labels": RESOURCE_LABELS
            },
            "metric_kind": METRIC_KIND,
            "value_type": VALUE_TYPE,
            "points": [POINT1, POINT2],
        })
        SERIES2 = monitoring_v3.TimeSeries({
            "metric": {
                "type": METRIC_TYPE,
                "labels": METRIC_LABELS2
            },
            "resource": {
                "type": RESOURCE_TYPE,
                "labels": RESOURCE_LABELS2
            },
            "metric_kind": METRIC_KIND,
            "value_type": VALUE_TYPE,
            "points": [POINT3, POINT4],
        })

        RESPONSE = {"time_series": [SERIES1, SERIES2], "next_page_token": ""}

        channel = ChannelStub(responses=[RESPONSE])
        client = self._create_client(channel)
        query = self._make_one(client, PROJECT, METRIC_TYPE)
        query = query.select_interval(start_time=T0, end_time=T1)
        response = list(query)

        self.assertEqual(len(response), 2)
        series1, series2 = response

        self.assertEqual(series1.metric.labels, METRIC_LABELS)
        self.assertEqual(series2.metric.labels, METRIC_LABELS2)
        self.assertEqual(series1.resource.labels, RESOURCE_LABELS)
        self.assertEqual(series2.resource.labels, RESOURCE_LABELS2)

        self.assertEqual([p.value.double_value for p in series1.points],
                         [VALUE1, VALUE1])
        self.assertEqual([p.value.double_value for p in series2.points],
                         [VALUE2, VALUE2])
        self.assertEqual([p.interval for p in series1.points],
                         [INTERVAL2, INTERVAL1])
        self.assertEqual([p.interval for p in series2.points],
                         [INTERVAL2, INTERVAL1])

        expected_request = monitoring_v3.ListTimeSeriesRequest(
            name="projects/" + PROJECT,
            filter='metric.type = "{type}"'.format(type=METRIC_TYPE),
            interval=self._make_interval(T1, T0),
            view=monitoring_v3.ListTimeSeriesRequest.TimeSeriesView.FULL,
        )
        request = channel.requests[0][1]
        self.assertEqual(request, expected_request)