Skip to content
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

Allow convenient filtering of metric descriptors by type prefix. #1751

Merged
merged 1 commit into from
Apr 25, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions gcloud/monitoring/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,26 +131,36 @@ def fetch_metric_descriptor(self, metric_type):
"""
return MetricDescriptor._fetch(self, metric_type)

def list_metric_descriptors(self, filter_string=None):
def list_metric_descriptors(self, filter_string=None, type_prefix=None):
"""List all metric descriptors for the project.

Example::
Examples::

>>> for descriptor in client.list_metric_descriptors():
... print(descriptor.type)

>>> for descriptor in client.list_metric_descriptors(
... type_prefix='custom.'):
... print(descriptor.type)

:type filter_string: string or None
:param filter_string:
An optional filter expression describing the metric descriptors
to be returned. See the `filter documentation`_.

:type type_prefix: string or None
:param type_prefix: An optional prefix constraining the selected
metric types. This adds ``metric.type = starts_with("<prefix>")``
to the filter.

:rtype: list of :class:`~gcloud.monitoring.metric.MetricDescriptor`
:returns: A list of metric descriptor instances.

.. _filter documentation:
https://cloud.google.com/monitoring/api/v3/filters
"""
return MetricDescriptor._list(self, filter_string)
return MetricDescriptor._list(self, filter_string,
type_prefix=type_prefix)

def fetch_resource_descriptor(self, resource_type):
"""Look up a resource descriptor by type.
Expand Down
20 changes: 16 additions & 4 deletions gcloud/monitoring/metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def _fetch(cls, client, metric_type):
return cls._from_dict(info)

@classmethod
def _list(cls, client, filter_string=None):
def _list(cls, client, filter_string=None, type_prefix=None):
"""List all metric descriptors for the project.

:type client: :class:`gcloud.monitoring.client.Client`
Expand All @@ -143,6 +143,11 @@ def _list(cls, client, filter_string=None):
An optional filter expression describing the metric descriptors
to be returned. See the `filter documentation`_.

:type type_prefix: string or None
:param type_prefix: An optional prefix constraining the selected
metric types. This adds ``metric.type = starts_with("<prefix>")``
to the filter.

:rtype: list of :class:`MetricDescriptor`
:returns: A list of metric descriptor instances.

Expand All @@ -152,14 +157,21 @@ def _list(cls, client, filter_string=None):
path = '/projects/{project}/metricDescriptors/'.format(
project=client.project)

descriptors = []
filters = []
if filter_string is not None:
filters.append(filter_string)

if type_prefix is not None:
filters.append('metric.type = starts_with("{prefix}")'.format(
prefix=type_prefix))

descriptors = []
page_token = None
while True:
params = {}

if filter_string is not None:
params['filter'] = filter_string
if filters:
params['filter'] = ' AND '.join(filters)

if page_token is not None:
params['pageToken'] = page_token
Expand Down
22 changes: 22 additions & 0 deletions gcloud/monitoring/test_metric.py
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,28 @@ def test_list_filtered(self):
'query_params': {'filter': FILTER}}
self.assertEqual(request, expected_request)

def test_list_filtered_by_type_prefix(self):
PROJECT = 'my-project'
PATH = 'projects/{project}/metricDescriptors/'.format(project=PROJECT)

# Request only custom metrics.
PREFIX = 'custom.googleapis.com/'
FILTER = 'metric.type = starts_with("{prefix}")'.format(prefix=PREFIX)

# But let's say there are no custom metrics.
RESPONSE = {'metricDescriptors': []}

connection = _Connection(RESPONSE)
client = _Client(project=PROJECT, connection=connection)
descriptors = self._getTargetClass()._list(client, type_prefix=PREFIX)

self.assertEqual(len(descriptors), 0)

request, = connection._requested
expected_request = {'method': 'GET', 'path': '/' + PATH,
'query_params': {'filter': FILTER}}
self.assertEqual(request, expected_request)


class TestMetric(unittest2.TestCase):

Expand Down
12 changes: 12 additions & 0 deletions system_tests/monitoring.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,18 @@ def test_list_metric_descriptors(self):
self.assertTrue(label.value_type)
self.assertTrue(label.description)

def test_list_metric_descriptors_filtered(self):
client = monitoring.Client()

PREFIX = 'compute.googleapis.com/'
descriptors = client.list_metric_descriptors(type_prefix=PREFIX)

# There are currently 18 types with this prefix, but that may change.

This comment was marked as spam.

self.assertGreater(len(descriptors), 10)

for descriptor in descriptors:
self.assertTrue(descriptor.type.startswith(PREFIX))

def test_fetch_resource_descriptor(self):
client = monitoring.Client()
descriptor = client.fetch_resource_descriptor(RESOURCE_TYPE)
Expand Down