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

feat: add profiling to Superset pages #16136

Merged
merged 2 commits into from
Aug 9, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements/development.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ tableschema
thrift>=0.11.0,<1.0.0
pygithub>=1.54.1,<2.0.0
progress>=1.5,<2
pyinstrument>=4.0.2,<5
4 changes: 3 additions & 1 deletion requirements/development.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# SHA1:c470411e2e9cb04b412a94f80a6a9d870bece74d
# SHA1:1144991012e228fb2ef85afbf78a635e7d5a33f1
#
# This file is autogenerated by pip-compile-multi
# To update, run:
Expand Down Expand Up @@ -54,6 +54,8 @@ pygithub==1.54.1
# via -r requirements/development.in
pyhive[hive]==0.6.3
# via -r requirements/development.in
pyinstrument==4.0.2
# via -r requirements/development.in
requests==2.24.0
# via
# pydruid
Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ combine_as_imports = true
include_trailing_comma = true
line_length = 88
known_first_party = superset
known_third_party =alembic,apispec,backoff,bleach,cachelib,celery,click,colorama,cron_descriptor,croniter,cryptography,dateutil,deprecation,flask,flask_appbuilder,flask_babel,flask_caching,flask_compress,flask_jwt_extended,flask_login,flask_migrate,flask_sqlalchemy,flask_talisman,flask_testing,flask_wtf,freezegun,geohash,geopy,graphlib,holidays,humanize,isodate,jinja2,jwt,markdown,markupsafe,marshmallow,marshmallow_enum,msgpack,numpy,pandas,parameterized,parsedatetime,pgsanity,pkg_resources,polyline,prison,progress,pyarrow,pyhive,pyparsing,pytest,pytest_mock,pytz,redis,requests,selenium,setuptools,simplejson,slack,sqlalchemy,sqlalchemy_utils,sqlparse,tabulate,typing_extensions,werkzeug,wtforms,wtforms_json,yaml
known_third_party =alembic,apispec,backoff,bleach,cachelib,celery,click,colorama,cron_descriptor,croniter,cryptography,dateutil,deprecation,flask,flask_appbuilder,flask_babel,flask_caching,flask_compress,flask_jwt_extended,flask_login,flask_migrate,flask_sqlalchemy,flask_talisman,flask_testing,flask_wtf,freezegun,geohash,geopy,graphlib,holidays,humanize,isodate,jinja2,jwt,markdown,markupsafe,marshmallow,marshmallow_enum,msgpack,numpy,pandas,parameterized,parsedatetime,pgsanity,pkg_resources,polyline,prison,progress,pyarrow,pyhive,pyinstrument,pyparsing,pytest,pytest_mock,pytz,redis,requests,selenium,setuptools,simplejson,slack,sqlalchemy,sqlalchemy_utils,sqlparse,tabulate,typing_extensions,werkzeug,wtforms,wtforms_json,yaml
multi_line_output = 3
order_by_type = false

Expand Down
5 changes: 5 additions & 0 deletions superset/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from flask import Flask

from superset.initialization import SupersetAppInitializer
from superset.utils.profiler import SupersetProfiler

logger = logging.getLogger(__name__)

Expand All @@ -36,6 +37,10 @@ def create_app() -> Flask:
app_initializer = app.config.get("APP_INITIALIZER", SupersetAppInitializer)(app)
app_initializer.init_app()

profiling = app.config.get("PROFILING", False)
if profiling:
betodealmeida marked this conversation as resolved.
Show resolved Hide resolved
app.wsgi_app = SupersetProfiler(app.wsgi_app) # type: ignore
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


return app

# Make sure that bootstrap errors ALWAYS get logged
Expand Down
4 changes: 4 additions & 0 deletions superset/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ def _try_json_readsha( # pylint: disable=unused-argument
DEBUG = os.environ.get("FLASK_ENV") == "development"
FLASK_USE_RELOAD = True

# Enable profiling of Python calls. Turn this on and append ``?_instrument=1``
# to the page to see the call stack.
PROFILING = False

# Superset allows server-side python stacktraces to be surfaced to the
# user when this feature is on. This may has security implications
# and it's more secure to turn it off in production settings.
Expand Down
52 changes: 52 additions & 0 deletions superset/utils/profiler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

from typing import Any, Callable
from unittest import mock

from pyinstrument import Profiler
from werkzeug.wrappers import Request, Response


class SupersetProfiler:
Copy link
Member

@john-bodley john-bodley Aug 9, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@betodealmeida would this be better defined an extension?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We still need the WSGI middleware, but I'll add an extension to register and set it up.

"""
WSGI middleware to instrument Superset.

To see the instrumentation for a given page, set `PROFILING=True`
in the config, and append `?_instrument=1` to the page.
"""

def __init__(
self, app: Callable[[Any, Any], Any], interval: float = 0.0001,
):
self.app = app
self.interval = interval

@Request.application
def __call__(self, request: Request) -> Response:
if request.args.get("_instrument") != "1":
return Response.from_app(self.app, request.environ)

profiler = Profiler(interval=self.interval)

# call original request
fake_start_response = mock.MagicMock()
with profiler:
self.app(request.environ, fake_start_response)

# return HTML profiling information
return Response(profiler.output_html(), mimetype="text/html")