-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added pre-commit hooks + example for FastAPI
- Loading branch information
Showing
17 changed files
with
169 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
repos: | ||
- repo: https://github.com/pre-commit/mirrors-isort | ||
rev: v4.3.21 | ||
hooks: | ||
- id: isort | ||
additional_dependencies: [toml] | ||
exclude: env.py | ||
- repo: https://github.com/pre-commit/pre-commit-hooks | ||
rev: v2.5.0 | ||
hooks: | ||
- id: trailing-whitespace | ||
- id: end-of-file-fixer | ||
- id: check-docstring-first | ||
- id: check-json | ||
- id: check-yaml | ||
- id: debug-statements | ||
- id: requirements-txt-fixer | ||
- id: detect-private-key | ||
- repo: https://gitlab.com/pycqa/flake8 | ||
rev: 3.7.9 | ||
hooks: | ||
- id: flake8 | ||
- repo: https://github.com/pre-commit/pygrep-hooks | ||
rev: v1.5.1 | ||
hooks: | ||
- id: python-check-blanket-noqa | ||
- id: python-use-type-annotations | ||
- repo: https://github.com/ambv/black | ||
rev: 19.10b0 | ||
hooks: | ||
- id: black | ||
language_version: python3.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -174,5 +174,3 @@ | |
of your accepting any such warranty or additional liability. | ||
|
||
END OF TERMS AND CONDITIONS | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
include VERSION LICENSE | ||
include VERSION LICENSE |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# FastAPI opentracing example | ||
|
||
An example of using the middleware with FastAPI and instrumentation for some commonly used python libs for DB access | ||
and external http requests. Using the `install_all_patches()` method from `opentracing_instrumentation` package gives | ||
you a way to trace your MySQLdb, SQLAlchemy, Redis queries and more without writing boilerplate code. | ||
|
||
It uses a local running `jaeger` instance as a tracing system. You can install `jaeger` for your OS with the info on | ||
https://www.jaegertracing.io/download/ or start a self contained dockerized variant with: | ||
|
||
``` | ||
docker run -d -e \ | ||
COLLECTOR_ZIPKIN_HTTP_PORT=9411 \ | ||
-p 5775:5775/udp \ | ||
-p 6831:6831/udp \ | ||
-p 6832:6832/udp \ | ||
-p 5778:5778 \ | ||
-p 16686:16686 \ | ||
-p 14268:14268 \ | ||
-p 9411:9411 \ | ||
jaegertracing/all-in-one:latest | ||
``` | ||
|
||
When `jaeger` is up and running you can access it at: http://localhost:16686/search | ||
|
||
Now install the requirements and start FastAPI: | ||
|
||
```bash | ||
python3 -m venv example | ||
source example/bin/activate | ||
pip install -r requirements.txt | ||
python app.py | ||
``` | ||
|
||
When you now access these URL's, you should see the traces in jaeger: | ||
|
||
http://localhost:8000/ | ||
|
||
http://localhost:8000/external-api | ||
|
||
http://localhost:8000/test-404 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import requests | ||
import uvicorn | ||
from fastapi import FastAPI | ||
from jaeger_client import Config as jaeger_config | ||
from opentracing_instrumentation.client_hooks import install_all_patches | ||
|
||
from starlette_opentracing import StarletteTracingMiddleWare | ||
|
||
app = FastAPI() | ||
|
||
|
||
@app.get("/") | ||
async def homepage(): | ||
return {"title": "Home", "content": "Home content"} | ||
|
||
|
||
@app.get("/external-api") | ||
def call_external_api(): | ||
# Note: Using requests instead of httpx here so you can see the effect of `install_all_patches()` | ||
try: | ||
r = requests.get("https://api.github.com/events") | ||
r.raise_for_status() | ||
except requests.exceptions.HTTPError as err: | ||
return {"title": "Call external API", "content": {"error": str(err)}} | ||
return {"title": "Call external API", "content": r.json()} | ||
|
||
|
||
opentracing_config = jaeger_config( | ||
config={ | ||
"sampler": {"type": "const", "param": 1}, | ||
"logging": False, | ||
"local_agent": {"reporting_host": "localhost"}, | ||
}, | ||
service_name="FastAPI tracer example", | ||
) | ||
jaeger_tracer = opentracing_config.initialize_tracer() | ||
install_all_patches() | ||
app.add_middleware(StarletteTracingMiddleWare, tracer=jaeger_tracer) | ||
|
||
|
||
if __name__ == "__main__": | ||
uvicorn.run(app, host="0.0.0.0", port=8000) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# add dependencies in setup.py | ||
|
||
-r requirements.txt | ||
|
||
|
||
-e .[tests] | ||
-r requirements.txt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# add dependencies in setup.py | ||
|
||
-e . |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,3 @@ | ||
from urllib.parse import urlunparse | ||
|
||
import opentracing | ||
from opentracing.ext import tags | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,4 @@ commands = flake8 starlette_opentracing tests | |
[testenv] | ||
extras = tests | ||
commands = | ||
pytest | ||
pytest |