Skip to content

Commit d7e733f

Browse files
committed
lession01
0 parents  commit d7e733f

19 files changed

+2016
-0
lines changed

README.md

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# Microservices in Python
2+
3+
## [Lession 1](docs/lession1.md) - Preparation
4+
- Install Tools
5+
- Create Project
6+
- VS Code Finetuning
7+
8+
## [Lession 2](docs/lession2.md) - Get into Kubernetes
9+
- Dockerize
10+
- Logging
11+
- Tilt & Debugging
12+
13+
## [Lession 3](docs/lession3.md) - REST Service
14+
- Model
15+
- Database Connection
16+
- Router
17+
- Database Schema
18+
- Health Check
19+
20+
## [Lession 4](docs/lession4.md) - Unit Testing
21+
- Dependency Injection
22+
- Unit Testing
23+
24+
## [Lession 5](docs/lession5.md) - Relation on Entities
25+
- Releations
26+
- Functional Validators
27+
- Booking API
28+
29+
## [Lession 6](docs/lession6.md) - Model Details
30+
- Nested Models
31+
- Separate Model for Creation
32+
- Working with Entities
33+
34+
## [Lession 7](docs/lession7.md) - Message Queue - Produce
35+
- Feeding the Message Queue
36+
- Broker
37+
- Producer
38+
39+
## [Lession 8](docs/lession8.md) - 2nd Microservice
40+
- Next Microservice
41+
42+
## [Lession 9](docs/lession9.md) - Message Queue - Receive
43+
- Receiving Messages
44+
45+
## [Lession 10](docs/lession10.md) - Monitoring
46+
- Monitoring Prometheus
47+
48+
## [Lession 11](docs/lession11.md) - Tracing
49+
- Tracing with OpenTelemetry
50+
- Instrumentation
51+
- Collector
52+
- Spans, Attributes, Events
53+
54+
## [Lession 12](docs/lession12.md) - Load Testing
55+
- Basic Load Testing

dispo/.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
__pycache__/
2+
.venv/

dispo/.vscode/launch.json

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
// Use IntelliSense to learn about possible attributes.
3+
// Hover to view descriptions of existing attributes.
4+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
5+
"version": "0.2.0",
6+
"configurations": [
7+
{
8+
"name": "Python Debugger: FastAPI",
9+
"type": "debugpy",
10+
"request": "launch",
11+
"module": "uvicorn",
12+
"args": [
13+
"main:app",
14+
"--reload"
15+
],
16+
"jinja": true
17+
}
18+
]
19+
}

dispo/README.md

Whitespace-only changes.

dispo/main.py

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from fastapi import FastAPI
2+
3+
app = FastAPI()
4+
5+
6+
@app.get("/")
7+
async def hello_world():
8+
return {"message": "hello world"}

dispo/poetry.lock

+270
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dispo/pyproject.toml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[tool.poetry]
2+
name = "dispo"
3+
version = "0.1.0"
4+
description = ""
5+
authors = ["Your Name <you@example.com>"]
6+
readme = "README.md"
7+
8+
[tool.poetry.dependencies]
9+
python = "^3.12"
10+
fastapi = "^0.109.2"
11+
uvicorn = {extras = ["standart"], version = "^0.27.0.post1"}
12+
13+
14+
[build-system]
15+
requires = ["poetry-core"]
16+
build-backend = "poetry.core.masonry.api"

docs/lession1.md

+130
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
### Lesson 1
2+
3+
- Install Tools
4+
- Create Project
5+
- VS Code Finetuning
6+
- Code Formatting
7+
- Linting
8+
- Debugging
9+
10+
11+
# Install Tools
12+
13+
- Python 3.9 or later \
14+
My suggestion ist to use the latest version posibile.
15+
- [Visual Studio Code](https://code.visualstudio.com/)
16+
- Plugins:
17+
- Python (with Pylance & Debugger)
18+
- Black Formatter
19+
- Flake8
20+
- isort
21+
- Optional Plugins:
22+
- YAML
23+
- Tiltfile
24+
- Kubernetes
25+
- Docker
26+
- Markdown Preview
27+
- Jupyter
28+
29+
- [Poetry](https://python-poetry.org/) \
30+
using pip (outside virtualenv), pipx or brew (macos)
31+
32+
- [Tilt.dev](https://tilt.dev/)
33+
34+
# Create Project
35+
36+
```sh
37+
mkdir dispo
38+
poetry init
39+
# do not define dependencies
40+
41+
```
42+
See result in `pyproject.toml` file.
43+
44+
# Create WebServer
45+
46+
`poetry add fastapi "uvicorn[standart]"`
47+
48+
With `poetry add` the package will also be installed. Use `poetry install` if the dependencies are already declared.
49+
50+
You must create an README.md file, otherwise poetry install will fail.
51+
52+
Let's start with our first code snippet:
53+
54+
```python
55+
#dispo/main.py
56+
from fastapi import FastAPI
57+
58+
app = FastAPI()
59+
60+
61+
@app.get("/")
62+
async def hello_world():
63+
return {"message": "hello world"}
64+
65+
```
66+
To start the app:
67+
```sh
68+
poetry shell
69+
uvicorn main:app
70+
```
71+
72+
Check `http://localhost:8000`
73+
74+
Swagger/OpenApi is already included, see `http://localhost:8000/docs`.
75+
76+
77+
# VS Code Finetuning
78+
79+
## Virtual Environment
80+
Virtual Environments will be recognized when activated at startup. If you prefer the the .venv inside the project directory, do `poetry config virtualenvs.in-project true`.
81+
82+
## Code Formatting
83+
84+
We do use Black as formatter and isort for sorted import statements.
85+
86+
```json
87+
# >Preferences, Open User Settings(Json)
88+
# settings.json - add following:
89+
"[python]": {
90+
"editor.defaultFormatter": "ms-python.black-formatter",
91+
"editor.formatOnSave": true,
92+
"editor.codeActionsOnSave": {
93+
"source.organizeImports": "explicit"
94+
},
95+
},
96+
"isort.args":["--profile", "black"],
97+
```
98+
99+
To fix issues with black vs isort, add this
100+
101+
```toml
102+
# pyproject.toml
103+
[tool.isort]
104+
profile = "black"
105+
```
106+
107+
## Linting
108+
109+
Is done by flake8. I do suggest to use the vs-code plugin for this. The following config will fix the usage with Black. This config could NOT be included in pyproject.toml.
110+
111+
```toml
112+
# .flake8
113+
[flake8]
114+
max-line-length = 88
115+
extend-ignore = E203
116+
```
117+
118+
If you will now add a not needed import like `import os` then you should notice:
119+
120+
- Sorting the import statements on save
121+
- A new Issue on Problems View
122+
123+
## Debugging
124+
125+
Go to Run&Debug, get to `create a launch.json` - `Python Debugger` - `FastAPI` - defaults should be ok.
126+
127+
- start the app
128+
- set a breakpoint
129+
- check it
130+

docs/lession10.md

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
### Lession 10
2+
3+
# Monitoring
4+
5+
This is optional step as Prometheus is also included in OpenTelementry, which we will do next.
6+
7+
In this example we will only instrument FastApi. There is also an instrumention solution for Celery.
8+
9+
## Add dependencies
10+
11+
```bash
12+
poetry add prometheus_fastapi_instrumentator
13+
14+
```
15+
16+
17+
## Add Instrumentation
18+
19+
```python
20+
# dispo/dispo/main.py - add
21+
from prometheus_fastapi_instrumentator import Instrumentator as PrometheusInstrumentator
22+
23+
# add beyond app creation
24+
prom_instrumentator = PrometheusInstrumentator().instrument(app)
25+
prom_instrumentator.expose(app)
26+
27+
```
28+
29+
## Add Collector
30+
31+
32+
```docker
33+
# prometheus/Dockerfile
34+
FROM prom/prometheus
35+
ADD prometheus.yml /etc/prometheus/
36+
37+
```
38+
39+
```yaml
40+
global:
41+
scrape_interval: "15s"
42+
scrape_configs:
43+
- job_name: 'dispo'
44+
static_configs:
45+
- targets: ['dispo:80']
46+
47+
```
48+
49+
```yaml
50+
# deploy/prometheus.yaml
51+
52+
apiVersion: apps/v1
53+
kind: Deployment
54+
metadata:
55+
name: prometheus
56+
labels:
57+
app: prometheus
58+
spec:
59+
selector:
60+
matchLabels:
61+
app: prometheus
62+
template:
63+
metadata:
64+
labels:
65+
app: prometheus
66+
spec:
67+
containers:
68+
- name: prometheus
69+
image: myprometheus
70+
ports:
71+
- containerPort: 9090
72+
resources:
73+
limits:
74+
memory: 256Mi
75+
cpu: 400m
76+
---
77+
apiVersion: v1
78+
kind: Service
79+
metadata:
80+
name: prometheus
81+
spec:
82+
selector:
83+
app: prometheus
84+
ports:
85+
- protocol: TCP
86+
port: 9090
87+
targetPort: 9090
88+
```
89+
90+
```python
91+
# Tiltfile - add
92+
docker_build(
93+
'myprometheus',
94+
context='./myprometheus',
95+
dockerfile='./myprometheus/Dockerfile'
96+
)
97+
98+
k8s_yaml('deploy/prometheus.yaml')
99+
100+
k8s_resource(
101+
'prometheus',
102+
port_forwards=['9090:9090']
103+
)
104+
105+
```
106+
107+
108+
109+
## Testing
110+
111+
```bash
112+
curl http://localhost:8080/metrics
113+
114+
```
115+
116+
Prometheus: http://localhost:9090

0 commit comments

Comments
 (0)