|
| 1 | +Using the API |
| 2 | +============= |
| 3 | + |
| 4 | + |
| 5 | +Introduction |
| 6 | +------------ |
| 7 | + |
| 8 | +With the Monitoring API, you can work with Stackdriver metric data |
| 9 | +pertaining to monitored resources in Google Cloud Platform (GCP) |
| 10 | +or elsewhere. |
| 11 | + |
| 12 | +Essential concepts: |
| 13 | + |
| 14 | +- Metric data is associated with a **monitored resource**. A monitored |
| 15 | + resource has a *resource type* and a set of *resource labels* — |
| 16 | + key-value pairs — that identify the particular resource. |
| 17 | +- A **metric** further identifies the particular kind of data that |
| 18 | + is being collected. It has a *metric type* and a set of *metric |
| 19 | + labels* that, when combined with the resource labels, identify |
| 20 | + a particular time series. |
| 21 | +- A **time series** is a collection of data points associated with |
| 22 | + points or intervals in time. |
| 23 | + |
| 24 | +Please refer to the documentation for the `Monitoring API`_ for |
| 25 | +more information. |
| 26 | + |
| 27 | +At present, this client library supports querying of time series, |
| 28 | +metric descriptors, and resource descriptors. |
| 29 | + |
| 30 | +.. _Monitoring API: https://cloud.google.com/monitoring/api/ |
| 31 | + |
| 32 | + |
| 33 | +The Monitoring Client Object |
| 34 | +---------------------------- |
| 35 | + |
| 36 | +The monitoring client library generally makes its |
| 37 | +functionality available as methods of the monitoring |
| 38 | +:class:`~gcloud.monitoring.client.Client` class. |
| 39 | +A :class:`~gcloud.monitoring.client.Client` instance holds |
| 40 | +authentication credentials and the ID of the target project with |
| 41 | +which the metric data of interest is associated. This project ID |
| 42 | +will often refer to a `Stackdriver account`_ binding multiple |
| 43 | +GCP projects and AWS accounts. It can also simply be the ID of |
| 44 | +a monitored project. |
| 45 | + |
| 46 | +Most often the authentication credentials will be determined |
| 47 | +implicitly from your environment. See :doc:`gcloud-auth` for |
| 48 | +more information. |
| 49 | + |
| 50 | +It is thus typical to create a client object as follows:: |
| 51 | + |
| 52 | + >>> from gcloud import monitoring |
| 53 | + >>> client = monitoring.Client(project='target-project') |
| 54 | + |
| 55 | +If you are running in Google Compute Engine or Google App Engine, |
| 56 | +the current project is the default target project. This default |
| 57 | +can be further overridden with the :envvar:`GCLOUD_PROJECT` |
| 58 | +environment variable. Using the default target project is |
| 59 | +even easier:: |
| 60 | + |
| 61 | + >>> client = monitoring.Client() |
| 62 | + |
| 63 | +If necessary, you can pass in ``credentials`` and ``project`` explicitly:: |
| 64 | + |
| 65 | + >>> client = monitoring.Client(project='target-project', credentials=...) |
| 66 | + |
| 67 | +.. _Stackdriver account: https://cloud.google.com/monitoring/accounts/ |
| 68 | + |
| 69 | + |
| 70 | +Monitored Resource Descriptors |
| 71 | +------------------------------ |
| 72 | + |
| 73 | +The available monitored resource types are defined by *monitored resource |
| 74 | +descriptors*. You can fetch a list of these with the |
| 75 | +:meth:`~gcloud.monitoring.client.Client.list_resource_descriptors` method:: |
| 76 | + |
| 77 | + >>> for descriptor in client.list_resource_descriptors(): |
| 78 | + ... print(descriptor.type) |
| 79 | + |
| 80 | +Each :class:`~gcloud.monitoring.resource.ResourceDescriptor` |
| 81 | +has a type, a display name, a description, and a list of |
| 82 | +:class:`~gcloud.monitoring.label.LabelDescriptor` instances. |
| 83 | +See the documentation about `Monitored Resources`_ |
| 84 | +for more information. |
| 85 | + |
| 86 | +.. _Monitored Resources: |
| 87 | + https://cloud.google.com/monitoring/api/v3/monitored-resources |
| 88 | + |
| 89 | + |
| 90 | +Metric Descriptors |
| 91 | +------------------ |
| 92 | + |
| 93 | +The available metric types are defined by *metric descriptors*. |
| 94 | +They include `platform metrics`_, `agent metrics`_, and `custom metrics`_. |
| 95 | +You can list all of these with the |
| 96 | +:meth:`~gcloud.monitoring.client.Client.list_metric_descriptors` method:: |
| 97 | + |
| 98 | + >>> for descriptor in client.list_metric_descriptors(): |
| 99 | + ... print(descriptor.type) |
| 100 | + |
| 101 | +See :class:`~gcloud.monitoring.metric.MetricDescriptor` and the |
| 102 | +`Metric Descriptors`_ API documentation for more information. |
| 103 | + |
| 104 | +.. _platform metrics: https://cloud.google.com/monitoring/api/metrics |
| 105 | +.. _agent metrics: https://cloud.google.com/monitoring/agent/ |
| 106 | +.. _custom metrics: https://cloud.google.com/monitoring/custom-metrics/ |
| 107 | +.. _Metric Descriptors: |
| 108 | + https://cloud.google.com/monitoring/api/ref_v3/rest/v3/\ |
| 109 | + projects.metricDescriptors |
| 110 | + |
| 111 | + |
| 112 | +Time Series Queries |
| 113 | +------------------- |
| 114 | + |
| 115 | +A time series includes a collection of data points and a set of |
| 116 | +resource and metric label values. |
| 117 | +See :class:`~gcloud.monitoring.timeseries.TimeSeries` and the |
| 118 | +`Time Series`_ API documentation for more information. |
| 119 | + |
| 120 | +While you can obtain time series objects by iterating over a |
| 121 | +:class:`~gcloud.monitoring.query.Query` object, usually it is |
| 122 | +more useful to retrieve time series data in the form of a |
| 123 | +:class:`pandas.DataFrame`, where each column corresponds to a |
| 124 | +single time series. For this, you must have :mod:`pandas` installed; |
| 125 | +it is not a required dependency of ``gcloud-python``. |
| 126 | + |
| 127 | +You can display CPU utilization across your GCE instances during |
| 128 | +the last five minutes as follows:: |
| 129 | + |
| 130 | + >>> METRIC = 'compute.googleapis.com/instance/cpu/utilization' |
| 131 | + >>> query = client.query(METRIC, minutes=5) |
| 132 | + >>> print(query.as_dataframe()) |
| 133 | + |
| 134 | +:class:`~gcloud.monitoring.query.Query` objects provide a variety of |
| 135 | +methods for refining the query. You can request temporal alignment |
| 136 | +and cross-series reduction, and you can filter by label values. |
| 137 | +See the client :meth:`~gcloud.monitoring.client.Client.query` method |
| 138 | +and the :class:`~gcloud.monitoring.query.Query` class for more |
| 139 | +information. |
| 140 | + |
| 141 | +For example, you can display CPU utilization during the last hour |
| 142 | +across GCE instances with names beginning with ``"mycluster-"``, |
| 143 | +averaged over five-minute intervals and aggregated per zone, as |
| 144 | +follows:: |
| 145 | + |
| 146 | + >>> from gcloud.monitoring import Aligner, Reducer |
| 147 | + >>> METRIC = 'compute.googleapis.com/instance/cpu/utilization' |
| 148 | + >>> query = (client.query(METRIC, hours=1) |
| 149 | + ... .select_metrics(instance_name_prefix='mycluster-') |
| 150 | + ... .align(Aligner.ALIGN_MEAN, minutes=5) |
| 151 | + ... .reduce(Reducer.REDUCE_MEAN, 'resource.zone')) |
| 152 | + >>> print(query.as_dataframe()) |
| 153 | + |
| 154 | +.. _Time Series: |
| 155 | + https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries |
0 commit comments