Store and monitor site traffic.
Project description
flask-traffic 🚦
Store and monitor site traffic.
pip install flask-traffic
Minimal Example
from flask import Flask
from flask_traffic import Traffic
from flask_traffic.stores import JSONStore
traffic = Traffic()
def create_app():
app = Flask(__name__)
traffic.init_app(app, stores=JSONStore())
@app.route('/')
def index():
return 'Hello, World!'
return app
instance/traffic.json
...
{
"request_date": "2024-12-03T20:10:34.932025",
"request_method": "GET",
"request_path": "/",
"request_remote_address": "127.0.0.1",
"request_referrer": null,
"request_user_agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36",
"request_browser": null,
"request_platform": null,
"response_time": 1,
"response_size": 13,
"response_status_code": 200,
"response_exception": null,
"response_mimetype": "text/html"
}
...
Stores
JSONStore
This store saves traffic data in a JSON file. The file is created in the
instance folder of the Flask app by default.
CSVStore
This store saves traffic data in a CSV file. The file is created in the
instance folder of the Flask app by default.
SQLStore
This store saves traffic data in a SQL type database. It defaults to using SQLite
which is created in the instance folder of the Flask app by default.
You can specify a database URL, or pass in an already created SQLAlchemy engine.
This store is used if you want to store traffic data in a SQL type database.
SQLORMStore
This is an ORM version of the SQLStore. It is designed to integrate with an existing
SQLAlchemy ORM environment like Flask-SQLAlchemy.
SQLORMModelMixin
This mixin is used to set the correct table columns for the SQLORMStore.
Example:
from flask_traffic.stores import SQLORMModelMixin
from app import db
class Traffic(db.Model, SQLORMModelMixin):
pass
The LogPolicy class
from flask_traffic import LogPolicy
The log policy is used to tell Flask-Traffic what data to store after a request is made in whatever store, or stores you have configured.
A new instance of LogPolicy will have all the log attributes set to True by
default.
You can use the methods set_from_true or set_from_false to select which attributes
to store.
set_from_true will allow you to disable certain attributes from being stored.
set_from_false will allow you to enable certain attributes to be stored.
If a store is created without a log policy passed in, one is created with all log
attributes set to True, and log_only_on_exception and skip_log_on_exception are
set to False.
Here's an example of the LogPolicy class only storing the date and request path:
from flask_traffic.stores import JSONStore
from flask_traffic import LogPolicy
log_policy = LogPolicy().set_from_false(
request_date=True,
request_path=True
)
json_store = JSONStore(log_policy=log_policy)
Results in:
...
{
"request_date": "2024-12-03T20:33:43.051597",
"request_path": "/"
}
...
Here's an example of the LogPolicy class storing everything except the response size:
from flask_traffic.stores import JSONStore
from flask_traffic import LogPolicy
log_policy = LogPolicy().set_from_true(
response_size=False
)
json_store = JSONStore(log_policy=log_policy)
Bigger Examples
SQLORMStore with Flask-SQLAlchemy, JSONStore for exceptions
This example will store traffic data in a SQL database using Flask-SQLAlchemy and store any traffic that causes exceptions in a JSON file.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_traffic import Traffic, LogPolicy
from flask_traffic.stores import JSONStore, SQLORMStore, SQLORMModelMixin
db = SQLAlchemy()
traffic = Traffic()
class Cars(db.Model):
car_id = db.Column(db.Integer, primary_key=True)
make = db.Column(db.String(80), unique=True, nullable=False)
model = db.Column(db.String(80), unique=True, nullable=False)
class Traffic(db.Model, SQLORMModelMixin):
pass
def create_app():
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///instance/db.sqlite'
db.init_app(app)
# init traffic after db.init_app to find the db session
traffic.init_app(
app, stores=[
JSONStore(
log_policy=LogPolicy(log_only_on_exception=True)
),
SQLORMStore(
Traffic,
log_policy=LogPolicy(skip_log_on_exception=True)
)
])
@app.route('/')
def index():
return 'Hello, World!'
return app
CSVStore only IP Addresses
This example will store traffic data in a CSV file and only store the IP address
from flask import Flask
from flask_traffic import Traffic, LogPolicy
from flask_traffic.stores import CSVStore
traffic = Traffic()
def create_app():
app = Flask(__name__)
traffic.init_app(
app,
stores=CSVStore(
log_policy=LogPolicy().set_from_false(request_remote_address=True)
)
)
@app.route('/')
def index():
return 'Hello, World!'
return app
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flask_traffic-0.2.0.tar.gz.
File metadata
- Download URL: flask_traffic-0.2.0.tar.gz
- Upload date:
- Size: 31.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb510f7fc5113558bd6a8f5553e0b82527f1e2fe7df595c0b7facde427d3a425
|
|
| MD5 |
897f4aec43a05bb82046ed39139fab5e
|
|
| BLAKE2b-256 |
606a1c836f8685ac40ae97146b886078b05ba70ac818f87b369bfce720844d0f
|
File details
Details for the file flask_traffic-0.2.0-py3-none-any.whl.
File metadata
- Download URL: flask_traffic-0.2.0-py3-none-any.whl
- Upload date:
- Size: 14.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ac1d1ab0bbf03b83527cf6f8a97ccbaef55a8c7517bb830f9e3aa266790a5622
|
|
| MD5 |
8b3375049ae44c61a2befe02b711e7fa
|
|
| BLAKE2b-256 |
c3b20e1061a9d0f6bb5a2c161f4a91517ffcf2337096c2ebd828ffd772873518
|