Skip to main content
# Flask-profiler


**version: 1.8** [![Build Status](https://travis-ci.org/muatik/flask-profiler.svg?branch=master)](https://travis-ci.org/muatik/flask-profiler)

##### Flask-profiler measures endpoints defined in your flask application; and provides you fine-grained report through a web interface.

It gives answers to these questions:
* Where are the bottlenecks in my application?
* Which endpoints are the slowest in my application?
* Which are the most frequently called endpoints?
* What causes my slow endpoints? In which context, with what args and kwargs are they slow?
* How much time did a specific request take?

In short, if you are curious about what your endpoints are doing and what requests they are receiving, give a try to flask-profiler.

With flask-profiler's web interface, you can monitor all your endpoints' performance and investigate endpoints and received requests by drilling down through filters.

## Screenshots

Dashboard view displays a summary.

![Alt text](resources/dashboard_screen.png?raw=true "Dashboard view")

You can create filters to investigate certain type requests.

![Alt text](resources/filtering_all_screen.png?raw=true "Filtering by endpoint")

![Alt text](resources/filtering_method_screen.png?raw=true "Filtering by method")

You can see all the details of a request.
![Alt text](resources/filtering_detail_screen.png?raw=true "Request detail")

## Quick Start
It is easy to understand flask-profiler going through an example. Let's dive in.

Install flask-profiler by pip.
```sh
pip install flask_profiler
```


Edit your code where you are creating Flask app.
```python
# your app.py
from flask import Flask
import 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'])
def getProduct(id):
return "product id is " + str(id)


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


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

@app.route('/static/something/', methods=['GET'])
def listProducts():
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'])
def doSomething():
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()
def doSomethingImportant():
return "flask-profiler will measure this request."

if __name__ == '__main__':
app.run(host="127.0.0.1", port=5000)


```

Now run your `app.py`
```
python app.py
```

And make some requests like:
```sh
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
```

If everything is okay, Flask-profiler will measure these requests. You can see the result heading to http://127.0.0.1:5000/flask-profiler/ or get results as JSON http://127.0.0.1:5000/flask-profiler/api/measurements?sort=elapsed,desc

If you like to initialize your extensions in other files or use factory apps pattern, you can also create a instance of the `Profiler` class, this will register all your endpoints once you app run by first time. E.g:

```python
from flask import Flask
from 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 module
profiler.init_app(app)
# Or just Profiler(app)

@app.route('/product/<id>', methods=['GET'])
def getProduct(id):
return "product id is " + str(id)

```

## Using with different database system
You can use flaskprofiler with **SqlLite**, **MongoDB**, **Postgresql**, **Mysql** or **MongoDB** database systems. However, it is easy to support other database systems. If you would like to have others, please go to contribution documentation. (It is really easy.)

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

```json
app.config["flask_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 profiling data will reside | measurements |

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

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

### SQLAchemy
In order to use SQLAchemy, just specify it as the value of `storage.engine` directive as follows.
Also first create an empty database with the name "flask_profiler".

```python
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.
}
}
```

### Custom database engine
Specify engine as string module and class path.

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

The other options are listed below.

| Filter key | Description | Default
|----------|-------------|------
| storage.MONGO_URL | mongodb connection string | mongodb://localhost
| storage.DATABASE | database name | flask_profiler
| storage.COLLECTION | collection name | measurements

### Sampling
Control the number of samples taken by flask-profiler

You would want control over how many times should the flask profiler 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
```python
app.config["flask_profiler"] = {
"sampling_function": lambda: True if random.sample(list(range(1, 101)), 1) == [42] else False
}
```

Example 2: Sample for specific users
```python
app.config["flask_profiler"] = {
"sampling_function": lambda: True if user is 'divyendu' else False
}
```

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

### Changing flask-profiler endpoint root
By default, we can access flask-profiler at <your-app>/flask-profiler

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

### Ignored endpoints
Flask-profiler 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:

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


## Contributing

Contributions are welcome!

Review the [Contributing Guidelines](https://github.com/muatik/flask-profiler/wiki/Development) for details on how to:

* Submit issues
* Add solutions to existing challenges
* Add new challenges

## Authors
* [Musafa Atik](https://www.linkedin.com/in/muatik)
* Fatih Sucu
* [Safa Yasin Yildirim](https://www.linkedin.com/in/safayasinyildirim)

## License
MIT

Metadata

Release files for flask_profiler 1.8.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for flask_profiler 1.8.1
File Size Uploaded
flask_profiler-1.8.1.tar.gz 328.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for flask_profiler 1.8.1
File Interpreter ABI Platform
flask_profiler-1.8.1-py3-none-any.whl Python 3 none any Details

Total release size: 675.1 kB

Release files / flask_profiler-1.8.1.tar.gz

Download URL flask_profiler-1.8.1.tar.gz
Size 328.1 kB
Tags Source
SHA-256 checksum
How to use checksums
fc9f2875a4f22223ddc04ffacd75792854162c4cdbef165598a51f898521ac51
BLAKE2b-256 checksum
How to use checksums
74ba91dd2fc1759b9e2e5117154017277e222b98774e7f2e5ac475db30e7eaf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release files / flask_profiler-1.8.1-py3-none-any.whl

Download URL flask_profiler-1.8.1-py3-none-any.whl
Size 347.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
56b51e47c98e9f36150fafffd449b075c58a9b404389a071d405222a35183758
BLAKE2b-256 checksum
How to use checksums
b6f4efd944904ed1fda58fcf856649a2a91ed5b04b44bda575fdcd0c69bd3972
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via Python-urllib/3.6

Release history Release notifications | RSS feed

This release

1.8.1 This release

2 release files

1.8

2 release files

1.7

2 release files

1.6

2 release files

1.5

2 release files

1.4

2 release files

1.3

2 release files

1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0

2 release files

0.9

2 release files

0.8

2 release files

0.7

2 release files

0.6

2 release files

0.5

2 release files

0.4.16

2 release files

0.4.15

1 release file

0.4.14

2 release files

0.4.13

2 release files

0.4.12

2 release files

0.4.10

2 release files

0.4.8

2 release files

0.4.7

2 release files

0.4.6

2 release files

0.4.5

2 release files

0.4.4

2 release files

0.4.3

2 release files

0.4.2

2 release files

0.4.1

2 release files

0.4

2 release files

0.3

2 release files

0.2.1

2 release files

0.2

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page