|
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,126 @@ def metric_descriptor(self, type_, |
195 | 203 | display_name=display_name, |
196 | 204 | ) |
197 | 205 |
|
| 206 | + @staticmethod |
| 207 | + def metric(type_, labels): |
| 208 | + """Factory for constructing metric objects. |
| 209 | +
|
| 210 | + :class:`~gcloud.monitoring.metric.Metric` objects are typically |
| 211 | + created to write custom metric values. The type should match the |
| 212 | + metric type specified in the |
| 213 | + :class:`~gcloud.monitoring.metric.MetricDescriptor` used to |
| 214 | + create the custom metric:: |
| 215 | +
|
| 216 | + >>> metric = client.metric('custom.googleapis.com/my_metric', |
| 217 | + ... labels={ |
| 218 | + ... 'status': 'successful', |
| 219 | + ... }) |
| 220 | +
|
| 221 | + :type type_: string |
| 222 | + :param type_: The metric type name. |
| 223 | +
|
| 224 | + :type labels: dict |
| 225 | + :param labels: A mapping from label names to values for all labels |
| 226 | + enumerated in the associated |
| 227 | + :class:`~gcloud.monitoring.metric.MetricDescriptor`. |
| 228 | +
|
| 229 | + :rtype: :class:`~gcloud.monitoring.metric.Metric` |
| 230 | + :returns: The metric object. |
| 231 | + """ |
| 232 | + return Metric(type=type_, labels=labels) |
| 233 | + |
| 234 | + @staticmethod |
| 235 | + def resource(type_, labels): |
| 236 | + """Factory for constructing monitored resource objects. |
| 237 | +
|
| 238 | + A monitored resource object ( |
| 239 | + :class:`~gcloud.monitoring.resource.Resource`) is |
| 240 | + typically used to create a |
| 241 | + :class:`~gcloud.monitoring.timeseries.TimeSeries` object. |
| 242 | +
|
| 243 | + For a list of possible monitored resource types and their associated |
| 244 | + labels, see: |
| 245 | +
|
| 246 | + https://cloud.google.com/monitoring/api/resources |
| 247 | +
|
| 248 | + :type type_: string |
| 249 | + :param type_: The monitored resource type name. |
| 250 | +
|
| 251 | + :type labels: dict |
| 252 | + :param labels: A mapping from label names to values for all labels |
| 253 | + enumerated in the associated |
| 254 | + :class:`~gcloud.monitoring.resource.ResourceDescriptor`, |
| 255 | + except that ``project_id`` can and should be omitted |
| 256 | + when writing time series data. |
| 257 | +
|
| 258 | + :rtype: :class:`~gcloud.monitoring.resource.Resource` |
| 259 | + :returns: A monitored resource object. |
| 260 | + """ |
| 261 | + return Resource(type_, labels) |
| 262 | + |
| 263 | + @staticmethod |
| 264 | + def time_series(metric, resource, value, |
| 265 | + end_time=None, start_time=None): |
| 266 | + """Construct a time series object for a single data point. |
| 267 | +
|
| 268 | + .. note:: |
| 269 | +
|
| 270 | + While :class:`~gcloud.monitoring.timeseries.TimeSeries` objects |
| 271 | + returned by the API typically have multiple data points, |
| 272 | + :class:`~gcloud.monitoring.timeseries.TimeSeries` objects |
| 273 | + sent to the API must have at most one point. |
| 274 | +
|
| 275 | + For example:: |
| 276 | +
|
| 277 | + >>> timeseries = client.time_series(metric, resource, 1.23, |
| 278 | + ... end_time=end) |
| 279 | +
|
| 280 | + For more information, see: |
| 281 | +
|
| 282 | + https://cloud.google.com/monitoring/api/ref_v3/rest/v3/TimeSeries |
| 283 | +
|
| 284 | + :type metric: :class:`~gcloud.monitoring.metric.Metric` |
| 285 | + :param metric: A :class:`~gcloud.monitoring.metric.Metric` object. |
| 286 | +
|
| 287 | + :type resource: :class:`~gcloud.monitoring.resource.Resource` |
| 288 | + :param resource: A :class:`~gcloud.monitoring.resource.Resource` |
| 289 | + object. |
| 290 | +
|
| 291 | + :type value: bool, int, string, or float |
| 292 | + :param value: |
| 293 | + The value of the data point to create for the |
| 294 | + :class:`~gcloud.monitoring.timeseries.TimeSeries`. |
| 295 | +
|
| 296 | + .. note:: |
| 297 | +
|
| 298 | + The Python type of the value will determine the |
| 299 | + `class`:ValueType: sent to the API, which must match the value |
| 300 | + type specified in the metric descriptor. For example, a Python |
| 301 | + float will be sent to the API as a :data:`ValueType.DOUBLE`. |
| 302 | +
|
| 303 | + :type end_time: :class:`~datetime.datetime` |
| 304 | + :param end_time: |
| 305 | + The end time for the point to be included in the time series. |
| 306 | + Assumed to be UTC if no time zone information is present. |
| 307 | + Defaults to the current time, as obtained by calling |
| 308 | + :meth:`datetime.datetime.utcnow`. |
| 309 | +
|
| 310 | + :type start_time: :class:`~datetime.datetime` |
| 311 | + :param start_time: |
| 312 | + The start time for the point to be included in the time series. |
| 313 | + Assumed to be UTC if no time zone information is present |
| 314 | + Defaults to None. If the start time is unspecified, |
| 315 | + the API interprets the start time to be the same as the end time. |
| 316 | +
|
| 317 | + :rtype: :class:`~gcloud.monitoring.timeseries.TimeSeries` |
| 318 | + :returns: A time series object. |
| 319 | + """ |
| 320 | + if end_time is None: |
| 321 | + end_time = _UTCNOW() |
| 322 | + point = Point(value=value, start_time=start_time, end_time=end_time) |
| 323 | + return TimeSeries(metric=metric, resource=resource, metric_kind=None, |
| 324 | + value_type=None, points=[point]) |
| 325 | + |
198 | 326 | def fetch_metric_descriptor(self, metric_type): |
199 | 327 | """Look up a metric descriptor by type. |
200 | 328 |
|
|
0 commit comments