From 33b2e436da368a32ec375784af2bda34d9b7ed95 Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Wed, 8 Feb 2017 18:35:40 -0800 Subject: [PATCH 1/5] Add Monitoring Google Cloud Samples --- monitoring/api/v3/{ => api-client}/README.rst | 0 .../api/v3/{ => api-client}/README.rst.in | 0 .../api/v3/{ => api-client}/custom_metric.py | 0 .../v3/{ => api-client}/custom_metric_test.py | 0 .../api/v3/{ => api-client}/list_resources.py | 0 .../{ => api-client}/list_resources_test.py | 0 monitoring/api/v3/api-client/requirements.txt | 1 + monitoring/api/v3/cloud-client/README.rst | 140 +++++++++++++ monitoring/api/v3/cloud-client/README.rst.in | 26 +++ monitoring/api/v3/cloud-client/quickstart.py | 42 ++++ .../api/v3/cloud-client/quickstart_test.py | 21 ++ .../api/v3/cloud-client/requirements.txt | 1 + monitoring/api/v3/cloud-client/snippets.py | 184 ++++++++++++++++++ .../api/v3/cloud-client/snippets_test.py | 69 +++++++ monitoring/api/v3/requirements.txt | 10 - 15 files changed, 484 insertions(+), 10 deletions(-) rename monitoring/api/v3/{ => api-client}/README.rst (100%) rename monitoring/api/v3/{ => api-client}/README.rst.in (100%) rename monitoring/api/v3/{ => api-client}/custom_metric.py (100%) rename monitoring/api/v3/{ => api-client}/custom_metric_test.py (100%) rename monitoring/api/v3/{ => api-client}/list_resources.py (100%) rename monitoring/api/v3/{ => api-client}/list_resources_test.py (100%) create mode 100644 monitoring/api/v3/api-client/requirements.txt create mode 100644 monitoring/api/v3/cloud-client/README.rst create mode 100644 monitoring/api/v3/cloud-client/README.rst.in create mode 100644 monitoring/api/v3/cloud-client/quickstart.py create mode 100644 monitoring/api/v3/cloud-client/quickstart_test.py create mode 100644 monitoring/api/v3/cloud-client/requirements.txt create mode 100644 monitoring/api/v3/cloud-client/snippets.py create mode 100644 monitoring/api/v3/cloud-client/snippets_test.py delete mode 100644 monitoring/api/v3/requirements.txt diff --git a/monitoring/api/v3/README.rst b/monitoring/api/v3/api-client/README.rst similarity index 100% rename from monitoring/api/v3/README.rst rename to monitoring/api/v3/api-client/README.rst diff --git a/monitoring/api/v3/README.rst.in b/monitoring/api/v3/api-client/README.rst.in similarity index 100% rename from monitoring/api/v3/README.rst.in rename to monitoring/api/v3/api-client/README.rst.in diff --git a/monitoring/api/v3/custom_metric.py b/monitoring/api/v3/api-client/custom_metric.py similarity index 100% rename from monitoring/api/v3/custom_metric.py rename to monitoring/api/v3/api-client/custom_metric.py diff --git a/monitoring/api/v3/custom_metric_test.py b/monitoring/api/v3/api-client/custom_metric_test.py similarity index 100% rename from monitoring/api/v3/custom_metric_test.py rename to monitoring/api/v3/api-client/custom_metric_test.py diff --git a/monitoring/api/v3/list_resources.py b/monitoring/api/v3/api-client/list_resources.py similarity index 100% rename from monitoring/api/v3/list_resources.py rename to monitoring/api/v3/api-client/list_resources.py diff --git a/monitoring/api/v3/list_resources_test.py b/monitoring/api/v3/api-client/list_resources_test.py similarity index 100% rename from monitoring/api/v3/list_resources_test.py rename to monitoring/api/v3/api-client/list_resources_test.py diff --git a/monitoring/api/v3/api-client/requirements.txt b/monitoring/api/v3/api-client/requirements.txt new file mode 100644 index 00000000000..ce6a9bf5bad --- /dev/null +++ b/monitoring/api/v3/api-client/requirements.txt @@ -0,0 +1 @@ +google-api-python-client==1.6.1 diff --git a/monitoring/api/v3/cloud-client/README.rst b/monitoring/api/v3/cloud-client/README.rst new file mode 100644 index 00000000000..2ee7eed78ba --- /dev/null +++ b/monitoring/api/v3/cloud-client/README.rst @@ -0,0 +1,140 @@ +.. This file is automatically generated. Do not edit this file directly. + +Google Stackdriver Monitoring API Python Samples +=============================================================================== + +This directory contains samples for Google Stackdriver Monitoring API. Stackdriver Monitoring collects metrics, events, and metadata from Google Cloud Platform, Amazon Web Services (AWS), hosted uptime probes, application instrumentation, and a variety of common application components including Cassandra, Nginx, Apache Web Server, Elasticsearch + and many others. Stackdriver ingests that data and generates insights + via dashboards, charts, and alerts. + + + + +.. _Google Stackdriver Monitoring API: https://cloud.google.com/monitoring/docs/ + +Setup +------------------------------------------------------------------------------- + + +Authentication +++++++++++++++ + +Authentication is typically done through `Application Default Credentials`_, +which means you do not have to change the code to authenticate as long as +your environment has credentials. You have a few options for setting up +authentication: + +#. When running locally, use the `Google Cloud SDK`_ + + .. code-block:: bash + + gcloud beta auth application-default login + + +#. When running on App Engine or Compute Engine, credentials are already + set-up. However, you may need to configure your Compute Engine instance + with `additional scopes`_. + +#. You can create a `Service Account key file`_. This file can be used to + authenticate to Google Cloud Platform services from any environment. To use + the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to + the path to the key file, for example: + + .. code-block:: bash + + export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json + +.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow +.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using +.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount + +Install Dependencies +++++++++++++++++++++ + +#. Install `pip`_ and `virtualenv`_ if you do not already have them. + +#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+. + + .. code-block:: bash + + $ virtualenv env + $ source env/bin/activate + +#. Install the dependencies needed to run the samples. + + .. code-block:: bash + + $ pip install -r requirements.txt + +.. _pip: https://pip.pypa.io/ +.. _virtualenv: https://virtualenv.pypa.io/ + +Samples +------------------------------------------------------------------------------- + +Quickstart ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python quickstart.py + + +Snippets ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ + + + +To run this sample: + +.. code-block:: bash + + $ python snippets.py + + usage: snippets.py [-h] [--project_id PROJECT_ID] + {create-metric-descriptor,list-metric-descriptors,delete-metric-descriptor,list-resources,get-resource,write-time-series,list-time-series,list-time-series-header,list-time-series-reduce,list-time-series-aggregate} + ... + + Demonstrates Monitoring API operations. + + positional arguments: + {create-metric-descriptor,list-metric-descriptors,delete-metric-descriptor,list-resources,get-resource,write-time-series,list-time-series,list-time-series-header,list-time-series-reduce,list-time-series-aggregate} + create-metric-descriptor + list-metric-descriptors + delete-metric-descriptor + list-resources + get-resource + write-time-series + list-time-series + list-time-series-header + list-time-series-reduce + list-time-series-aggregate + + optional arguments: + -h, --help show this help message and exit + --project_id PROJECT_ID + Your cloud project ID. + + + + +The client library +------------------------------------------------------------------------------- + +This sample uses the `Google Cloud Client Library for Python`_. +You can read the documentation for more details on API usage and use GitHub +to `browse the source`_ and `report issues`_. + +.. Google Cloud Client Library for Python: + https://googlecloudplatform.github.io/google-cloud-python/ +.. browse the source: + https://github.com/GoogleCloudPlatform/google-cloud-python +.. report issues: + https://github.com/GoogleCloudPlatform/google-cloud-python/issues + + +.. _Google Cloud SDK: https://cloud.google.com/sdk/ \ No newline at end of file diff --git a/monitoring/api/v3/cloud-client/README.rst.in b/monitoring/api/v3/cloud-client/README.rst.in new file mode 100644 index 00000000000..c69b5e6fb24 --- /dev/null +++ b/monitoring/api/v3/cloud-client/README.rst.in @@ -0,0 +1,26 @@ +# This file is used to generate README.rst + +product: + name: Google Stackdriver Monitoring API + short_name: Stackdriver Monitoring API + url: https://cloud.google.com/monitoring/docs/ + description: > + Stackdriver Monitoring collects metrics, events, and metadata from Google + Cloud Platform, Amazon Web Services (AWS), hosted uptime probes, + application instrumentation, and a variety of common application + components including Cassandra, Nginx, Apache Web Server, Elasticsearch + and many others. Stackdriver ingests that data and generates insights + via dashboards, charts, and alerts. + +setup: +- auth +- install_deps + +samples: +- name: Quickstart + file: quickstart.py +- name: Snippets + file: snippets.py + show_help: true + +cloud_client_library: true diff --git a/monitoring/api/v3/cloud-client/quickstart.py b/monitoring/api/v3/cloud-client/quickstart.py new file mode 100644 index 00000000000..0af93288b4b --- /dev/null +++ b/monitoring/api/v3/cloud-client/quickstart.py @@ -0,0 +1,42 @@ +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +# [START monitoring_quickstart] +from google.cloud import monitoring + + +def run_quickstart(): + client = monitoring.Client() + + resource = client.resource( + type_='gce_instance', + labels={ + 'instance_id': '1234567890123456789', + 'zone': 'us-central1-f', + } + ) + + metric = client.metric( + type_='custom.googleapis.com/my_metric', + labels={ + 'status': 'successful', + } + ) + + client.write_point(metric, resource, 3.14) + print('Successfully wrote time series.') +# [END monitoring_quickstart] + +if __name__ == '__main__': + run_quickstart() diff --git a/monitoring/api/v3/cloud-client/quickstart_test.py b/monitoring/api/v3/cloud-client/quickstart_test.py new file mode 100644 index 00000000000..d29d5ffe878 --- /dev/null +++ b/monitoring/api/v3/cloud-client/quickstart_test.py @@ -0,0 +1,21 @@ +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import quickstart + + +def test_quickstart(capsys): + quickstart.run_quickstart() + out, _ = capsys.readouterr() + assert 'wrote' in out diff --git a/monitoring/api/v3/cloud-client/requirements.txt b/monitoring/api/v3/cloud-client/requirements.txt new file mode 100644 index 00000000000..c2b0e92a825 --- /dev/null +++ b/monitoring/api/v3/cloud-client/requirements.txt @@ -0,0 +1 @@ +google-cloud-monitoring==0.22.0 diff --git a/monitoring/api/v3/cloud-client/snippets.py b/monitoring/api/v3/cloud-client/snippets.py new file mode 100644 index 00000000000..3caed930940 --- /dev/null +++ b/monitoring/api/v3/cloud-client/snippets.py @@ -0,0 +1,184 @@ +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import argparse + +from google.cloud.monitoring import ( + Aligner, Client, MetricKind, Reducer, ValueType) + + +def create_metric_descriptor(client): + descriptor = client.metric_descriptor( + 'custom.googleapis.com/my_metric', + metric_kind=MetricKind.GAUGE, + value_type=ValueType.DOUBLE, + description='This is a simple example of a custom metric.') + descriptor.create() + + +def delete_metric_descriptor(client, descriptor): + descriptor = client.metric_descriptor( + 'custom.googleapis.com/my_metric' + ) + descriptor.delete() + print 'Deleted metric.' + + +def write_time_series(client): + resource = client.resource( + 'gce_instance', + labels={ + 'instance_id': '1234567890123456789', + 'zone': 'us-central1-f', + } + ) + + metric = client.metric( + type_='custom.googleapis.com/my_metric', + labels={ + 'status': 'successful', + } + ) + client.write_point(metric, resource, 3.14) + + +def list_time_series(client): + metric = 'compute.googleapis.com/instance/cpu/utilization' + print(list(client.query(metric, minutes=5))) + + +def list_time_series_header(client): + metric = 'compute.googleapis.com/instance/cpu/utilization' + print(list(client.query(metric, minutes=5).iter(headers_only=True))) + + +def list_time_series_aggregate(client): + metric = 'compute.googleapis.com/instance/cpu/utilization' + print(list(client.query(metric, hours=1).align( + Aligner.ALIGN_MEAN, minutes=5))) + + +def list_time_series_reduce(client): + metric = 'compute.googleapis.com/instance/cpu/utilization' + print(list(client.query(metric, hours=1).align( + Aligner.ALIGN_MEAN, minutes=5).reduce( + Reducer.REDUCE_MEAN, 'resource.zone'))) + + +def list_metric_descriptors(client): + for descriptor in client.list_metric_descriptors(): + print(descriptor.type) + + +def list_monitored_resources(client): + for descriptor in client.list_resource_descriptors(): + print (descriptor.type) + + +def get_monitored_resource_descriptor(client, type): + print(client.fetch_resource_descriptor(type)) + + +if __name__ == '__main__': + parser = argparse.ArgumentParser( + description='Demonstrates Monitoring API operations.') + parser.add_argument('--project_id', help='Your cloud project ID.') + + subparsers = parser.add_subparsers(dest='command') + + create_metric_descriptor_parser = subparsers.add_parser( + 'create-metric-descriptor', + help=create_metric_descriptor.__doc__ + ) + + list_metric_descriptor_parser = subparsers.add_parser( + 'list-metric-descriptors', + help=list_metric_descriptors.__doc__ + ) + + delete_metric_descriptor_parser = subparsers.add_parser( + 'delete-metric-descriptor', + help=list_metric_descriptors.__doc__ + ) + + delete_metric_descriptor_parser.add_argument( + '--metric', + help='Metric descriptor to delete', + required=True + ) + + list_resources_parser = subparsers.add_parser( + 'list-resources', + help=list_monitored_resources.__doc__ + ) + + get_resource_parser = subparsers.add_parser( + 'get-resource', + help=get_monitored_resource_descriptor.__doc__ + ) + + get_resource_parser.add_argument( + '--resource', + help='Monitored resource to view more information about.', + required=True + ) + + write_time_series_parser = subparsers.add_parser( + 'write-time-series', + help=write_time_series.__doc__ + ) + + list_time_series_parser = subparsers.add_parser( + 'list-time-series', + help=list_time_series.__doc__ + ) + + list_time_series_header_parser = subparsers.add_parser( + 'list-time-series-header', + help=list_time_series_header.__doc__ + ) + + read_time_series_reduce = subparsers.add_parser( + 'list-time-series-reduce', + help=list_time_series_reduce.__doc__ + ) + + read_time_series_aggregate = subparsers.add_parser( + 'list-time-series-aggregate', + help=list_time_series_aggregate.__doc__ + ) + + args = parser.parse_args() + client = Client(args.project_id) + + if args.command == 'create-metric-descriptor': + create_metric_descriptor(client) + if args.command == 'list-metric-descriptors': + list_metric_descriptors(client) + if args.command == 'delete-metric-descriptor': + delete_metric_descriptor(client, args.metric) + if args.command == 'list-resources': + list_monitored_resources(client) + if args.command == 'get-resource': + get_monitored_resource_descriptor(client, args.resource) + if args.command == 'write-time-series': + write_time_series(client) + if args.command == 'list-time-series': + list_time_series(client) + if args.command == 'list-time-series-header': + list_time_series_header(client) + if args.command == 'list-time-series-reduce': + list_time_series_reduce(client) + if args.command == 'list-time-series-aggregate': + list_time_series_aggregate(client) diff --git a/monitoring/api/v3/cloud-client/snippets_test.py b/monitoring/api/v3/cloud-client/snippets_test.py new file mode 100644 index 00000000000..892f3d4ca39 --- /dev/null +++ b/monitoring/api/v3/cloud-client/snippets_test.py @@ -0,0 +1,69 @@ +# Copyright 2017 Google Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +from google.cloud import monitoring +import pytest + +import snippets + + +@pytest.yield_fixture +def client(cloud_config): + client = monitoring.Client(cloud_config.project) + yield client + + +def test_create_and_delete_metric_descriptor(capsys, client): + snippets.create_metric_descriptor(client) + snippets.delete_metric_descriptor(client, 'custom.googleapis.com/my_metric') + out, _ = capsys.readouterr() + assert 'Deleted metric' in out + + +def test_list_metric_descriptors(capsys, client): + snippets.list_metric_descriptors(client) + out, _ = capsys.readouterr() + assert 'logging.googleapis.com/byte_count' in out + + +def test_list_resources(capsys, client): + snippets.list_monitored_resources(client) + out, _ = capsys.readouterr() + assert 'pubsub_topic' in out + + +def test_get_resource(capsys, client): + snippets.get_monitored_resource_descriptor(client, 'pubsub_topic') + out, _ = capsys.readouterr() + assert 'A topic in Google Cloud Pub/Sub' in out + + +def test_time_series(capsys, client): + snippets.write_time_series(client) + + snippets.list_time_series(client) + out, _ = capsys.readouterr() + assert 'TimeSeries with' in out + + snippets.list_time_series_header(client) + out, _ = capsys.readouterr() + assert 'TimeSeries with' in out + + snippets.list_time_series_aggregate(client) + out, _ = capsys.readouterr() + assert 'TimeSeries with' in out + + snippets.list_time_series_reduce(client) + out, _ = capsys.readouterr() + assert 'TimeSeries with' in out diff --git a/monitoring/api/v3/requirements.txt b/monitoring/api/v3/requirements.txt deleted file mode 100644 index 7fc1cd0ccff..00000000000 --- a/monitoring/api/v3/requirements.txt +++ /dev/null @@ -1,10 +0,0 @@ -google-api-python-client==1.6.2 -httplib2==0.10.3 -oauth2client==4.0.0 -pyasn1==0.2.2 -pyasn1-modules==0.0.8 -rsa==3.4.2 -simplejson==3.10.0 -six==1.10.0 -uritemplate==3.0.0 -wheel==0.29.0 From c8902a98b84714bce3f4d669f903d7877227b795 Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Thu, 9 Feb 2017 15:55:27 -0800 Subject: [PATCH 2/5] jon wayne review --- monitoring/api/v3/cloud-client/quickstart.py | 1 + monitoring/api/v3/cloud-client/snippets.py | 77 +++++++++++-------- .../api/v3/cloud-client/snippets_test.py | 19 ++--- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/monitoring/api/v3/cloud-client/quickstart.py b/monitoring/api/v3/cloud-client/quickstart.py index 0af93288b4b..c814d2f519d 100644 --- a/monitoring/api/v3/cloud-client/quickstart.py +++ b/monitoring/api/v3/cloud-client/quickstart.py @@ -38,5 +38,6 @@ def run_quickstart(): print('Successfully wrote time series.') # [END monitoring_quickstart] + if __name__ == '__main__': run_quickstart() diff --git a/monitoring/api/v3/cloud-client/snippets.py b/monitoring/api/v3/cloud-client/snippets.py index 3caed930940..cbb18235804 100644 --- a/monitoring/api/v3/cloud-client/snippets.py +++ b/monitoring/api/v3/cloud-client/snippets.py @@ -14,28 +14,30 @@ import argparse -from google.cloud.monitoring import ( - Aligner, Client, MetricKind, Reducer, ValueType) +from google.cloud import monitoring -def create_metric_descriptor(client): +def create_metric_descriptor(): + client = monitoring.Client() descriptor = client.metric_descriptor( 'custom.googleapis.com/my_metric', - metric_kind=MetricKind.GAUGE, - value_type=ValueType.DOUBLE, + metric_kind=monitoring.MetricKind.GAUGE, + value_type=monitoring.ValueType.DOUBLE, description='This is a simple example of a custom metric.') descriptor.create() -def delete_metric_descriptor(client, descriptor): +def delete_metric_descriptor(descriptor): + client = monitoring.Client() descriptor = client.metric_descriptor( - 'custom.googleapis.com/my_metric' + descriptor ) descriptor.delete() - print 'Deleted metric.' + print 'Deleted metric descriptor.' -def write_time_series(client): +def write_time_series(): + client = monitoring.Client() resource = client.resource( 'gce_instance', labels={ @@ -53,47 +55,57 @@ def write_time_series(client): client.write_point(metric, resource, 3.14) -def list_time_series(client): +def list_time_series(): + client = monitoring.Client() metric = 'compute.googleapis.com/instance/cpu/utilization' - print(list(client.query(metric, minutes=5))) + query_results = client.query(metric, minutes=5) + for result in query_results: + print result -def list_time_series_header(client): +def list_time_series_header(): + client = monitoring.Client() metric = 'compute.googleapis.com/instance/cpu/utilization' - print(list(client.query(metric, minutes=5).iter(headers_only=True))) + query_results = client.query(metric, minutes=5).iter(headers_only=True) + for result in query_results: + print result -def list_time_series_aggregate(client): +def list_time_series_aggregate(): + client = monitoring.Client() metric = 'compute.googleapis.com/instance/cpu/utilization' print(list(client.query(metric, hours=1).align( - Aligner.ALIGN_MEAN, minutes=5))) + monitoring.Aligner.ALIGN_MEAN, minutes=5))) -def list_time_series_reduce(client): +def list_time_series_reduce(): + client = monitoring.Client() metric = 'compute.googleapis.com/instance/cpu/utilization' print(list(client.query(metric, hours=1).align( - Aligner.ALIGN_MEAN, minutes=5).reduce( - Reducer.REDUCE_MEAN, 'resource.zone'))) + monitoring.Aligner.ALIGN_MEAN, minutes=5).reduce( + monitoring.Reducer.REDUCE_MEAN, 'resource.zone'))) -def list_metric_descriptors(client): +def list_metric_descriptors(): + client = monitoring.Client() for descriptor in client.list_metric_descriptors(): print(descriptor.type) -def list_monitored_resources(client): +def list_monitored_resources(): + client = monitoring.Client() for descriptor in client.list_resource_descriptors(): print (descriptor.type) -def get_monitored_resource_descriptor(client, type): +def get_monitored_resource_descriptor(type): + client = monitoring.Client() print(client.fetch_resource_descriptor(type)) if __name__ == '__main__': parser = argparse.ArgumentParser( description='Demonstrates Monitoring API operations.') - parser.add_argument('--project_id', help='Your cloud project ID.') subparsers = parser.add_subparsers(dest='command') @@ -160,25 +172,24 @@ def get_monitored_resource_descriptor(client, type): ) args = parser.parse_args() - client = Client(args.project_id) if args.command == 'create-metric-descriptor': - create_metric_descriptor(client) + create_metric_descriptor() if args.command == 'list-metric-descriptors': - list_metric_descriptors(client) + list_metric_descriptors() if args.command == 'delete-metric-descriptor': - delete_metric_descriptor(client, args.metric) + delete_metric_descriptor(args.metric) if args.command == 'list-resources': - list_monitored_resources(client) + list_monitored_resources() if args.command == 'get-resource': - get_monitored_resource_descriptor(client, args.resource) + get_monitored_resource_descriptor(args.resource) if args.command == 'write-time-series': - write_time_series(client) + write_time_series() if args.command == 'list-time-series': - list_time_series(client) + list_time_series() if args.command == 'list-time-series-header': - list_time_series_header(client) + list_time_series_header() if args.command == 'list-time-series-reduce': - list_time_series_reduce(client) + list_time_series_reduce() if args.command == 'list-time-series-aggregate': - list_time_series_aggregate(client) + list_time_series_aggregate() diff --git a/monitoring/api/v3/cloud-client/snippets_test.py b/monitoring/api/v3/cloud-client/snippets_test.py index 892f3d4ca39..7ebb249ae5f 100644 --- a/monitoring/api/v3/cloud-client/snippets_test.py +++ b/monitoring/api/v3/cloud-client/snippets_test.py @@ -12,27 +12,18 @@ # See the License for the specific language governing permissions and # limitations under the License. -from google.cloud import monitoring -import pytest - import snippets -@pytest.yield_fixture -def client(cloud_config): - client = monitoring.Client(cloud_config.project) - yield client - - -def test_create_and_delete_metric_descriptor(capsys, client): - snippets.create_metric_descriptor(client) - snippets.delete_metric_descriptor(client, 'custom.googleapis.com/my_metric') +def test_create_and_delete_metric_descriptor(capsys): + snippets.create_metric_descriptor() + snippets.delete_metric_descriptor('custom.googleapis.com/my_metric') out, _ = capsys.readouterr() assert 'Deleted metric' in out -def test_list_metric_descriptors(capsys, client): - snippets.list_metric_descriptors(client) +def test_list_metric_descriptors(capsys): + snippets.list_metric_descriptors() out, _ = capsys.readouterr() assert 'logging.googleapis.com/byte_count' in out From 27d44b66760d6a5c4f6e616b93776fdeb8eedc43 Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Thu, 9 Feb 2017 16:09:21 -0800 Subject: [PATCH 3/5] fixups --- .../api/v3/cloud-client/snippets_test.py | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/monitoring/api/v3/cloud-client/snippets_test.py b/monitoring/api/v3/cloud-client/snippets_test.py index 7ebb249ae5f..a77968849fa 100644 --- a/monitoring/api/v3/cloud-client/snippets_test.py +++ b/monitoring/api/v3/cloud-client/snippets_test.py @@ -28,33 +28,33 @@ def test_list_metric_descriptors(capsys): assert 'logging.googleapis.com/byte_count' in out -def test_list_resources(capsys, client): - snippets.list_monitored_resources(client) +def test_list_resources(capsys): + snippets.list_monitored_resources() out, _ = capsys.readouterr() assert 'pubsub_topic' in out -def test_get_resource(capsys, client): - snippets.get_monitored_resource_descriptor(client, 'pubsub_topic') +def test_get_resource(capsys): + snippets.get_monitored_resource_descriptor('pubsub_topic') out, _ = capsys.readouterr() assert 'A topic in Google Cloud Pub/Sub' in out -def test_time_series(capsys, client): - snippets.write_time_series(client) +def test_time_series(capsys): + snippets.write_time_series() - snippets.list_time_series(client) + snippets.list_time_series() out, _ = capsys.readouterr() assert 'TimeSeries with' in out - snippets.list_time_series_header(client) + snippets.list_time_series_header() out, _ = capsys.readouterr() assert 'TimeSeries with' in out - snippets.list_time_series_aggregate(client) + snippets.list_time_series_aggregate() out, _ = capsys.readouterr() assert 'TimeSeries with' in out - snippets.list_time_series_reduce(client) + snippets.list_time_series_reduce() out, _ = capsys.readouterr() assert 'TimeSeries with' in out From 821ef63e3db0c20b783fdd3caf01bba0b1a0b8fc Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Thu, 9 Feb 2017 16:29:13 -0800 Subject: [PATCH 4/5] Fix tests --- monitoring/api/v3/cloud-client/snippets.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/monitoring/api/v3/cloud-client/snippets.py b/monitoring/api/v3/cloud-client/snippets.py index cbb18235804..dfbd0fe969b 100644 --- a/monitoring/api/v3/cloud-client/snippets.py +++ b/monitoring/api/v3/cloud-client/snippets.py @@ -33,7 +33,7 @@ def delete_metric_descriptor(descriptor): descriptor ) descriptor.delete() - print 'Deleted metric descriptor.' + print('Deleted metric descriptor.') def write_time_series(): @@ -60,7 +60,7 @@ def list_time_series(): metric = 'compute.googleapis.com/instance/cpu/utilization' query_results = client.query(metric, minutes=5) for result in query_results: - print result + print(result) def list_time_series_header(): @@ -68,7 +68,7 @@ def list_time_series_header(): metric = 'compute.googleapis.com/instance/cpu/utilization' query_results = client.query(metric, minutes=5).iter(headers_only=True) for result in query_results: - print result + print(result) def list_time_series_aggregate(): @@ -95,7 +95,7 @@ def list_metric_descriptors(): def list_monitored_resources(): client = monitoring.Client() for descriptor in client.list_resource_descriptors(): - print (descriptor.type) + print(descriptor.type) def get_monitored_resource_descriptor(type): From 6d5b5f111bbdf2f2e12b67d3c958a8bee0597945 Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Thu, 9 Feb 2017 16:50:11 -0800 Subject: [PATCH 5/5] jonwayne --- monitoring/api/v3/cloud-client/snippets.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/monitoring/api/v3/cloud-client/snippets.py b/monitoring/api/v3/cloud-client/snippets.py index dfbd0fe969b..c46a6d22f1e 100644 --- a/monitoring/api/v3/cloud-client/snippets.py +++ b/monitoring/api/v3/cloud-client/snippets.py @@ -74,16 +74,20 @@ def list_time_series_header(): def list_time_series_aggregate(): client = monitoring.Client() metric = 'compute.googleapis.com/instance/cpu/utilization' - print(list(client.query(metric, hours=1).align( - monitoring.Aligner.ALIGN_MEAN, minutes=5))) + query_results = client.query(metric, hours=1).align( + monitoring.Aligner.ALIGN_MEAN, minutes=5) + for result in query_results: + print(result) def list_time_series_reduce(): client = monitoring.Client() metric = 'compute.googleapis.com/instance/cpu/utilization' - print(list(client.query(metric, hours=1).align( + query_results = client.query(metric, hours=1).align( monitoring.Aligner.ALIGN_MEAN, minutes=5).reduce( - monitoring.Reducer.REDUCE_MEAN, 'resource.zone'))) + monitoring.Reducer.REDUCE_MEAN, 'resource.zone') + for result in query_results: + print(result) def list_metric_descriptors():