-
Notifications
You must be signed in to change notification settings - Fork 6.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move monitoring sample in, modified for ADC. #50
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
# Copyright 2015 Google Inc. All rights reserved. | ||
# | ||
# 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. | ||
|
||
""" | ||
Simple command-line program to demonstrate connecting to the Google Cloud | ||
Monitoring API to retrieve API data, using application default credentials to | ||
authenticate. | ||
|
||
This sample obtains authentication information from its environment via | ||
application default credentials [1]. | ||
|
||
If you're not running the sample on Google App Engine or Compute Engine (where | ||
the environment comes pre-authenticated as a service account), you'll have to | ||
initialize your environment with credentials the sample can use. | ||
|
||
One way to do this is through the cloud console's credentials page [2]. Create | ||
a new client ID of type 'Service account', and download its JSON key. This will | ||
be the account the sample authenticates as. | ||
|
||
Once you've downloaded the service account's JSON key, you provide it to the | ||
sample by setting the GOOGLE_APPLICATION_CREDENTIALS environment variable to | ||
point to the key file: | ||
|
||
$ export GOOGLE_APPLICATION_CREDENTIALS=/path/to/json-key.json | ||
|
||
[1] https://developers.google.com/identity/protocols/application-default-credentials | ||
[2] https://console.developers.google.com/project/_/apiui/credential | ||
""" | ||
|
||
# [START all] | ||
import json | ||
import sys | ||
|
||
from googleapiclient.discovery import build | ||
|
||
from oauth2client.client import GoogleCredentials | ||
|
||
|
||
METRIC = 'compute.googleapis.com/instance/disk/read_ops_count' | ||
YOUNGEST = '2015-01-01T00:00:00Z' | ||
|
||
|
||
def ListTimeseries(project_name, service): | ||
"""Query the Timeseries.list API | ||
method. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "method." should be in the above line without a newline? |
||
|
||
Args: | ||
project_name: the name of the project you'd like to monitor. | ||
service: the CloudMonitoring service object. | ||
""" | ||
|
||
timeseries = service.timeseries() | ||
|
||
print 'Timeseries.list raw response:' | ||
try: | ||
response = timeseries.list( | ||
project=project_name, metric=METRIC, youngest=YOUNGEST).execute() | ||
|
||
print json.dumps(response, | ||
sort_keys=True, | ||
indent=4, | ||
separators=(',', ': ')) | ||
except: | ||
print 'Error:' | ||
for error in sys.exc_info(): | ||
print error | ||
|
||
|
||
def main(project_name): | ||
# Create and return the CloudMonitoring service object. | ||
service = build('cloudmonitoring', 'v2beta2', | ||
credentials=GoogleCredentials.get_application_default() | ||
) | ||
|
||
ListTimeseries(project_name, service) | ||
|
||
|
||
if __name__ == '__main__': | ||
if len(sys.argv) != 2: | ||
print "Usage: %s <project-name>" % sys.argv[0] | ||
sys.exit(1) | ||
main(sys.argv[1]) | ||
# [END all] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Copyright 2015, 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 re | ||
import sys | ||
import unittest | ||
|
||
from monitoring.samples import auth | ||
|
||
|
||
class TestListObjects(unittest.TestCase): | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
if not hasattr(sys.stdout, 'getvalue'): | ||
raise unittest.SkipTest('Test must be in buffered mode to run.') | ||
|
||
def test_main(self): | ||
auth.main('bigquery-devrel-samples') | ||
output = sys.stdout.getvalue().strip() | ||
self.assertRegexpMatches( | ||
output, re.compile(r'Timeseries.list raw response:\s*' | ||
r'{\s*"kind": "[^"]+",' | ||
r'\s*"oldest": *"[0-9]+', re.S)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the docstring convention is: