@@ -30,7 +30,7 @@ of the API:
3030- Querying of time series.
3131- Querying of metric descriptors and monitored resource descriptors.
3232- Creation and deletion of metric descriptors for custom metrics.
33- - ( Writing of custom metric data will be coming soon.)
33+ - Writing of custom metric data.
3434
3535.. _Stackdriver Monitoring API : https://cloud.google.com/monitoring/api/v3/
3636
@@ -278,3 +278,88 @@ follows::
278278
279279.. _Time Series :
280280 https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries
281+
282+
283+ Writing Custom Metrics
284+ ---------------------------
285+
286+ The Stackdriver Monitoring API can be used to write data points to custom metrics. Please refer to
287+ the documentation on `Custom Metrics `_ for more information.
288+
289+ To write a data point to a custom metric, you must provide an instance of
290+ :class: `~google.cloud.monitoring.metric.Metric ` specifying the metric type as well as the values for
291+ the metric labels. You will need to have either created the metric descriptor earlier (see the
292+ `Metric Descriptors `_ section) or rely on metric type auto-creation (see `Auto-creation of
293+ custom metrics `_).
294+
295+ You will also need to provide a :class: `~google.cloud.monitoring.resource.Resource ` instance
296+ specifying a monitored resource type as well as values for all of the monitored resource labels,
297+ except for ``project_id ``, which is ignored when it's included in writes to the API. A good
298+ choice is to use the underlying physical resource where your application code runs – e.g., a
299+ monitored resource type of ``gce_instance `` or ``aws_ec2_instance ``. In some limited
300+ circumstances, such as when only a single process writes to the custom metric, you may choose to
301+ use the ``global `` monitored resource type.
302+
303+ See `Monitored resource types `_ for more information about particular monitored resource types.
304+
305+ >>> from google.cloud import monitoring
306+ >>> # Create a Resource object for the desired monitored resource type.
307+ >>> resource = client.resource(' gce_instance' , labels = {
308+ ... ' instance_id' : ' 1234567890123456789' ,
309+ ... ' zone' : ' us-central1-f'
310+ ... })
311+ >>> # Create a Metric object, specifying the metric type as well as values for any metric labels.
312+ >>> metric = client.metric(type = ' custom.googleapis.com/my_metric' , labels = {
313+ ... ' status' : ' successful'
314+ ... })
315+
316+ With a ``Metric `` and ``Resource `` in hand, the :class: `~google.cloud.monitoring.client.Client `
317+ can be used to write :class: `~google.cloud.monitoring.timeseries.Point ` values.
318+
319+ When writing points, the Python type of the value must match the *value type * of the metric
320+ descriptor associated with the metric. For example, a Python float will map to ``ValueType.DOUBLE ``.
321+
322+ Stackdriver Monitoring supports several *metric kinds *: ``GAUGE ``, ``CUMULATIVE ``, and ``DELTA ``.
323+ However, ``DELTA `` is not supported for custom metrics.
324+
325+ ``GAUGE `` metrics represent only a single point in time, so only the ``end_time `` should be
326+ specified::
327+
328+ >>> client.write_point(metric=metric, resource=resource,
329+ ... value=3.14, end_time=end_time) # API call
330+
331+ By default, ``end_time `` defaults to :meth: `~datetime.datetime.utcnow() `, so metrics can be written
332+ to the current time as follows::
333+
334+ >>> client.write_point(metric, resource, 3.14) # API call
335+
336+ ``CUMULATIVE `` metrics enable the monitoring system to compute rates of increase on metrics that
337+ sometimes reset, such as after a process restart. Without cumulative metrics, this
338+ reset would otherwise show up as a huge negative spike. For cumulative metrics, the same start
339+ time should be re-used repeatedly as more points are written to the time series.
340+
341+ In the examples below, the ``end_time `` again defaults to the current time::
342+
343+ >>> RESET = datetime.utcnow()
344+ >>> client.write_point(metric, resource, 3, start_time=RESET) # API call
345+ >>> client.write_point(metric, resource, 6, start_time=RESET) # API call
346+
347+ To write multiple ``TimeSeries `` in a single batch, you can use
348+ :meth: `~google.cloud.monitoring.client.write_time_series `::
349+
350+ >>> ts1 = client.time_series(metric1, resource, 3.14, end_time=end_time)
351+ >>> ts2 = client.time_series(metric2, resource, 42, end_time=end_time)
352+ >>> client.write_time_series([ts1, ts2]) # API call
353+
354+ While multiple time series can be written in a single batch, each ``TimeSeries `` object sent to
355+ the API must only include a single point.
356+
357+ All timezone-naive Python ``datetime `` objects are assumed to be UTC.
358+
359+ .. _TimeSeries : https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries
360+ .. _Custom Metrics : https://cloud.google.com/monitoring/custom-metrics/
361+ .. _Auto-creation of custom metrics :
362+ https://cloud.google.com/monitoring/custom-metrics/creating-metrics#auto-creation
363+ .. _Metrics : https://cloud.google.com/monitoring/api/v3/metrics
364+ .. _Monitored resource types :
365+ https://cloud.google.com/monitoring/api/resources
0 commit comments