Skip to content

Commit 299b14c

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

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed

docs/monitoring-usage.rst

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,110 @@ 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.exists():
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(descriptor=metric_descriptor, 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(descriptor=metric_descriptor, 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 specified, the :class:`~gcloud.monitoring.timeseries.MetricStream`
330+
can be used to write :class:`~gcloud.monitoring.timeseries.Point` values.
331+
:class:`~gcloud.monitoring.timeseries.MetricStream` is a helper class that helps create
332+
:class:`~gcloud.monitoring.timeseries.TimeSeries` objects with the same Metric and Resource types.
333+
334+
When writing points with :class:`~gcloud.monitoring.timeseries.MetricStream`, you specify
335+
points containing a value of the type that matches the *value_type* specified in the associated
336+
:class:`~gcloud.monitoring.metric.MetricDescriptor`. The point must also contain a time interval.
337+
The point's time interval must be later than any points already created for the same
338+
:class:`~gcloud.monitoring.timeseries.TimeSeries`
339+
(where :class:`~gcloud.monitoring.timeseries.TimeSeries` with matching :class:`~gcloud.monitoring.metric.Metric` and :class:`~gcloud.monitoring.resource.Resource` types are considered the same).
340+
The end
341+
time of the interval must not be more than 24 hours in the past or more than five minutes in the
342+
future::
343+
344+
>>> start = datetime.utcnow()
345+
>>> end = datetime.utcnow() - datetime.timedelta(minutes=5)
346+
>>> value = get_num_404s_in_last_five_minutes() # get value from a hypothetical call
347+
>>> metric_stream = client.metric_stream(metric, resource)
348+
>>> metric_stream.write_point(value, start_time=start, end_time=end) # API call
349+
350+
Stackdriver Monitoring supports several **value types**. A *CUMULATIVE* metric represents a value
351+
over time. Successive points in a :class:`~gcloud.monitoring.timeseries.TimeSeries` should have the
352+
same start time and increasing end times, until an event resets the cumulative value to zero and
353+
sets a new start time for the follow points.
354+
355+
For *GAUGE* metrics, the time intervals a single point in time, so only the end_time should be
356+
specified::
357+
358+
>>> metric_stream.write_point(value, end_time=end) # API call
359+
360+
By default, end_time defaults to :meth:`~datetime.datetime.utcnow()`, so metrics written for the
361+
can be simplified to::
362+
363+
>>> metric_stream.write_point(value) # API call
364+
365+
For *DELTA* metrics, the points reflect a change during a finite time interval, so both start_time
366+
and end_time should be specified, though end_time again defaults to the current time.
367+
368+
>>> metric_stream.write_point(value, start_time=start_time) # API call
369+
370+
As an alternative to :class:`~gcloud.monitoring.timeseries.MetricStream`, it's also possible
371+
to have the :class:`gcloud.monitoring.client.Client` return a
372+
:class:`gcloud.monitoring.timeseries.TimeSeries` object, which can then written later::
373+
374+
>>> ts1 = client.timeseries(value, end_time=end_time, metric=metric1, resource=resource1)
375+
>>> ts1.create()
376+
377+
or be used in :meth:`~gcloud.monitoring.client.write_time_series`::
378+
379+
>>> ts1 = client.timeseries(value, end_time=end_time, metric=metric1, resource=resource1)
380+
>>> ts2 = client.timeseries(value2, end_time=end_time2, metric=metric2, resource=resource2)
381+
>>> client.write_time_series([ts1, ts2])
382+
383+
All timezone-naive Python datetimes are assumed to be UTC.
384+
385+
386+
.. _Creating Custom Metrics: https://cloud.google.com/monitoring/custom-metrics/creating-metrics
387+
.. _Metrics: https://cloud.google.com/monitoring/api/v3/metrics

0 commit comments

Comments
 (0)