Skip to content

Commit

Permalink
Allow using a metric type in the Query method select_metrics.
Browse files Browse the repository at this point in the history
This is useful when a user wants to apply the exact same filters
(duration, aggregation, resource labels) for a different metric type.
  • Loading branch information
supriyagarg committed Nov 15, 2016
1 parent 1b1bfe4 commit 84122a1
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
22 changes: 22 additions & 0 deletions monitoring/google/cloud/monitoring/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,8 @@ def select_metrics(self, *args, **kwargs):
query = query.select_metrics(instance_name='myinstance')
query = query.select_metrics(instance_name_prefix='mycluster-')
query = query.select_metrics(
metric_type='compute.googleapis.com/instance/cpu/utilization')
A keyword argument ``<label>=<value>`` ordinarily generates a filter
expression of the form::
Expand All @@ -314,6 +316,20 @@ def select_metrics(self, *args, **kwargs):
metric.label.<label> = ends_with("<value>")
As a special case, ``"metric_type"`` is treated as a special
pseudo-label corresponding to the filter object ``metric.type``.
For example, ``metric_type=<value>`` generates::
metric.type = "<value>"
See the `supported metrics`_.
.. note::
Currently, the query can only support a single metric type. Given
this, prefix and suffix filtering is not supported for
``"metric_type"``.
If the label's value type is ``INT64``, a similar notation can be
used to express inequalities:
Expand Down Expand Up @@ -344,6 +360,8 @@ def select_metrics(self, *args, **kwargs):
:rtype: :class:`Query`
:returns: The new query object.
.. _supported metrics: https://cloud.google.com/monitoring/api/metrics
"""
new_query = self.copy()
new_query._filter.select_metrics(*args, **kwargs)
Expand Down Expand Up @@ -630,6 +648,10 @@ def select_metrics(self, *args, **kwargs):
See :meth:`Query.select_metrics`.
"""
new_metric_type = kwargs.pop('metric_type', None)
if new_metric_type is not None:
self.metric_type = new_metric_type

self.metric_label_filter = _build_label_filter('metric',
*args, **kwargs)

Expand Down
22 changes: 18 additions & 4 deletions monitoring/unit_tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,13 +206,15 @@ def test_filter_by_resources(self):

def test_filter_by_metrics(self):
INSTANCE = 'my-instance'
NEW_METRIC_TYPE = 'compute.googleapis.com/instance/utilization'
client = _Client(project=PROJECT, connection=_Connection())
query = self._make_one(client, METRIC_TYPE)
query = query.select_metrics(instance_name=INSTANCE)
query = query.select_metrics(metric_type=NEW_METRIC_TYPE,
instance_name=INSTANCE)
expected = (
'metric.type = "{type}"'
' AND metric.label.instance_name = "{instance}"'
).format(type=METRIC_TYPE, instance=INSTANCE)
).format(type=NEW_METRIC_TYPE, instance=INSTANCE)
self.assertEqual(query.filter, expected)

def test_request_parameters_minimal(self):
Expand Down Expand Up @@ -522,15 +524,27 @@ def test_maximal(self):
obj.projects = 'project-1', 'project-2'
obj.select_resources(resource_type='some-resource',
resource_label='foo')
obj.select_metrics(metric_label_prefix='bar-')
obj.select_metrics(metric_type='some-metric',
metric_label_prefix='bar-')

expected = (
'metric.type = "{type}"'
'metric.type = "some-metric"'
' AND group.id = "1234567"'
' AND project = "project-1" OR project = "project-2"'
' AND resource.label.resource_label = "foo"'
' AND resource.type = "some-resource"'
' AND metric.label.metric_label = starts_with("bar-")'
)

self.assertEqual(str(obj), expected)

def test_select_metrics_without_type(self):
obj = self._make_one(METRIC_TYPE)
obj.select_metrics(metric_label_prefix='bar-')

expected = (
'metric.type = "{type}"'
' AND metric.label.metric_label = starts_with("bar-")'
).format(type=METRIC_TYPE)

self.assertEqual(str(obj), expected)
Expand Down

0 comments on commit 84122a1

Please sign in to comment.