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)
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)
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)
def _setUpResources(self): from google.cloud.monitoring.resource import Resource info1 = { 'type': 'gce_instance', 'labels': { 'project_id': 'my-project', 'instance_id': '1234567890123456788', 'zone': 'us-central1-a', } } info2 = { 'type': 'gce_instance', 'labels': { 'project_id': 'my-project', 'instance_id': '1234567890123456789', 'zone': 'us-central1-a', } } self.RESOURCE1 = Resource._from_dict(info1) self.RESOURCE2 = Resource._from_dict(info2) self.MEMBERS = [info1, info2]
def _from_dict(cls, info): """Construct a time series from the parsed JSON representation. :type info: dict :param info: A ``dict`` parsed from the JSON wire-format representation. :rtype: :class:`TimeSeries` :returns: A time series object. """ metric = Metric._from_dict(info['metric']) resource = Resource._from_dict(info['resource']) metric_kind = info['metricKind'] value_type = info['valueType'] points = [Point._from_dict(p) for p in info.get('points', ())] return cls(metric, resource, metric_kind, value_type, points)
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], )
def list_members(self, filter_string=None, end_time=None, start_time=None): """Lists all members of this group via a ``GET`` request. If no ``end_time`` is provided then the group membership over the last minute is returned. Example:: >>> for member in group.list_members(): ... print(member) List members that are Compute Engine VM instances:: >>> filter_string = 'resource.type = "gce_instance"' >>> for member in group.list_members(filter_string=filter_string): ... print(member) List historical members that existed between 4 and 5 hours ago:: >>> import datetime >>> t1 = datetime.datetime.utcnow() - datetime.timedelta(hours=4) >>> t0 = t1 - datetime.timedelta(hours=1) >>> for member in group.list_members(end_time=t1, start_time=t0): ... print(member) :type filter_string: string or None :param filter_string: An optional list filter describing the members to be returned. The filter may reference the type, labels, and metadata of monitored resources that comprise the group. See the `filter documentation`_. :type end_time: :class:`datetime.datetime` or None :param end_time: The end time (inclusive) of the time interval for which results should be returned, as a datetime object. If ``start_time`` is specified, then this must also be specified. :type start_time: :class:`datetime.datetime` or None :param start_time: The start time (exclusive) of the time interval for which results should be returned, as a datetime object. :rtype: list of :class:`~google.cloud.monitoring.resource.Resource` :returns: A list of resource instances. :raises: :exc:`ValueError` if the ``start_time`` is specified, but the ``end_time`` is missing. .. _filter documentation: https://cloud.google.com/monitoring/api/v3/filters#group-filter """ if start_time is not None and end_time is None: raise ValueError('If "start_time" is specified, "end_time" must ' 'also be specified') path = '%s/members' % (self.path, ) resources = [] page_token = None params = {} if filter_string is not None: params['filter'] = filter_string if end_time is not None: params['interval.endTime'] = _datetime_to_rfc3339( end_time, ignore_zone=False) if start_time is not None: params['interval.startTime'] = _datetime_to_rfc3339( start_time, ignore_zone=False) while True: if page_token is not None: params['pageToken'] = page_token response = self.client.connection.api_request( method='GET', path=path, query_params=params.copy()) for info in response.get('members', ()): resources.append(Resource._from_dict(info)) page_token = response.get('nextPageToken') if not page_token: break return resources
def list_members(self, filter_string=None, end_time=None, start_time=None): """Lists all members of this group via a ``GET`` request. If no ``end_time`` is provided then the group membership over the last minute is returned. Example:: >>> for member in group.list_members(): ... print(member) List members that are Compute Engine VM instances:: >>> filter_string = 'resource.type = "gce_instance"' >>> for member in group.list_members(filter_string=filter_string): ... print(member) List historical members that existed between 4 and 5 hours ago:: >>> import datetime >>> t1 = datetime.datetime.utcnow() - datetime.timedelta(hours=4) >>> t0 = t1 - datetime.timedelta(hours=1) >>> for member in group.list_members(end_time=t1, start_time=t0): ... print(member) :type filter_string: str :param filter_string: (Optional) An optional list filter describing the members to be returned. The filter may reference the type, labels, and metadata of monitored resources that comprise the group. See the `filter documentation`_. :type end_time: :class:`datetime.datetime` :param end_time: (Optional) The end time (inclusive) of the time interval for which results should be returned, as a datetime object. If ``start_time`` is specified, then this must also be specified. :type start_time: :class:`datetime.datetime` :param start_time: (Optional) The start time (exclusive) of the time interval for which results should be returned, as a datetime object. :rtype: list of :class:`~google.cloud.monitoring.resource.Resource` :returns: A list of resource instances. :raises: :exc:`ValueError` if the ``start_time`` is specified, but the ``end_time`` is missing. .. _filter documentation: https://cloud.google.com/monitoring/api/v3/filters#group-filter """ if start_time is not None and end_time is None: raise ValueError('If "start_time" is specified, "end_time" must ' 'also be specified') path = '%s/members' % (self.path,) resources = [] page_token = None params = {} if filter_string is not None: params['filter'] = filter_string if end_time is not None: params['interval.endTime'] = _datetime_to_rfc3339( end_time, ignore_zone=False) if start_time is not None: params['interval.startTime'] = _datetime_to_rfc3339( start_time, ignore_zone=False) while True: if page_token is not None: params['pageToken'] = page_token response = self.client._connection.api_request( method='GET', path=path, query_params=params.copy()) for info in response.get('members', ()): resources.append(Resource._from_dict(info)) page_token = response.get('nextPageToken') if not page_token: break return resources