def metric(type_, labels): """Factory for constructing metric objects. :class:`~google.cloud.monitoring.metric.Metric` objects are typically created to write custom metric values. The type should match the metric type specified in the :class:`~google.cloud.monitoring.metric.MetricDescriptor` used to create the custom metric:: >>> metric = client.metric('custom.googleapis.com/my_metric', ... labels={ ... 'status': 'successful', ... }) :type type_: str :param type_: The metric type name. :type labels: dict :param labels: A mapping from label names to values for all labels enumerated in the associated :class:`~.metric.MetricDescriptor`. :rtype: :class:`~google.cloud.monitoring.metric.Metric` :returns: The metric object. """ return Metric(type=type_, labels=labels)
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 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 _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], )