Skip to content

Commit

Permalink
Switch Error Reporting to Google Cloud Python [(#663)](GoogleCloudPla…
Browse files Browse the repository at this point in the history
…tform/python-docs-samples#663)

* Switch Error Reporting to Google Cloud Python

* Switch Error Reporting to Google Cloud Python
  • Loading branch information
waprin authored Nov 17, 2016
1 parent af085d9 commit 36c51b1
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 88 deletions.
30 changes: 0 additions & 30 deletions samples/snippets/README.md

This file was deleted.

103 changes: 103 additions & 0 deletions samples/snippets/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
.. This file is automatically generated. Do not edit this file directly.
Stackdriver Error Reporting Python Samples
===============================================================================

This directory contains samples for Stackdriver Error Reporting. `Stackdriver Error Reporting`_ aggregates and displays errors produced in
your running cloud services.




.. _Stackdriver Error Reporting: https://cloud.google.com/error-reporting/docs/

Setup
-------------------------------------------------------------------------------


Authentication
++++++++++++++

Authentication is typically done through `Application Default Credentials`_,
which means you do not have to change the code to authenticate as long as
your environment has credentials. You have a few options for setting up
authentication:

#. When running locally, use the `Google Cloud SDK`_

.. code-block:: bash
gcloud beta auth application-default login
#. When running on App Engine or Compute Engine, credentials are already
set-up. However, you may need to configure your Compute Engine instance
with `additional scopes`_.

#. You can create a `Service Account key file`_. This file can be used to
authenticate to Google Cloud Platform services from any environment. To use
the file, set the ``GOOGLE_APPLICATION_CREDENTIALS`` environment variable to
the path to the key file, for example:

.. code-block:: bash
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/service_account.json
.. _Application Default Credentials: https://cloud.google.com/docs/authentication#getting_credentials_for_server-centric_flow
.. _additional scopes: https://cloud.google.com/compute/docs/authentication#using
.. _Service Account key file: https://developers.google.com/identity/protocols/OAuth2ServiceAccount#creatinganaccount

Install Dependencies
++++++++++++++++++++

#. Install `pip`_ and `virtualenv`_ if you do not already have them.

#. Create a virtualenv. Samples are compatible with Python 2.7 and 3.4+.

.. code-block:: bash
$ virtualenv env
$ source env/bin/activate
#. Install the dependencies needed to run the samples.

.. code-block:: bash
$ pip install -r requirements.txt
.. _pip: https://pip.pypa.io/
.. _virtualenv: https://virtualenv.pypa.io/

Samples
-------------------------------------------------------------------------------

Report Exception
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++



To run this sample:

.. code-block:: bash
$ python report_exception.py
The client library
-------------------------------------------------------------------------------

This sample uses the `Google Cloud Client Library for Python`_.
You can read the documentation for more details on API usage and use GitHub
to `browse the source`_ and `report issues`_.

.. Google Cloud Client Library for Python:
https://googlecloudplatform.github.io/google-cloud-python/
.. browse the source:
https://github.com/GoogleCloudPlatform/google-cloud-python
.. report issues:
https://github.com/GoogleCloudPlatform/google-cloud-python/issues
.. _Google Cloud SDK: https://cloud.google.com/sdk/
19 changes: 19 additions & 0 deletions samples/snippets/README.rst.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# This file is used to generate README.rst

product:
name: Stackdriver Error Reporting
short_name: Error Reporting
url: https://cloud.google.com/error-reporting/docs/
description: >
`Stackdriver Error Reporting`_ aggregates and displays errors produced in
your running cloud services.

setup:
- auth
- install_deps

samples:
- name: Report Exception
file: report_exception.py

cloud_client_library: true
18 changes: 3 additions & 15 deletions samples/snippets/main.py → samples/snippets/report_exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,16 @@
# limitations under the License.

# [START error_reporting]
import traceback

import fluent.event
import fluent.sender
from google.cloud import error_reporting


def simulate_error():
fluent.sender.setup('myapp', host='localhost', port=24224)

def report(ex):
data = {}
data['message'] = '{0}'.format(ex)
data['serviceContext'] = {'service': 'myapp'}
# ... add more metadata
fluent.event.Event('errors', data)

# report exception data using:
client = error_reporting.Client()
try:
# simulate calling a method that's not defined
raise NameError
except Exception:
report(traceback.format_exc())
client.report_exception()
# [END error_reporting]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.

import mock
import report_exception

import main


@mock.patch("fluent.event")
def test_error_sends(event_mock):
main.simulate_error()
event_mock.Event.assert_called_once_with(mock.ANY, mock.ANY)
def test_error_sends():
report_exception.simulate_error()
2 changes: 1 addition & 1 deletion samples/snippets/requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
fluent-logger==0.4.4
google-cloud-error-reporting==0.21.0
35 changes: 0 additions & 35 deletions samples/snippets/startup_script.sh

This file was deleted.

0 comments on commit 36c51b1

Please sign in to comment.