Skip to content
This repository has been archived by the owner on Sep 3, 2022. It is now read-only.

Commit

Permalink
Further fixes. Going to put this branch on the backburner for a bit b…
Browse files Browse the repository at this point in the history
…ecause it seems the google.cloud.monitoring library made a significant change from a single Client class to a series of smaller ServiceClients, and our code seems to assume we have a single object with the functionality of multiple of these ServiceClients in various places.
  • Loading branch information
rileyjbauer committed Aug 15, 2018
1 parent 4c9a048 commit 22b4efd
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 19 deletions.
4 changes: 3 additions & 1 deletion google/datalab/stackdriver/monitoring/_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ def __init__(self,
:meth:`~google.cloud.monitoring_v3.query.Query.select_interval`.
"""
client = _utils.make_client(context)
super(Query, self).__init__(client, metric_type,
super(Query, self).__init__(client,
project=context.project_id,
metric_type=metric_type,
end_time=end_time,
days=days, hours=hours, minutes=minutes)

Expand Down
11 changes: 7 additions & 4 deletions google/datalab/stackdriver/monitoring/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,18 @@

from __future__ import absolute_import

from google.cloud.logging.client import Client
# google.cloud.monitoring used to have a single Client, but
# it has now been broken up into multiple subclients. Most
# of the usages I found aligned with the MetricServiceClient,
# but other uses, such as those in group_tests.py, require a
# GroupServiceClient, and there are potentially others.
from google.cloud.monitoring_v3 import MetricServiceClient

import google.datalab


def make_client(context=None):
context = context or google.datalab.Context.default()
client = Client(
project=context.project_id,
client = MetricServiceClient(
credentials=context.credentials,
)
client._connection.USER_AGENT = 'pydatalab/v0'
Expand Down
14 changes: 7 additions & 7 deletions tests/stackdriver/monitoring/metric_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import mock
import unittest

import google.cloud.logging.client
import google.cloud.monitoring_v3

import google.auth
import google.datalab
Expand Down Expand Up @@ -70,7 +70,7 @@ def test_constructor_maximal(self):
self.assertEqual(descriptors._type_prefix, TYPE_PREFIX)
self.assertIsNone(descriptors._descriptors)

@mock.patch('google.cloud.logging.client.Client.list_metric_descriptors')
@mock.patch('google.cloud.monitoring_v3.MetricServiceClient.list_metric_descriptors')
def test_list(self, mock_gcloud_list_descriptors):
mock_gcloud_list_descriptors.return_value = self._list_metrics_get_result(
context=self.context)
Expand All @@ -83,7 +83,7 @@ def test_list(self, mock_gcloud_list_descriptors):
self.assertEqual(metric_descriptor_list[0].type, METRIC_TYPES[0])
self.assertEqual(metric_descriptor_list[1].type, METRIC_TYPES[1])

@mock.patch('google.cloud.logging.client.Client.list_metric_descriptors')
@mock.patch('google.cloud.monitoring_v3.MetricServiceClient.list_metric_descriptors')
def test_list_w_api_filter(self, mock_gcloud_list_descriptors):
mock_gcloud_list_descriptors.return_value = self._list_metrics_get_result(
context=self.context)
Expand All @@ -99,7 +99,7 @@ def test_list_w_api_filter(self, mock_gcloud_list_descriptors):
self.assertEqual(metric_descriptor_list[0].type, METRIC_TYPES[0])
self.assertEqual(metric_descriptor_list[1].type, METRIC_TYPES[1])

@mock.patch('google.cloud.logging.client.Client.list_metric_descriptors')
@mock.patch('google.cloud.monitoring_v3.MetricServiceClient.list_metric_descriptors')
def test_list_w_pattern_match(self, mock_gcloud_list_descriptors):
mock_gcloud_list_descriptors.return_value = self._list_metrics_get_result(
context=self.context)
Expand All @@ -111,7 +111,7 @@ def test_list_w_pattern_match(self, mock_gcloud_list_descriptors):
self.assertEqual(len(metric_descriptor_list), 1)
self.assertEqual(metric_descriptor_list[0].type, METRIC_TYPES[1])

@mock.patch('google.cloud.logging.client.Client.list_metric_descriptors')
@mock.patch('google.cloud.monitoring_v3.MetricServiceClient.list_metric_descriptors')
def test_list_caching(self, mock_gcloud_list_descriptors):
mock_gcloud_list_descriptors.return_value = self._list_metrics_get_result(
context=self.context)
Expand Down Expand Up @@ -164,10 +164,10 @@ def _create_context(project_id):
@staticmethod
def _list_metrics_get_result(context):
client = gcm._utils.make_client(context=context)
all_labels = [google.cloud.monitoring.LabelDescriptor(**labels)
all_labels = [google.cloud.monitoring_v3.enums.LabelDescriptor(**labels)
for labels in LABELS]
descriptors = [
client.metric_descriptor(
client.create_metric_descriptor(
metric_type, metric_kind=METRIC_KIND, value_type=VALUE_TYPE,
unit=UNIT, display_name=display_name, labels=all_labels,
)
Expand Down
14 changes: 7 additions & 7 deletions tests/stackdriver/monitoring/resource_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import mock
import unittest

import google.cloud.monitoring
import google.cloud.monitoring_v3

import google.auth
import google.datalab
Expand Down Expand Up @@ -59,7 +59,7 @@ def test_constructor_maximal(self):
self.assertEqual(descriptors._filter_string, FILTER_STRING)
self.assertIsNone(descriptors._descriptors)

@mock.patch('google.cloud.monitoring.Client.list_resource_descriptors')
@mock.patch('google.cloud.monitoring_v3.MetricServiceClient.list_monitored_resource_descriptors')
def test_list(self, mock_api_list_descriptors):
mock_api_list_descriptors.return_value = self._list_resources_get_result()

Expand All @@ -70,7 +70,7 @@ def test_list(self, mock_api_list_descriptors):
self.assertEqual(resource_descriptor_list[0].type, RESOURCE_TYPES[0])
self.assertEqual(resource_descriptor_list[1].type, RESOURCE_TYPES[1])

@mock.patch('google.cloud.monitoring.Client.list_resource_descriptors')
@mock.patch('google.cloud.monitoring_v3.MetricServiceClient.list_monitored_resource_descriptors')
def test_list_w_api_filter(self, mock_api_list_descriptors):
mock_api_list_descriptors.return_value = self._list_resources_get_result()

Expand All @@ -84,7 +84,7 @@ def test_list_w_api_filter(self, mock_api_list_descriptors):
self.assertEqual(resource_descriptor_list[0].type, RESOURCE_TYPES[0])
self.assertEqual(resource_descriptor_list[1].type, RESOURCE_TYPES[1])

@mock.patch('google.cloud.monitoring.Client.list_resource_descriptors')
@mock.patch('google.cloud.monitoring_v3.MetricServiceClient.list_monitored_resource_descriptors')
def test_list_w_pattern_match(self, mock_api_list_descriptors):
mock_api_list_descriptors.return_value = self._list_resources_get_result()

Expand All @@ -94,7 +94,7 @@ def test_list_w_pattern_match(self, mock_api_list_descriptors):
self.assertEqual(len(resource_descriptor_list), 1)
self.assertEqual(resource_descriptor_list[0].type, RESOURCE_TYPES[1])

@mock.patch('google.cloud.monitoring.Client.list_resource_descriptors')
@mock.patch('google.cloud.monitoring_v3.MetricServiceClient.list_monitored_resource_descriptors')
def test_list_caching(self, mock_gcloud_list_descriptors):
mock_gcloud_list_descriptors.return_value = (
self._list_resources_get_result())
Expand Down Expand Up @@ -144,10 +144,10 @@ def _create_context(project_id):

@staticmethod
def _list_resources_get_result():
all_labels = [google.cloud.monitoring.LabelDescriptor(**labels)
all_labels = [google.cloud.monitoring_v3.enums.LabelDescriptor(**labels)
for labels in LABELS]
descriptors = [
google.cloud.monitoring.ResourceDescriptor(
google.cloud.monitoring_v3.types.MonitoredResourceDescriptor(
name=None, type_=resource_type, display_name=display_name,
description=None, labels=all_labels,
)
Expand Down

0 comments on commit 22b4efd

Please sign in to comment.