Skip to content

Commit 25573ea

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

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed

docs/monitoring-usage.rst

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

0 commit comments

Comments
 (0)