Stackdriver Error Reporting aggregates errors in your application. It provides a centralized dashboard showing counts of each unique error, stack traces, and a history of occurences. You can also set up alerting for when new errors occur.
This page is part of a multipage tutorial. To start from the beginning and read the setup instructions, go to Python Bookshelf app.
This section uses code in the
5-logging directory. Edit the files
and run commands in this directory.
Set the value of
PROJECT_IDto your project ID, which is visible in the GCP Console.
DATA_BACKENDto the same value you used during the Using Structured Data tutorial.
If you are using Cloud SQL or MongoDB, set the values under
Mongosection to the same values you used during the Using Structured Data step.
Set the value
CLOUD_STORAGE_BUCKETto your Cloud Storage bucket name.
OAuth2 configurationsection, set the values of
GOOGLE_OAUTH2_CLIENT_SECRETto the application client ID and secret that you created previously.
Save and close
If you are using Cloud SQL:
Set the value of
cloud_sql_instancesto the same value used for
config.py. It should be in the format
project:region:cloudsql-instance. Uncomment this entire line.
Save and close
Enter these commands to create a virtual environment and install dependencies:
virtualenv -p python3 env source env/bin/activate pip install -r requirements.txt
virtualenv -p python3 env env\scripts\activate pip install -r requirements.txt
Running the app on your local machine:
Start a local web server:
In your web browser, enter this address:
Press Control+C to exit the worker and then the local web server.
Deploying the app to the App Engine flexible environment
Deploy the sample app:
gcloud app deploy
In your web browser, enter this address. Replace
[YOUR_PROJECT_ID]with your project ID:
If you update your app, you can deploy the updated version by entering the same command you used to deploy the app the first time. The new deployment creates a new version of your app and promotes it to the default version. The older versions of your app remain, as do their associated VM instances. Be aware that all of these app versions and VM instances are billable resources.
You can reduce costs by deleting the non-default versions of your app.
To delete an app version:
- In the GCP Console, go to the App Engine Versions page.
- Click the checkbox next to the non-default app version you want to delete.
- Click Delete delete at the top of the page to delete the app version.
For complete information about cleaning up billable resources, see the Cleaning up section in the final step of this tutorial.
Simulate an error
In order to see Stackdriver Error Reporting in action, you can intentionally introduce a mistake into the code so you can see the exception in the Error Reporting console.
bookshelf/crud.py, add an operation that accesses an undefined variable and will throw an
ReferenceError in the index view.
@crud.route("/") def list(): x
Now redeploy the app:
gcloud app deploy
And access the index page to cause the error:
gcloud app browse
You should see
An internal error occurred.
In the Google Cloud Platform Console, select Stackdriver > Error Reporting:
You should see the error listed in the dashboard:
You can then click on the error to see more details about when it was last seen, the number of times it occurred, a histogram of occurrence times, and the stack trace.
Understanding the code
In order to report uncaught exceptions, the code uses the Flask
decorator, and then uses the Google Cloud Python Client
client to report the exception to Stackdriver Error Reporting.
@app.errorhandler(500) def server_error(e): client = error_reporting.Client(app.config['PROJECT_ID']) client.report_exception( http_context=error_reporting.build_flask_context(request)) return """ An internal error occurred. """, 500
The client automatically adds the traceback info, and uses a helper function to extract the relevant request
details from the Flask request. This way, any uncaught
InternalServerError HTTP 500 exception in your application will have
the relevant stack traces and HTTP contexts available in Error Reporting.
Note that the 500 error handler only triggers when the app is not in
DEBUG mode. When you
deploy the app,
DEBUG will be off, but by default
DEBUG is on locally, so
exceptions won't be reported to Error Reporting. Instead, the stack trace information will be rendered directly to the browser. You can change the local debug setting in
To avoid incurring charges to your Google Cloud Platform account for the resources used in this tutorial:
The easiest way to eliminate billing is to delete the project that you created for the tutorial.
To delete the project:
- In the GCP Console, go to the Projects page.
- In the project list, select the project you want to delete and click Delete delete.
- In the dialog, type the project ID, and then click Shut down to delete the project.