You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Time to run and test our code. Change into the base directory of your
156
-
project where `app.py` is located. Execute `app.py` using the `python`
157
-
command as follows:
162
+
project where `app.py` file is located. Execute `app.py` using the `python`
163
+
command as follows (make sure your virtualenv is still activated in the
164
+
terminal where you are running this command):
158
165
159
166
```bash
160
167
python app.py
@@ -165,13 +172,33 @@ of output.
165
172
166
173
<imgsrc="/img/170723-monitor-flask-apps/python-app-py.png"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Run the Flask development server locally.">
167
174
168
-
**Test app out... happy path, error 404 and error 500.***
175
+
What happens when we access the application running on
176
+
[localhost port 5000](http://localhost:5000)?
169
177
170
-
The error is obvious to us because we are testing the application locally,
171
-
but what happens when the application is deployed and a user gets the
172
-
error in their own web browser? They will typically quit out of
173
-
frustration and you will never know what happened unless you add some
174
-
error tracking and application monitoring.
178
+
<imgsrc="/img/170723-monitor-flask-apps/localhost-base-url.png"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Testing our Flask application at the base URL receives an HTTP 404 error.">
179
+
180
+
HTTP status 404 page not found, which is what we expected because we only
181
+
defined a single route and it did not live at the base path.
182
+
183
+
We created a template named `battlegrounds.html` that should be accessible
<imgsrc="/img/170723-monitor-flask-apps/localhost-pubg-gif.jpg"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Testing our Flask application at /battlegrounds/ gets the proper template with a GIF.">
188
+
189
+
The application successfully found the `battlegrounds.html` template but
<imgsrc="/img/170723-monitor-flask-apps/localhost-no-template.jpg"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="If no template is found we receive a 500 error.">
194
+
195
+
HTTP 500 error. That's no good.
196
+
197
+
The 404 and 500 errors are obvious to us right now because we are
198
+
testing the application locally. However, what happens when the app is
199
+
deployed and a user gets the error in their own web browser? They will
200
+
typically quit out of frustration and you will never know what happened
201
+
unless you add some error tracking and application monitoring.
175
202
176
203
We will now modify our code to add Rollbar to catch and report those
177
204
errors that occur for our users.
@@ -188,19 +215,101 @@ email address, a username and the password you want on the sign up page.
188
215
189
216
<imgsrc="/img/170723-monitor-flask-apps/sign-up.jpg"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Enter your basic account information on the sign up page.">
190
217
191
-
After the sign up page you'll get to the onboarding flow where you can
218
+
After the sign up page you will see the onboarding flow where you can
192
219
enter a project name and select a programming language. For project
193
-
name enter "Echo" and select that you are monitoring a Python app.
220
+
name enter "Battlegrounds" and select that you are monitoring a Python app.
194
221
195
-
<imgsrc="/img/170723-monitor-flask-apps/create-new-project.png"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Create a new project named 'echo' and select Python as the programming language.">
222
+
<imgsrc="/img/170723-monitor-flask-apps/create-new-project.jpg"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Create a new project named 'Battlegrounds' and select Python as the programming language.">
196
223
224
+
Press the "Continue" button at the bottom to move along. The next
225
+
screen shows us a few quick instructions to add monitoring to our Flask
226
+
application.
197
227
198
-
(links to Python docs)
228
+
<imgsrc="/img/170723-monitor-flask-apps/project-setup.jpg"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Set up your project using your server-side access token.">
229
+
230
+
Let's modify our Flask application to test whether we can properly connect
231
+
to Rollbar's service. Change `app.py` to include the following highlighted
232
+
lines.
233
+
234
+
```python
235
+
~~import os
236
+
import re
237
+
~~import rollbar
238
+
from flask import Flask, render_template
239
+
from werkzeug.exceptions import NotFound
240
+
241
+
242
+
app = Flask(__name__)
243
+
~~rollbar.init(os.environ.get('ROLLBAR_SECRET'))
244
+
~~rollbar.report_message('Rollbar is configured correctly')
msg ="Sorry, couldn't find page with name {}".format(page)
257
+
raise NotFound(msg)
258
+
259
+
260
+
if__name__=="__main__":
261
+
app.run(debug=True)
262
+
```
263
+
264
+
We added a couple of new imports, `os` and `rollbar`. `os` allows us to
265
+
grab environment variable values, such as our Rollbar secret key. `rollbar`
266
+
is the library we installed earlier. The two lines below the Flask app
267
+
instantiation are to initialize Rollbar using the Rollbar secret token and
268
+
send a message to the service that it started correctly.
269
+
270
+
The `ROLLBAR_SECRET` token needs to be set in an environment variable.
271
+
Save an quit the `app.py`. Run `export ROLLBAR_SECRET='token here'` on the
272
+
command line where your virtualenv is activated. This token can be found
273
+
on the Rollbar onboarding screen.
274
+
275
+
I typically store all my environment variables in a file like
276
+
[template.env](https://github.com/fullstackpython/blog-code-examples/blob/master/monitor-flask-apps/template.env) and invoke it from the terminal using
277
+
the `. ./template.env` command. Make sure to avoid committing your secret
278
+
tokens to a source control repository, especially if the repository is
279
+
public!
280
+
281
+
After exporting your `ROLLBAR_SECRET` key as an environment variable
282
+
we can test that Rollbar is working as we run our application. Run it
283
+
now using `python`:
284
+
285
+
```bash
286
+
python app.py
287
+
```
288
+
289
+
Back in your web browser press the "Done! Go to Dashboard" button. Don't
290
+
worry about the "Report an Error" section code, we can get back to that in a
291
+
moment.
292
+
293
+
If the event hasn't been reported yet we'll see a waiting screen like this
294
+
one:
295
+
296
+
<imgsrc="/img/170723-monitor-flask-apps/waiting.jpg"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="Waiting for data on the dashboard.">
297
+
298
+
Once Flask starts up though, the first event will be populated on the
299
+
dashboard.
300
+
301
+
<imgsrc="/img/170723-monitor-flask-apps/first-event.jpg"width="100%"class="technical-diagram img-rounded"style="border:1pxsolid#ccc"alt="First event populated on our dashboard for this project.">
302
+
303
+
Okay, our first test event has been populated, but we really want to see
304
+
all the errors from our application, not a test event.
199
305
200
306
201
307
## Testing Error Handling
202
308
203
309
310
+
(links to Python docs)
311
+
312
+
204
313
## What's Next?
205
314
We just learned how to catch and handle errors with Rollbar as a hosted
206
315
monitoring platform in a simple Flask application. Next you will want to
0 commit comments