Skip to content

Commit e982bf0

Browse files
author
Bill Prin
committed
Add Error Reporting Client
1 parent d958f74 commit e982bf0

File tree

7 files changed

+191
-0
lines changed

7 files changed

+191
-0
lines changed

docs/error-reporting-client.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
Error Reporting Client
2+
=======================
3+
4+
.. automodule:: gcloud.error_reporting.client
5+
:members:
6+
:show-inheritance:
7+

docs/error-reporting-usage.rst

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
Using the API
2+
=============
3+
4+
5+
Authentication and Configuration
6+
--------------------------------
7+
8+
- For an overview of authentication in ``gcloud-python``,
9+
see :doc:`gcloud-auth`.
10+
11+
- In addition to any authentication configuration, you should also set the
12+
:envvar:`GCLOUD_PROJECT` environment variable for the project you'd like
13+
to interact with. If you are Google App Engine or Google Compute Engine
14+
this will be detected automatically.
15+
16+
- After configuring your environment, create a
17+
:class:`Client <gcloud.logging.client.Client>`
18+
19+
.. doctest::
20+
21+
>>> from gcloud import error_reporting
22+
>>> client = error_reporting.Client()
23+
24+
or pass in ``credentials`` and ``project`` explicitly
25+
26+
.. doctest::
27+
28+
>>> from gcloud import error_reporting
29+
>>> client = error_reporting.Client(project='my-project', credentials=creds)
30+
31+
32+
Reporting an exception
33+
-----------------------
34+
35+
Report a stacktrace to Stackdriver Error Reporting after an exception
36+
37+
.. doctest::
38+
39+
>>> from gcloud import error_reporting
40+
>>> from
41+
>>> client = error_reporting.Client()
42+
>>> try:
43+
>>> raise NameError
44+
>>> except Exception:
45+
>>> client.report_error(message="Something went wrong")
46+

docs/index.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,14 @@
111111
logging-metric
112112
logging-sink
113113

114+
.. toctree::
115+
:maxdepth: 0
116+
:hidden:
117+
:caption: Stackdriver Error Reporting
118+
119+
error-reporting-usage
120+
Client <error-reporting-client>
121+
114122
.. toctree::
115123
:maxdepth: 0
116124
:hidden:

gcloud/error_reporting/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#!/usr/bin/env python
2+
# Copyright 2016 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Client library for Stackdriver Error Reporting"""
17+
18+
from gcloud.error_reporting.client import Client

gcloud/error_reporting/client.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
#!/usr/bin/env python
2+
# Copyright 2016 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
"""Client for interacting with the Stackdriver Logging API"""
17+
18+
import traceback
19+
20+
import gcloud.logging.client
21+
22+
DEFAULT_SERVICE = 'python'
23+
24+
25+
class Client(gcloud.logging.client.Client):
26+
"""Error Reporting client. Currently Error Reporting is done via the
27+
Logging Client so it just subclasses that."""
28+
29+
def report_error(self, message="", service=DEFAULT_SERVICE):
30+
""" Reports the details of the latest exceptions to Stackdriver Error
31+
Reporting.
32+
33+
https://cloud.google.com/error-reporting/docs/formatting-error-messages
34+
35+
:type message: str
36+
:param message: An optional message to include with the exception
37+
detail
38+
39+
:type service: str
40+
:param service: An identifier of the service, such as the name of
41+
the executable, job, or Google App Engine module
42+
name. This field is expected to have a low number
43+
of values that are relatively stable over time,
44+
as opposed to version, which can be changed
45+
whenever new code is deployed.
46+
47+
Example::
48+
49+
>>> try:
50+
>>> raise NameError
51+
>>> except Exception:
52+
>>> client.report_error("Something went wrong!")
53+
"""
54+
payload = {
55+
'serviceContext': {'service': service},
56+
'message': '{0} : {1}'.format(message, traceback.format_exc())
57+
}
58+
logger = self.logger('errors')
59+
logger.log_struct(payload)
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#!/usr/bin/env python
2+
# Copyright 2016 Google Inc. All Rights Reserved.
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
17+
import unittest2
18+
19+
20+
class TestClient(unittest2.TestCase):
21+
22+
def _getTargetClass(self):
23+
from gcloud.error_reporting.client import Client
24+
return Client
25+
26+
def _makeOne(self, *args, **kw):
27+
return self._getTargetClass()(*args, **kw)
28+
29+
def test_report_error(self):
30+
target = self._makeOne()
31+
MESSAGE = 'hello world'
32+
33+
logger = _Logger()
34+
target.logger = lambda _: logger
35+
36+
try:
37+
raise NameError
38+
except NameError:
39+
target.report_error(MESSAGE)
40+
41+
payload = logger.log_struct_called_with
42+
self.assertEquals(payload['serviceContext'], {
43+
'service': 'python'
44+
})
45+
self.assertIn(MESSAGE, payload['message'])
46+
self.assertIn('test_report_error', payload['message'])
47+
self.assertIn('test_client.py', payload['message'])
48+
49+
50+
class _Logger(object):
51+
def log_struct(self, payload):
52+
self.log_struct_called_with = payload

scripts/verify_included_modules.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
'gcloud.bigtable.__init__',
3636
'gcloud.datastore.__init__',
3737
'gcloud.dns.__init__',
38+
'gcloud.error_reporting.__init__',
3839
'gcloud.iterator',
3940
'gcloud.logging.__init__',
4041
'gcloud.monitoring.__init__',

0 commit comments

Comments
 (0)