Skip to content

Commit ff8bd99

Browse files
author
Bill Prin
committed
Monitoring Custom Metric Usage
1 parent fc0bc0e commit ff8bd99

File tree

1 file changed

+98
-1
lines changed

1 file changed

+98
-1
lines changed

docs/monitoring-usage.rst

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ You can delete such a metric descriptor as follows::
127127
... 'custom.googleapis.com/my_metric')
128128
>>> descriptor.delete()
129129

130-
To define a custom metric parameterized by one or more labels,
130+
To define a custom metric parametrized by one or more labels,
131131
you must build the appropriate
132132
:class:`~gcloud.monitoring.label.LabelDescriptor` objects
133133
and include them in the
@@ -278,3 +278,100 @@ follows::
278278

279279
.. _Time Series:
280280
https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries
281+
282+
283+
Writing Custom Time Series
284+
---------------------------
285+
286+
To create a custom TimeSeries value, you must first create a custom
287+
:class:`~gcloud.monitoring.metric.MetricDescriptor` object, and then use its
288+
type to create a fully parametrized :class:`~gcloud.monitoring.metric.Metric` by providing specific
289+
values for the available labels. You must create a fully parametrized :class:`~gcloud.monitoring.resource.Resource` from one of the
290+
available ResourceDescriptors which can be listed using the :class:`~gcloud.monitoring.resource.ResourceDescriptor` class,
291+
which similarly requires that its available labels have values specified.
292+
293+
>>> from gcloud import monitoring
294+
>>> import datetime
295+
>>> client = monitoring.Client()
296+
>>> label = LabelDescriptor('response_code', LabelValueType.INT64,
297+
... description='HTTP status code')
298+
>>> metric_descriptor = = client.metric_descriptor(
299+
'custom.googleapis.com/my_app/response_count',
300+
metric_kind=MetricKind.CUMULATIVE,
301+
value_type=ValueType.INT64,
302+
labels=[label],
303+
description='Cumulative count of HTTP responses.')
304+
>>> if not metric_descriptor.exist():
305+
>>> metric_descriptor.create()
306+
>>> # Create a Resource by its resource name, assign labels as dict
307+
>>> resource = client.resource('gce_instance',
308+
... labels={
309+
... 'instance_id': 'my-instance',
310+
... 'zone': 'us-central1-f'
311+
... })
312+
>>> # Create a Metric using its metric descriptor parametrized with labels
313+
>>> metric = client.metric(type=metric_descriptor.type, labels={
314+
... 'response_code': 404
315+
... }
316+
317+
It's also possible to assign labels to Metrics and Resources with keyword arguments::
318+
319+
>>> metric = client.metric(type=metric_descriptor.type, response_code=404)
320+
>>> resource = client.resource('gce_instance', instance_id='my-instance', zone='us-central1-f')
321+
322+
323+
With a Metric and Resource created, the :class:`~gcloud.monitoring.timeseries.TimeSeries`
324+
can be used to write :class:`~gcloud.monitoring.timeseries.Point` values.
325+
326+
When creating :class:`~gcloud.monitoring.timeseries.TimeSeries`, you specify points containing a
327+
value of the type that matches the *value_type* specified in the associated `MetricDescriptor`.
328+
The point must also contain a time interval. The point's time interval must be later than any
329+
points already created for the TimeSeries (where TimeSeries with matching `Metric` and `Resource`
330+
types are considered the same). The end time of the interval must not be more than 24 hours in
331+
the past or more than five minutes in the future::
332+
333+
>>> start = datetime.now()
334+
>>> end = datetime.now() - datetime.timedelta(minutes=5)
335+
>>> value = get_num_404s_in_last_five_minutes() # get value from a hypothetical call
336+
>>>
337+
>>> client.write_timeseries(value, start_time=start, end_time=end,
338+
... metric=metric, resource=resource) # API call
339+
340+
You can also use the :class:`~gcloud.monitoring.TimeseriesWriter` class to
341+
associate a fully specified Metric and Resource pair to write Timeseries together::
342+
343+
>>> timeseries_writer = client.get_timeseries_writer(metric, resource)
344+
>>> timeseries_writer.write_point(value, start_time=start, end_time=end) # API call
345+
346+
Stackdriver Monitoring supports several **value types**. A *CUMULATIVE* metric represents a value
347+
over time. Successive points in a TimeSeries should have the same start time and increasing
348+
end times, until an event resets the cumulative value to zero and sets a new start time for
349+
the follow points.
350+
351+
For *GAUGE* metrics, the time intervals a single point in time, so only the end_time should be
352+
specified::
353+
354+
>>> timeseries_writer.write_point(value, end_time=end) # API call
355+
356+
By default, end_time defaults to :meth:`~datetime.datetime.utcnow()`, so metrics written for the
357+
can be simplified to::
358+
359+
>>> timeseries_writer.write_point(value) # API call
360+
361+
For *DELTA* metrics, the points reflect a change during a finite time interval, so both start_time
362+
and end_time should be specified, though end_time again defaults to the current time.
363+
364+
>>> timeseries_writer.write_point(value, start_time=start_time) # API call
365+
366+
It's also possible to have the `client` return a TimeSeries object, which can then written later::
367+
368+
>>> ts1 = client.timeseries(value, end_time=end_time, metric=metric1, resource=resource1)
369+
>>> ts1.create()
370+
371+
or be used in :meth:`~gcloud.monitoring.client.write_time_series`::
372+
373+
>>> ts1 = client.timeseries(value, end_time=end_time, metric=metric1, resource=resource1)
374+
>>> ts2 = client.timeseries(value2, end_time=end_time2, metric=metric2, resource=resource2)
375+
>>> client.write_time_series([ts1, ts2])
376+
377+
All timezone-naive Python datetimes are assumed to be UTC.

0 commit comments

Comments
 (0)