Skip to content

mizhexiaoxiao/fastapi-opentracing

 
 

Repository files navigation

fastapi-opentracing

fastapi opentracing middleware works with istio

install:

pip install fastapi-opentracing

example:

from fastapi import FastAPI
import uvicorn
from fastapi_opentracing import get_opentracing_span_headers
from fastapi_opentracing.middleware import OpenTracingMiddleware

app = FastAPI()

app.add_middleware(OpenTracingMiddleware)


@app.get("/")
async def root():
    carrier = await get_opentracing_span_headers()
    return {'span': carrier}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

if your application uses tortoise-orm, you can specify the client mysql_client.install_patch to patch you SQLClient

example:

from fastapi import FastAPI
import uvicorn
from fastapi_opentracing import get_opentracing_span_headers
from fastapi_opentracing.middleware import OpenTracingMiddleware
from fastapi_opentracing.client_hooks.mysql_client import install_patch
from fastapi_opentracing.client_hooks import install_all_patch


app = FastAPI()

app.add_middleware(OpenTracingMiddleware)
TORTOISE_ORM = {
    "connections": {"default": "mysql://root:123456@127.0.0.1:3306/test"},
    "apps": {
        "models": {
            "models": ["tests.models", "aerich.models"],
            "default_connection": "default",
        },
    },
}
register_tortoise(
    app,
    config=TORTOISE_ORM,
    generate_schemas=True
)

install_all_patch()

@app.get("/")
async def root():
    carrier = await get_opentracing_span_headers()
    return {'span': carrier}

if __name__ == "__main__":
    uvicorn.run(app, host="0.0.0.0", port=8000)

Contributing and Developing

To install all dependencies, run:

python3 -m venv venv
source venv/bin/activate
make bootstrap

Running Tests

make test

Check the style and quality of python code

make lint

About

fastapi opentracing middleware works on k8s

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 99.8%
  • Makefile 0.2%