flask-profiler, 监视端点调用并尝试进行某些分析的Flask 事件探查器

flask-profiler源代码下载

  • Git URL:
    git://www.github.com/muatik/flask-profiler.git
  • Git Clone代码到本地:
    git clone http://www.github.com/muatik/flask-profiler
  • Subversion代码到本地:
    $ svn co --depth empty http://www.github.com/muatik/flask-profiler
    Checked out revision 1.
    $ cd repo
    $ svn up trunk
    
 
烧瓶分析器

版本:1.6Build Status

使用 profiler测量在你的Flask 应用程序中定义的端点;并通过web界面提供细粒度的报告。

它给出了这些问题的答案:

  • 应用程序中的瓶颈在哪里?
  • 应用程序中最慢的终结点?
  • 哪些是最常被调用的终结点?
  • 什么导致我的慢速端点? 在哪个上下文中,什么是 ARGS 和 kwargs?
  • 特定请求花费了多少时间?

简而言之,如果你对端点正在做什么和接收的请求进行了了解,请尝试打瓶探查器。

通过使用烧瓶分析器接口,你可以监视所有端点的性能,并通过向下钻取过滤器来调查端点和接收的请求。

屏幕截图

指示板视图显示摘要。Alt text

你可以创建过滤器来调查某些类型的请求。 [ alt text](/resources/filtering_all_screen。pngraw=true"按端点筛选")?

Alt text

你可以看到请求的所有细节。Alt text

快速启动

通过例子可以很容易理解烧瓶的轮廓。 让我们来。

按pip安装烧瓶探查器。

pip install flask_profiler

在创建 Flask 应用程序时编辑你的代码。

# your app.pyfrom flask import Flaskimport flask_profiler
app = Flask(__name__)
app.config["DEBUG"] =True# You need to declare necessary configuration to initialize# flask-profiler as follows:app.config["flask_profiler"] = {
 "enabled": app.config["DEBUG"],
 "storage": {
 "engine": "sqlite" },
 "basicAuth":{
 "enabled": True,
 "username": "admin",
 "password": "admin" },
 "ignore": [
 "^/static/.*" ]
}@app.route('/product/<id>', methods=['GET'])defgetProduct(id):
 return"product id is "+str(id)@app.route('/product/<id>', methods=['PUT'])defupdateProduct(id):
 return"product {} is being updated".format(id)@app.route('/products', methods=['GET'])deflistProducts():
 return"suppose I send you product list..."@app.route('/static/something/', methods=['GET'])deflistProducts():
 return"this should not be tracked..."# In order to active flask-profiler, you have to pass flask# app as an argument to flask-profiler.# All the endpoints declared so far will be tracked by flask-profiler.flask_profiler.init_app(app)# endpoint declarations after flask_profiler.init_app() will be# hidden to flask_profiler.@app.route('/doSomething', methods=['GET'])defdoSomething():
 return"flask-profiler will not measure this."# But in case you want an endpoint to be measured by flask-profiler,# you can specify this explicitly by using profile() decorator@app.route('/doSomethingImportant', methods=['GET'])@flask_profiler.profile()defdoSomethingImportant():
 return"flask-profiler will measure this request."if__name__=='__main__':
 app.run(host="127.0.0.1", port=5000)

现在运行你的app.py

 
python app.py



并发出一些请求:

curl http://127.0.0.1:5000/products
curl http://127.0.0.1:5000/product/123
curl -X PUT -d arg1=val1 http://127.0.0.1:5000/product/123

如果一切正常烧瓶探查器将测量这些请求。 在 http://127.0.0.1 :5000/flask-profiler/ 中可以看到结果,或者将结果作为 JSON http://127.0.0.1 :5000/flask-profiler/api/measurements?sort=elapsed,desc。

如果你想在其他文件或者使用工厂应用程序 Pattern 初始化扩展,也可以创建 Profiler 类的实例,这将在首次运行应用程序时将你的所有端点设置为 register。 例如

from flask import Flaskfrom flask_profiler import Profiler
profiler = Profiler()
app = Flask(__name__)
app.config["DEBUG"] =True# You need to declare necessary configuration to initialize# flask-profiler as follows:app.config["flask_profiler"] = {
 "enabled": app.config["DEBUG"],
 "storage": {
 "engine": "sqlite" },
 "basicAuth":{
 "enabled": True,
 "username": "admin",
 "password": "admin" },
 "ignore": [
 "^/static/.*" ]
}
profiler = Profiler() # You can have this in another moduleprofiler.init_app(app)# Or just Profiler(app)@app.route('/product/<id>', methods=['GET'])defgetProduct(id):
 return"product id is "+str(id)

使用不同数据库系统的

你可以使用 flaskprofiler SqlLite MongoDB MongoDB 或者MongoDB数据库系统。 但是,它很容易支持其他数据库系统。 如果你希望拥有其他人,请访问捐赠文档。 ( 这很简单)

SQLite

为了使用 SQLite,只需将它指定为 storage.engine 指令的值,如下。

app.config["flask_profiler"] = {
 "storage": {
 "engine": "sqlite",
 }
}

下面列出了其他选项。

筛选键说明默认值
 
storage.FILE SQLite数据库文件 NAME flask_profiler.sql
storage.TABLE 分析数据将驻留在其中的表 NAME 度量

为了使用 MongoDB,只需将它指定为 storage.engine 指令的值,如下所示。

app.config["flask_profiler"] = {
 "storage": {
 "engine": "mongodb",
 }
}

SQLAchemy

使用 SQLAchemy,只需将它的指定为 storage.engine 指令的值,如下所示。 还首先使用 NAME"flask_profiler"创建一个空数据库。

app.config["flask_profiler"] = {
 "storage": {
 "engine": "sqlalchemy",
 "db_url": "postgresql://user:pass@localhost:5432/flask_profiler"# optional, if no db_url specified then sqlite will be used. }
}

自定义数据库引擎

将引擎指定为字符串 MODULE 和类路径。

app.config["flask_profiler"] = {
 "storage": {
 "engine": "custom.project.flask_profiler.mysql.MysqlStorage",
 "MYSQL": "mysql://user:password@localhost/flask_profiler" }
}

下面列出了其他选项。

筛选键说明默认值
 
storage.MONGO_URL mongodb连接字符串 mongodb://localhost
storage.DATABASE 数据库名 flask_profiler
storage.COLLECTION 收藏集名称 度量

采样

控制烧瓶探查器取样的数量

你将希望控制 Flask 分析器在生产模式下运行时需要取样的次数。 你可以根据业务逻辑提供一个函数并控制抽样。

示例 1: 100时间的样本 1,随机数

app.config["flask_profiler"] = {
 "sampling_function": lambda: Trueif random.sample(list(range(1, 101)), 1) == [42] elseFalse}

示例 2: 特定用户的示例

app.config["flask_profiler"] = {
 "sampling_function": lambda: Trueif user is'divyendu'elseFalse}

如果不存在采样函数,将对所有请求进行采样。

更换烧瓶分析器端点 root

默认情况下,我们可以访问/烧瓶分析器中的烧瓶探查器

app.config["flask_profiler"] = {
 "endpointRoot": "secret-flask-profiler"}

忽略终结点

烧瓶探查器将尝试跟踪在调用 init_app() 时定义的每个端点。 如果要排除某些终结点,则可以为它们定义匹配的正规表达式,如下所示:

app.config["flask_profiler"] = {
 "ignore": [
 "^/static/.*",
 "/api/users/w+/password" ]
}

posted on 2019-08-05 14:57  ExplorerMan  阅读(758)  评论(0编辑  收藏  举报

导航