Skip to content

Commit 9811f6b

Browse files
committed
update example app for flask monitoring
1 parent c1ac7e5 commit 9811f6b

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

monitor-flask-apps/app.py

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,37 @@
11
import os
22
import re
33
import rollbar
4-
from flask import Flask, render_template
4+
import rollbar.contrib.flask
5+
from flask import Flask, render_template, Response
6+
from flask import got_request_exception
57
from werkzeug.exceptions import NotFound
68

79

810
app = Flask(__name__)
9-
rollbar.init(os.environ.get('ROLLBAR_SECRET'))
10-
rollbar.report_message('Rollbar is configured correctly')
11-
1211
MIN_PAGE_NAME_LENGTH = 2
1312

1413

14+
@app.before_first_request
15+
def add_monitoring():
16+
rollbar.init(os.environ.get('ROLLBAR_SECRET'))
17+
## delete the next line if you dont want this event anymore
18+
rollbar.report_message('Rollbar is configured correctly')
19+
got_request_exception.connect(rollbar.contrib.flask.report_exception, app)
20+
21+
1522
@app.route("/<string:page>/")
1623
def show_page(page):
17-
valid_length = len(page) >= MIN_PAGE_NAME_LENGTH
18-
valid_name = re.match('^[a-z]+$', page.lower()) is not None
19-
if valid_length and valid_name:
20-
return render_template("{}.html".format(page))
21-
else:
22-
msg = "Sorry, couldn't find page with name {}".format(page)
23-
raise NotFound(msg)
24+
try:
25+
valid_length = len(page) >= MIN_PAGE_NAME_LENGTH
26+
valid_name = re.match('^[a-z]+$', page.lower()) is not None
27+
if valid_length and valid_name:
28+
return render_template("{}.html".format(page))
29+
else:
30+
msg = "Sorry, couldn't find page with name {}".format(page)
31+
raise NotFound(msg)
32+
except:
33+
rollbar.report_exc_info()
34+
return Response("404 Not Found")
2435

2536

2637
if __name__ == "__main__":

monitor-flask-apps/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
blinker==1.4
12
Flask==0.12.2
23
rollbar==0.13.12

0 commit comments

Comments
 (0)