Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add: pyinstrument for profiling api requests #6411

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 16 additions & 2 deletions backend/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import json
from logging.handlers import RotatingFileHandler

from flask import Flask, redirect, request
from flask import Flask, redirect, request, g, make_response
from flask_cors import CORS
from flask_migrate import Migrate
from requests_oauthlib import OAuth2Session
Expand All @@ -26,7 +26,7 @@
from flask_mail import Mail

from backend.config import EnvironmentConfig

from pyinstrument import Profiler

# Load error_messages.json and store it so that it is loaded only once at startup (Used in exceptions.py)
# Construct the path to the JSON file
Expand Down Expand Up @@ -150,6 +150,20 @@ def handle_generic_error(error):
def index_redirect():
return redirect(format_url("system/heartbeat/"), code=302)

@app.before_request
def before_request():
if "pyinstrument" in request.args:
g.profiler = Profiler()
g.profiler.start()

@app.after_request
def after_request(response):
if not hasattr(g, "profiler"):
return response
g.profiler.stop()
output_html = g.profiler.output_html()
return make_response(output_html)

# Add paths to API endpoints
add_api_endpoints(app)

Expand Down
48 changes: 43 additions & 5 deletions pdm.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ dependencies = [
"shapely==2.0.1",
"SQLAlchemy==2.0.19",
"Werkzeug==2.3.6",
"pyinstrument==4.6.2",
# Indirect, but required dependencies (often required for efficient deployments)
"gevent==22.10.2",
"greenlet==2.0.2",
Expand Down