Skip to content

Commit be182a0

Browse files
author
Jerjou Cheng
committed
Move monitoring sample in, modified for ADC.
1 parent 4f41bdf commit be182a0

File tree

4 files changed

+132
-0
lines changed

4 files changed

+132
-0
lines changed

monitoring/__init__.py

Whitespace-only changes.

monitoring/samples/__init__.py

Whitespace-only changes.

monitoring/samples/auth.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
# Copyright 2015 Google Inc. All rights reserved.
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+
"""
16+
Simple command-line program to demonstrate connecting to the Google Cloud
17+
Monitoring API to retrieve API data, using application default credentials to
18+
authenticate.
19+
20+
This sample obtains authentication information from its environment via
21+
application default credentials [1].
22+
23+
If you're not running the sample on Google App Engine or Compute Engine (where
24+
the environment comes pre-authenticated as a service account), you'll have to
25+
initialize your environment with credentials the sample can use.
26+
27+
One way to do this is through the cloud console's credentials page [2]. Create
28+
a new client ID of type 'Service account', and download its JSON key. This will
29+
be the account the sample authenticates as.
30+
31+
Once you've downloaded the service account's JSON key, you provide it to the
32+
sample by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to
33+
point to the key file:
34+
35+
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/json-key.json
36+
37+
[1] https://developers.google.com/identity/protocols/application-default-credentials
38+
[2] https://console.developers.google.com/project/_/apiui/credential
39+
"""
40+
41+
# [START all]
42+
import json
43+
import sys
44+
45+
from googleapiclient.discovery import build
46+
47+
from oauth2client.client import GoogleCredentials
48+
49+
50+
METRIC = 'compute.googleapis.com/instance/disk/read_ops_count'
51+
YOUNGEST = '2015-01-01T00:00:00Z'
52+
53+
54+
def ListTimeseries(project_name, service):
55+
"""Query the Timeseries.list API
56+
method.
57+
58+
Args:
59+
project_name: the name of the project you'd like to monitor.
60+
service: the CloudMonitoring service object.
61+
"""
62+
63+
timeseries = service.timeseries()
64+
65+
print 'Timeseries.list raw response:'
66+
try:
67+
response = timeseries.list(
68+
project=project_name, metric=METRIC, youngest=YOUNGEST).execute()
69+
70+
print json.dumps(response,
71+
sort_keys=True,
72+
indent=4,
73+
separators=(',', ': '))
74+
except:
75+
print 'Error:'
76+
for error in sys.exc_info():
77+
print error
78+
79+
80+
def main(project_name):
81+
# Create and return the CloudMonitoring service object.
82+
service = build('cloudmonitoring', 'v2beta2',
83+
credentials=GoogleCredentials.get_application_default()
84+
)
85+
86+
ListTimeseries(project_name, service)
87+
88+
89+
if __name__ == '__main__':
90+
if len(sys.argv) != 2:
91+
print "Usage: %s <project-name>" % sys.argv[0]
92+
sys.exit(1)
93+
main(sys.argv[1])
94+
# [END all]

monitoring/samples/tests/test_auth.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# Copyright 2015, Google, Inc.
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
#
6+
# http://www.apache.org/licenses/LICENSE-2.0
7+
#
8+
# Unless required by applicable law or agreed to in writing, software
9+
# distributed under the License is distributed on an "AS IS" BASIS,
10+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
# See the License for the specific language governing permissions and
12+
# limitations under the License.
13+
14+
import re
15+
import sys
16+
import unittest
17+
18+
from monitoring.samples import auth
19+
20+
21+
class TestListObjects(unittest.TestCase):
22+
23+
@classmethod
24+
def setUpClass(cls):
25+
if not hasattr(sys.stdout, 'getvalue'):
26+
raise unittest.SkipTest('Test must be in buffered mode to run.')
27+
28+
def test_main(self):
29+
auth.main('bigquery-devrel-samples')
30+
output = sys.stdout.getvalue().strip()
31+
self.assertRegexpMatches(
32+
output, re.compile(r'Timeseries.list raw response:\s*'
33+
r'{\s*"kind": "[^"]+",'
34+
r'\s*"oldest": *"[0-9]+', re.S))
35+
36+
37+
if __name__ == '__main__':
38+
unittest.main()

0 commit comments

Comments
 (0)