def test_metadata(self, mock_query_iter): query = gcm.Query(METRIC_TYPE, hours=1, context=self.context) query_metadata = query.metadata() mock_query_iter.assert_called_once_with(headers_only=True) self.assertIsInstance(query_metadata, gcm.QueryMetadata) self.assertEqual(query_metadata.metric_type, METRIC_TYPE)
def test_constructor_maximal(self): UPTIME_METRIC = 'compute.googleapis.com/instance/uptime' T1 = datetime.datetime(2016, 4, 7, 2, 30, 30) DAYS, HOURS, MINUTES = 1, 2, 3 T0 = T1 - datetime.timedelta(days=DAYS, hours=HOURS, minutes=MINUTES) query = gcm.Query(UPTIME_METRIC, end_time=T1, days=DAYS, hours=HOURS, minutes=MINUTES, context=self.context) expected_client = gcm._utils.make_client(context=self.context) self.assertEqual(query._client.project, expected_client.project) self.assertEqual(query._client._connection.credentials, expected_client._connection.credentials) self.assertEqual(query._filter.metric_type, UPTIME_METRIC) self.assertEqual(query._start_time, T0) self.assertEqual(query._end_time, T1) self.assertIsNone(query._per_series_aligner) self.assertIsNone(query._alignment_period_seconds) self.assertIsNone(query._cross_series_reducer) self.assertEqual(query._group_by_fields, ())
def test_constructor_minimal(self, mock_context_default): mock_context_default.return_value = self.context query = gcm.Query() self.assertEqual(query._filter.metric_type, BaseQuery.DEFAULT_METRIC_TYPE) self.assertIsNone(query._start_time) self.assertIsNone(query._end_time) self.assertEqual(query._per_series_aligner, 0) self.assertEqual(query._alignment_period_seconds, 0) self.assertEqual(query._cross_series_reducer, 0) self.assertEqual(query._group_by_fields, ())
def test_constructor_minimal(self, mock_context_default): mock_context_default.return_value = self.context query = gcm.Query() expected_client = gcm._utils.make_client(context=self.context) self.assertEqual(query._client.project, expected_client.project) self.assertEqual(query._client._connection.credentials, expected_client._connection.credentials) self.assertEqual(query._filter.metric_type, BaseQuery.DEFAULT_METRIC_TYPE) self.assertIsNone(query._start_time) self.assertIsNone(query._end_time) self.assertIsNone(query._per_series_aligner) self.assertIsNone(query._alignment_period_seconds) self.assertIsNone(query._cross_series_reducer) self.assertEqual(query._group_by_fields, ())
def setUp(self): creds = mock.Mock(spec=google.auth.credentials.Credentials) context = google.datalab.Context(PROJECT, creds) self.query = gcm.Query(METRIC_TYPE, context=context)
#%% import google.datalab import google.datalab.stackdriver.monitoring as gcm import collections import StringIO import pandas import os #os.environ["GOOGLE_CLOUD_DISABLE_GRPC"] = "true" query_cpu = gcm.Query('compute.googleapis.com/instance/cpu/utilization', hours=7*24) cpu_metadata = query_cpu.metadata() instance_prefix_counts = collections.Counter( timeseries.metric.labels['instance_name'].rsplit('-', 1)[0] for timeseries in cpu_metadata) instance_prefix_counts.most_common(5) common_prefix = "ad-dc1" print 'done' #%% query_cpu = query_cpu.select_metrics(instance_name_prefix=common_prefix) # Aggregate to hourly intervals per zone. query_cpu = query_cpu.align(gcm.Aligner.ALIGN_MEAN, hours=1) query_cpu = query_cpu.reduce(gcm.Reducer.REDUCE_MEAN, 'resource.zone')