From 9c77fb97658de8f61c2aeb0d3c89ccb18f676d36 Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Wed, 16 Nov 2016 15:39:20 -0800 Subject: [PATCH 1/2] Switch Error Reporting to Google Cloud Python --- error_reporting/README.md | 19 ----------------- error_reporting/main.py | 18 +++------------- error_reporting/main_test.py | 8 ++++--- error_reporting/requirements.txt | 2 +- error_reporting/startup_script.sh | 35 ------------------------------- 5 files changed, 9 insertions(+), 73 deletions(-) delete mode 100644 error_reporting/startup_script.sh diff --git a/error_reporting/README.md b/error_reporting/README.md index 4c9a24c60dc..028260c3f8a 100644 --- a/error_reporting/README.md +++ b/error_reporting/README.md @@ -2,25 +2,6 @@ This section contains samples for [Google Cloud Error Reporting](https://cloud.google.com/error-reporting). -A startup script has been provided to demonstrated how to properly provision a GCE -instance with fluentd configured. Note the intallation of fluentd, the addition of the config file, - and the restarting of the fluetnd service. You can start an instance using -it like this: - - gcloud compute instances create example-instance --metadata-from-file startup-script=startup_script.sh - -or simply use it as reference when creating your own instance. - -After fluentd is configured, main.py could be used to simulate an error: - - gcloud compute copy-files main.py example-instance:~/main.py - -Then, - - gcloud compute ssh example-instance - python ~/main.py - -And you will see the message in the Errors Console. These samples are used on the following documentation page: diff --git a/error_reporting/main.py b/error_reporting/main.py index 45208c913ac..40b7ba369d9 100644 --- a/error_reporting/main.py +++ b/error_reporting/main.py @@ -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] diff --git a/error_reporting/main_test.py b/error_reporting/main_test.py index 11a24d03543..a59d4eb16cb 100644 --- a/error_reporting/main_test.py +++ b/error_reporting/main_test.py @@ -17,7 +17,9 @@ import main -@mock.patch("fluent.event") -def test_error_sends(event_mock): +@mock.patch("main.error_reporting") +def test_error_sends(error_reporting_mock): + client_mock = mock.Mock() + error_reporting_mock.Client.return_value = client_mock main.simulate_error() - event_mock.Event.assert_called_once_with(mock.ANY, mock.ANY) + client_mock.report_exception.assert_called_once() diff --git a/error_reporting/requirements.txt b/error_reporting/requirements.txt index 0119e25c6df..0bb3f8d5125 100644 --- a/error_reporting/requirements.txt +++ b/error_reporting/requirements.txt @@ -1 +1 @@ -fluent-logger==0.4.4 +google-cloud-error-reporting==0.21.0 diff --git a/error_reporting/startup_script.sh b/error_reporting/startup_script.sh deleted file mode 100644 index f2ef895dcfd..00000000000 --- a/error_reporting/startup_script.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env bash -# 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. - -set -v - -curl -sSO "https://dl.google.com/cloudagents/install-logging-agent.sh" -chmod +x install-logging-agent.sh -./install-logging-agent.sh -mkdir -p /etc/google-fluentd/config.d/ -cat < /etc/google-fluentd/config.d/forward.conf - - type forward - port 24224 - -EOF -service google-fluentd restart - -apt-get update -apt-get install -yq \ - git build-essential supervisor python python-dev python-pip libffi-dev \ - libssl-dev -pip install fluent-logger - From 93f6a8566f6cf73017d6c2707d3a0b6aad411cac Mon Sep 17 00:00:00 2001 From: Bill Prin Date: Wed, 16 Nov 2016 16:45:53 -0800 Subject: [PATCH 2/2] Switch Error Reporting to Google Cloud Python --- error_reporting/README.md | 11 -- error_reporting/README.rst | 103 ++++++++++++++++++ error_reporting/README.rst.in | 19 ++++ .../{main.py => report_exception.py} | 0 ...{main_test.py => report_exception_test.py} | 12 +- 5 files changed, 125 insertions(+), 20 deletions(-) delete mode 100644 error_reporting/README.md create mode 100644 error_reporting/README.rst create mode 100644 error_reporting/README.rst.in rename error_reporting/{main.py => report_exception.py} (100%) rename error_reporting/{main_test.py => report_exception_test.py} (68%) diff --git a/error_reporting/README.md b/error_reporting/README.md deleted file mode 100644 index 028260c3f8a..00000000000 --- a/error_reporting/README.md +++ /dev/null @@ -1,11 +0,0 @@ -# Google Error Reorting Samples Samples - -This section contains samples for [Google Cloud Error Reporting](https://cloud.google.com/error-reporting). - - - -These samples are used on the following documentation page: - -> https://cloud.google.com/error-reporting/docs/setting-up-on-compute-engine - - diff --git a/error_reporting/README.rst b/error_reporting/README.rst new file mode 100644 index 00000000000..42f401a4f19 --- /dev/null +++ b/error_reporting/README.rst @@ -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/ \ No newline at end of file diff --git a/error_reporting/README.rst.in b/error_reporting/README.rst.in new file mode 100644 index 00000000000..4eeab4bbeba --- /dev/null +++ b/error_reporting/README.rst.in @@ -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 diff --git a/error_reporting/main.py b/error_reporting/report_exception.py similarity index 100% rename from error_reporting/main.py rename to error_reporting/report_exception.py diff --git a/error_reporting/main_test.py b/error_reporting/report_exception_test.py similarity index 68% rename from error_reporting/main_test.py rename to error_reporting/report_exception_test.py index a59d4eb16cb..1ec03d1e19a 100644 --- a/error_reporting/main_test.py +++ b/error_reporting/report_exception_test.py @@ -12,14 +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("main.error_reporting") -def test_error_sends(error_reporting_mock): - client_mock = mock.Mock() - error_reporting_mock.Client.return_value = client_mock - main.simulate_error() - client_mock.report_exception.assert_called_once() +def test_error_sends(): + report_exception.simulate_error()