Skip to content

Commit 36c51b1

Browse files
authored
Switch Error Reporting to Google Cloud Python [(#663)](GoogleCloudPlatform/python-docs-samples#663)
* Switch Error Reporting to Google Cloud Python * Switch Error Reporting to Google Cloud Python
1 parent af085d9 commit 36c51b1

File tree

7 files changed

+129
-88
lines changed

7 files changed

+129
-88
lines changed

samples/snippets/README.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

samples/snippets/README.rst

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
.. This file is automatically generated. Do not edit this file directly.
2+
3+
Stackdriver Error Reporting Python Samples
4+
===============================================================================
5+
6+
This directory contains samples for Stackdriver Error Reporting. `Stackdriver Error Reporting`_ aggregates and displays errors produced in
7+
your running cloud services.
8+
9+
10+
11+
12+
.. _Stackdriver Error Reporting: https://cloud.google.com/error-reporting/docs/
13+
14+
Setup
15+
-------------------------------------------------------------------------------
16+
17+
18+
Authentication
19+
++++++++++++++
20+
21+
Authentication is typically done through `Application Default Credentials`_,
22+
which means you do not have to change the code to authenticate as long as
23+
your environment has credentials. You have a few options for setting up
24+
authentication:
25+
26+
#. When running locally, use the `Google Cloud SDK`_
27+
28+
.. code-block:: bash
29+
30+
gcloud beta auth application-default login
31+
32+
33+
#. When running on App Engine or Compute Engine, credentials are already
34+
set-up. However, you may need to configure your Compute Engine instance
35+
with `additional scopes`_.
36+
37+
#. You can create a `Service Account key file`_. This file can be used to
38+
authenticate to Google Cloud Platform services from any environment. To use
39+
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
40+
the path to the key file, for example:
41+
42+
.. code-block:: bash
43+
44+
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
45+
46+
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
47+
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
48+
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount
49+
50+
Install Dependencies
51+
++++++++++++++++++++
52+
53+
#. Install `pip`_ and `virtualenv`_ if you do not already have them.
54+
55+
#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.
56+
57+
.. code-block:: bash
58+
59+
$ virtualenv env
60+
$ source env/bin/activate
61+
62+
#. Install the dependencies needed to run the samples.
63+
64+
.. code-block:: bash
65+
66+
$ pip install -r requirements.txt
67+
68+
.. _pip: https://pip.pypa.io/
69+
.. _virtualenv: https://virtualenv.pypa.io/
70+
71+
Samples
72+
-------------------------------------------------------------------------------
73+
74+
Report Exception
75+
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
76+
77+
78+
79+
To run this sample:
80+
81+
.. code-block:: bash
82+
83+
$ python report_exception.py
84+
85+
86+
87+
88+
The client library
89+
-------------------------------------------------------------------------------
90+
91+
This sample uses the `Google Cloud Client Library for Python`_.
92+
You can read the documentation for more details on API usage and use GitHub
93+
to `browse the source`_ and `report issues`_.
94+
95+
.. Google Cloud Client Library for Python:
96+
https://googlecloudplatform.github.io/google-cloud-python/
97+
.. browse the source:
98+
https://github.com/GoogleCloudPlatform/google-cloud-python
99+
.. report issues:
100+
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
101+
102+
103+
.. _Google Cloud SDK: https://cloud.google.com/sdk/

samples/snippets/README.rst.in

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# This file is used to generate README.rst
2+
3+
product:
4+
name: Stackdriver Error Reporting
5+
short_name: Error Reporting
6+
url: https://cloud.google.com/error-reporting/docs/
7+
description: >
8+
`Stackdriver Error Reporting`_ aggregates and displays errors produced in
9+
your running cloud services.
10+
11+
setup:
12+
- auth
13+
- install_deps
14+
15+
samples:
16+
- name: Report Exception
17+
file: report_exception.py
18+
19+
cloud_client_library: true

samples/snippets/main.py renamed to samples/snippets/report_exception.py

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,16 @@
1313
# limitations under the License.
1414

1515
# [START error_reporting]
16-
import traceback
17-
18-
import fluent.event
19-
import fluent.sender
16+
from google.cloud import error_reporting
2017

2118

2219
def simulate_error():
23-
fluent.sender.setup('myapp', host='localhost', port=24224)
24-
25-
def report(ex):
26-
data = {}
27-
data['message'] = '{0}'.format(ex)
28-
data['serviceContext'] = {'service': 'myapp'}
29-
# ... add more metadata
30-
fluent.event.Event('errors', data)
31-
32-
# report exception data using:
20+
client = error_reporting.Client()
3321
try:
3422
# simulate calling a method that's not defined
3523
raise NameError
3624
except Exception:
37-
report(traceback.format_exc())
25+
client.report_exception()
3826
# [END error_reporting]
3927

4028

samples/snippets/main_test.py renamed to samples/snippets/report_exception_test.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,8 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import mock
15+
import report_exception
1616

17-
import main
1817

19-
20-
@mock.patch("fluent.event")
21-
def test_error_sends(event_mock):
22-
main.simulate_error()
23-
event_mock.Event.assert_called_once_with(mock.ANY, mock.ANY)
18+
def test_error_sends():
19+
report_exception.simulate_error()

samples/snippets/requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
fluent-logger==0.4.4
1+
google-cloud-error-reporting==0.21.0

samples/snippets/startup_script.sh

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)