Skip to content

Commit

Permalink
Fix observation of status codes
Browse files Browse the repository at this point in the history
This should now cover all cases where the request is even routed
to an endpoint. Note that this still does not catch
`405 Method not allowed` errors because those aren’t even routed to
an endpoint.
  • Loading branch information
horazont committed Oct 3, 2019
1 parent 14f4faf commit 5ee463a
Showing 1 changed file with 41 additions and 27 deletions.
68 changes: 41 additions & 27 deletions muchopper/web/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,17 +259,31 @@ def render_static_template(path):
"text/html")


def collect_status(metric):
def observe(app):
status_metric = PROMETHEUS_METRIC_STATUS_CODE

def wrapper(f):
endpoint = f.__name__
@functools.wraps(f)
def wrapped(*args, **kwargs):
result = f(*args, **kwargs)
if not isinstance(result, werkzeug.BaseResponse):
result = make_response(result)
status_code = 500
try:
try:
result = f(*args, **kwargs)
except BaseException as exc:
result = app.handle_user_exception(exc)

if not isinstance(result, werkzeug.BaseResponse):
result = make_response(result)

metric.labels(f.__name__, str(result.status_code)).inc()
status_code = result.status_code

return result
return result
except BaseException as exc:
status_code = 500
raise
finally:
status_metric.labels(endpoint, str(status_code)).inc()

return wrapped
return wrapper
Expand Down Expand Up @@ -407,7 +421,7 @@ def ccg_rgb_triplet(s):


@app.route("/")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def index():
return redirect(url_for("room_list", pageno=1))

Expand All @@ -434,7 +448,7 @@ def room_page(page, per_page, **kwargs):
@app.route("/rooms/")
@app.route("/rooms/<int:pageno>")
@register_menu(app, "data.rooms", "All Rooms", order=1)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
@PROMETHEUS_METRIC_ROOM_PAGE_HTML.time()
def room_list(pageno=1):
per_page = 25
Expand All @@ -460,7 +474,7 @@ def room_list(pageno=1):


@app.route("/avatar/v1/<address>")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def avatar_v1(address):
try:
address = aioxmpp.JID.fromstr(address)
Expand Down Expand Up @@ -500,7 +514,7 @@ def avatar_v1(address):

@app.route("/search")
@register_menu(app, "data.search", "Search", order=2)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
@PROMETHEUS_METRIC_SEARCH_HTML.time()
def search():
no_keywords = False
Expand Down Expand Up @@ -613,7 +627,7 @@ def get_metrics():

@app.route("/stats")
@register_menu(app, "data.stats", "Statistics", order=4)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
@PROMETHEUS_METRIC_STATS_HTML.time()
def statistics():
common_metrics = get_metrics()
Expand Down Expand Up @@ -701,62 +715,62 @@ def statistics():

@app.route("/docs/faq")
@register_menu(app, "docs.faq", "Frequent Questions (FAQ)", order=1)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def faq():
return render_static_template("faq.html")


@app.route("/docs/owners")
@register_menu(app, "docs.owners", "For room owners", order=2)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def owners():
return render_static_template("for_owners.html")


@app.route("/docs/operators")
@register_menu(app, "docs.operators", "For service operators", order=3)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def operators():
return render_static_template("for_operators.html")


@app.route("/docs/api")
@register_menu(app, "docs.developers", "For developers", order=4)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def developers():
return render_static_template("for_developers.html")


@app.route("/about")
@register_menu(app, "meta.about", "About", order=1)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def about():
return render_static_template("about.html")


@app.route("/tos")
@register_menu(app, "meta.tos", "Terms of Service", order=2)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def tos():
return render_static_template("tos.html")


@app.route("/privacy")
@register_menu(app, "meta.privacy", "Privacy Policy", order=3)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def privacy():
return render_static_template("privacy.html")


@app.route("/legal")
@register_menu(app, "meta.legal", "Legal notes & Contact", order=4)
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def legal():
return render_static_template("legal.html")


@app.route("/contact")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def contact():
return redirect(url_for('legal'))

Expand All @@ -780,7 +794,7 @@ def room_to_json(muc, public_info):

@app.route("/api/1.0/rooms.json")
@app.route("/api/1.0/rooms/unsafe")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
@PROMETHEUS_METRIC_ROOM_PAGE_UNSAFE_API.time()
def api_rooms_unsafe():
try:
Expand Down Expand Up @@ -822,7 +836,7 @@ def optional_typecast_argument(args, name, type_):


@app.route("/api/1.0/rooms")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
@PROMETHEUS_METRIC_ROOM_PAGE_API.time()
def api_rooms_safe():
PAGE_SIZE = 200
Expand Down Expand Up @@ -865,7 +879,7 @@ def api_rooms_safe():


@app.route("/api/1.0/search", methods=["POST", "GET"])
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
@PROMETHEUS_METRIC_SEARCH_API.time()
def api_search():
payload = request.get_json()
Expand Down Expand Up @@ -992,7 +1006,7 @@ def api_search():


@app.route("/api/1.0/badge")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
@PROMETHEUS_METRIC_BADGE_API.time()
def api_badge():
CHARWIDTH = 7
Expand Down Expand Up @@ -1080,7 +1094,7 @@ def collect(self):


@app.route("/metrics")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def metrics():
return Response(
prometheus_client.exposition.generate_latest(),
Expand All @@ -1089,7 +1103,7 @@ def metrics():


@app.route("/site.webmanifest")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def site_manifest():
# this is needed for icons
generator = functools.partial(
Expand Down Expand Up @@ -1121,7 +1135,7 @@ def site_manifest():


@app.route("/favicon.ico")
@collect_status(PROMETHEUS_METRIC_STATUS_CODE)
@observe(app)
def favicon():
# fallback resource
return app.send_static_file('img/favicon.ico')
Expand Down

0 comments on commit 5ee463a

Please sign in to comment.