Metis log collector for Flask and SQLAlchemy
Project description
Metis Flask SQLAlchemy log collector
About
This library logs the HTTP requests created by Flask and SQLAlchemy with the SQL commands they generate. The library can also collect the execution plan for deeper analysis.
The log records stored in a local file. Future versions will allow saving the log records directly to log collectors such as DataDog, Logz.io and Splunk.
The log can be analyzed using our Visual Studio Code extension
Technical
This library uses OpenTelemetry to instrument both Flask and SQLAlchemy.
Tested on python 3.8.9, Flask 2.1.1, SQLAlchemy 1.4.33, PostgreSQL 12 or higher.
Instrumentation
Installation:
pip install sqlalchemycollector
Instrumentation:
-
Configure the destination file name
-
Configure logging the execution plan
By default the package only logs the SQL commands and the estimated execution plan (PlanCollectType.ESTIMATED).
The library:
- Logs the estimated execution plan by running the SQL command using
EXPLAIN ( VERBOSE, COSTS, SUMMARY, FORMAT JSON)
- Runs the SQL command.
Logging the estimated plan has an impact on performances but usually, in a dev environment who doesn't run a large number of SQL commands, the impact is very low, around 3%.
Warning! Do NOT run the code in Production! An environment variable should prevent the package from collecting the estimated execution plan in Production.
The library can be configured using PlanCollectType.NONE to log only the SQL Commands while the execution plans, estimated or actual, will be calculated later using our platform.
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemycollector import setup, MetisInstrumentor, PlanCollectType
# existing app initialization
app = Flask(...)
db = SQLAlchemy(app)
# The default log file name is 'metis-log-collector.log. You can change the default name.
optional_log_file_name = 'new_file_name_of_sqlalchemy_logs.log'
# By default, the package logs the SQL commands and their execution plan.
# It can also be configured manually to collect only the SQL commands using PlanCollectType.NONE.
# We recommend collecting the estimated plan too.
optional_plan_collect_type = PlanCollectType.NONE
# class PlanCollectType(Enum):
# NONE = 0
# ESTIMATED = 1
# To start collecting the logs:
instrumentation: MetisInstrumentor = setup('service-name',
service_version='1.0.0',
plan_collection_option=PlanCollectType.ESTIMATED,
file_name=optional_log_file_name)
instrumentation.instrument_app(app, db.get_engine())
Set up your own tags:
You can assign metadata to your resources in the form of tags. Each tag is a label consisting of a user-defined key and value. Tags can help you manage, identify, organize, search for, and filter resources. You can create tags to categorize resources by purpose, owner, environment, or other criteria.
Define tags using environment variables:
METIS_TAG_<TAG_NAME>=
export METIS_TAG_ENV=staging
export METIS_TAG_CI_NUM=10
In this case we defined 2 tags env=staging and ci_num=10
Define tag using code:
Initialized setup with additional param called resource_tags
from sqlalchemycollector import setup, MetisInstrumentor
instrumentation: MetisInstrumentor = setup('service-name',
service_version='1.0.0',
resource_tags={"env": "staging"})
Example of a log entry (might be changed in the future)
{
"logs":
[
{
"_uuid": "0b3f9b86-c620-11ec-9d14-b276246b1dc9",
"query": "SELECT booking.book.title AS booking_book_title \nFROM booking.book",
"dbEngine": "postgresql",
"date": "2022-04-27T11:49:07.161743",
"plan":
{
"Plan":
{
"Node Type": "Seq Scan",
"Parallel Aware": false,
"Async Capable": false,
"Relation Name": "book",
"Schema": "booking",
"Alias": "book",
"Startup Cost": 0.0,
"Total Cost": 1.17,
"Plan Rows": 17,
"Plan Width": 14,
"Output":
[
"title"
],
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
},
"Planning":
{
"Shared Hit Blocks": 0,
"Shared Read Blocks": 0,
"Shared Dirtied Blocks": 0,
"Shared Written Blocks": 0,
"Local Hit Blocks": 0,
"Local Read Blocks": 0,
"Local Dirtied Blocks": 0,
"Local Written Blocks": 0,
"Temp Read Blocks": 0,
"Temp Written Blocks": 0
},
"Planning Time": 0.044
}
}
],
"framework": "Flask",
"path": "/",
"operationType": "GET",
"requestDuration": 210.35,
"requestStatus": 200
}
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
Hashes for sqlalchemycollector-1.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f889e7e3d60eef3b01dfb60c656fdbf3cb210fd94e08cd4fa2f01b79e542393 |
|
MD5 | 1c96d23c7eb5753516238bced612405d |
|
BLAKE2b-256 | 65b398bbee7d14a56f547d01275ebb23457b944dae465ae4c3068acf477594de |