Skip to content

Commit

Permalink
Add Error Reporting Client
Browse files Browse the repository at this point in the history
  • Loading branch information
Bill Prin committed Jun 30, 2016
1 parent d958f74 commit e982bf0
Show file tree
Hide file tree
Showing 7 changed files with 191 additions and 0 deletions.
7 changes: 7 additions & 0 deletions docs/error-reporting-client.rst
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:

46 changes: 46 additions & 0 deletions docs/error-reporting-usage.rst
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")

8 changes: 8 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,14 @@
logging-metric
logging-sink

.. toctree::
:maxdepth: 0
:hidden:
:caption: Stackdriver Error Reporting

error-reporting-usage
Client <error-reporting-client>

.. toctree::
:maxdepth: 0
:hidden:
Expand Down
18 changes: 18 additions & 0 deletions gcloud/error_reporting/__init__.py
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
59 changes: 59 additions & 0 deletions gcloud/error_reporting/client.py
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)
52 changes: 52 additions & 0 deletions gcloud/error_reporting/test_client.py
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
1 change: 1 addition & 0 deletions scripts/verify_included_modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
'gcloud.bigtable.__init__',
'gcloud.datastore.__init__',
'gcloud.dns.__init__',
'gcloud.error_reporting.__init__',
'gcloud.iterator',
'gcloud.logging.__init__',
'gcloud.monitoring.__init__',
Expand Down

0 comments on commit e982bf0

Please sign in to comment.