Skip to content

Commit a752cb3

Browse files
authored
Add Monitoring Google Cloud Samples (#789)
* Add Monitoring Google Cloud Samples * jon wayne review * fixups * Fix tests * jonwayne
1 parent 4002e93 commit a752cb3

15 files changed

+491
-10
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-api-python-client==1.6.1
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Google Stackdriver Monitoring API Python Samples
4+
===============================================================================
5+
6+
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
7+
and many others. Stackdriver ingests that data and generates insights
8+
via dashboards, charts, and alerts.
9+
10+
11+
12+
13+
.. _Google Stackdriver Monitoring API: https://cloud.google.com/monitoring/docs/
14+
15+
Setup
16+
-------------------------------------------------------------------------------
17+
18+
19+
Authentication
20+
++++++++++++++
21+
22+
Authentication is typically done through `Application Default Credentials`_,
23+
which means you do not have to change the code to authenticate as long as
24+
your environment has credentials. You have a few options for setting up
25+
authentication:
26+
27+
#. When running locally, use the `Google Cloud SDK`_
28+
29+
.. code-block:: bash
30+
31+
gcloud beta auth application-default login
32+
33+
34+
#. When running on App Engine or Compute Engine, credentials are already
35+
set-up. However, you may need to configure your Compute Engine instance
36+
with `additional scopes`_.
37+
38+
#. You can create a `Service Account key file`_. This file can be used to
39+
authenticate to Google Cloud Platform services from any environment. To use
40+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
41+
the path to the key file, for example:
42+
43+
.. code-block:: bash
44+
45+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
46+
47+
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
48+
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
49+
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
50+
51+
Install Dependencies
52+
++++++++++++++++++++
53+
54+
#. Install `pip`_ and `virtualenv`_ if you do not already have them.
55+
56+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
57+
58+
.. code-block:: bash
59+
60+
$ virtualenv env
61+
$ source env/bin/activate
62+
63+
#. Install the dependencies needed to run the samples.
64+
65+
.. code-block:: bash
66+
67+
$ pip install -r requirements.txt
68+
69+
.. _pip: https://pip.pypa.io/
70+
.. _virtualenv: https://virtualenv.pypa.io/
71+
72+
Samples
73+
-------------------------------------------------------------------------------
74+
75+
Quickstart
76+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
77+
78+
79+
80+
To run this sample:
81+
82+
.. code-block:: bash
83+
84+
$ python quickstart.py
85+
86+
87+
Snippets
88+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
89+
90+
91+
92+
To run this sample:
93+
94+
.. code-block:: bash
95+
96+
$ python snippets.py
97+
98+
usage: snippets.py [-h] [--project_id PROJECT_ID]
99+
{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}
100+
...
101+
102+
Demonstrates Monitoring API operations.
103+
104+
positional arguments:
105+
{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}
106+
create-metric-descriptor
107+
list-metric-descriptors
108+
delete-metric-descriptor
109+
list-resources
110+
get-resource
111+
write-time-series
112+
list-time-series
113+
list-time-series-header
114+
list-time-series-reduce
115+
list-time-series-aggregate
116+
117+
optional arguments:
118+
-h, --help show this help message and exit
119+
--project_id PROJECT_ID
120+
Your cloud project ID.
121+
122+
123+
124+
125+
The client library
126+
-------------------------------------------------------------------------------
127+
128+
This sample uses the `Google Cloud Client Library for Python`_.
129+
You can read the documentation for more details on API usage and use GitHub
130+
to `browse the source`_ and `report issues`_.
131+
132+
.. Google Cloud Client Library for Python:
133+
https://googlecloudplatform.github.io/google-cloud-python/
134+
.. browse the source:
135+
https://github.com/GoogleCloudPlatform/google-cloud-python
136+
.. report issues:
137+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
138+
139+
140+
.. _Google Cloud SDK: https://cloud.google.com/sdk/
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Google Stackdriver Monitoring API
5+
short_name: Stackdriver Monitoring API
6+
url: https://cloud.google.com/monitoring/docs/
7+
description: >
8+
Stackdriver Monitoring collects metrics, events, and metadata from Google
9+
Cloud Platform, Amazon Web Services (AWS), hosted uptime probes,
10+
application instrumentation, and a variety of common application
11+
components including Cassandra, Nginx, Apache Web Server, Elasticsearch
12+
and many others. Stackdriver ingests that data and generates insights
13+
via dashboards, charts, and alerts.
14+
15+
setup:
16+
- auth
17+
- install_deps
18+
19+
samples:
20+
- name: Quickstart
21+
file: quickstart.py
22+
- name: Snippets
23+
file: snippets.py
24+
show_help: true
25+
26+
cloud_client_library: true
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
# [START monitoring_quickstart]
16+
from google.cloud import monitoring
17+
18+
19+
def run_quickstart():
20+
client = monitoring.Client()
21+
22+
resource = client.resource(
23+
type_='gce_instance',
24+
labels={
25+
'instance_id': '1234567890123456789',
26+
'zone': 'us-central1-f',
27+
}
28+
)
29+
30+
metric = client.metric(
31+
type_='custom.googleapis.com/my_metric',
32+
labels={
33+
'status': 'successful',
34+
}
35+
)
36+
37+
client.write_point(metric, resource, 3.14)
38+
print('Successfully wrote time series.')
39+
# [END monitoring_quickstart]
40+
41+
42+
if __name__ == '__main__':
43+
run_quickstart()
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright 2017 Google Inc.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import quickstart
16+
17+
18+
def test_quickstart(capsys):
19+
quickstart.run_quickstart()
20+
out, _ = capsys.readouterr()
21+
assert 'wrote' in out
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
google-cloud-monitoring==0.22.0

0 commit comments

Comments
 (0)