|
28 | 28 | https://cloud.google.com/monitoring/api/v3/ |
29 | 29 | """ |
30 | 30 |
|
| 31 | +import datetime |
| 32 | + |
31 | 33 | from gcloud.client import JSONClient |
32 | 34 | from gcloud.monitoring.connection import Connection |
33 | 35 | from gcloud.monitoring.group import Group |
| 36 | +from gcloud.monitoring.metric import Metric |
34 | 37 | from gcloud.monitoring.metric import MetricDescriptor |
35 | 38 | from gcloud.monitoring.metric import MetricKind |
36 | 39 | from gcloud.monitoring.metric import ValueType |
37 | 40 | from gcloud.monitoring.query import Query |
| 41 | +from gcloud.monitoring.resource import Resource |
38 | 42 | from gcloud.monitoring.resource import ResourceDescriptor |
| 43 | +from gcloud.monitoring.timeseries import Point |
| 44 | +from gcloud.monitoring.timeseries import TimeSeries |
| 45 | + |
| 46 | +_UTCNOW = datetime.datetime.utcnow # To be replaced by tests. |
39 | 47 |
|
40 | 48 |
|
41 | 49 | class Client(JSONClient): |
@@ -195,6 +203,130 @@ def metric_descriptor(self, type_, |
195 | 203 | display_name=display_name, |
196 | 204 | ) |
197 | 205 |
|
| 206 | + @staticmethod |
| 207 | + def metric(type_, labels): |
| 208 | + """Construct a metric object. |
| 209 | +
|
| 210 | + The type of the Metric should match the type of a MetricDescriptor. |
| 211 | + For a list of built-in Metric types and the associated labels, see: |
| 212 | +
|
| 213 | + https://cloud.google.com/monitoring/api/metrics |
| 214 | +
|
| 215 | + Here is an example of creating using a built-in metric type: |
| 216 | +
|
| 217 | + >>> type_ = 'compute.googleapis.com/instance/cpu/utilization' |
| 218 | + >>> metric = client.metric(type_, labels={ |
| 219 | + ... 'instance_name': 'my-instance', |
| 220 | + ... }) |
| 221 | +
|
| 222 | + For custom metrics, the simplest way to get the type is from the |
| 223 | + :class:`~gcloud.monitoring.metric.MetricDescriptor` class used to |
| 224 | + create the custom metric:: |
| 225 | +
|
| 226 | + >>> metric = client.metric(metric_descriptor.type, labels={ |
| 227 | + ... 'instance_name': 'my-instance', |
| 228 | + ... }) |
| 229 | +
|
| 230 | + :type type_: string |
| 231 | + :param type_: The metric type name. |
| 232 | +
|
| 233 | + :type labels: dict |
| 234 | + :param labels: A mapping from label names to values for all labels |
| 235 | + enumerated in the associated :class:`MetricDescriptor`. |
| 236 | +
|
| 237 | + :rtype: :class:`~gcloud.monitoring.metric.Metric` |
| 238 | + :returns: The Metric created with the passed in arguments |
| 239 | + """ |
| 240 | + return Metric(type=type_, labels=labels) |
| 241 | + |
| 242 | + @staticmethod |
| 243 | + def resource(type_, labels): |
| 244 | + """Construct a resource object. |
| 245 | +
|
| 246 | + For a list of possible MonitoredResources and their associated labels, |
| 247 | + see: |
| 248 | +
|
| 249 | + https://cloud.google.com/monitoring/api/resources |
| 250 | +
|
| 251 | + For example, to create a Resource object for a GCE instance:: |
| 252 | +
|
| 253 | + >>> resource = client.resource('gce_instance', labels={ |
| 254 | + ... 'project_id': 'my-project-id', |
| 255 | + ... 'instance_id': 'my-instance-id', |
| 256 | + ... 'zone': 'us-central1-f' |
| 257 | + ... }) |
| 258 | +
|
| 259 | + :type type_: string |
| 260 | + :param type_: The monitored resource type name. |
| 261 | +
|
| 262 | + :type labels: dict |
| 263 | + :param labels: A mapping from label names to values for all labels |
| 264 | + enumerated in the associated |
| 265 | + :class:`ResourceDescriptor`. |
| 266 | +
|
| 267 | + :rtype: :class:`gcloud.resource.Resource` |
| 268 | + :returns: The Resource created with the passed in arguments |
| 269 | + """ |
| 270 | + return Resource(type_, labels) |
| 271 | + |
| 272 | + @staticmethod |
| 273 | + def timeseries(metric, resource, value, |
| 274 | + start_time=None, end_time=None): |
| 275 | + """Constructs a TimeSeries object for a single data point. |
| 276 | +
|
| 277 | + .. note:: |
| 278 | +
|
| 279 | + While TimeSeries objects returned by the API can have |
| 280 | + multiple data points when queried as aggregates, TimeSeries created |
| 281 | + on the client-side can have at most one point. |
| 282 | +
|
| 283 | + For example:: |
| 284 | +
|
| 285 | + >>> timeseries = client.timeseries(metric, resource, value, |
| 286 | + ... end_time=end) |
| 287 | +
|
| 288 | + For more information, see: |
| 289 | +
|
| 290 | + https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries |
| 291 | +
|
| 292 | + :type metric: :class:`~gcloud.monitoring.metric.Metric` |
| 293 | + :param metric: A metric object. |
| 294 | +
|
| 295 | + :type resource: :class:`~gcloud.monitoring.resource.Resource` |
| 296 | + :param resource: A resource object. |
| 297 | +
|
| 298 | + :type value: bool, int, string, or float |
| 299 | + :param value: |
| 300 | + The value of the data point to create for the TimeSeries |
| 301 | +
|
| 302 | + .. note:: |
| 303 | +
|
| 304 | + This parameter can be multiple types and the TypedValue of the |
| 305 | + `gcloud.monitoring.TimeSeries.Point` will be mapped from the |
| 306 | + type passed in. For example, a Python float will be sent to |
| 307 | + the API as a doubleValue. |
| 308 | +
|
| 309 | + :type start_time: :class:`datetime.datetime` |
| 310 | + :param start_time: |
| 311 | + The start time for the point written to the |
| 312 | + timeseries. Defaults to None, which has meaning dependent on the |
| 313 | + metric_kind of the metric being written to. |
| 314 | +
|
| 315 | + :type end_time: :class`datetime.datetime` |
| 316 | + :param end_time: |
| 317 | + The end time for the point written to the timeseries. Defaults to |
| 318 | + the current time, as obtained by calling |
| 319 | + :meth:`datetime.datetime.utcnow` |
| 320 | +
|
| 321 | + :rtype: :class:`gcloud.timeseries.TimeSeries` |
| 322 | + :returns: The TimeSeries created with the passed in arguments |
| 323 | + """ |
| 324 | + if end_time is None: |
| 325 | + end_time = _UTCNOW() |
| 326 | + point = Point(value=value, start_time=start_time, end_time=end_time) |
| 327 | + return TimeSeries(metric=metric, resource=resource, metric_kind=None, |
| 328 | + value_type=None, points=[point]) |
| 329 | + |
198 | 330 | def fetch_metric_descriptor(self, metric_type): |
199 | 331 | """Look up a metric descriptor by type. |
200 | 332 |
|
|
0 commit comments