Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Add 'Client.metric' factory.
  • Loading branch information
tseaver committed Mar 14, 2016
commit 5d342faef2ec1762ddfd42e1be9dccdfdbe43c81
18 changes: 18 additions & 0 deletions gcloud/logging/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,24 @@ def list_sinks(self, page_size=None, page_token=None):
for resource in resp.get('sinks', ())]
return sinks, resp.get('nextPageToken')

def metric(self, name, filter_, description=''):
"""Creates a metric bound to the current client.

:type name: string
:param name: the name of the metric to be constructed.

:type filter_: string
:param filter_: the advanced logs filter expression defining the
entries tracked by the metric.

:type description: string
:param description: the description of the metric to be constructed.

:rtype: :class:`gcloud.pubsub.logger.Logger`
:returns: Logger created with the current client.

This comment was marked as spam.

This comment was marked as spam.

"""
return Metric(name, filter_, client=self, description=description)

def list_metrics(self, page_size=None, page_token=None):
"""List metrics for the project associated with this client.

Expand Down
47 changes: 29 additions & 18 deletions gcloud/logging/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ class TestClient(unittest2.TestCase):
SINK_NAME = 'SINK_NAME'
FILTER = 'logName:syslog AND severity>=ERROR'
DESTINATION_URI = 'faux.googleapis.com/destination'
METRIC_NAME = 'metric_name'
FILTER = 'logName:syslog AND severity>=ERROR'
DESCRIPTION = 'DESCRIPTION'

def _getTargetClass(self):
from gcloud.logging.client import Client
Expand Down Expand Up @@ -270,23 +273,34 @@ def test_list_sinks_missing_key(self):
self.assertEqual(req['path'], '/projects/%s/sinks' % PROJECT)
self.assertEqual(req['query_params'], {})

def test_metric(self):
from gcloud.logging.metric import Metric
creds = _Credentials()

client_obj = self._makeOne(project=self.PROJECT, credentials=creds)
metric = client_obj.metric(self.METRIC_NAME, self.FILTER,
description=self.DESCRIPTION)
self.assertTrue(isinstance(metric, Metric))
self.assertEqual(metric.name, self.METRIC_NAME)
self.assertEqual(metric.filter_, self.FILTER)
self.assertEqual(metric.description, self.DESCRIPTION)
self.assertTrue(metric.client is client_obj)
self.assertEqual(metric.project, self.PROJECT)

def test_list_metrics_no_paging(self):
from gcloud.logging.metric import Metric
PROJECT = 'PROJECT'
CREDS = _Credentials()

CLIENT_OBJ = self._makeOne(project=PROJECT, credentials=CREDS)

METRIC_NAME = 'metric_name'
FILTER = 'logName:syslog AND severity>=ERROR'
DESCRIPTION = 'DESCRIPTION'
METRIC_PATH = 'projects/%s/metrics/%s' % (PROJECT, METRIC_NAME)
METRIC_PATH = 'projects/%s/metrics/%s' % (PROJECT, self.METRIC_NAME)

RETURNED = {
'metrics': [{
'name': METRIC_PATH,
'filter': FILTER,
'description': DESCRIPTION,
'filter': self.FILTER,
'description': self.DESCRIPTION,
}],
}
# Replace the connection on the client with one of our own.
Expand All @@ -298,9 +312,9 @@ def test_list_metrics_no_paging(self):
self.assertEqual(len(metrics), 1)
metric = metrics[0]
self.assertTrue(isinstance(metric, Metric))
self.assertEqual(metric.name, METRIC_NAME)
self.assertEqual(metric.filter_, FILTER)
self.assertEqual(metric.description, DESCRIPTION)
self.assertEqual(metric.name, self.METRIC_NAME)
self.assertEqual(metric.filter_, self.FILTER)
self.assertEqual(metric.description, self.DESCRIPTION)
self.assertEqual(next_page_token, None)
self.assertEqual(len(CLIENT_OBJ.connection._requested), 1)
req = CLIENT_OBJ.connection._requested[0]
Expand All @@ -315,18 +329,15 @@ def test_list_metrics_with_paging(self):

CLIENT_OBJ = self._makeOne(project=PROJECT, credentials=CREDS)

METRIC_NAME = 'metric_name'
FILTER = 'logName:syslog AND severity>=ERROR'
DESCRIPTION = 'DESCRIPTION'
METRIC_PATH = 'projects/%s/metrics/%s' % (PROJECT, METRIC_NAME)
METRIC_PATH = 'projects/%s/metrics/%s' % (PROJECT, self.METRIC_NAME)
TOKEN1 = 'TOKEN1'
TOKEN2 = 'TOKEN2'
SIZE = 1
RETURNED = {
'metrics': [{
'name': METRIC_PATH,
'filter': FILTER,
'description': DESCRIPTION,
'filter': self.FILTER,
'description': self.DESCRIPTION,
}],
'nextPageToken': TOKEN2,
}
Expand All @@ -339,9 +350,9 @@ def test_list_metrics_with_paging(self):
self.assertEqual(len(metrics), 1)
metric = metrics[0]
self.assertTrue(isinstance(metric, Metric))
self.assertEqual(metric.name, METRIC_NAME)
self.assertEqual(metric.filter_, FILTER)
self.assertEqual(metric.description, DESCRIPTION)
self.assertEqual(metric.name, self.METRIC_NAME)
self.assertEqual(metric.filter_, self.FILTER)
self.assertEqual(metric.description, self.DESCRIPTION)
self.assertEqual(next_page_token, TOKEN2)
req = CLIENT_OBJ.connection._requested[0]
self.assertEqual(req['path'], '/projects/%s/metrics' % PROJECT)
Expand Down