Integrate Rollbar to Flask application
Add real-time error monitoring, tracking and debugging for Containerised Flask Application
What is Rollbar
Rollbar is a cloud-based error tracking and debugging tool used in web development. It provides real-time monitoring of errors, exceptions, and logs that occur in web applications and services, allowing developers to quickly identify and fix issues before they cause problems for users.
Rollbar integrates with various programming languages and frameworks, including Ruby, Python, PHP, Java, JavaScript, and others, making it a versatile tool for web developers working with different technologies. When an error occurs, Rollbar captures information such as the error message, the stack trace, and the context in which the error occurred, allowing developers to quickly diagnose and fix the problem.
In addition to error tracking, Rollbar also provides features such as deployment tracking, release tracking, and custom alerts, allowing developers to stay on top of changes and potential issues in their codebase. Overall, Rollbar can be a valuable tool for any web development team looking to improve their application's stability and performance.
Instrument Flask code with a Rollbar Flask SDK
Sign up on Rollbar and create a project
Create a project
Integrate SDK
Get the rollbar access token There is a piece of code appears. The access token is included in the code. i.e.
... ... @app.before_first_request def init_rollbar(): """init rollbar module""" rollbar.init( # access token '4eb63a5998684d899fe6ec03c3a7d5be', # environment name 'production', # server root directory, makes tracebacks prettier root=os.path.dirname(os.path.realpath(__file__)), # flask already sets up logging allow_logging_basic_config=False) # send exceptions from `app` to rollbar, using flask's signal system. got_request_exception.connect(rollbar.contrib.flask.report_exception, app) ... ...
The project appears as "Configured"
Install dependencies.
Add two libraries
blinker
androllbar
torequirements.txt
# For Rollbar blinker rollbar
Run this command in a terminal to install the libraries for local development
pip install -r requirements.txt
Add an environment variable in
docker-compose.yml
for the related servicebackend-flask
services: backend-flask: environment: ROLLBAR_ACCESS_TOKEN: "${ROLLBAR_ACCESS_TOKEN}"
Set the environment variable in the local bash terminal
export ROLLBAR_ACCESS_TOKEN=<rollbar_access_token>
Optional: When using Gitpod as CDE, tell Gitpod to remember the environment variable when relaunching our workspaces
gp env AWS_DEFAULT_REGION=<rollbar_access_token>
Instrument Flask code with a Rollbar Flask SDK
Add the following code in
app.py
import os import rollbar import rollbar.contrib.flask from flask import got_request_exception
rollbar_access_token = os.getenv('ROLLBAR_ACCESS_TOKEN') @app.before_first_request def init_rollbar(): """init rollbar module""" rollbar.init( # access token rollbar_access_token, # environment name 'production', # server root directory, makes tracebacks prettier root=os.path.dirname(os.path.realpath(__file__)), # flask already sets up logging allow_logging_basic_config=False) # send exceptions from `app` to rollbar, using flask's signal system. got_request_exception.connect(rollbar.contrib.flask.report_exception, app)
In
app.py
, add an endpoint/rollbar/test
for testing Rollbar. It reports a warning-level message to rollbar.@app.route('/rollbar/test') def rollbar_test(): rollbar.report_message('Hello World!', 'warning') return "Hello World!"
Perform an API request on the added endpoint and Observe the errors in Rollbar UI
From the 'Item' tab, the error is shown, including its context and details.