From 35138aba537964a5ddb55b947c8a47f759de2b9b Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Thu, 9 Feb 2017 17:05:41 -0800 Subject: [PATCH] Add Monitoring Google Cloud Samples [(#789)](https://github.com/GoogleCloudPlatform/python-docs-samples/issues/789) * Add Monitoring Google Cloud Samples * jon wayne review * fixups * Fix tests * jonwayne --- .../snippets/v3/{ => api-client}/README.rst | 0 .../v3/{ => api-client}/README.rst.in | 0 .../v3/{ => api-client}/custom_metric.py | 0 .../v3/{ => api-client}/custom_metric_test.py | 0 .../v3/{ => api-client}/list_resources.py | 0 .../{ => api-client}/list_resources_test.py | 0 .../snippets/v3/api-client/requirements.txt | 1 + samples/snippets/v3/cloud-client/README.rst | 140 ++++++++++++ .../snippets/v3/cloud-client/README.rst.in | 26 +++ .../snippets/v3/cloud-client/quickstart.py | 43 ++++ .../v3/cloud-client/quickstart_test.py | 21 ++ .../snippets/v3/cloud-client/requirements.txt | 1 + samples/snippets/v3/cloud-client/snippets.py | 199 ++++++++++++++++++ .../snippets/v3/cloud-client/snippets_test.py | 60 ++++++ samples/snippets/v3/requirements.txt | 10 - 15 files changed, 491 insertions(+), 10 deletions(-) rename samples/snippets/v3/{ => api-client}/README.rst (100%) rename samples/snippets/v3/{ => api-client}/README.rst.in (100%) rename samples/snippets/v3/{ => api-client}/custom_metric.py (100%) rename samples/snippets/v3/{ => api-client}/custom_metric_test.py (100%) rename samples/snippets/v3/{ => api-client}/list_resources.py (100%) rename samples/snippets/v3/{ => api-client}/list_resources_test.py (100%) create mode 100644 samples/snippets/v3/api-client/requirements.txt create mode 100644 samples/snippets/v3/cloud-client/README.rst create mode 100644 samples/snippets/v3/cloud-client/README.rst.in create mode 100644 samples/snippets/v3/cloud-client/quickstart.py create mode 100644 samples/snippets/v3/cloud-client/quickstart_test.py create mode 100644 samples/snippets/v3/cloud-client/requirements.txt create mode 100644 samples/snippets/v3/cloud-client/snippets.py create mode 100644 samples/snippets/v3/cloud-client/snippets_test.py delete mode 100644 samples/snippets/v3/requirements.txt diff --git a/samples/snippets/v3/README.rst b/samples/snippets/v3/api-client/README.rst similarity index 100% rename from samples/snippets/v3/README.rst rename to samples/snippets/v3/api-client/README.rst diff --git a/samples/snippets/v3/README.rst.in b/samples/snippets/v3/api-client/README.rst.in similarity index 100% rename from samples/snippets/v3/README.rst.in rename to samples/snippets/v3/api-client/README.rst.in diff --git a/samples/snippets/v3/custom_metric.py b/samples/snippets/v3/api-client/custom_metric.py similarity index 100% rename from samples/snippets/v3/custom_metric.py rename to samples/snippets/v3/api-client/custom_metric.py diff --git a/samples/snippets/v3/custom_metric_test.py b/samples/snippets/v3/api-client/custom_metric_test.py similarity index 100% rename from samples/snippets/v3/custom_metric_test.py rename to samples/snippets/v3/api-client/custom_metric_test.py diff --git a/samples/snippets/v3/list_resources.py b/samples/snippets/v3/api-client/list_resources.py similarity index 100% rename from samples/snippets/v3/list_resources.py rename to samples/snippets/v3/api-client/list_resources.py diff --git a/samples/snippets/v3/list_resources_test.py b/samples/snippets/v3/api-client/list_resources_test.py similarity index 100% rename from samples/snippets/v3/list_resources_test.py rename to samples/snippets/v3/api-client/list_resources_test.py diff --git a/samples/snippets/v3/api-client/requirements.txt b/samples/snippets/v3/api-client/requirements.txt new file mode 100644 index 00000000..ce6a9bf5 --- /dev/null +++ b/samples/snippets/v3/api-client/requirements.txt @@ -0,0 +1 @@ +google-api-python-client==1.6.1 diff --git a/samples/snippets/v3/cloud-client/README.rst b/samples/snippets/v3/cloud-client/README.rst new file mode 100644 index 00000000..2ee7eed7 --- /dev/null +++ b/samples/snippets/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/samples/snippets/v3/cloud-client/README.rst.in b/samples/snippets/v3/cloud-client/README.rst.in new file mode 100644 index 00000000..c69b5e6f --- /dev/null +++ b/samples/snippets/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/samples/snippets/v3/cloud-client/quickstart.py b/samples/snippets/v3/cloud-client/quickstart.py new file mode 100644 index 00000000..c814d2f5 --- /dev/null +++ b/samples/snippets/v3/cloud-client/quickstart.py @@ -0,0 +1,43 @@ +# 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/samples/snippets/v3/cloud-client/quickstart_test.py b/samples/snippets/v3/cloud-client/quickstart_test.py new file mode 100644 index 00000000..d29d5ffe --- /dev/null +++ b/samples/snippets/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/samples/snippets/v3/cloud-client/requirements.txt b/samples/snippets/v3/cloud-client/requirements.txt new file mode 100644 index 00000000..c2b0e92a --- /dev/null +++ b/samples/snippets/v3/cloud-client/requirements.txt @@ -0,0 +1 @@ +google-cloud-monitoring==0.22.0 diff --git a/samples/snippets/v3/cloud-client/snippets.py b/samples/snippets/v3/cloud-client/snippets.py new file mode 100644 index 00000000..c46a6d22 --- /dev/null +++ b/samples/snippets/v3/cloud-client/snippets.py @@ -0,0 +1,199 @@ +# 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 import monitoring + + +def create_metric_descriptor(): + client = monitoring.Client() + descriptor = client.metric_descriptor( + 'custom.googleapis.com/my_metric', + 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(descriptor): + client = monitoring.Client() + descriptor = client.metric_descriptor( + descriptor + ) + descriptor.delete() + print('Deleted metric descriptor.') + + +def write_time_series(): + client = monitoring.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 = monitoring.Client() + metric = 'compute.googleapis.com/instance/cpu/utilization' + query_results = client.query(metric, minutes=5) + for result in query_results: + print(result) + + +def list_time_series_header(): + client = monitoring.Client() + 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) + + +def list_time_series_aggregate(): + client = monitoring.Client() + metric = 'compute.googleapis.com/instance/cpu/utilization' + 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' + query_results = client.query(metric, hours=1).align( + monitoring.Aligner.ALIGN_MEAN, minutes=5).reduce( + monitoring.Reducer.REDUCE_MEAN, 'resource.zone') + for result in query_results: + print(result) + + +def list_metric_descriptors(): + client = monitoring.Client() + for descriptor in client.list_metric_descriptors(): + print(descriptor.type) + + +def list_monitored_resources(): + client = monitoring.Client() + for descriptor in client.list_resource_descriptors(): + print(descriptor.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.') + + 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() + + if args.command == 'create-metric-descriptor': + create_metric_descriptor() + if args.command == 'list-metric-descriptors': + list_metric_descriptors() + if args.command == 'delete-metric-descriptor': + delete_metric_descriptor(args.metric) + if args.command == 'list-resources': + list_monitored_resources() + if args.command == 'get-resource': + get_monitored_resource_descriptor(args.resource) + if args.command == 'write-time-series': + write_time_series() + if args.command == 'list-time-series': + list_time_series() + if args.command == 'list-time-series-header': + list_time_series_header() + if args.command == 'list-time-series-reduce': + list_time_series_reduce() + if args.command == 'list-time-series-aggregate': + list_time_series_aggregate() diff --git a/samples/snippets/v3/cloud-client/snippets_test.py b/samples/snippets/v3/cloud-client/snippets_test.py new file mode 100644 index 00000000..a7796884 --- /dev/null +++ b/samples/snippets/v3/cloud-client/snippets_test.py @@ -0,0 +1,60 @@ +# 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 snippets + + +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): + snippets.list_metric_descriptors() + out, _ = capsys.readouterr() + assert 'logging.googleapis.com/byte_count' in out + + +def test_list_resources(capsys): + snippets.list_monitored_resources() + out, _ = capsys.readouterr() + assert 'pubsub_topic' in out + + +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): + snippets.write_time_series() + + snippets.list_time_series() + out, _ = capsys.readouterr() + assert 'TimeSeries with' in out + + snippets.list_time_series_header() + out, _ = capsys.readouterr() + assert 'TimeSeries with' in out + + snippets.list_time_series_aggregate() + out, _ = capsys.readouterr() + assert 'TimeSeries with' in out + + snippets.list_time_series_reduce() + out, _ = capsys.readouterr() + assert 'TimeSeries with' in out diff --git a/samples/snippets/v3/requirements.txt b/samples/snippets/v3/requirements.txt deleted file mode 100644 index 7fc1cd0c..00000000 --- a/samples/snippets/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