/fastapi-blog

A FastAPI backend for blog with OpenTelemtry and Jaeger

Primary LanguagePythonMIT LicenseMIT

Test

A FastAPI sample

It's a FastAPI implementation as the backend for a blog system. This project's a funny goal to apply things that I'm learning. Things that you see here like telemetry using open source projects, CRUD using REST, and GraphQL (I hope that I have time to do that).

Dependencies

  • Poetry
  • SQLAlmechy
  • Alembic for migrations
  • OpenTelemtry for instrumentation
  • Docker
  • Docker-Compose
  • Jaeger
  • Next.js
  • React
  • Make

How to use

Use make command to run inside a docker container, it'll expose the port 8000 and access using URL: http://localhost:8000. I'm not sure if a makefile runs in the Powershell, probably yes but I suggest using WSL2.

The frontend is in the early stage but you can see access: http://localhost:3000/.

Up and run:

make dev-up

"Screenshot with REST backend endpoints"

This repo is instrumented by OpenTelemtry and export metric to Jaeger that starts together with the app by Docker-Compose, to access: http://localhost:16686/

Tests

To run tests will up a PostgreSQL container and expose 5433 port to populate. The report will write in htmlcov dir.

make test-app

Run and watch on the Jaeger things breaking

The FastAPI code here was OpenTelemetry instrumented and to export Traces to Jaeger. If you want to see a break simulation, follow the steps below.

Install dependencies

python -m venv .venv

pip install poetry

poetry install

Start FastAPI, PostgreSQL and Jaeger

make dev-up

asciicast

Open another shell session and run populate_posts.py to create users and posts samples.

opentelemetry-instrument tests/populate_posts.py

asciicast

Break things

Open a third shell session to stop Postgres, this will break the FastAPI app. You'll see error messages in the log and the script client on the console. Stop Postgres, wait some seconds or a minute, start Postgres again.

make dev-db-kill

sleep 30

make dev-db-start

asciicast

Stop populate script

Stop the populate_posts.py scritpt (ctrl+c), go to Jaeger UI (http://localhost:16686/). If everything fine (or fail... 😁), you'll see on Jaeger like screenshots below.

  • FastAPI errors exported by OpenTelemetry to Jaeger

"jaeger traces errors"

  • An error detail from the client request and server.

"an example of errors in the jaeger"

Known issues

  • Some Opentelemetry warnings will show when tests ran.

TODO

  • Pagination
  • CORS
  • Token authentication
  • GraphQL
  • Improve Token
  • SAML/Oauth2
  • Run tests in container
  • Autentication
  • Implement UI (React)
  • Helm Chart
  • Improvement tests with tox
  • Implement a default path (/)
  • Custom Limit and Offset
  • Add script to ingest samples
  • CI tests