Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 1.8k
Monitoring Custom Metrics#2057
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uh oh!
There was an error while loading. Please reload this page.
Changes from all commits
6a643c083ce1082ddc536a8337a01a0fff221b6bb958ba168c075afcb6d3e9c2e79651ff46b70File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -30,7 +30,7 @@ of the API: | ||
| - Querying of time series. | ||
| - Querying of metric descriptors and monitored resource descriptors. | ||
| - Creation and deletion of metric descriptors for custom metrics. | ||
| - (Writing of custom metric data will be coming soon.) | ||
| - Writing of custom metric data. | ||
| .. _Stackdriver Monitoring API: https://cloud.google.com/monitoring/api/v3/ | ||
| @@ -278,3 +278,88 @@ follows:: | ||
| .. _Time Series: | ||
| https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries | ||
| Writing Custom Metrics | ||
| --------------------------- | ||
| The Stackdriver Monitoring API can be used to write data points to custom metrics. Please refer to | ||
| the documentation on `Custom Metrics`_ for more information. | ||
| To write a data point to a custom metric, you must provide an instance of | ||
| :class:`~google.cloud.monitoring.metric.Metric` specifying the metric type as well as the values for | ||
| the metric labels. You will need to have either created the metric descriptor earlier (see the | ||
| `Metric Descriptors`_ section) or rely on metric type auto-creation (see `Auto-creation of | ||
| custom metrics`_). | ||
| You will also need to provide a :class:`~google.cloud.monitoring.resource.Resource` instance | ||
| specifying a monitored resource type as well as values for all of the monitored resource labels, | ||
| except for ``project_id``, which is ignored when it's included in writes to the API. A good | ||
| choice is to use the underlying physical resource where your application code runs – e.g., a | ||
| monitored resource type of ``gce_instance`` or ``aws_ec2_instance``. In some limited | ||
| circumstances, such as when only a single process writes to the custom metric, you may choose to | ||
| use the ``global`` monitored resource type. | ||
| See `Monitored resource types`_ for more information about particular monitored resource types. | ||
| >>> from google.cloud import monitoring | ||
| >>> # Create a Resource object for the desired monitored resource type. | ||
| >>> resource = client.resource('gce_instance', labels={ | ||
| ... 'instance_id': '1234567890123456789', | ||
| ... 'zone': 'us-central1-f' | ||
| ... }) | ||
| >>> # Create a Metric object, specifying the metric type as well as values for any metric labels. | ||
| >>> metric = client.metric(type='custom.googleapis.com/my_metric', labels={ | ||
| ... 'status': 'successful' | ||
| ... }) | ||
| With a ``Metric`` and ``Resource`` in hand, the :class:`~google.cloud.monitoring.client.Client` | ||
| can be used to write :class:`~google.cloud.monitoring.timeseries.Point` values. | ||
| When writing points, the Python type of the value must match the *value type* of the metric | ||
| descriptor associated with the metric. For example, a Python float will map to ``ValueType.DOUBLE``. | ||
| Stackdriver Monitoring supports several *metric kinds*: ``GAUGE``, ``CUMULATIVE``, and ``DELTA``. | ||
| However, ``DELTA`` is not supported for custom metrics. | ||
| ``GAUGE`` metrics represent only a single point in time, so only the ``end_time`` should be | ||
| specified:: | ||
| >>> client.write_point(metric=metric, resource=resource, | ||
| ... value=3.14, end_time=end_time) # API call | ||
| By default, ``end_time`` defaults to :meth:`~datetime.datetime.utcnow()`, so metrics can be written | ||
| to the current time as follows:: | ||
| >>> client.write_point(metric, resource, 3.14) # API call | ||
| ``CUMULATIVE`` metrics enable the monitoring system to compute rates of increase on metrics that | ||
| sometimes reset, such as after a process restart. Without cumulative metrics, this | ||
| reset would otherwise show up as a huge negative spike. For cumulative metrics, the same start | ||
| time should be re-used repeatedly as more points are written to the time series. | ||
| In the examples below, the ``end_time`` again defaults to the current time:: | ||
| >>> RESET = datetime.utcnow() | ||
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page.
This comment was marked as spam.Sorry, something went wrong. Uh oh!There was an error while loading. Please reload this page. | ||
| >>> client.write_point(metric, resource, 3, start_time=RESET) # API call | ||
| >>> client.write_point(metric, resource, 6, start_time=RESET) # API call | ||
| To write multiple ``TimeSeries`` in a single batch, you can use | ||
| :meth:`~google.cloud.monitoring.client.write_time_series`:: | ||
| >>> ts1 = client.time_series(metric1, resource, 3.14, end_time=end_time) | ||
| >>> ts2 = client.time_series(metric2, resource, 42, end_time=end_time) | ||
| >>> client.write_time_series([ts1, ts2]) # API call | ||
| While multiple time series can be written in a single batch, each ``TimeSeries`` object sent to | ||
| the API must only include a single point. | ||
| All timezone-naive Python ``datetime`` objects are assumed to be UTC. | ||
| .. _TimeSeries: https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries | ||
| .. _Custom Metrics: https://cloud.google.com/monitoring/custom-metrics/ | ||
| .. _Auto-creation of custom metrics: | ||
| https://cloud.google.com/monitoring/custom-metrics/creating-metrics#auto-creation | ||
| .. _Metrics: https://cloud.google.com/monitoring/api/v3/metrics | ||
| .. _Monitored resource types: | ||
| https://cloud.google.com/monitoring/api/resources | ||
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
This comment was marked as spam.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.