Esempio n. 1
0
    def resource(type_, labels):
        """Factory for constructing monitored resource objects.

        A monitored resource object (
        :class:`~google.cloud.monitoring.resource.Resource`) is
        typically used to create a
        :class:`~google.cloud.monitoring.timeseries.TimeSeries` object.

        For a list of possible monitored resource types and their associated
        labels, see:

        https://cloud.google.com/monitoring/api/resources

        :type type_: str
        :param type_: The monitored resource type name.

        :type labels: dict
        :param labels: A mapping from label names to values for all labels
                       enumerated in the associated
                       :class:`~.resource.ResourceDescriptor`,
                       except that ``project_id`` can and should be omitted
                       when writing time series data.

        :rtype: :class:`~google.cloud.monitoring.resource.Resource`
        :returns: A monitored resource object.
        """
        return Resource(type_, labels)
Esempio n. 2
0
    def test_to_dict(self):
        import datetime
        from google.cloud._helpers import _datetime_to_rfc3339

        from google.cloud.monitoring.metric import Metric
        from google.cloud.monitoring.resource import Resource
        from google.cloud.monitoring.timeseries import Point

        VALUE = 42
        end_time = datetime.datetime.now()
        end_time_str = _datetime_to_rfc3339(end_time, ignore_zone=False)

        METRIC = Metric(type=METRIC_TYPE, labels=METRIC_LABELS)
        RESOURCE = Resource(type=RESOURCE_TYPE, labels=RESOURCE_LABELS)
        POINT = Point(start_time=None, end_time=end_time_str, value=VALUE)

        info = {
            'metric': {'type': METRIC_TYPE, 'labels': METRIC_LABELS},
            'resource': {'type': RESOURCE_TYPE, 'labels': RESOURCE_LABELS},
            'points': [{
                'interval': {
                    'endTime': end_time_str},
                'value': {'int64Value': str(VALUE)},
            }]
        }

        series = self._makeOne(metric=METRIC, resource=RESOURCE,
                               metric_kind=None, value_type=None,
                               points=[POINT])
        series_dict = series._to_dict()
        self.assertEqual(info, series_dict)
Esempio n. 3
0
    def test_constructor(self):
        from google.cloud.monitoring.metric import Metric
        from google.cloud.monitoring.resource import Resource
        from google.cloud.monitoring.timeseries import Point

        VALUE = 60  # seconds

        METRIC = Metric(type=METRIC_TYPE, labels=METRIC_LABELS)
        RESOURCE = Resource(type=RESOURCE_TYPE, labels=RESOURCE_LABELS)
        POINTS = [
            Point(start_time=TS0, end_time=TS1, value=VALUE),
            Point(start_time=TS1, end_time=TS2, value=VALUE),
        ]

        series = self._makeOne(metric=METRIC,
                               resource=RESOURCE,
                               metric_kind=METRIC_KIND,
                               value_type=VALUE_TYPE,
                               points=POINTS)

        self.assertEqual(series.metric, METRIC)
        self.assertEqual(series.resource, RESOURCE)
        self.assertEqual(series.metric_kind, METRIC_KIND)
        self.assertEqual(series.value_type, VALUE_TYPE)
        self.assertEqual(series.points, POINTS)
Esempio n. 4
0
def generate_query_results():  # pragma: NO COVER
    from google.cloud.monitoring.metric import Metric
    from google.cloud.monitoring.resource import Resource
    from google.cloud.monitoring.timeseries import Point
    from google.cloud.monitoring.timeseries import TimeSeries

    def P(timestamp, value):
        return Point(
            start_time=timestamp,
            end_time=timestamp,
            value=value,
        )

    for metric_labels, resource_labels, value in zip(
            METRIC_LABELS, RESOURCE_LABELS, VALUES):
        yield TimeSeries(
            metric=Metric(type=METRIC_TYPE, labels=metric_labels),
            resource=Resource(type=RESOURCE_TYPE, labels=resource_labels),
            metric_kind=METRIC_KIND,
            value_type=VALUE_TYPE,
            points=[P(t, value) for t in TIMESTAMPS],
        )