Skip to content

Commit 8597037

Browse files
committed
Updated README.md and refactored opentelemetry docker-compose.
Signed-off-by: Pavel Kirilin <win10@list.ru>
1 parent 52a634c commit 8597037

4 files changed

Lines changed: 112 additions & 26 deletions

File tree

fastapi_template/template/{{cookiecutter.project_name}}/README.md

Lines changed: 83 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,112 @@
11
# {{cookiecutter.project_name}}
22

3+
This project was generated using fastapi_template.
34

4-
Start a project with:
5+
## Poetry
6+
7+
This project uses poetry. It's a modern dependency management
8+
tool.
9+
10+
To run the project use this set of commands:
511

612
```bash
7-
docker-compose -f deploy/docker-compose.yml --project-directory . up
13+
poetry install
14+
poetry run python -m {{cookiecutter.project_name}}
815
```
916

10-
If you want to develop in docker with autoreload, use this command:
17+
This will start the server on the configured host.
18+
19+
You can find swagger documentation at `/api/docs`.
20+
21+
You can read more about poetry here: https://python-poetry.org/
22+
23+
## Docker
24+
25+
You can start the project with docker using this command:
26+
27+
```bash
28+
docker-compose -f deploy/docker-compose.yml --project-directory . up --build
29+
```
30+
31+
If you want to develop in docker with autoreload add `-f deploy/docker-compose.dev.yml` to your docker command.
32+
Like this:
1133

1234
```bash
1335
docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.dev.yml --project-directory . up
1436
```
1537

16-
This command exposes application on port 8000, mounts current directory and enables autoreload.
38+
This command exposes the web application on port 8000, mounts current directory and enables autoreload.
1739

1840
But you have to rebuild image every time you modify `poetry.lock` or `pyproject.toml` with this command:
1941

2042
```bash
2143
docker-compose -f deploy/docker-compose.yml --project-directory . build
2244
```
2345

46+
{%- if cookiecutter.otlp_enabled == "True" %}
47+
## Opentelemetry
48+
49+
If you want to start your project with opentelemetry collector
50+
you can add `-f ./deploy/docker-compose.otlp.yml` to your docker command.
51+
52+
Like this:
53+
54+
```bash
55+
docker-compose -f deploy/docker-compose.yml -f deploy/docker-compose.otlp.yml --project-directory . up
56+
```
57+
58+
This command will start opentelemetry collector and jaeger.
59+
After sending a requests you can see traces in jaeger's UI
60+
at http://localhost:16686/.
61+
62+
This docker configuration is not supposed to be used in production.
63+
It's only for demo purpose.
64+
65+
You can read more about opentelemetry here: https://opentelemetry.io/
66+
{%- endif %}
67+
68+
## Configuration
69+
70+
This application can be configured with environment variables.
71+
72+
You can create `.env` file in the root directory and place all
73+
environment variables here.
74+
75+
All environment variabels should start with "{{cookiecutter.project_name | upper}}_" prefix.
76+
77+
For example if you see in your "{{cookiecutter.project_name}}/settings.py" a variable named like
78+
`random_parameter`, you should provide the "{{cookiecutter.project_name | upper}}_RANDOM_PARAMETER"
79+
variable to configure the value.
80+
81+
An exmaple of .env file:
82+
```bash
83+
{{cookiecutter.project_name | upper}}_RELOAD="True"
84+
{{cookiecutter.project_name | upper}}_PORT="8000"
85+
{{cookiecutter.project_name | upper}}_ENVIRONMENT="dev"
86+
```
87+
88+
You can read more about BaseSettings class here: https://pydantic-docs.helpmanual.io/usage/settings/
2489

2590
## Pre-commit
2691

2792
To install pre-commit simply run inside the shell:
2893
```bash
2994
pre-commit install
3095
```
96+
97+
pre-commit is very useful to check your code before publishing it.
98+
It's configured using .pre-commit-config.yaml file.
99+
100+
By default it runs:
101+
* black (formats your code);
102+
* mypy (validates types);
103+
* isort (sorts imports in all files);
104+
* flake8 (spots possibe bugs);
105+
* yesqa (removes useless `# noqa` comments).
106+
107+
108+
You can read more about pre-commit here: https://pre-commit.com/
109+
31110
{%- if cookiecutter.enable_kube == 'True' %}
32111

33112
## Kubernetes

fastapi_template/template/{{cookiecutter.project_name}}/conditional_files.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,18 @@
178178
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_mysql.sql"
179179
]
180180
},
181+
"Opentelemetry support": {
182+
"enabled": "{{cookiecutter.otlp_enabled}}",
183+
"resources": [
184+
"deploy/docker-compose.otlp.yml",
185+
"deploy/otel-collector-config.yml"
186+
]
187+
},
181188
"SQLite DB": {
182189
"enabled": "{{cookiecutter.db_info.name == 'sqlite'}}",
183190
"resources": [
184191
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/0_20210928165300_init_sqlite.sql",
185192
"{{cookiecutter.project_name}}/db_tortoise/migrations/models/1_20210928165300_init_dummy_sqlite.sql"
186193
]
187194
}
188-
}
195+
}

fastapi_template/template/{{cookiecutter.project_name}}/deploy/docker-compose.dev.yml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,24 +8,3 @@ services:
88
- .:/app/src/
99
environment:
1010
{{cookiecutter.project_name | upper}}_RELOAD: "True"
11-
{%- if cookiecutter.otlp_enabled == "True" %}
12-
{{cookiecutter.project_name | upper}}_OPENTELEMETRY_ENDPOINT: "http://otel-collector:4317"
13-
{% endif %}
14-
15-
{%- if cookiecutter.otlp_enabled == "True" %}
16-
otel-collector:
17-
image: otel/opentelemetry-collector-contrib:0.53.0
18-
volumes:
19-
- ./deploy/otel-collector-config.yml:/config.yml
20-
command: --config config.yml
21-
ports:
22-
# Collector's endpoint
23-
- "4317:4317"
24-
25-
jaeger:
26-
image: jaegertracing/all-in-one:1.35
27-
hostname: jaeger
28-
ports:
29-
# Jaeger UI
30-
- 16686:16686
31-
{%- endif %}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
services:
2+
api:
3+
environment:
4+
{{cookiecutter.project_name | upper}}_OPENTELEMETRY_ENDPOINT: "http://otel-collector:4317"
5+
6+
otel-collector:
7+
image: otel/opentelemetry-collector-contrib:0.53.0
8+
volumes:
9+
- ./deploy/otel-collector-config.yml:/config.yml
10+
command: --config config.yml
11+
ports:
12+
# Collector's endpoint
13+
- "4317:4317"
14+
15+
jaeger:
16+
image: jaegertracing/all-in-one:1.35
17+
hostname: jaeger
18+
ports:
19+
# Jaeger UI
20+
- 16686:16686
21+

0 commit comments

Comments
 (0)