2828 https://cloud.google.com/monitoring/api/v3/
2929"""
3030
31+ import datetime
32+
3133from gcloud .client import JSONClient
3234from gcloud .monitoring .connection import Connection
3335from gcloud .monitoring .group import Group
36+ from gcloud .monitoring .metric import Metric
3437from gcloud .monitoring .metric import MetricDescriptor
3538from gcloud .monitoring .metric import MetricKind
3639from gcloud .monitoring .metric import ValueType
3740from gcloud .monitoring .query import Query
41+ from gcloud .monitoring .resource import Resource
3842from gcloud .monitoring .resource import ResourceDescriptor
43+ from gcloud .monitoring .timeseries import Point
44+ from gcloud .monitoring .timeseries import TimeSeries
45+
46+ _UTCNOW = datetime .datetime .utcnow # To be replaced by tests.
3947
4048
4149class Client (JSONClient ):
@@ -195,6 +203,126 @@ def metric_descriptor(self, type_,
195203 display_name = display_name ,
196204 )
197205
206+ @staticmethod
207+ def metric (type_ , labels ):
208+ """Construct a metric object.
209+
210+ The type of the Metric should match the type of a MetricDescriptor.
211+ For a list of built-in Metric types and the associated labels, see:
212+
213+ https://cloud.google.com/monitoring/api/metrics
214+
215+ Here is an example of creating using a built-in metric type:
216+
217+ >>> type_ = 'compute.googleapis.com/instance/cpu/utilization'
218+ >>> metric = client.metric(type_, labels={
219+ ... 'instance_name': 'my-instance',
220+ ... })
221+
222+ For custom metrics, the simplest way to get the type is from the
223+ :class:`~gcloud.monitoring.metric.MetricDescriptor` class used to
224+ create the custom metric::
225+
226+ >>> metric = client.metric(metric_descriptor.type, labels={
227+ ... 'instance_name': 'my-instance',
228+ ... })
229+
230+ :type type_: string
231+ :param type_: The metric type name.
232+
233+ :type labels: dict
234+ :param labels: A mapping from label names to values for all labels
235+ enumerated in the associated :class:`MetricDescriptor`.
236+
237+ :rtype: :class:`~gcloud.monitoring.metric.Metric`
238+ :returns: The Metric created with the passed in arguments
239+ """
240+ return Metric (type = type_ , labels = labels )
241+
242+ @staticmethod
243+ def resource (type_ , labels ):
244+ """Construct a resource object.
245+
246+ For a list of possible MonitoredResources and their associated labels,
247+ see:
248+
249+ https://cloud.google.com/monitoring/api/resources
250+
251+ For example, to create a Resource object for a GCE instance::
252+
253+ >>> resource = client.resource('gce_instance', labels= {
254+ ... 'project_id': 'my-project-id',
255+ ... 'instance_id': 'my-instance-id',
256+ ... 'zone': 'us-central1-f'
257+ ... })
258+
259+ :type type_: string
260+ :param type_: The monitored resource type name.
261+
262+ :type labels: dict
263+ :param labels: A mapping from label names to values for all labels
264+ enumerated in the associated
265+ :class:`ResourceDescriptor`.
266+
267+ :rtype: :class:`gcloud.resource.Resource`
268+ :returns: The Resource created with the passed in arguments
269+ """
270+ return Resource (type_ , labels )
271+
272+ @staticmethod
273+ def timeseries (metric , resource , value ,
274+ start_time = None , end_time = None ):
275+ """Constructs a TimeSeries object for a single data point.
276+
277+ .. note::
278+
279+ While TimeSeries objects returned by the API can have
280+ multiple data points when queried as aggregates, TimeSeries created
281+ on the client-side can have at most one point.
282+
283+ For example::
284+
285+ >>> timeseries = client.timeseries(metric, resource, value,
286+ ... end_time=end)
287+
288+ For more information, see:
289+
290+ https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries
291+
292+ :type metric: :class:`~gcloud.monitoring.metric.Metric`
293+ :param metric: A metric object.
294+
295+ :type resource: :class:`~gcloud.monitoring.resource.Resource`
296+ :param resource: A resource object.
297+
298+ :type value: bool, int, string, or float
299+ :param value: The value of the data point to create for the TimeSeries
300+ Note that this parameter can be multiple types and the
301+ TypedValue of the `gcloud.monitoring.TimeSeries.Point` will be
302+ mapped from the type passed in. For example, a Python float
303+ will be sent to the API as a doubleValue.
304+
305+ :type start_time: :class:`datetime.datetime`
306+ :param start_time:
307+ The start time for the point written to the
308+ timeseries. Defaults to None, which has meaning dependent on the
309+ metric_kind of the metric being written to.
310+
311+ :type end_time: :class`datetime.datetime`
312+ :param end_time:
313+ The end time for the point written to the timeseries. Defaults to
314+ the current time, as obtained by calling
315+ :meth:`datetime.datetime.utcnow`
316+
317+ :rtype: :class:`gcloud.timeseries.TimeSeries`
318+ :returns: The TimeSeries created with the passed in arguments
319+ """
320+ if end_time is None :
321+ end_time = _UTCNOW ()
322+ point = Point (value = value , start_time = start_time , end_time = end_time )
323+ return TimeSeries (metric = metric , resource = resource , metric_kind = None ,
324+ value_type = None , points = [point ])
325+
198326 def fetch_metric_descriptor (self , metric_type ):
199327 """Look up a metric descriptor by type.
200328
@@ -210,7 +338,7 @@ def fetch_metric_descriptor(self, metric_type):
210338 :returns: The metric descriptor instance.
211339
212340 :raises: :class:`gcloud.exceptions.NotFound` if the metric descriptor
213- is not found.
341+ is not found.
214342 """
215343 return MetricDescriptor ._fetch (self , metric_type )
216344
0 commit comments