Skip to content
This repository has been archived by the owner on Dec 31, 2023. It is now read-only.

Commit

Permalink
Add Monitoring Google Cloud Samples [(#789)](GoogleCloudPlatform/pyth…
Browse files Browse the repository at this point in the history
…on-docs-samples#789)

* Add Monitoring Google Cloud Samples

* jon wayne review

* fixups

* Fix tests

* jonwayne
  • Loading branch information
waprin authored Feb 10, 2017
1 parent 76f224a commit 35138ab
Show file tree
Hide file tree
Showing 15 changed files with 491 additions and 10 deletions.
File renamed without changes.
1 change: 1 addition & 0 deletions samples/snippets/v3/api-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-api-python-client==1.6.1
140 changes: 140 additions & 0 deletions samples/snippets/v3/cloud-client/README.rst
Original file line number Diff line number Diff line change
@@ -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/
26 changes: 26 additions & 0 deletions samples/snippets/v3/cloud-client/README.rst.in
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions samples/snippets/v3/cloud-client/quickstart.py
Original file line number Diff line number Diff line change
@@ -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()
21 changes: 21 additions & 0 deletions samples/snippets/v3/cloud-client/quickstart_test.py
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions samples/snippets/v3/cloud-client/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
google-cloud-monitoring==0.22.0
Loading

0 comments on commit 35138ab

Please sign in to comment.