Python - SQLAlchemy
You will need your project token for initializing your library. You can get your project token from Projects Page.
Flask
FastAPI (Sync)
FastAPI (Async)
pip install sqlalchemycollector
pip install fastapialchemycollector
pip install fastapialchemycollector
Open your app's
main.py
and add the following code-Flask
FastAPI (Sync)
FastAPI (Async)
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from sqlalchemycollector import setup, MetisInstrumentor, PlanCollectType
app = Flask(__name__)
db = SQLAlchemy(app)
with app.app_context():
instrumentation: MetisInstrumentor = setup(
service_name='<SERVICE_NAME>',
api_key='<API_KEY>',
service_version='<SERVICE_VERSION>'
)
instrumentation.instrument_app(app, db.get_engine())
@app.route("/hello")
def home():
return "Hello, Flask!"
from fastapi import FastAPI, APIRouter
from fastapialchemycollector import setup, MetisInstrumentor, PlanCollectType
from database.connection import engine
app = FastAPI()
api_router = APIRouter()
app.include_router(api_router)
DATABASE_URL = "postgresql://user_name:[email protected]_name:port/db_name"
engine = create_engine(DATABASE_URL)
instrumentation: MetisInstrumentor = setup(
service_name='<SERVICE_NAME>',
api_key='<API_KEY>',
service_version='<SERVICE_VERSION>'
)
instrumentation.instrument_app(app, engine)
@api_router.get("/hello", status_code=200)
def helloWorld():
return "Hello World!"
from fastapi import FastAPI, APIRouter
from sqlalchemy.ext.asyncio import create_async_engine
from fastapialchemycollector import setup, MetisInstrumentor, PlanCollectType
app = FastAPI()
api_router = APIRouter()
app.include_router(api_router)
DATABASE_URL = "postgresql+asyncpg://user_name:[email protected]_name:port/db_name"
async_engine = create_async_engine(DATABASE_URL, echo=True)
instrumentation: MetisInstrumentor = setup(
service_name='<SERVICE_NAME>',
api_key='<API_KEY>',
service_version='<SERVICE_VERSION>',
)
instrumentation.instrument_app(app, async_engine)
Variable Name | Description |
---|---|
METIS_API_KEY | <String> API Key to use |
METIS_ENVIRONMENT | <String> Text used to identify the source that sends the instrumentation data. |
METIS_DISABLED | <Boolean> If True Metis Instrumentation is fully disabled. We strongly advise to disable the instrumentation when in production to prevent sensitive data from leaving your organization's database. |
METIS_SERVICE_NAME | <String> Gives ability to distinguish between services. Useful when working with Micro Services. |
Now you can run your application as you normally would, and interact with it in a manner that causes the server to interact with your database.
Last modified 2mo ago