-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bill Prin
committed
Jun 30, 2016
1 parent
d958f74
commit e982bf0
Showing
7 changed files
with
191 additions
and
0 deletions.
There are no files selected for viewing
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,7 @@ | ||
Error Reporting Client | ||
======================= | ||
|
||
.. automodule:: gcloud.error_reporting.client | ||
:members: | ||
:show-inheritance: | ||
|
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,46 @@ | ||
Using the API | ||
============= | ||
|
||
|
||
Authentication and Configuration | ||
-------------------------------- | ||
|
||
- For an overview of authentication in ``gcloud-python``, | ||
see :doc:`gcloud-auth`. | ||
|
||
- In addition to any authentication configuration, you should also set the | ||
:envvar:`GCLOUD_PROJECT` environment variable for the project you'd like | ||
to interact with. If you are Google App Engine or Google Compute Engine | ||
this will be detected automatically. | ||
|
||
- After configuring your environment, create a | ||
:class:`Client <gcloud.logging.client.Client>` | ||
|
||
.. doctest:: | ||
|
||
>>> from gcloud import error_reporting | ||
>>> client = error_reporting.Client() | ||
|
||
or pass in ``credentials`` and ``project`` explicitly | ||
|
||
.. doctest:: | ||
|
||
>>> from gcloud import error_reporting | ||
>>> client = error_reporting.Client(project='my-project', credentials=creds) | ||
|
||
|
||
Reporting an exception | ||
----------------------- | ||
|
||
Report a stacktrace to Stackdriver Error Reporting after an exception | ||
|
||
.. doctest:: | ||
|
||
>>> from gcloud import error_reporting | ||
>>> from | ||
>>> client = error_reporting.Client() | ||
>>> try: | ||
>>> raise NameError | ||
>>> except Exception: | ||
>>> client.report_error(message="Something went wrong") | ||
|
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
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,18 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2016 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. | ||
|
||
"""Client library for Stackdriver Error Reporting""" | ||
|
||
from gcloud.error_reporting.client import Client |
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,59 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2016 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. | ||
|
||
"""Client for interacting with the Stackdriver Logging API""" | ||
|
||
import traceback | ||
|
||
import gcloud.logging.client | ||
|
||
DEFAULT_SERVICE = 'python' | ||
|
||
|
||
class Client(gcloud.logging.client.Client): | ||
"""Error Reporting client. Currently Error Reporting is done via the | ||
Logging Client so it just subclasses that.""" | ||
|
||
def report_error(self, message="", service=DEFAULT_SERVICE): | ||
""" Reports the details of the latest exceptions to Stackdriver Error | ||
Reporting. | ||
https://cloud.google.com/error-reporting/docs/formatting-error-messages | ||
:type message: str | ||
:param message: An optional message to include with the exception | ||
detail | ||
:type service: str | ||
:param service: An identifier of the service, such as the name of | ||
the executable, job, or Google App Engine module | ||
name. This field is expected to have a low number | ||
of values that are relatively stable over time, | ||
as opposed to version, which can be changed | ||
whenever new code is deployed. | ||
Example:: | ||
>>> try: | ||
>>> raise NameError | ||
>>> except Exception: | ||
>>> client.report_error("Something went wrong!") | ||
""" | ||
payload = { | ||
'serviceContext': {'service': service}, | ||
'message': '{0} : {1}'.format(message, traceback.format_exc()) | ||
} | ||
logger = self.logger('errors') | ||
logger.log_struct(payload) |
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,52 @@ | ||
#!/usr/bin/env python | ||
# Copyright 2016 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. | ||
|
||
|
||
import unittest2 | ||
|
||
|
||
class TestClient(unittest2.TestCase): | ||
|
||
def _getTargetClass(self): | ||
from gcloud.error_reporting.client import Client | ||
return Client | ||
|
||
def _makeOne(self, *args, **kw): | ||
return self._getTargetClass()(*args, **kw) | ||
|
||
def test_report_error(self): | ||
target = self._makeOne() | ||
MESSAGE = 'hello world' | ||
|
||
logger = _Logger() | ||
target.logger = lambda _: logger | ||
|
||
try: | ||
raise NameError | ||
except NameError: | ||
target.report_error(MESSAGE) | ||
|
||
payload = logger.log_struct_called_with | ||
self.assertEquals(payload['serviceContext'], { | ||
'service': 'python' | ||
}) | ||
self.assertIn(MESSAGE, payload['message']) | ||
self.assertIn('test_report_error', payload['message']) | ||
self.assertIn('test_client.py', payload['message']) | ||
|
||
|
||
class _Logger(object): | ||
def log_struct(self, payload): | ||
self.log_struct_called_with = payload |
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