Skip to main content

Monitor and analyze flask endpoint and request performance.

Project description

Intruduction

Profiler-Flask comes from the refactoring of flask-profiler, and I would like to express my heartfelt thanks to its author @muatik for his open source spirit and excellent code.

The project beautifies the front-end interface on the basis of flask-profiler, and is basically consistent with its function.

With the web interface, You can monitor all your endpoints' performance you want to monitor and check the performance of endpoints and requests through filters.

Screenshots

Dashboard view

image

You can create filters to investigate certain type requests.

image

You can see all the details of a request.

image

Installation

use pdm

pdm add profiler-flask

use pip

pip install profiler-flask

Example

This is an example. Let's dive in.

from flask import Flask
import flask_profiler


app = Flask(__name__)
app.config["DEBUG"] = True
app.config["profiler"] = {
    "storage": {"engine": "sqlite"},
    "basicAuth": {"enabled": True, "username": "admin", "password": "admin"},
    "ignore": ["^/static/.*"],
}

@app.route("/product/<id>", methods=["GET"])
def getProduct(id):
    return f"product id is {id}"


@app.route("/product/<id>", methods=["PUT"])
def updateProduct(id):
    return f"product {id} is being updated"


@app.route("/products", methods=["GET"])
def listProducts():
    return "suppose I send you product list..."

flask_profiler.init_app(app)

# 也可以使用装饰器的方法使用
@app.route("/doSomethingImportant/<id>", methods=["GET"])
@flask_profiler.profile()
def doSomethingImportant(id):
    return "profiler will measure this request."

Now, run your flask app.

Using with different database system

You can use profiler-flask with Sqlite database systems.

SQLite

In order to use SQLite, just specify it as the value of storage.engine directive as follows.

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

Below the other options are listed.

Filter key Description Default
storage.FILE SQLite database file name flask_profiler.sql
storage.TABLE table name in which profiler data will reside measurements

Sampling

Control the number of samples taken by profiler-flask

You would want control over how many times should the profiler-flask take samples while running in production mode. You can supply a function and control the sampling according to your business logic.

Example 1: Sample 1 in 100 times with random numbers

app.config["profiler"] = {
    "sampling_function": lambda: True if random.sample(list(range(1, 101)), 1) == [42] else False
}

Example 2: Sample for specific users

app.config["profiler"] = {
    "sampling_function": lambda: True if user is 'Dumblidore' else False
}

If sampling function is not present, all requests will be sampled.

Changing profiler-flask endpoint root

By default, we can access profiler-flask at /profiler, but you can change the endpoint root.

Example:

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

the endpoint root will be changed to /profiler-flask.

Ignored endpoints

Profiler-Flask will try to track every endpoint defined so far when init_app() is invoked. If you want to exclude some of the endpoints, you can define matching regex for them as follows:

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

License

This project is licensed under the MIT License (see the LICENSE file for details). Some macros were part of Flask-Profiler and were modified under the terms of its MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

profiler_flask-0.1.0.tar.gz (893.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

profiler_flask-0.1.0-py3-none-any.whl (923.9 kB view details)

Uploaded Python 3

File details

Details for the file profiler_flask-0.1.0.tar.gz.

File metadata

  • Download URL: profiler_flask-0.1.0.tar.gz
  • Upload date:
  • Size: 893.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.13.2 CPython/3.11.3 Windows/10

File hashes

Hashes for profiler_flask-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4e06453c5a700052b5340fa7dd90609d3588698640ac4e188f1cd25fa3127180
MD5 b34113c87d76caf1a97f1de3212081a1
BLAKE2b-256 3d00062cbc2988723d0cea4902cefd0f196233378d170390af0fa72906fdbed8

See more details on using hashes here.

File details

Details for the file profiler_flask-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: profiler_flask-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 923.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: pdm/2.13.2 CPython/3.11.3 Windows/10

File hashes

Hashes for profiler_flask-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e70cd22158b15a52454def54a93a5cf74cc61cc65f580b1927e4f6b9cb15500f
MD5 cb29b991871e19c28fb9098c056223a6
BLAKE2b-256 7154c4948f945047d3573d14ee00955f0a0fb100ebd1581b6a2a104158ec7e9c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page