├── .env.sample ├── .gitignore ├── README.md ├── alembic.ini ├── alertmanager └── config.yml ├── api_v1 ├── __init__.py ├── auth │ ├── __init__.py │ ├── backends.py │ ├── permissions.py │ ├── schemas.py │ ├── tokens.py │ └── views.py ├── exeptions.py ├── routers.py ├── tests │ ├── __init__.py │ ├── conftest.py │ └── test_users.py └── users │ ├── __init__.py │ ├── dao.py │ ├── exceptions.py │ ├── mixins.py │ ├── tasks.py │ ├── user_manager.py │ └── views.py ├── app_includes ├── __init__.py ├── logs_errors.py ├── middlewares.py └── prometheus.py ├── async_alembic ├── README ├── env.py ├── script.py.mako └── versions │ └── 2024_12_12_1935-ae5175a92845_add_user_model.py ├── caddy └── Caddyfile ├── config ├── __init__.py ├── alembic │ ├── __init__.py │ └── alembic_helper.py ├── celery │ ├── __init__.py │ └── connection.py ├── config.py ├── dao │ ├── __init__.py │ └── base_dao.py ├── database │ ├── __init__.py │ └── db_helper.py ├── models │ ├── __init__.py │ ├── base.py │ └── user.py └── setup_logs │ ├── __init__.py │ └── logging.py ├── docker-compose.yml ├── docker ├── celery │ ├── beat │ │ └── start │ ├── flower │ │ └── start │ └── worker │ │ └── start └── fastapi │ ├── Dockerfile │ ├── entrypoint │ └── start ├── grafana └── provisioning │ ├── dashboards │ ├── dashboard.yml │ ├── docker_containers.json │ ├── docker_host.json │ ├── monitor_services.json │ └── nginx_container.json │ └── datasources │ └── datasource.yml ├── main.py ├── prometheus ├── alert.rules └── prometheus.yml └── pyproject.toml /.env.sample: -------------------------------------------------------------------------------- 1 | # ==================APP_SETTINGS================== 2 | DEBUG=1 3 | SECRET=SOMESECRET 4 | # ==================DATA_BASE================== 5 | POSTGRES_DB=analizer 6 | POSTGRES_PASSWORD= 7 | DB_ENGINE=postgresql+asyncpg 8 | DB_NAME=analizer 9 | DB_USER=postgres 10 | DB_PASSWORD= 11 | DB_HOST=db 12 | DB_PORT=5432 13 | # ==================RABBIT_MQ================== 14 | RMQ_HOST=rabbitmq 15 | RMQ_PORT=5672 16 | RABBITMQ_DEFAULT_USER=guest 17 | RABBITMQ_DEFAULT_PASS=guest 18 | # ==================REDIS================== 19 | REDIS_HOST=redis 20 | REDIS_PORT=6379 21 | # ==================ORIGINS================== 22 | CURRENT_ORIGIN=http://localhost:8080 23 | # ==================GRAFANA================== 24 | GF_SECURITY_ADMIN_USER=admin 25 | GF_SECURITY_ADMIN_PASSWORD=admin 26 | # ==================CADDY================== 27 | ADMIN_USER=admin 28 | ADMIN_PASSWORD=admin 29 | # ==================TESTS================== 30 | TEST_POSTGRES_DB=test_analizer 31 | TEST_POSTGRES_PASSWORD= 32 | TEST_DB_ENGINE=postgresql+asyncpg 33 | TEST_DB_NAME=test_analizer 34 | TEST_DB_USER=postgres 35 | TEST_DB_PASSWORD= 36 | TEST_DB_HOST=test_db 37 | TEST_DB_PORT=5431 -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Byte-compiled / optimized / DLL files 2 | __pycache__/ 3 | *.py[cod] 4 | *$py.class 5 | 6 | # C extensions 7 | *.so 8 | 9 | # Distribution / packaging 10 | .Python 11 | build/ 12 | develop-eggs/ 13 | dist/ 14 | downloads/ 15 | eggs/ 16 | .eggs/ 17 | lib/ 18 | lib64/ 19 | parts/ 20 | sdist/ 21 | var/ 22 | wheels/ 23 | share/python-wheels/ 24 | *.egg-info/ 25 | .installed.cfg 26 | *.egg 27 | MANIFEST 28 | database.ini 29 | 30 | # PyInstaller 31 | # Usually these files are written by a python script from a template 32 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 33 | *.manifest 34 | *.spec 35 | 36 | # Installer logs 37 | pip-log.txt 38 | pip-delete-this-directory.txt 39 | 40 | # Unit test / coverage reports 41 | htmlcov/ 42 | .tox/ 43 | .nox/ 44 | .coverage 45 | .coverage.* 46 | .cache 47 | nosetests.xml 48 | coverage.xml 49 | *.cover 50 | *.py,cover 51 | .hypothesis/ 52 | .pytest_cache/ 53 | cover/ 54 | 55 | # Translations 56 | *.mo 57 | *.pot 58 | 59 | # Django stuff: 60 | *.log 61 | local_settings.py 62 | db.sqlite3 63 | db.sqlite3-journal 64 | .migrations 65 | media/ 66 | 67 | # Flask stuff: 68 | instance/ 69 | .webassets-cache 70 | 71 | # Scrapy stuff: 72 | .scrapy 73 | 74 | # Sphinx documentation 75 | docs/_build/ 76 | 77 | # PyBuilder 78 | .pybuilder/ 79 | target/ 80 | 81 | # Jupyter Notebook 82 | .ipynb_checkpoints 83 | 84 | # IPython 85 | profile_default/ 86 | ipython_config.py 87 | 88 | # pyenv 89 | # For a library or package, you might want to ignore these files since the code is 90 | # intended to run in multiple environments; otherwise, check them in: 91 | # .python-version 92 | 93 | # pipenv 94 | # According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. 95 | # However, in case of collaboration, if having platform-specific dependencies or dependencies 96 | # having no cross-platform support, pipenv may install dependencies that don't work, or not 97 | # install all needed dependencies. 98 | #Pipfile.lock 99 | 100 | # poetry 101 | # Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. 102 | # This is especially recommended for binary packages to ensure reproducibility, and is more 103 | # commonly ignored for libraries. 104 | # https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control 105 | poetry.lock 106 | 107 | # pdm 108 | # Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. 109 | #pdm.lock 110 | # pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it 111 | # in version control. 112 | # https://pdm.fming.dev/#use-with-ide 113 | .pdm.toml 114 | 115 | # PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm 116 | __pypackages__/ 117 | 118 | # Celery stuff 119 | celerybeat-schedule 120 | celerybeat.pid 121 | 122 | # SageMath parsed files 123 | *.sage.py 124 | 125 | # Environments 126 | .env 127 | .venv 128 | env/ 129 | venv/ 130 | ENV/ 131 | env.bak/ 132 | venv.bak/ 133 | 134 | # Spyder project settings 135 | .spyderproject 136 | .spyproject 137 | 138 | # Rope project settings 139 | .ropeproject 140 | 141 | # mkdocs documentation 142 | /site 143 | 144 | # mypy 145 | .mypy_cache/ 146 | .dmypy.json 147 | dmypy.json 148 | 149 | # Pyre type checker 150 | .pyre/ 151 | 152 | # pytype static type analyzer 153 | .pytype/ 154 | 155 | # Cython debug symbols 156 | cython_debug/ 157 | 158 | # PyCharm 159 | # JetBrains specific template is maintained in a separate JetBrains.gitignore that can 160 | # be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore 161 | # and can be added to the global gitignore or merged into this file. For a more nuclear 162 | # option (not recommended) you can uncomment the following to ignore the entire idea folder. 163 | .idea/ 164 | 165 | #Vscode 166 | .vscode/* 167 | !.vscode/settings.json 168 | !.vscode/tasks.json 169 | .vscode/launch.json 170 | !.vscode/extensions.json 171 | !.vscode/*.code-snippets 172 | 173 | # Local History for Visual Studio Code 174 | .history/ 175 | 176 | # Built Visual Studio Code Extensions 177 | *.vsix 178 | */*.ini 179 | dump.rdb 180 | .vscode 181 | test.py 182 | 183 | # certs 184 | certs/jwt* -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Title 2 | 3 | Данный шаблон был разработан для одной цели - облегчения и повышения качества 4 | выполненых тестовых заданий в рамках **FastAPI**. 5 | 6 | # Quick start 7 | 8 | Для тех кто уже знаком с реализацией и всеми деталями - могут приступить к установке. 9 | 10 | ## GIT 11 | 12 | Для безопасного выполнения копирования из GitHub Необходимо сделать следующее: 13 | 14 | - Клонировать Git репозиторий 15 | 16 | ```bash 17 | git clone https://github.com/sumaro2101/BaseFastAPI your_name_dir 18 | ``` 19 | 20 | Где - адресс репозитория, 21 | `your_name_dir` - имя папки для клонирования 22 | 23 | - Удалить .git из клонированного репозитория 24 | 25 | ```bash 26 | rm -r .git 27 | ``` 28 | 29 | или если отказывает в доступе можете - переместить 30 | 31 | ```bash 32 | mv .git ../git 33 | ``` 34 | 35 | - Инициализировать свой `Git` 36 | 37 | ```bash 38 | git init 39 | ``` 40 | 41 | - Сделать свой коммит 42 | 43 | ```bash 44 | git add . 45 | git commit -m 'base commit' 46 | ``` 47 | 48 | - Привязать текущий Git к вашему удаленному репозиторию 49 | 50 | ```bash 51 | git remove add origin some_url_or_ssh_repo 52 | ``` 53 | 54 | - Отправить на удаленный репозиторий текущий репозиторий 55 | 56 | ```bash 57 | git push -u origin main 58 | ``` 59 | 60 | ## Enviroments 61 | 62 | Необходимо заполнить **.env.sample** и в последствии перемеиновать его в **.env** 63 | 64 | ```python 65 | # .env.sample 66 | POSTGRES_PASSWORD=password # Пароль от базы данных (Настройка) 67 | DB_PASSWORD=password # Пароль от базы данных (Использование) 68 | TEST_POSTGRES_PASSWORD=password # Пароль от тестовой базы данный (Настройка) 69 | TEST_DB_PASSWORD=password # Пароль от тестовой базы данных (Использование) 70 | ``` 71 | 72 | ## Docker 73 | 74 | Шаблон находится под системой управления и контеризации - **Docker**. 75 | Если у вас нет Docker - вы можете установить его с официального сайта: [Docker](https://www.docker.com/get-started/) 76 | 77 | - Вам необходимо сделать "Билд" 78 | 79 | ```bash 80 | docker compose build 81 | ``` 82 | 83 | - Вам необходимо запустить окружение 84 | 85 | ```bash 86 | docker compose up 87 | ``` 88 | 89 | - После успешного запуска приложение будет доступно по адрессу: 90 | - Grafana: 91 | - Flower: 92 | 93 | # View 94 | 95 | Обзор и детали данного шаблона 96 | 97 | ## Users 98 | 99 | В данном шаблоне реализован CRUD для пользователя с помощью библиотеки 100 | [fastapi-users](https://fastapi-users.github.io/fastapi-users/latest/) 101 | 102 | ### End-points 103 | 104 | `USERS` 105 | 106 | - GET 107 | Получение текущего пользователя 108 | - PATCH 109 | Изменение текущего пользователя 110 | - GET 111 | Получение пользователя по ID Необходимо иметь права доступа уровня `admin` 112 | - PATCH 113 | Изменение пользоватея по ID Необходимо иметь права доступа уровня `admin` 114 | - DELETE 115 | Удаление пользоватея по ID Необходимо иметь права доступа уровня `admin` 116 | 117 | `AUTH` 118 | 119 | - POST 120 | Аутентификация в систему с последующим получением JWT для авторизации 121 | - POST 122 | Вызод из системы 123 | - POST 124 | Регистрация нового пользователя 125 | - POST 126 | Получение токена для верификации. `ВАЖНО` Читайте ниже, есть нюанс. 127 | - POST 128 | Верификация пользователя по токену 129 | - POST 130 | Получение токена для изменения пароль. `ВАЖНО` Читайте ниже, есть нюанс. 131 | - POST 132 | Изменения пароля посредством токена 133 | 134 | `ВАЖНО` 135 | 136 | - POST 137 | Должен осуществлять логику отправки токена по эмеилу либо другим способом. 138 | В данный момент отправка осуществляется через `консоль`. 139 | Для деталей смотрите миксин `api_v1/users/mixins/ActionUserManagerMixin` 140 | - POST 141 | Должен осуществлять логику отправки токена по эмеилу либо другим способом. 142 | В данный момент отправка осуществляется через `консоль`. 143 | Для деталей смотрите миксин `api_v1/users/mixins/ActionUserManagerMixin` 144 | 145 | ### UserManager 146 | 147 | UserManager Это специальная обвертка для `SQLAlchemyUserDatabase` который в свою 148 | очередь вмещает в себя класс модели `User` и сессию `AsyncSession` 149 | Смотрите класс `api_v1/users/user_manager/UserManager` 150 | 151 | ### Transport 152 | 153 | В fastapi-users есть 2 вида транспорта: 154 | 155 | - [Bearer](https://fastapi-users.github.io/fastapi-users/latest/configuration/authentication/transports/bearer/) 156 | - [Cookie](https://fastapi-users.github.io/fastapi-users/latest/configuration/authentication/transports/cookie/) 157 | 158 | В шаблоне реализован Bearer способ: 159 | 160 | ```python 161 | from fastapi_users.authentication import BearerTransport 162 | 163 | from config import settings 164 | 165 | 166 | bearer_transport = BearerTransport(settings.CURRENT_ORIGIN + 167 | settings.API_PREFIX + 168 | settings.JWT.JWT_PATH + 169 | '/login') 170 | ``` 171 | 172 | ### Strategy 173 | 174 | В fastapi-users есть 3 вида стратегии: 175 | 176 | - [Database](https://fastapi-users.github.io/fastapi-users/latest/configuration/authentication/strategies/database/) 177 | - [JWT](https://fastapi-users.github.io/fastapi-users/latest/configuration/authentication/strategies/jwt/) 178 | - [Redis](https://fastapi-users.github.io/fastapi-users/latest/configuration/authentication/strategies/redis/) 179 | 180 | В Шаблоне реализован JWT способ: 181 | 182 | ```python 183 | from fastapi_users.authentication import JWTStrategy 184 | 185 | from config import settings 186 | 187 | 188 | def get_jwt_strategy() -> JWTStrategy: 189 | return JWTStrategy( 190 | secret=settings.JWT.SECRET, 191 | lifetime_seconds=settings.JWT.RESET_LIFESPAN_TOKEN_SECONDS, 192 | ) 193 | ``` 194 | 195 | ### Backend 196 | 197 | auth_backend собирает `Transport` и `Strategy` воединно в `AuthenticationBackend` 198 | Этот класс необходим будет для: 199 | 200 | - `Authenticator` 201 | - `FastAPIUsers` 202 | 203 | ```python 204 | from fastapi_users.authentication import AuthenticationBackend 205 | 206 | from config import settings 207 | 208 | 209 | auth_backend = AuthenticationBackend( 210 | name=settings.JWT.NAME, 211 | transport=bearer_transport, 212 | get_strategy=get_jwt_strategy, 213 | ) 214 | ``` 215 | 216 | ### Authenticator 217 | 218 | Authenticator необходим для `Callback[Depenpency]` пользователей, 219 | применяется для вытаскивания текущего пользователя. 220 | 221 | ```python 222 | from fastapi import Depends 223 | from fastapi_users.authentication import Authenticator 224 | 225 | from config.models import User 226 | 227 | 228 | authenticator = Authenticator( 229 | (auth_backend,), 230 | get_user_manager, 231 | ) 232 | 233 | active_user = authenticator.current_user( 234 | active=True, 235 | verified=True, 236 | ) 237 | 238 | async def get_user(user: User = Depends(active_user)) -> User: 239 | return user 240 | ``` 241 | 242 | ### FastAPIUsers 243 | 244 | FastAPIUsers применяется в итоговом выводе End-points в API 245 | 246 | ```python 247 | from fastapi import APIRouter 248 | from fastapi_users import FastAPIUsers 249 | 250 | from api_v1.auth.schemas import UserRead, UserUpdate 251 | 252 | 253 | fastapi_users = FastAPIUsers[User, int]( 254 | get_user_manager, 255 | (auth_backend,) 256 | ) 257 | 258 | router = APIRouter() 259 | 260 | router.include_router(fastapi_users.get_users_router(UserRead, UserUpdate), 261 | tags=['Users'], 262 | prefix='/users', 263 | ) 264 | ``` 265 | 266 | По итогу будет добавлены новые End-points в API 267 | 268 | ### Permissions 269 | 270 | С помощью Authenticator возможно осуществлять права доступа к End-points 271 | `api_v1/auth/permissions.py` содержит несколько `Callback` функции для permissions 272 | По желанию вы можете подолнять их. 273 | 274 | ```python 275 | from fastapi import APIRouter 276 | 277 | from api_v1.auth import active_user 278 | 279 | 280 | router = APIRouter() 281 | 282 | 283 | @router.get(path='/get-user') 284 | async def get_user(user: User = Depends(active_user)) -> User: 285 | return user 286 | ``` 287 | 288 | ## Найболее используемые 289 | 290 | Найболее используемые конструкции с которыми приходится часто взаимодействовать. 291 | 292 | ### Registration Routers 293 | 294 | - В каждом приложений необходимо инициализировать router 295 | 296 | ```python 297 | # api/users/views.py 298 | from fastapi import APIRouter 299 | 300 | 301 | router = APIRouter( 302 | prefix='/users', 303 | tags=['Users'], 304 | ) 305 | ``` 306 | 307 | - Затем зарегистрировать роутер 308 | 309 | ```python 310 | # api_v1/routers.py 311 | from api_v1.users.views import router as users 312 | from config import settings 313 | 314 | 315 | # В этой функции нужно по порядку регистрировать routers 316 | def register_routers(app: FastAPI) -> None: 317 | app.include_router( 318 | router=users, 319 | prefix=settings.API_PREFIX, 320 | ) 321 | ``` 322 | 323 | После регистрации данные маршруты будут доступны. 324 | 325 | ### Registration Logs 326 | 327 | - Логи захватывают все исключения возникшие в системе 328 | и с помошью диспечиризации распределяется по нужным **file.log** 329 | 330 | ```python 331 | # app_includes/logs_errors.py 332 | from fastapi import FastAPI 333 | from fastapi.responses import JSONResponse 334 | 335 | from api_v1.exeptions import ValidationError 336 | 337 | 338 | # В данной функции регистрируются все исключения для захватывания Логами 339 | def register_errors(app: FastAPI) -> None: 340 | @app.exception_handler(ValidationError) 341 | async def validation_error_handler( 342 | request: Request, 343 | exc: ValidationError, 344 | ): 345 | logger.opt(exception=True).warning(exc) 346 | response = dict( 347 | status=False, 348 | error_code=exc.status_code, 349 | message=exc.detail, 350 | ) 351 | return JSONResponse(response) 352 | ``` 353 | 354 | - Если вы пишете пользовательское исключение например: 355 | 356 | ```python 357 | from starlette.exceptions import HTTPException 358 | 359 | 360 | class ValidationError(HTTPException): 361 | 362 | pass 363 | ``` 364 | 365 | То вам нужно его зарегистрировать как было показанно выше, 366 | иначе logs не смогут выявить данное исключение и данные будут утеряны. 367 | 368 | ### Registration Middlaware 369 | 370 | - Для регистрации Middlaware вам нужно добавить его в функцию 371 | 372 | ```python 373 | from fastapi.middleware.cors import CORSMiddleware 374 | from fastapi import FastAPI 375 | 376 | from config import settings 377 | 378 | 379 | # Данная функция регистрирует все middleware 380 | def register_middlewares(app: FastAPI) -> None: 381 | app.add_middleware( 382 | CORSMiddleware, 383 | allow_origins=[ 384 | settings.CURRENT_ORIGIN, 385 | ], 386 | allow_credentials=True, 387 | allow_methods=['*'], 388 | allow_headers=['*'], 389 | ) 390 | ``` 391 | 392 | - При появлении новых middleware добавляйте их по порядку в эту функцию 393 | 394 | ### DAO 395 | 396 | DAO необходим для CRUD manager любой модели. 397 | `config.dao.base_dao.BaseDAO` 398 | 399 | Для опеределения DAO вашей модели нужно наследовать `BaseDAO`, а Затем 400 | переопределить поле класса `model` 401 | 402 | ```python 403 | from config.dao import BaseDAO 404 | from config.models import Product 405 | 406 | 407 | class ProductDAO(BaseDAO): 408 | model = Product 409 | ``` 410 | 411 | ### Celery 412 | 413 | - Для регистрации task вам нужно создать файл с именем **tasks.py** в вашем приложении: 414 | 415 | ```python 416 | # api_v1/users/tasks.py 417 | from config import celery_app 418 | import asyncio 419 | 420 | 421 | @celery_app.task 422 | async def time_sleep_task(): 423 | """ 424 | Тестовая задача для Celery 425 | """ 426 | await asyncio.sleep(2.0) 427 | return 'Task is done' 428 | ``` 429 | 430 | - Затем добавить этот файл в список пакетов Celery 431 | 432 | ```python 433 | # confin.celery.connection.py 434 | 435 | app = Celery(__name__) 436 | app.conf.broker_url = settings.rabbit.broker_url 437 | # Регистрация до окружения где находится tasks.py 438 | app.autodiscover_tasks(packages=['api_v1.users']) 439 | ``` 440 | 441 | - После этих действий ваша task будет зарегистрирована 442 | 443 | ### Test 444 | 445 | - Для тестирования у вас есть тестовая база данных, а так же 446 | уже инициализированный отдельный клиент. 447 | Cпособ реализации в **api_v1/tests/conftest.py** 448 | - Что бы написать тестовую функцию которой нужен доступ к API, 449 | вам нужно использовать fixture - client. 450 | 451 | > [!NOTE] 452 | > Для асинхронных тестов используйте **@pytest.mark.asyncio** 453 | 454 | ```python 455 | # api_v1.tests.test_users.py 456 | import pytest 457 | 458 | 459 | @pytest.mark.asyncio 460 | async def test_get_user_error(client: AsyncClient): 461 | response = await client.get( 462 | '/users/get', 463 | ) 464 | assert response.status_code == 400 465 | ``` 466 | 467 | - Для запуска используйте команду 468 | 469 | ```bash 470 | pytest 471 | ``` 472 | -------------------------------------------------------------------------------- /alembic.ini: -------------------------------------------------------------------------------- 1 | # A generic, single database configuration. 2 | 3 | [alembic] 4 | # path to migration scripts. 5 | # Use forward slashes (/) also on windows to provide an os agnostic path 6 | script_location = async_alembic 7 | 8 | # template used to generate migration file names; The default value is %%(rev)s_%%(slug)s 9 | # Uncomment the line below if you want the files to be prepended with date and time 10 | file_template = %%(year)d_%%(month).2d_%%(day).2d_%%(hour).2d%%(minute).2d-%%(rev)s_%%(slug)s 11 | 12 | # sys.path path, will be prepended to sys.path if present. 13 | # defaults to the current working directory. 14 | prepend_sys_path = . 15 | 16 | # timezone to use when rendering the date within the migration file 17 | # as well as the filename. 18 | # If specified, requires the python>=3.9 or backports.zoneinfo library. 19 | # Any required deps can installed by adding `alembic[tz]` to the pip requirements 20 | # string value is passed to ZoneInfo() 21 | # leave blank for localtime 22 | # timezone = 23 | 24 | # max length of characters to apply to the "slug" field 25 | # truncate_slug_length = 40 26 | 27 | # set to 'true' to run the environment during 28 | # the 'revision' command, regardless of autogenerate 29 | # revision_environment = false 30 | 31 | # set to 'true' to allow .pyc and .pyo files without 32 | # a source .py file to be detected as revisions in the 33 | # versions/ directory 34 | # sourceless = false 35 | 36 | # version location specification; This defaults 37 | # to async_alembic/versions. When using multiple version 38 | # directories, initial revisions must be specified with --version-path. 39 | # The path separator used here should be the separator specified by "version_path_separator" below. 40 | # version_locations = %(here)s/bar:%(here)s/bat:async_alembic/versions 41 | 42 | # version path separator; As mentioned above, this is the character used to split 43 | # version_locations. The default within new alembic.ini files is "os", which uses os.pathsep. 44 | # If this key is omitted entirely, it falls back to the legacy behavior of splitting on spaces and/or commas. 45 | # Valid values for version_path_separator are: 46 | # 47 | # version_path_separator = : 48 | # version_path_separator = ; 49 | # version_path_separator = space 50 | # version_path_separator = newline 51 | version_path_separator = os # Use os.pathsep. Default configuration used for new projects. 52 | 53 | # set to 'true' to search source files recursively 54 | # in each "version_locations" directory 55 | # new in Alembic version 1.10 56 | # recursive_version_locations = false 57 | 58 | # the output encoding used when revision files 59 | # are written from script.py.mako 60 | # output_encoding = utf-8 61 | 62 | sqlalchemy.url = driver://user:pass@localhost/dbname 63 | 64 | 65 | [post_write_hooks] 66 | # post_write_hooks defines scripts or Python functions that are run 67 | # on newly generated revision scripts. See the documentation for further 68 | # detail and examples 69 | 70 | # format using "black" - use the console_scripts runner, against the "black" entrypoint 71 | hooks = black 72 | black.type = console_scripts 73 | black.entrypoint = black 74 | black.options = -l 89 REVISION_SCRIPT_FILENAME 75 | 76 | # lint with attempts to fix using "ruff" - use the exec runner, execute a binary 77 | # hooks = ruff 78 | # ruff.type = exec 79 | # ruff.executable = %(here)s/.venv/bin/ruff 80 | # ruff.options = --fix REVISION_SCRIPT_FILENAME 81 | 82 | # Logging configuration 83 | [loggers] 84 | keys = root,sqlalchemy,alembic 85 | 86 | [handlers] 87 | keys = console 88 | 89 | [formatters] 90 | keys = generic 91 | 92 | [logger_root] 93 | level = WARNING 94 | handlers = console 95 | qualname = 96 | 97 | [logger_sqlalchemy] 98 | level = WARNING 99 | handlers = 100 | qualname = sqlalchemy.engine 101 | 102 | [logger_alembic] 103 | level = INFO 104 | handlers = 105 | qualname = alembic 106 | 107 | [handler_console] 108 | class = StreamHandler 109 | args = (sys.stderr,) 110 | level = NOTSET 111 | formatter = generic 112 | 113 | [formatter_generic] 114 | format = %(levelname)-5.5s [%(name)s] %(message)s 115 | datefmt = %H:%M:%S 116 | -------------------------------------------------------------------------------- /alertmanager/config.yml: -------------------------------------------------------------------------------- 1 | route: 2 | receiver: 'slack' 3 | 4 | receivers: 5 | - name: 'slack' 6 | slack_configs: 7 | - send_resolved: true 8 | text: "{{ .CommonAnnotations.description }}" 9 | username: 'Prometheus' 10 | channel: '#prometheus' 11 | api_url: https://hooks.slack.com/services/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 -------------------------------------------------------------------------------- /api_v1/__init__.py: -------------------------------------------------------------------------------- 1 | from .routers import register_routers 2 | 3 | 4 | __all__ = ('register_routers',) 5 | -------------------------------------------------------------------------------- /api_v1/auth/__init__.py: -------------------------------------------------------------------------------- 1 | from .permissions import active_user, superuser 2 | from .backends import authenticator, auth_backend 3 | 4 | 5 | __all__ = ('active_user', 6 | 'superuser', 7 | 'authenticator', 8 | 'auth_backend', 9 | ) 10 | -------------------------------------------------------------------------------- /api_v1/auth/backends.py: -------------------------------------------------------------------------------- 1 | from fastapi_users.authentication import ( 2 | BearerTransport, 3 | AuthenticationBackend, 4 | Authenticator, 5 | JWTStrategy, 6 | ) 7 | 8 | from api_v1.users.user_manager import get_user_manager 9 | from config import settings 10 | 11 | 12 | bearer_transport = BearerTransport(settings.CURRENT_ORIGIN + 13 | settings.API_PREFIX + 14 | settings.JWT.JWT_PATH + 15 | '/login') 16 | 17 | 18 | def get_jwt_strategy() -> JWTStrategy: 19 | return JWTStrategy( 20 | secret=settings.JWT.SECRET, 21 | lifetime_seconds=settings.JWT.RESET_LIFESPAN_TOKEN_SECONDS, 22 | ) 23 | 24 | 25 | auth_backend = AuthenticationBackend( 26 | name=settings.JWT.NAME, 27 | transport=bearer_transport, 28 | get_strategy=get_jwt_strategy, 29 | ) 30 | 31 | authenticator = Authenticator( 32 | (auth_backend,), 33 | get_user_manager, 34 | ) 35 | -------------------------------------------------------------------------------- /api_v1/auth/permissions.py: -------------------------------------------------------------------------------- 1 | from .backends import authenticator 2 | 3 | 4 | active_user = authenticator.current_user( 5 | active=True, 6 | verified=True, 7 | ) 8 | 9 | superuser = authenticator.current_user( 10 | active=True, 11 | verified=True, 12 | superuser=True, 13 | ) 14 | -------------------------------------------------------------------------------- /api_v1/auth/schemas.py: -------------------------------------------------------------------------------- 1 | from fastapi_users import schemas 2 | 3 | 4 | class UserRead(schemas.BaseUser[int]): 5 | """ 6 | Схема пользователя 7 | При добавлении полей в модель, так же 8 | необходимо добавить поля - тут! 9 | """ 10 | 11 | pass 12 | 13 | 14 | class UserCreate(schemas.BaseUserCreate): 15 | """ 16 | Схема создания пользователя 17 | При добавлении полей в модель, так же 18 | необходимо добавить поля - тут! 19 | """ 20 | 21 | pass 22 | 23 | 24 | class UserUpdate(schemas.BaseUserUpdate): 25 | """ 26 | Схема обновления пользователя 27 | При добавлении полей в модель, так же 28 | необходимо добавить поля - тут! 29 | """ 30 | 31 | pass 32 | -------------------------------------------------------------------------------- /api_v1/auth/tokens.py: -------------------------------------------------------------------------------- 1 | from fastapi_users.authentication.strategy import JWTStrategy 2 | from fastapi_users.jwt import generate_jwt 3 | 4 | 5 | class JWTStrategyWithEmail(JWTStrategy): 6 | """ 7 | JWT стратегия с дополнительными полями 8 | """ 9 | 10 | async def write_token(self, user): 11 | data = {"sub": str(user.id), 12 | "aud": self.token_audience, 13 | "email": str(user.email)} 14 | return generate_jwt( 15 | data, 16 | self.encode_key, 17 | self.lifetime_seconds, 18 | algorithm=self.algorithm, 19 | ) 20 | -------------------------------------------------------------------------------- /api_v1/auth/views.py: -------------------------------------------------------------------------------- 1 | from fastapi import APIRouter 2 | from fastapi_users import FastAPIUsers 3 | 4 | from config.models import User 5 | from config import settings 6 | from .backends import auth_backend 7 | from .schemas import UserRead, UserCreate 8 | from api_v1.users.user_manager import get_user_manager 9 | 10 | 11 | fastapi_users = FastAPIUsers[User, int]( 12 | get_user_manager, 13 | (auth_backend,) 14 | ) 15 | 16 | 17 | router = APIRouter() 18 | router.include_router(fastapi_users.get_auth_router(auth_backend), 19 | tags=['Auth'], 20 | prefix=settings.JWT.JWT_PATH, 21 | ) 22 | router.include_router(fastapi_users.get_register_router(UserRead, UserCreate), 23 | tags=['Auth'], 24 | prefix='/auth', 25 | ) 26 | router.include_router(fastapi_users.get_verify_router(UserRead), 27 | tags=['Auth'], 28 | prefix='/auth', 29 | ) 30 | router.include_router(fastapi_users.get_reset_password_router(), 31 | tags=['Auth'], 32 | prefix='/auth', 33 | ) 34 | -------------------------------------------------------------------------------- /api_v1/exeptions.py: -------------------------------------------------------------------------------- 1 | from starlette.exceptions import HTTPException 2 | 3 | 4 | class ValidationError(HTTPException): 5 | """ 6 | Исключение вызванное проблемами с валидацией 7 | """ 8 | 9 | pass 10 | -------------------------------------------------------------------------------- /api_v1/routers.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI 2 | 3 | from config import settings 4 | from api_v1.users.views import router as users 5 | from api_v1.auth.views import router as auth 6 | 7 | 8 | def register_routers(app: FastAPI) -> None: 9 | """ 10 | Функция по регистрации роутеров 11 | 12 | ## Args: 13 | app (FastAPI): ASGI приложение. 14 | 15 | ## Returns: 16 | None 17 | 18 | ## Example 19 | ```python 20 | from fastapi import FastAPI 21 | 22 | from config import settings 23 | from api_v1.api_xml.views import router as xml 24 | from api_v1.users.views import router as users 25 | 26 | 27 | def register_routers(app: FastAPI) -> None: 28 | app.include_router( 29 | router=xml, 30 | prefix=settings.API_PREFIX, 31 | ) 32 | # Новый роутер 33 | app.include_router( 34 | router=users, 35 | prefix=settings.API_PREFIX, 36 | ) 37 | ``` 38 | """ 39 | app.include_router( 40 | router=users, 41 | prefix=settings.API_PREFIX, 42 | ) 43 | 44 | app.include_router( 45 | router=auth, 46 | prefix=settings.API_PREFIX, 47 | ) 48 | -------------------------------------------------------------------------------- /api_v1/tests/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sumaro2101/BaseFastAPI/292aae5f2ab3959d5116a3cc9f8ec0e5dcbfd822/api_v1/tests/__init__.py -------------------------------------------------------------------------------- /api_v1/tests/conftest.py: -------------------------------------------------------------------------------- 1 | import httpx 2 | import pytest_asyncio 3 | import pytest 4 | 5 | from typing import Any, AsyncGenerator 6 | from asgi_lifespan import LifespanManager 7 | from fastapi import FastAPI 8 | from sqlalchemy.pool import NullPool 9 | 10 | from config import test_connection, settings 11 | from config import db_connection 12 | from config.models.base import Base 13 | from main import app 14 | 15 | 16 | db_setup = test_connection( 17 | settings.test_db.url, 18 | poolclass=NullPool, 19 | ) 20 | 21 | 22 | async def override_get_async_session(): 23 | async with db_setup.session() as session: 24 | yield session 25 | 26 | 27 | @pytest_asyncio.fixture(scope='session', autouse=True) 28 | async def test_app() -> AsyncGenerator[LifespanManager, Any]: 29 | app.dependency_overrides[db_connection.session_geter] = override_get_async_session 30 | 31 | async with LifespanManager(app) as manager: 32 | yield manager.app 33 | 34 | 35 | @pytest_asyncio.fixture(scope='session') 36 | async def client(test_app: FastAPI) -> AsyncGenerator[httpx.AsyncClient, Any]: 37 | current_home = settings.CURRENT_ORIGIN 38 | current_api = settings.API_PREFIX 39 | async with httpx.AsyncClient( 40 | transport=httpx.ASGITransport( 41 | app=app, 42 | ), 43 | base_url=current_home + current_api, 44 | ) as client: 45 | async with db_setup.engine.begin() as conn: 46 | await conn.run_sync(Base.metadata.create_all) 47 | yield client 48 | async with db_setup.engine.begin() as conn: 49 | await conn.run_sync(Base.metadata.drop_all) 50 | 51 | 52 | @pytest_asyncio.fixture() 53 | async def get_async_session(): 54 | async with db_setup.session() as session: 55 | yield session 56 | 57 | 58 | @pytest.fixture() 59 | def user_test_data(): 60 | data = { 61 | "email": "user@example.com", 62 | "password": "password", 63 | "is_active": True, 64 | "is_superuser": False, 65 | "is_verified": False, 66 | } 67 | return data 68 | -------------------------------------------------------------------------------- /api_v1/tests/test_users.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from httpx import AsyncClient 4 | 5 | 6 | @pytest.mark.asyncio 7 | async def test_get_user_error(client: AsyncClient, user_test_data): 8 | response = await client.post( 9 | '/auth/register', 10 | json=user_test_data, 11 | ) 12 | assert response.status_code == 201 13 | -------------------------------------------------------------------------------- /api_v1/users/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sumaro2101/BaseFastAPI/292aae5f2ab3959d5116a3cc9f8ec0e5dcbfd822/api_v1/users/__init__.py -------------------------------------------------------------------------------- /api_v1/users/dao.py: -------------------------------------------------------------------------------- 1 | from config.models import User 2 | from config.dao import BaseDAO 3 | 4 | 5 | class UserDAO(BaseDAO): 6 | """ 7 | DAO для CRUD пользователя 8 | """ 9 | model = User 10 | -------------------------------------------------------------------------------- /api_v1/users/exceptions.py: -------------------------------------------------------------------------------- 1 | from fastapi_users.exceptions import FastAPIUsersException 2 | from starlette.exceptions import HTTPException 3 | 4 | 5 | class PasswordNotValidError(HTTPException): 6 | """ 7 | Исключение не валидного пароля 8 | """ 9 | 10 | pass 11 | 12 | 13 | class UserNotVerified(FastAPIUsersException): 14 | """ 15 | Исключение не верифицинованного пользователя 16 | """ 17 | 18 | pass 19 | -------------------------------------------------------------------------------- /api_v1/users/mixins.py: -------------------------------------------------------------------------------- 1 | from fastapi import status 2 | from loguru import logger 3 | 4 | from .exceptions import PasswordNotValidError 5 | 6 | 7 | class ActionUserManagerMixin: 8 | """ 9 | Миксин для поддержки методов UserManager которые отвечают 10 | за дополнительную логику 11 | """ 12 | async def on_after_request_verify(self, user, token, request): 13 | """ 14 | Здесь должна быть отправка на E-mail `token` который уже 15 | вмещает в себя дополнительное поле `email` для верификации. 16 | Этот токен нужно вписать в end-point `verify`. 17 | 18 | Для эмуляции отправки сюда в консоль выведется сам токен. 19 | 20 | Если в консоли вы не видете токена значит: 21 | - Вы уже верифицированы 22 | - Не правильные данные 23 | - Не активный пользователь 24 | 25 | Args: 26 | user (_type_): пользователь 27 | token (_type_): Токен с email 28 | request (_type_): сущность request 29 | """ 30 | logger.warning('TOKEN VERIFY ^^^^^------^^^^^ TOKEN VERIFY\n') 31 | logger.warning(token) 32 | logger.warning('\nEND TOKEN ^^^^^------^^^^^ END TOKEN') 33 | 34 | async def on_after_forgot_password(self, user, token, request): 35 | """ 36 | Здесь должна быть отправка на E-mail `token` который уже 37 | вмещает в себя дополнительное поле `password_fgpt` для верификации. 38 | Этот токен нужно вписать в end-point `reset-password`. 39 | 40 | Для эмуляции отправки сюда в консоль выведется сам токен. 41 | 42 | Если в консоли вы не видете токена значит: 43 | - Не правильные данные 44 | - Не активный пользователь 45 | 46 | Args: 47 | user (_type_): пользователь 48 | token (_type_): Токен с password_fgpt 49 | request (_type_): сущность request 50 | """ 51 | logger.warning('TOKEN RESET ^^^^^------^^^^^ TOKEN RESET\n') 52 | logger.warning(token) 53 | logger.warning('\nEND TOKEN ^^^^^------^^^^^ END TOKEN') 54 | 55 | async def on_after_reset_password(self, user, request): 56 | """ 57 | Здесь должна быть отправка на E-mail cообщения о изменения пароля. 58 | 59 | Args: 60 | user (_type_): пользователь 61 | request (_type_): сущность request 62 | """ 63 | return await super().on_after_reset_password(user, request) 64 | 65 | 66 | class PasswordValidationMixin: 67 | """ 68 | Миксин добавляющий валидацию пароля 69 | """ 70 | 71 | async def validate_password(self, password, user): 72 | if not len(password) > 7: 73 | raise PasswordNotValidError( 74 | status_code=status.HTTP_400_BAD_REQUEST, 75 | detail='Password is to short', 76 | ) 77 | -------------------------------------------------------------------------------- /api_v1/users/tasks.py: -------------------------------------------------------------------------------- 1 | from config import celery_app, settings 2 | import asyncio 3 | 4 | 5 | @celery_app.task 6 | async def time_sleep_task(): 7 | """ 8 | Тестовая задача для Celery 9 | """ 10 | await asyncio.sleep(2.0) 11 | return 'Task is done' 12 | 13 | 14 | celery_app.conf.beat_schedule = { 15 | 'test-every-10-seconds': { 16 | 'task': 'api_v1.users.tasks.time_sleep_task', 17 | 'schedule': settings.celery.TEST_TIMEDELTA, 18 | }, 19 | } 20 | -------------------------------------------------------------------------------- /api_v1/users/user_manager.py: -------------------------------------------------------------------------------- 1 | from fastapi import Depends 2 | from fastapi_users import BaseUserManager, IntegerIDMixin 3 | from fastapi_users.db import SQLAlchemyUserDatabase 4 | from sqlalchemy.ext.asyncio import AsyncSession 5 | 6 | from .mixins import ActionUserManagerMixin, PasswordValidationMixin 7 | from config.models import User 8 | from config import settings 9 | from config import db_connection 10 | 11 | 12 | class UserManager(ActionUserManagerMixin, 13 | PasswordValidationMixin, 14 | IntegerIDMixin, 15 | BaseUserManager[User, int]): 16 | """ 17 | UserManager для работы с пользователем 18 | 19 | Вмещает в себя все неоходимые методы для CRUD пользователя 20 | 21 | Требует при инициализации :class:`BaseUserDatabase` экземпляр 22 | с активной текущей сессией 23 | """ 24 | 25 | verification_token_secret = settings.JWT.SECRET 26 | verification_token_audience = 'fastapi-users:auth' 27 | 28 | reset_password_token_secret = settings.JWT.SECRET 29 | reset_password_token_lifetime_seconds = settings.JWT.RESET_LIFESPAN_TOKEN_SECONDS 30 | 31 | 32 | async def get_user_manager(session: AsyncSession = Depends( 33 | db_connection.session_geter, 34 | )) -> UserManager: 35 | """ 36 | Получение Инициализированного с сессией 37 | UserManager 38 | """ 39 | return UserManager(user_db=SQLAlchemyUserDatabase( 40 | session=session, 41 | user_table=User, 42 | )) 43 | -------------------------------------------------------------------------------- /api_v1/users/views.py: -------------------------------------------------------------------------------- 1 | from fastapi import APIRouter, Depends 2 | from fastapi_users import FastAPIUsers 3 | from fastapi_cache.decorator import cache 4 | 5 | from config.models import User 6 | from api_v1.auth.backends import auth_backend 7 | from api_v1.auth import active_user 8 | from api_v1.auth.schemas import UserRead, UserUpdate 9 | from api_v1.users.user_manager import get_user_manager 10 | 11 | 12 | fastapi_users = FastAPIUsers[User, int]( 13 | get_user_manager, 14 | (auth_backend,) 15 | ) 16 | 17 | 18 | router = APIRouter() 19 | 20 | 21 | @router.get(path='/test', 22 | dependencies=[Depends(active_user)], 23 | ) 24 | @cache() 25 | async def test_end_point(user: User = Depends(active_user)): 26 | return dict(name=user.email) 27 | 28 | 29 | router.include_router(fastapi_users.get_users_router(UserRead, UserUpdate), 30 | tags=['Users'], 31 | prefix='/users', 32 | ) 33 | -------------------------------------------------------------------------------- /app_includes/__init__.py: -------------------------------------------------------------------------------- 1 | from .logs_errors import register_errors 2 | from .middlewares import register_middlewares 3 | from .prometheus import register_prometheus 4 | 5 | 6 | __all__ = ('register_errors', 7 | 'register_middlewares', 8 | 'register_prometheus', 9 | ) 10 | -------------------------------------------------------------------------------- /app_includes/logs_errors.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI, Request 2 | from fastapi.responses import JSONResponse 3 | from fastapi.exceptions import HTTPException 4 | from starlette.exceptions import HTTPException as StarletteHTTPException 5 | from fastapi_users.exceptions import ( 6 | InvalidID, 7 | UserAlreadyExists, 8 | UserNotExists, 9 | UserInactive, 10 | UserAlreadyVerified, 11 | InvalidVerifyToken, 12 | InvalidResetPasswordToken, 13 | InvalidPasswordException, 14 | ) 15 | 16 | from http import HTTPStatus 17 | 18 | from config.setup_logs.logging import logger 19 | from api_v1.exeptions import ValidationError 20 | from api_v1.users.exceptions import PasswordNotValidError 21 | 22 | 23 | def register_errors(app: FastAPI) -> None: 24 | """ 25 | Крючек для логирования различных исключений 26 | 27 | ## Args: 28 | app (FastAPI): ASGI приложение. 29 | 30 | ## Returns: 31 | None 32 | 33 | ## Example 34 | ```python 35 | from fastapi import FastAPI, Request 36 | from fastapi.responses import JSONResponse 37 | from fastapi.exceptions import HTTPException 38 | from http import HTTPStatus 39 | 40 | from config.setup_logs.logging import logger 41 | from api_v1.api_xml.exeptions import APIFileNotFoundError 42 | 43 | 44 | def register_errors(app: FastAPI) -> None: 45 | @app.exception_handler(HTTPException) 46 | async def http_error_handler( 47 | request: Request, 48 | exc: HTTPException, 49 | ): 50 | logger.opt(exception=True).warning(exc) 51 | response = dict( 52 | status=False, 53 | error_code=exc.status_code, 54 | message=exc.detail, 55 | ) 56 | return JSONResponse(response) 57 | 58 | # Добавление нового крюка 59 | @app.exception_handler(APIFileNotFoundError) 60 | async def file_not_found_error_handler( 61 | request: Request, 62 | exc: APIFileNotFoundError, 63 | ): 64 | logger.opt(exception=True).warning(exc) 65 | response = dict( 66 | status=False, 67 | error_code=exc.status_code, 68 | message=exc.detail, 69 | ) 70 | return JSONResponse(response) 71 | ``` 72 | """ 73 | 74 | @app.exception_handler(InvalidPasswordException) 75 | async def password_invalid_error_handler( 76 | request: Request, 77 | exc: InvalidPasswordException, 78 | ): 79 | """ 80 | Логирование всех InvalidPasswordException 81 | """ 82 | logger.opt(exception=True).warning(exc) 83 | response = dict( 84 | status=False, 85 | error_code=exc.status_code, 86 | message=exc.detail, 87 | ) 88 | return JSONResponse(response, status_code=exc.status_code) 89 | 90 | @app.exception_handler(InvalidResetPasswordToken) 91 | async def password_token_error_handler( 92 | request: Request, 93 | exc: InvalidResetPasswordToken, 94 | ): 95 | """ 96 | Логирование всех InvalidResetPasswordToken 97 | """ 98 | logger.opt(exception=True).warning(exc) 99 | response = dict( 100 | status=False, 101 | error_code=exc.status_code, 102 | message=exc.detail, 103 | ) 104 | return JSONResponse(response, status_code=exc.status_code) 105 | 106 | @app.exception_handler(InvalidVerifyToken) 107 | async def verify_token_error_handler( 108 | request: Request, 109 | exc: InvalidVerifyToken, 110 | ): 111 | """ 112 | Логирование всех InvalidVerifyToken 113 | """ 114 | logger.opt(exception=True).warning(exc) 115 | response = dict( 116 | status=False, 117 | error_code=exc.status_code, 118 | message=exc.detail, 119 | ) 120 | return JSONResponse(response, status_code=exc.status_code) 121 | 122 | @app.exception_handler(UserAlreadyVerified) 123 | async def user_exists_error_handler( 124 | request: Request, 125 | exc: UserAlreadyVerified, 126 | ): 127 | """ 128 | Логирование всех UserAlreadyVerified 129 | """ 130 | logger.opt(exception=True).warning(exc) 131 | response = dict( 132 | status=False, 133 | error_code=exc.status_code, 134 | message=exc.detail, 135 | ) 136 | return JSONResponse(response, status_code=exc.status_code) 137 | 138 | @app.exception_handler(UserInactive) 139 | async def user_activity_error_handler( 140 | request: Request, 141 | exc: UserInactive, 142 | ): 143 | """ 144 | Логирование всех UserInactive 145 | """ 146 | logger.opt(exception=True).warning(exc) 147 | response = dict( 148 | status=False, 149 | error_code=exc.status_code, 150 | message=exc.detail, 151 | ) 152 | return JSONResponse(response, status_code=exc.status_code) 153 | 154 | @app.exception_handler(UserNotExists) 155 | async def user_not_exists_error_handler( 156 | request: Request, 157 | exc: UserNotExists, 158 | ): 159 | """ 160 | Логирование всех UserNotExists 161 | """ 162 | logger.opt(exception=True).warning(exc) 163 | response = dict( 164 | status=False, 165 | error_code=exc.status_code, 166 | message=exc.detail, 167 | ) 168 | return JSONResponse(response, status_code=exc.status_code) 169 | 170 | @app.exception_handler(UserAlreadyExists) 171 | async def user_already_exists_error_handler( 172 | request: Request, 173 | exc: UserAlreadyExists, 174 | ): 175 | """ 176 | Логирование всех UserAlreadyExists 177 | """ 178 | logger.opt(exception=True).warning(exc) 179 | response = dict( 180 | status=False, 181 | error_code=exc.status_code, 182 | message=exc.detail, 183 | ) 184 | return JSONResponse(response, status_code=exc.status_code) 185 | 186 | @app.exception_handler(InvalidID) 187 | async def invalid_id_error_handler( 188 | request: Request, 189 | exc: InvalidID, 190 | ): 191 | """ 192 | Логирование всех InvalidID 193 | """ 194 | logger.opt(exception=True).warning(exc) 195 | response = dict( 196 | status=False, 197 | error_code=exc.status_code, 198 | message=exc.detail, 199 | ) 200 | return JSONResponse(response, status_code=exc.status_code) 201 | 202 | @app.exception_handler(PasswordNotValidError) 203 | async def password_validator_error_handler( 204 | request: Request, 205 | exc: PasswordNotValidError, 206 | ): 207 | """ 208 | Логирование всех PasswordNotValidError 209 | """ 210 | logger.opt(exception=True).warning(exc) 211 | response = dict( 212 | status=False, 213 | error_code=exc.status_code, 214 | message=exc.detail, 215 | ) 216 | return JSONResponse(response, status_code=exc.status_code) 217 | 218 | @app.exception_handler(ValidationError) 219 | async def validation_error_handler( 220 | request: Request, 221 | exc: ValidationError, 222 | ): 223 | """ 224 | Логирование всех ValidationError 225 | """ 226 | logger.opt(exception=True).warning(exc) 227 | response = dict( 228 | status=False, 229 | error_code=exc.status_code, 230 | message=exc.detail, 231 | ) 232 | return JSONResponse(response, status_code=exc.status_code) 233 | 234 | @app.exception_handler(HTTPException) 235 | async def http_error_handler( 236 | request: Request, 237 | exc: HTTPException, 238 | ): 239 | """ 240 | Логирование всех HTTPException 241 | """ 242 | logger.opt(exception=True).warning(exc) 243 | response = dict( 244 | status=False, 245 | error_code=exc.status_code, 246 | message=exc.detail, 247 | ) 248 | return JSONResponse(response, status_code=exc.status_code) 249 | 250 | @app.exception_handler(Exception) 251 | async def error_handler( 252 | request: Request, 253 | exc: Exception, 254 | ): 255 | """ 256 | Логирование всех Exception 257 | """ 258 | logger.exception(exc) 259 | response = dict( 260 | status=False, 261 | error_code=500, 262 | message=HTTPStatus(500).phrase, 263 | ) 264 | return JSONResponse(response, status_code=500) 265 | 266 | @app.exception_handler(StarletteHTTPException) 267 | async def validation_starlette_error_handler( 268 | request: Request, 269 | exc: StarletteHTTPException, 270 | ): 271 | """ 272 | Логирование всех StarletteHTTPException 273 | """ 274 | logger.opt(exception=True).warning(exc) 275 | response = dict( 276 | status=False, 277 | error_code=exc.status_code, 278 | message=exc.detail, 279 | ) 280 | return JSONResponse(response, status_code=exc.status_code) 281 | -------------------------------------------------------------------------------- /app_includes/middlewares.py: -------------------------------------------------------------------------------- 1 | from fastapi.middleware.cors import CORSMiddleware 2 | from fastapi import FastAPI 3 | 4 | from config import settings 5 | 6 | 7 | def register_middlewares(app: FastAPI) -> None: 8 | """ 9 | Регистрация middleware 10 | 11 | ## Args: 12 | app (FastAPI): ASGI приложение. 13 | 14 | ## Returns: 15 | None 16 | 17 | ## Example 18 | ```python 19 | from fastapi.middleware.cors import CORSMiddleware 20 | from fastapi import FastAPI 21 | 22 | from config import settings 23 | from middlewares import SomeMiddleware 24 | 25 | 26 | def register_middlewares(app: FastAPI) -> None: 27 | app.add_middleware( 28 | CORSMiddleware, 29 | allow_origins=[y 30 | settings.CURRENT_ORIGIN, 31 | ], 32 | allow_credentials=True, 33 | allow_methods=['*'], 34 | allow_headers=['*'], 35 | ) 36 | 37 | # Новая регистрация 38 | app.add_middleware( 39 | SomeMiddleware, 40 | *args, 41 | ) 42 | ``` 43 | """ 44 | app.add_middleware( 45 | CORSMiddleware, 46 | allow_origins=[ 47 | settings.CURRENT_ORIGIN, 48 | ], 49 | allow_credentials=True, 50 | allow_methods=['*'], 51 | allow_headers=['*'], 52 | ) 53 | -------------------------------------------------------------------------------- /app_includes/prometheus.py: -------------------------------------------------------------------------------- 1 | from fastapi import FastAPI 2 | from prometheus_fastapi_instrumentator import Instrumentator 3 | 4 | 5 | def register_prometheus(app: FastAPI) -> None: 6 | """ 7 | Регистрация Промитеуса 8 | """ 9 | Instrumentator().instrument(app=app).expose(app=app) 10 | -------------------------------------------------------------------------------- /async_alembic/README: -------------------------------------------------------------------------------- 1 | Generic single-database configuration with an async dbapi. -------------------------------------------------------------------------------- /async_alembic/env.py: -------------------------------------------------------------------------------- 1 | import asyncio 2 | from logging.config import fileConfig 3 | 4 | from sqlalchemy import pool 5 | from sqlalchemy.engine import Connection 6 | from sqlalchemy.ext.asyncio import AsyncEngine 7 | from sqlalchemy import engine_from_config 8 | 9 | from alembic import context 10 | 11 | from config import settings 12 | from config.models.base import Base 13 | 14 | 15 | # this is the Alembic Config object, which provides 16 | # access to the values within the .ini file in use. 17 | config = context.config 18 | 19 | # Interpret the config file for Python logging. 20 | # This line sets up loggers basically. 21 | if config.config_file_name is not None: 22 | fileConfig(config.config_file_name) 23 | 24 | # add your model's MetaData object here 25 | # for 'autogenerate' support 26 | target_metadata = Base.metadata 27 | 28 | # other values from the config, defined by the needs of env.py, 29 | # can be acquired: 30 | # my_important_option = config.get_main_option("my_important_option") 31 | # ... etc. 32 | config.set_main_option( 33 | 'sqlalchemy.url', 34 | settings.db.url, 35 | ) 36 | 37 | 38 | def run_migrations_offline() -> None: 39 | """Run migrations in 'offline' mode. 40 | 41 | This configures the context with just a URL 42 | and not an Engine, though an Engine is acceptable 43 | here as well. By skipping the Engine creation 44 | we don't even need a DBAPI to be available. 45 | 46 | Calls to context.execute() here emit the given string to the 47 | script output. 48 | 49 | """ 50 | url = config.get_main_option("sqlalchemy.url") 51 | context.configure( 52 | url=url, 53 | target_metadata=target_metadata, 54 | literal_binds=True, 55 | dialect_opts={"paramstyle": "named"}, 56 | ) 57 | 58 | with context.begin_transaction(): 59 | context.run_migrations() 60 | 61 | 62 | def do_run_migrations(connection: Connection) -> None: 63 | context.configure( 64 | connection=connection, 65 | target_metadata=target_metadata, 66 | compare_type=True, 67 | ) 68 | with context.begin_transaction(): 69 | context.run_migrations() 70 | 71 | 72 | async def run_async_migrations(connectable) -> None: 73 | """In this scenario we need to create an Engine 74 | and associate a connection with the context. 75 | 76 | """ 77 | async with connectable.connect() as connection: 78 | await connection.run_sync(do_run_migrations) 79 | await connectable.dispose() 80 | 81 | 82 | def run_migrations_online() -> None: 83 | """Run migrations in 'online' mode.""" 84 | connectable = context.config.attributes.get('connection', None) 85 | if connectable is None: 86 | connectable = AsyncEngine( 87 | engine_from_config( 88 | context.config.get_section( 89 | context.config.config_ini_section, 90 | ), 91 | prefix='sqlalchemy.', 92 | poolclass=pool.NullPool, 93 | future=True, 94 | ) 95 | ) 96 | if isinstance(connectable, AsyncEngine): 97 | asyncio.run(run_async_migrations(connectable)) 98 | else: 99 | do_run_migrations(connectable) 100 | 101 | 102 | if context.is_offline_mode(): 103 | run_migrations_offline() 104 | else: 105 | run_migrations_online() 106 | -------------------------------------------------------------------------------- /async_alembic/script.py.mako: -------------------------------------------------------------------------------- 1 | """${message} 2 | 3 | Revision ID: ${up_revision} 4 | Revises: ${down_revision | comma,n} 5 | Create Date: ${create_date} 6 | 7 | """ 8 | from typing import Sequence, Union 9 | 10 | from alembic import op 11 | import sqlalchemy as sa 12 | ${imports if imports else ""} 13 | 14 | # revision identifiers, used by Alembic. 15 | revision: str = ${repr(up_revision)} 16 | down_revision: Union[str, None] = ${repr(down_revision)} 17 | branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)} 18 | depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)} 19 | 20 | 21 | def upgrade() -> None: 22 | ${upgrades if upgrades else "pass"} 23 | 24 | 25 | def downgrade() -> None: 26 | ${downgrades if downgrades else "pass"} 27 | -------------------------------------------------------------------------------- /async_alembic/versions/2024_12_12_1935-ae5175a92845_add_user_model.py: -------------------------------------------------------------------------------- 1 | """add user model 2 | 3 | Revision ID: ae5175a92845 4 | Revises: 5 | Create Date: 2024-12-12 19:35:49.340027 6 | 7 | """ 8 | 9 | from typing import Sequence, Union 10 | 11 | from alembic import op 12 | import sqlalchemy as sa 13 | 14 | 15 | # revision identifiers, used by Alembic. 16 | revision: str = "ae5175a92845" 17 | down_revision: Union[str, None] = None 18 | branch_labels: Union[str, Sequence[str], None] = None 19 | depends_on: Union[str, Sequence[str], None] = None 20 | 21 | 22 | def upgrade() -> None: 23 | # ### commands auto generated by Alembic - please adjust! ### 24 | op.create_table( 25 | "user", 26 | sa.Column("id", sa.Integer(), nullable=False), 27 | sa.Column("email", sa.String(length=320), nullable=False), 28 | sa.Column("hashed_password", sa.String(length=1024), nullable=False), 29 | sa.Column("is_active", sa.Boolean(), nullable=False), 30 | sa.Column("is_superuser", sa.Boolean(), nullable=False), 31 | sa.Column("is_verified", sa.Boolean(), nullable=False), 32 | sa.PrimaryKeyConstraint("id"), 33 | ) 34 | op.create_index(op.f("ix_user_email"), "user", ["email"], unique=True) 35 | # ### end Alembic commands ### 36 | 37 | 38 | def downgrade() -> None: 39 | # ### commands auto generated by Alembic - please adjust! ### 40 | op.drop_index(op.f("ix_user_email"), table_name="user") 41 | op.drop_table("user") 42 | # ### end Alembic commands ### 43 | -------------------------------------------------------------------------------- /caddy/Caddyfile: -------------------------------------------------------------------------------- 1 | :9090 { 2 | basicauth / {$ADMIN_USER} {$ADMIN_PASSWORD} 3 | proxy / prometheus:9090 { 4 | transparent 5 | } 6 | 7 | errors stderr 8 | tls off 9 | } 10 | 11 | :9093 { 12 | basicauth / {$ADMIN_USER} {$ADMIN_PASSWORD} 13 | proxy / alertmanager:9093 { 14 | transparent 15 | } 16 | 17 | errors stderr 18 | tls off 19 | } 20 | 21 | :9091 { 22 | basicauth / {$ADMIN_USER} {$ADMIN_PASSWORD} 23 | proxy / pushgateway:9091 { 24 | transparent 25 | } 26 | 27 | errors stderr 28 | tls off 29 | } 30 | 31 | :3000 { 32 | proxy / grafana:3000 { 33 | transparent 34 | websocket 35 | } 36 | 37 | errors stderr 38 | tls off 39 | } -------------------------------------------------------------------------------- /config/__init__.py: -------------------------------------------------------------------------------- 1 | from .config import settings 2 | from .celery.connection import app as celery_app 3 | from .database.db_helper import db_helper as db_connection 4 | from .database.db_helper import db_test as test_connection 5 | 6 | 7 | __all__ = ('settings', 8 | 'celery_app', 9 | 'db_connection', 10 | 'test_connection', 11 | ) 12 | -------------------------------------------------------------------------------- /config/alembic/__init__.py: -------------------------------------------------------------------------------- 1 | from .alembic_helper import AlembicHelper 2 | 3 | 4 | __all__ = ('AlembicHelper',) 5 | -------------------------------------------------------------------------------- /config/alembic/alembic_helper.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from alembic.command import upgrade 3 | from alembic.config import Config 4 | from typing import ClassVar 5 | from sqlalchemy.ext.asyncio.engine import AsyncConnection 6 | 7 | 8 | from config import settings 9 | 10 | 11 | class AlembicHelper: 12 | """ 13 | Вспомогательный класс для Alembic 14 | """ 15 | config: ClassVar[Config] = Config 16 | migration_path: ClassVar[Path] = settings.alembic.MIGRATION_PATH 17 | 18 | def __init__(self, 19 | sql_url: str | Path, 20 | ): 21 | self.sql_url = sql_url 22 | 23 | def get_config(self, connection: AsyncConnection) -> Config: 24 | cfg = self.config() 25 | cfg.set_main_option('sqlalchemy.url', 26 | self.sql_url) 27 | cfg.set_main_option('script_location', 28 | self.migration_path.as_posix()) 29 | cfg.attributes['connection'] = connection 30 | return cfg 31 | 32 | def make_upgrade(self, connection: AsyncConnection): 33 | config = self.get_config(connection=connection) 34 | upgrade(config, 'head') 35 | -------------------------------------------------------------------------------- /config/celery/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sumaro2101/BaseFastAPI/292aae5f2ab3959d5116a3cc9f8ec0e5dcbfd822/config/celery/__init__.py -------------------------------------------------------------------------------- /config/celery/connection.py: -------------------------------------------------------------------------------- 1 | from typing import Any, Awaitable, Callable 2 | import celery 3 | from functools import wraps 4 | 5 | from config import settings 6 | 7 | import asyncio 8 | 9 | 10 | class Celery(celery.Celery): 11 | """ 12 | Инициализация асинхронного Celery 13 | """ 14 | 15 | def __init__(self, *args, **kwargs) -> None: 16 | super().__init__(*args, **kwargs) 17 | self.loop = asyncio.get_event_loop() 18 | 19 | def task( 20 | self, 21 | task: Callable[..., Awaitable] | None = None, 22 | **opts: Any, 23 | ) -> Callable: 24 | create_task = super().task 25 | 26 | def decorator(func: Callable[..., Awaitable]) -> Callable: 27 | @create_task(**opts) 28 | @wraps(func) 29 | def wrapper(*args, 30 | loop: asyncio.AbstractEventLoop | None = None, 31 | **kwargs, 32 | ): 33 | loop = loop or self.loop 34 | return loop.run_until_complete(func(*args, **kwargs)) 35 | return wrapper 36 | 37 | if task: 38 | return decorator(task) 39 | return decorator 40 | 41 | 42 | app = Celery(__name__) 43 | app.conf.broker_url = settings.rabbit.broker_url 44 | app.conf.result_backend = settings.redis.redis_url 45 | app.conf.timezone = settings.celery.TIMEZONE 46 | app.conf.broker_connection_retry_on_startup = True 47 | app.autodiscover_tasks(packages=['api_v1.users']) 48 | -------------------------------------------------------------------------------- /config/config.py: -------------------------------------------------------------------------------- 1 | from pathlib import Path 2 | from pydantic_settings import BaseSettings, SettingsConfigDict 3 | from pydantic import BaseModel, ConfigDict 4 | from starlette.config import Config 5 | from celery.schedules import crontab 6 | 7 | 8 | base_dir = Path(__file__).resolve().parent.parent 9 | log_dir = base_dir.joinpath('logs') 10 | 11 | 12 | config = Config('.env') 13 | 14 | 15 | class JWTSettings(BaseModel): 16 | """ 17 | Настройки JWT токена 18 | """ 19 | NAME: str = 'jwt' 20 | SECRET: str = config('SECRET') 21 | RESET_LIFESPAN_TOKEN_SECONDS: int = 3600 22 | JWT_PATH: str = '/auth/jwt' 23 | 24 | 25 | class AlembicSettings(BaseModel): 26 | """ 27 | Настройки Alembic 28 | """ 29 | CONFIG_PATH: Path = Path('alembic.ini') 30 | MIGRATION_PATH: Path = Path('async_alembic') 31 | 32 | 33 | class TestDBSettings(BaseModel): 34 | """ 35 | Настройки тестовой базы данных 36 | """ 37 | _engine: str = config('TEST_DB_ENGINE') 38 | _owner: str = config('TEST_DB_USER') 39 | _password: str = config('TEST_DB_PASSWORD') 40 | _name: str = config('TEST_DB_HOST') 41 | _db_name: str = config('TEST_DB_NAME') 42 | url: str = f'{_engine}://{_owner}:{_password}@{_name}/{_db_name}' 43 | 44 | 45 | class DBSettings(BaseModel): 46 | """ 47 | Настройки DataBase 48 | """ 49 | _engine: str = config('DB_ENGINE') 50 | _owner: str = config('DB_USER') 51 | _password: str = config('DB_PASSWORD') 52 | _name: str = config('DB_HOST') 53 | _db_name: str = config('DB_NAME') 54 | url: str = f'{_engine}://{_owner}:{_password}@{_name}/{_db_name}' 55 | 56 | 57 | class CelerySettings(BaseModel): 58 | """ 59 | Настройки Celery 60 | """ 61 | model_config = ConfigDict( 62 | arbitrary_types_allowed=True, 63 | ) 64 | TIMEZONE: str = 'Europe/Moscow' 65 | TIMEDELTA_PER_DAY: crontab = crontab(minute=0, 66 | hour=2, 67 | day_of_week='*/1', 68 | day_of_month='*/1', 69 | month_of_year='*/1', 70 | ) 71 | TEST_TIMEDELTA: crontab = crontab(minute='*/1') 72 | 73 | 74 | class RabbitSettings(BaseModel): 75 | """ 76 | Настройки RabbitMQ 77 | """ 78 | RMQ_HOST: str = config('RMQ_HOST') 79 | RMQ_PORT: str = config('RMQ_PORT') 80 | RMQ_USER: str = config('RABBITMQ_DEFAULT_USER') 81 | RMQ_PASSWORD: str = config('RABBITMQ_DEFAULT_PASS') 82 | broker_url: str = ('amqp://' + 83 | RMQ_USER + 84 | ':' + 85 | RMQ_PASSWORD + 86 | '@' + 87 | RMQ_HOST + 88 | ':' + 89 | RMQ_PORT) 90 | 91 | 92 | class RedisSettings(BaseModel): 93 | """ 94 | Настройки Redis 95 | """ 96 | REDIS_HOST: str = config('REDIS_HOST') 97 | REDIS_PORT: str = config('REDIS_PORT') 98 | redis_url: str = ('redis://' + 99 | REDIS_HOST) 100 | 101 | 102 | class Settings(BaseSettings): 103 | """ 104 | Настройки проекта 105 | """ 106 | model_config = SettingsConfigDict( 107 | extra='ignore', 108 | ) 109 | db: DBSettings = DBSettings() 110 | test_db: TestDBSettings = TestDBSettings() 111 | celery: CelerySettings = CelerySettings() 112 | rabbit: RabbitSettings = RabbitSettings() 113 | redis: RedisSettings = RedisSettings() 114 | alembic: AlembicSettings = AlembicSettings() 115 | JWT: JWTSettings = JWTSettings() 116 | debug: bool = bool(int(config('DEBUG'))) 117 | API_PREFIX: str = '/api/v1' 118 | BASE_DIR: Path = base_dir 119 | LOG_DIR: Path = log_dir 120 | CURRENT_ORIGIN: str = config('CURRENT_ORIGIN') 121 | 122 | 123 | settings = Settings() 124 | -------------------------------------------------------------------------------- /config/dao/__init__.py: -------------------------------------------------------------------------------- 1 | from .base_dao import BaseDAO 2 | 3 | 4 | __all__ = ('BaseDAO',) 5 | -------------------------------------------------------------------------------- /config/dao/base_dao.py: -------------------------------------------------------------------------------- 1 | from typing import TypeVar, Generic 2 | 3 | from sqlalchemy.ext.asyncio import AsyncSession 4 | from sqlalchemy.exc import SQLAlchemyError 5 | from sqlalchemy import Select 6 | from sqlalchemy.orm import joinedload, selectinload 7 | from typing import ClassVar, Sequence 8 | 9 | 10 | T_co = TypeVar('T_co', covariant=True) 11 | 12 | 13 | class BaseDAO(Generic[T_co]): 14 | """ 15 | Базовый DAO класс для CRUD модели 16 | 17 | Универсальный класс для легкого опеределения 18 | 19 | CRUD модели 20 | 21 | Примеры:: 22 | 23 | # Поиск сущности 24 | item = ModelDAO.find_item_by_args( 25 | session=session, 26 | id=3, 27 | ) 28 | # Множественный поиск сущностей 29 | items = ModelDAO.find_all_items_by_args( 30 | session = session, 31 | one_to_many = (Model.tag,), 32 | many_to_many = (Model.users, Model.stations,) 33 | name='model', 34 | ) 35 | # Создание сущности 36 | item = ModelDAO.add( 37 | session, 38 | id=3, 39 | name='model', 40 | ) 41 | """ 42 | model: ClassVar[T_co | None] = None 43 | 44 | @classmethod 45 | async def find_item_by_args(cls, 46 | session: AsyncSession, 47 | one_to_many: Sequence[T_co] | None = None, 48 | many_to_many: Sequence[T_co] | None = None, 49 | **kwargs: dict[str, str | int], 50 | ) -> T_co: 51 | """ 52 | Нахождение и возращение сущности 53 | 54 | Args: 55 | session (AsyncSession): Текущая сессия 56 | 57 | one_to_many (Sequence[T_co] | None, optional): Выбранные поля 58 | для one_to_many 59 | которые имеют отношение например: (Product.user) 60 | 61 | many_to_many (Sequence[T_co] | None, optional): Выбранные поля 62 | для many_to_many 63 | которые имеют отношение например: (Product.categories) 64 | 65 | Returns: 66 | T_co: Сущность из выборки 67 | """ 68 | stmt = struct_options_statment( 69 | model=cls.model, 70 | one_to_many=one_to_many, 71 | many_to_many=many_to_many, 72 | **kwargs, 73 | ) 74 | result = await session.scalar(statement=stmt) 75 | return result 76 | 77 | @classmethod 78 | async def find_all_items_by_args(cls, 79 | session: AsyncSession, 80 | one_to_many: Sequence[T_co] | None = None, 81 | many_to_many: Sequence[T_co] | None = None, 82 | **kwargs: dict[str, str | int], 83 | ) -> list[T_co]: 84 | """ 85 | Нахождение и возращение множества сущностей 86 | 87 | Args: 88 | session (AsyncSession): Текущая сессия 89 | 90 | one_to_many (Sequence[T_co] | None, optional): Выбранные поля 91 | для one_to_many 92 | которые имеют отношение например: (Product.user) 93 | 94 | many_to_many (Sequence[T_co] | None, optional): Выбранные поля 95 | для many_to_many 96 | которые имеют отношение например: (Product.categories) 97 | 98 | Returns: 99 | T_co: Сущности из выборки 100 | """ 101 | stmt = struct_options_statment( 102 | model=cls.model, 103 | one_to_many=one_to_many, 104 | many_to_many=many_to_many, 105 | **kwargs, 106 | ) 107 | result = await session.scalars(statement=stmt) 108 | return list(result) 109 | 110 | @classmethod 111 | async def add(cls, 112 | session: AsyncSession, 113 | **values, 114 | ) -> T_co: 115 | instance = cls.model(**values) 116 | session.add(instance=instance) 117 | try: 118 | await session.commit() 119 | except SQLAlchemyError as ex: 120 | await session.rollback() 121 | raise ex 122 | return instance 123 | 124 | @classmethod 125 | async def update(cls, 126 | session: AsyncSession, 127 | instance: T_co, 128 | **values, 129 | ) -> T_co: 130 | [setattr(instance, name, value) 131 | for name, value 132 | in values.items()] 133 | await session.commit() 134 | return instance 135 | 136 | @classmethod 137 | async def delete(cls, 138 | session: AsyncSession, 139 | instance: T_co, 140 | ) -> None: 141 | await session.delete(instance) 142 | await session.commit() 143 | 144 | 145 | def struct_options_statment(model: T_co, 146 | one_to_many: Sequence[T_co] | None = None, 147 | many_to_many: Sequence[T_co] | None = None, 148 | **kwargs: dict[str, str | int], 149 | ) -> Select: 150 | """ 151 | Струкрутирование запроса SELECT для выборки 152 | 153 | Args: 154 | model (BaseModel): Модель таблицы для выборки 155 | one_to_many (Sequence[BaseModel] | None, optional): Выбранные поля 156 | для one_to_many 157 | которые имеют отношение например: (Product.user) 158 | 159 | many_to_many (Sequence[BaseModel] | None, optional): Выбранные поля 160 | для many_to_many 161 | которые имеют отношение например: (Product.categories) 162 | 163 | Returns: 164 | Select: Экземпляр запроса 165 | """ 166 | stms_one_to_many = ([selectinload(join) for join in one_to_many] 167 | if one_to_many 168 | else list()) 169 | stmt_any_to_many = ([joinedload(join)for join in many_to_many] 170 | if many_to_many 171 | else list()) 172 | stmt = (Select(model) 173 | .filter_by(**kwargs) 174 | .options(*stms_one_to_many) 175 | .options(*stmt_any_to_many)) 176 | return stmt 177 | -------------------------------------------------------------------------------- /config/database/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sumaro2101/BaseFastAPI/292aae5f2ab3959d5116a3cc9f8ec0e5dcbfd822/config/database/__init__.py -------------------------------------------------------------------------------- /config/database/db_helper.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy.ext.asyncio import (create_async_engine, 2 | async_sessionmaker, 3 | async_scoped_session, 4 | AsyncSession, 5 | ) 6 | from sqlalchemy.pool import Pool 7 | from asyncio import current_task 8 | 9 | from typing import AsyncGenerator, Any 10 | 11 | from config import settings 12 | 13 | 14 | DATA_BASE_URL = settings.db.url 15 | 16 | 17 | class DataBaseHelper: 18 | """ 19 | Вспомогательный класс для работы с Базой Данных. 20 | 21 | Помогает инициализировать соединение с Базой Данных, а так же 22 | работу с сессиями. 23 | 24 | ## Инициализация: 25 | :string:`db_url` - Адресс базы данных. 26 | :string:`poolclass` - Пул типа :class:`sqlalchemy.pool.Pool` 27 | 28 | ## Методы: 29 | :function:`DataBaseHelper.session_geter` - Получение генератора текущей сессии. 30 | :function:`DataBaseHelper.get_scoped_session` - Получение текущей сессии. 31 | :function:`DataBaseHelper.dispose` - Закрытые соединения. 32 | 33 | ## Примеры: 34 | ```python 35 | from fastapi import FastAPI 36 | from contextlib import asynccontextmanager 37 | from config import db_connection, BaseModel 38 | 39 | 40 | @asynccontextmanager 41 | async def lifespan(app: FastAPI): 42 | # Инициализация соединения для создания таблиц. 43 | async with db_connection.engine.begin() as conn: 44 | await conn.run_sync(BaseModel.metadata.create_all) 45 | yield 46 | await db_connection.dispose() 47 | 48 | app = FastApi(lifespan=lifespan) 49 | ``` 50 | """ 51 | def __init__(self, 52 | db_url: str = DATA_BASE_URL, 53 | poolclass: Pool | None = None, 54 | ) -> None: 55 | """ 56 | Args: 57 | db_url (str, optional): Адресс Базы Данных. Defaults to DATA_BASE_URL. 58 | 59 | poolclass (Pool | None, optional): Пул типа :class:`sqlalchemy.pool.Pool`. 60 | Defaults to None. 61 | """ 62 | self._db_url = db_url 63 | setup = dict( 64 | url=self._db_url, 65 | echo=settings.debug, 66 | ) 67 | if poolclass: 68 | setup.update( 69 | poolclass=poolclass, 70 | ) 71 | self.engine = create_async_engine( 72 | **setup 73 | ) 74 | self.session = async_sessionmaker( 75 | bind=self.engine, 76 | autoflush=False, 77 | autocommit=False, 78 | expire_on_commit=False, 79 | ) 80 | 81 | async def dispose(self) -> None: 82 | """ 83 | Закрытие соединения 84 | """ 85 | await self.engine.dispose() 86 | 87 | def get_scoped_session(self) -> AsyncSession: 88 | """ 89 | Получение сессии 90 | """ 91 | session = async_scoped_session( 92 | session_factory=self.session, 93 | scopefunc=current_task, 94 | ) 95 | return session 96 | 97 | async def session_geter(self) -> AsyncGenerator[AsyncSession, Any]: 98 | """ 99 | Получение генератора сессии 100 | 101 | Returns: 102 | AsyncGenerator[AsyncSession, Any]: Возвращает 103 | генератор с сессиями 104 | 105 | Yields: 106 | Iterator[AsyncGenerator[AsyncSession, Any]]: Генератор подает 107 | сессии 108 | """ 109 | session = self.get_scoped_session() 110 | yield session 111 | await session.remove() 112 | 113 | 114 | db_helper = DataBaseHelper() 115 | db_test = DataBaseHelper 116 | -------------------------------------------------------------------------------- /config/models/__init__.py: -------------------------------------------------------------------------------- 1 | from .user import User 2 | 3 | 4 | __all__ = ('User',) 5 | -------------------------------------------------------------------------------- /config/models/base.py: -------------------------------------------------------------------------------- 1 | from sqlalchemy.orm import (DeclarativeBase, 2 | Mapped, 3 | mapped_column, 4 | declared_attr, 5 | ) 6 | 7 | 8 | class Base(DeclarativeBase): 9 | """ 10 | Базовая модель для инициализации других моделей. 11 | 12 | Данная базовая модель является абстактной, и определяет 13 | базовое поведение других таблиц. 14 | 15 | ## Определение поведения таблиц: 16 | 17 | - Имя любой таблицы приводится к :method:`lower()` и 18 | добаляется `s` к окончанию. 19 | 20 | - Для каждой таблицы создается автогенерируемое поле `id` или `uid`, 21 | которое автоинкремирует счетчик интидификатора сущностей. 22 | 23 | ## Примеры: 24 | ```python 25 | from sqlalchemy.orm import Mapped, mapped_column 26 | from sqlalchemy import String 27 | from sqlalchemy.types import LargeBinary 28 | from datetime import date 29 | 30 | from config.models import Base 31 | 32 | 33 | class User(Base): 34 | name: Mapped[str] = mapped_column(String(length=100)) 35 | surname: Mapped[str] = mapped_column(String(length=200)) 36 | password: Mapped[str] = mapped_column(LargeBinary) 37 | active: Mapped[bool] = mapped_column(default=True) 38 | is_admin: Mapped[bool] = mapped_column(default=False) 39 | create_date: Mapped[datetime] = mapped_column( 40 | insert_default=func.now(), 41 | server_default=func.now(), 42 | ) 43 | login_date: Mapped[datetime | None] = mapped_column( 44 | default=None, 45 | server_default=None, 46 | nullable=True, 47 | ) 48 | ``` 49 | По итогу к классу :class:`User` будет добавленно поле `id` или `uid`, 50 | а так же в Базу данных таблица будет с названием `users`. 51 | """ 52 | __abstract__ = True 53 | 54 | @declared_attr.directive 55 | def __tablename__(cls) -> str: 56 | return cls.__name__.lower() + 's' 57 | 58 | id: Mapped[int] = mapped_column(primary_key=True) 59 | -------------------------------------------------------------------------------- /config/models/user.py: -------------------------------------------------------------------------------- 1 | from fastapi_users.db import SQLAlchemyBaseUserTable 2 | from sqlalchemy.orm import Mapped, mapped_column 3 | from sqlalchemy import Integer 4 | 5 | from config.models.base import Base 6 | 7 | 8 | class User(SQLAlchemyBaseUserTable[int], Base): 9 | """ 10 | Модель пользователя 11 | """ 12 | 13 | id: Mapped[int] = mapped_column(Integer, primary_key=True) 14 | -------------------------------------------------------------------------------- /config/setup_logs/__init__.py: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/sumaro2101/BaseFastAPI/292aae5f2ab3959d5116a3cc9f8ec0e5dcbfd822/config/setup_logs/__init__.py -------------------------------------------------------------------------------- /config/setup_logs/logging.py: -------------------------------------------------------------------------------- 1 | r""" 2 | Основной файл настройки логирования проектов 3 | """ 4 | 5 | import sys 6 | from loguru import logger 7 | 8 | from config import settings 9 | 10 | 11 | log_directory = settings.LOG_DIR 12 | log_directory.mkdir(parents=True, exist_ok=True) 13 | 14 | logger.remove() 15 | 16 | logger.add( 17 | log_directory.joinpath('access.log'), 18 | rotation='15MB', 19 | format='{time:YYYY-MM-DD HH:mm:ss} - {level} - {message}', 20 | encoding='utf-8', 21 | enqueue=True, 22 | level='DEBUG', 23 | diagnose=False, 24 | backtrace=False, 25 | colorize=False, 26 | filter=lambda record: record['level'].no < 40, 27 | ) 28 | 29 | logger.add( 30 | log_directory.joinpath('error.log'), 31 | rotation='15MB', 32 | format='{time:YYYY-MM-DD HH:mm:ss} - {level} - {message}', 33 | encoding='utf-8', 34 | enqueue=True, 35 | level='ERROR', 36 | diagnose=False, 37 | backtrace=False, 38 | colorize=False, 39 | ) 40 | 41 | logger.add( 42 | sink=sys.stderr, 43 | format='{time:YYYY-MM-DD HH:mm:ss} - {level} - {message}', 44 | enqueue=True, 45 | level='ERROR', 46 | diagnose=False, 47 | backtrace=False, 48 | colorize=False, 49 | ) 50 | 51 | logger.add( 52 | sink=sys.stdout, 53 | format='{time:YYYY-MM-DD HH:mm:ss} - {level} - {message}', 54 | enqueue=True, 55 | level='DEBUG', 56 | diagnose=False, 57 | backtrace=False, 58 | colorize=False, 59 | filter=lambda record: record['level'].no < 40, 60 | ) 61 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | services: 2 | fast_api: 3 | restart: always 4 | build: 5 | context: . 6 | dockerfile: ./docker/fastapi/Dockerfile 7 | volumes: 8 | - .:/app 9 | command: /start 10 | ports: 11 | - 8080:8000 12 | env_file: 13 | - .env 14 | depends_on: 15 | - db 16 | - rabbitmq 17 | - redis 18 | 19 | rabbitmq: 20 | hostname: rabbitmq 21 | image: rabbitmq:4.0.3-management 22 | env_file: 23 | - .env 24 | ports: 25 | - 5672:5672 26 | - 15672:15672 27 | volumes: 28 | - rabbitmq-data:/var/lib/rabbitmq 29 | depends_on: 30 | - db 31 | 32 | celery_worker: 33 | build: 34 | context: . 35 | dockerfile: ./docker/fastapi/Dockerfile 36 | command: /start-celeryworker 37 | volumes: 38 | - .:/app 39 | env_file: 40 | - .env 41 | depends_on: 42 | - rabbitmq 43 | - db 44 | - fast_api 45 | 46 | celery_beat: 47 | build: 48 | context: . 49 | dockerfile: ./docker/fastapi/Dockerfile 50 | command: /start-celerybeat 51 | volumes: 52 | - .:/app 53 | env_file: 54 | - .env 55 | depends_on: 56 | - rabbitmq 57 | - db 58 | - fast_api 59 | 60 | dashboard: 61 | build: 62 | context: . 63 | dockerfile: ./docker/fastapi/Dockerfile 64 | command: /start-flower 65 | volumes: 66 | - .:/app 67 | ports: 68 | - 5555:5555 69 | env_file: 70 | - .env 71 | depends_on: 72 | - celery_worker 73 | - rabbitmq 74 | - db 75 | - fast_api 76 | 77 | db: 78 | restart: always 79 | image: postgres:16.3-alpine 80 | volumes: 81 | - postgres_data:/var/lib/postgresql/data/ 82 | hostname: db 83 | env_file: 84 | - .env 85 | 86 | redis: 87 | restart: always 88 | image: redis:7.2.5-alpine 89 | expose: 90 | - 6379 91 | 92 | test_db: 93 | restart: always 94 | image: postgres:16.3-alpine 95 | volumes: 96 | - test_postgres_data:/var/lib/postgresql/data/ 97 | hostname: test_db 98 | expose: 99 | - 5431 100 | environment: 101 | - POSTGRES_DB=${TEST_POSTGRES_DB} 102 | - POSTGRES_PASSWORD=${TEST_POSTGRES_PASSWORD} 103 | 104 | prometheus: 105 | image: prom/prometheus 106 | container_name: prometheus 107 | volumes: 108 | - ./prometheus/prometheus.yml:/etc/prometheus/prometheus.yml 109 | - prometheus-data:/prometheus 110 | command: 111 | - '--config.file=/etc/prometheus/prometheus.yml' 112 | - '--storage.tsdb.path=/prometheus' 113 | - '--web.console.libraries=/etc/prometheus/console_libraries' 114 | - '--web.console.templates=/etc/prometheus/consoles' 115 | - '--storage.tsdb.retention.time=200h' 116 | - '--web.enable-lifecycle' 117 | restart: unless-stopped 118 | expose: 119 | - 9090 120 | networks: 121 | - monitor-net 122 | labels: 123 | org.label-schema.group: "monitoring" 124 | 125 | alertmanager: 126 | image: prom/alertmanager:v0.20.0 127 | container_name: alertmanager 128 | volumes: 129 | - ./alertmanager:/etc/alertmanager 130 | command: 131 | - '--config.file=/etc/alertmanager/config.yml' 132 | - '--storage.path=/alertmanager' 133 | restart: unless-stopped 134 | expose: 135 | - 9093 136 | networks: 137 | - monitor-net 138 | labels: 139 | org.label-schema.group: "monitoring" 140 | 141 | grafana: 142 | image: grafana/grafana 143 | container_name: grafana 144 | volumes: 145 | - grafana-data:/var/lib/grafana 146 | - ./grafana/provisioning:/etc/grafana/provisioning 147 | environment: 148 | - DATABASE_USER=${DB_USER} 149 | - DATABASE_PASS=${DB_PASSWORD} 150 | - DATABASE_NAME=${DB_NAME} 151 | - DATABASE_HOST=${DB_HOST} 152 | - DATABASE_SSL_MODE=disable 153 | - GF_USERS_ALLOW_SIGN_UP=false 154 | - GF_SECURITY_ADMIN_USER=${GF_SECURITY_ADMIN_USER} 155 | - GF_SECURITY_ADMIN_PASSWORD=${GF_SECURITY_ADMIN_PASSWORD} 156 | restart: unless-stopped 157 | expose: 158 | - 3000 159 | networks: 160 | - monitor-net 161 | labels: 162 | org.label-schema.group: "monitoring" 163 | 164 | nodeexporter: 165 | image: prom/node-exporter:v0.18.1 166 | container_name: nodeexporter 167 | volumes: 168 | - /proc:/host/proc:ro 169 | - /sys:/host/sys:ro 170 | - /:/rootfs:ro 171 | command: 172 | - '--path.procfs=/host/proc' 173 | - '--path.rootfs=/rootfs' 174 | - '--path.sysfs=/host/sys' 175 | - '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)' 176 | restart: unless-stopped 177 | expose: 178 | - 9100 179 | networks: 180 | - monitor-net 181 | labels: 182 | org.label-schema.group: "monitoring" 183 | 184 | pushgateway: 185 | image: prom/pushgateway:v1.2.0 186 | container_name: pushgateway 187 | restart: unless-stopped 188 | expose: 189 | - 9091 190 | networks: 191 | - monitor-net 192 | labels: 193 | org.label-schema.group: "monitoring" 194 | 195 | caddy: 196 | image: stefanprodan/caddy 197 | container_name: caddy 198 | ports: 199 | - "3000:3000" 200 | - "9090:9090" 201 | - "9093:9093" 202 | - "9091:9091" 203 | volumes: 204 | - ./caddy:/etc/caddy 205 | environment: 206 | - ADMIN_USER=${ADMIN_USER} 207 | - ADMIN_PASSWORD=${ADMIN_PASSWORD} 208 | restart: unless-stopped 209 | networks: 210 | - monitor-net 211 | labels: 212 | org.label-schema.group: "monitoring" 213 | 214 | volumes: 215 | postgres_data: 216 | test_postgres_data: 217 | rabbitmq-data: 218 | prometheus-data: {} 219 | grafana-data: {} 220 | 221 | networks: 222 | monitor-net: 223 | driver: bridge 224 | -------------------------------------------------------------------------------- /docker/celery/beat/start: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit 4 | set -o nounset 5 | 6 | celery -A config.celery.connection.app \ 7 | --broker=amqp://"${RABBITMQ_DEFAULT_USER}":"${RABBITMQ_DEFAULT_PASS}"@"${RMQ_HOST}":"${RMQ_PORT}" \ 8 | beat \ 9 | --loglevel=info 10 | -------------------------------------------------------------------------------- /docker/celery/flower/start: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit 4 | set -o nounset 5 | 6 | worker_ready() { 7 | celery -A config.celery.connection.app --broker=amqp://"${RABBITMQ_DEFAULT_USER}":"${RABBITMQ_DEFAULT_PASS}"@"${RMQ_HOST}":"${RMQ_PORT}" inspect ping 8 | } 9 | 10 | until worker_ready; do 11 | >&2 echo 'Celery workers not available' 12 | sleep 1 13 | done 14 | >&2 echo 'Celery workers is available' 15 | 16 | celery -A config.celery.connection.app \ 17 | --broker=amqp://"${RABBITMQ_DEFAULT_USER}":"${RABBITMQ_DEFAULT_PASS}"@"${RMQ_HOST}":"${RMQ_PORT}" \ 18 | flower 19 | -------------------------------------------------------------------------------- /docker/celery/worker/start: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit 4 | set -o nounset 5 | 6 | celery -A config.celery.connection.app \ 7 | --broker=amqp://"${RABBITMQ_DEFAULT_USER}":"${RABBITMQ_DEFAULT_PASS}"@"${RMQ_HOST}":"${RMQ_PORT}" \ 8 | worker \ 9 | --loglevel=info 10 | -------------------------------------------------------------------------------- /docker/fastapi/Dockerfile: -------------------------------------------------------------------------------- 1 | FROM python:3.12-slim 2 | 3 | ENV PYTHONUNBUFFERED 1 4 | ENV PYTHONDONTWRITEBYTECODE 1 5 | ENV POETRY_V=1.8.3 6 | ENV POETRY_VIRTUALENVS_CREATE=false 7 | ENV PYTHONPATH=/app 8 | 9 | RUN adduser user \ 10 | && addgroup docker \ 11 | && adduser user docker 12 | 13 | RUN pip install poetry 14 | 15 | COPY pyproject.toml ./ 16 | 17 | RUN poetry install --no-root 18 | 19 | RUN apt-get update \ 20 | && pip install --upgrade pip \ 21 | && apt-get install -y build-essential \ 22 | && apt-get install -y libpq-dev \ 23 | && apt-get install -y gettext \ 24 | && apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false \ 25 | && rm -rf /var/lib/apt/lists/* 26 | 27 | 28 | COPY ./docker/fastapi/entrypoint /entrypoint 29 | RUN sed -i 's/\r$//g' /entrypoint 30 | RUN chmod +x /entrypoint 31 | 32 | COPY ./docker/fastapi/start /start 33 | RUN sed -i 's/\r$//g' /start 34 | RUN chmod +x /start 35 | 36 | COPY ./docker/celery/worker/start /start-celeryworker 37 | RUN sed -i 's/\r$//g' /start-celeryworker 38 | RUN chmod +x /start-celeryworker 39 | 40 | COPY ./docker/celery/beat/start /start-celerybeat 41 | RUN sed -i 's/\r$//g' /start-celerybeat 42 | RUN chmod +x /start-celerybeat 43 | 44 | COPY ./docker/celery/flower/start /start-flower 45 | RUN sed -i 's/\r$//g' /start-flower 46 | RUN chmod +x /start-flower 47 | 48 | WORKDIR /app 49 | 50 | RUN chown -R user:user . 51 | 52 | USER user -------------------------------------------------------------------------------- /docker/fastapi/entrypoint: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit 4 | 5 | set -o pipefail 6 | 7 | set -o nounset 8 | 9 | postgres_ready() { 10 | python << END 11 | import sys 12 | 13 | import psycopg2 14 | 15 | try: 16 | psycopg2.connect( 17 | dbname="${DB_NAME}", 18 | user="${DB_USER}", 19 | password="${DB_PASSWORD}", 20 | host="${DB_HOST}", 21 | port="${DB_PORT}", 22 | ) 23 | except psycopg2.OperationalError: 24 | sys.exit(-1) 25 | sys.exit(0) 26 | 27 | END 28 | } 29 | until postgres_ready; do 30 | >&2 echo 'Waiting for PostgreSQL to become available...' 31 | sleep 1 32 | done 33 | >&2 echo 'PostgreSQL is available' 34 | 35 | exec "$@" -------------------------------------------------------------------------------- /docker/fastapi/start: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | set -o errexit 4 | set -o pipefail 5 | set -o nounset 6 | 7 | alembic upgrade head 8 | 9 | uvicorn main:app --host 0.0.0.0 --port 8000 -------------------------------------------------------------------------------- /grafana/provisioning/dashboards/dashboard.yml: -------------------------------------------------------------------------------- 1 | apiVersion: 1 2 | 3 | providers: 4 | - name: 'Prometheus' 5 | orgId: 1 6 | folder: '' 7 | type: file 8 | disableDeletion: false 9 | editable: true 10 | allowUiUpdates: true 11 | options: 12 | path: /etc/grafana/provisioning/dashboards 13 | -------------------------------------------------------------------------------- /grafana/provisioning/dashboards/docker_containers.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": null, 3 | "title": "Docker Containers", 4 | "description": "Containers metrics", 5 | "tags": [ 6 | "docker" 7 | ], 8 | "style": "dark", 9 | "timezone": "browser", 10 | "editable": true, 11 | "hideControls": false, 12 | "sharedCrosshair": true, 13 | "rows": [ 14 | { 15 | "collapse": false, 16 | "editable": true, 17 | "height": "150px", 18 | "panels": [ 19 | { 20 | "cacheTimeout": null, 21 | "colorBackground": false, 22 | "colorValue": false, 23 | "colors": [ 24 | "rgba(50, 172, 45, 0.97)", 25 | "rgba(237, 129, 40, 0.89)", 26 | "rgba(245, 54, 54, 0.9)" 27 | ], 28 | "datasource": "Prometheus", 29 | "decimals": 2, 30 | "editable": true, 31 | "error": false, 32 | "format": "percent", 33 | "gauge": { 34 | "maxValue": 100, 35 | "minValue": 0, 36 | "show": true, 37 | "thresholdLabels": false, 38 | "thresholdMarkers": true 39 | }, 40 | "id": 4, 41 | "interval": null, 42 | "isNew": true, 43 | "links": [], 44 | "mappingType": 1, 45 | "mappingTypes": [ 46 | { 47 | "name": "value to text", 48 | "value": 1 49 | }, 50 | { 51 | "name": "range to text", 52 | "value": 2 53 | } 54 | ], 55 | "maxDataPoints": 100, 56 | "nullPointMode": "connected", 57 | "nullText": null, 58 | "postfix": "", 59 | "postfixFontSize": "50%", 60 | "prefix": "", 61 | "prefixFontSize": "50%", 62 | "rangeMaps": [ 63 | { 64 | "from": "null", 65 | "text": "N/A", 66 | "to": "null" 67 | } 68 | ], 69 | "span": 2, 70 | "sparkline": { 71 | "fillColor": "rgba(31, 118, 189, 0.18)", 72 | "full": false, 73 | "lineColor": "rgb(31, 120, 193)", 74 | "show": false 75 | }, 76 | "targets": [ 77 | { 78 | "expr": "sum(rate(container_cpu_user_seconds_total{image!=\"\"}[1m])) / count(node_cpu_seconds_total{mode=\"user\"}) * 100", 79 | "interval": "10s", 80 | "intervalFactor": 1, 81 | "legendFormat": "", 82 | "refId": "A", 83 | "step": 10 84 | } 85 | ], 86 | "thresholds": "65, 90", 87 | "title": "CPU Load", 88 | "transparent": false, 89 | "type": "singlestat", 90 | "valueFontSize": "80%", 91 | "valueMaps": [ 92 | { 93 | "op": "=", 94 | "text": "N/A", 95 | "value": "null" 96 | } 97 | ], 98 | "valueName": "avg", 99 | "timeFrom": "10s", 100 | "hideTimeOverride": true 101 | }, 102 | { 103 | "cacheTimeout": null, 104 | "colorBackground": false, 105 | "colorValue": false, 106 | "colors": [ 107 | "rgba(245, 54, 54, 0.9)", 108 | "rgba(237, 129, 40, 0.89)", 109 | "rgba(50, 172, 45, 0.97)" 110 | ], 111 | "datasource": "Prometheus", 112 | "editable": true, 113 | "error": false, 114 | "format": "none", 115 | "gauge": { 116 | "maxValue": 100, 117 | "minValue": 0, 118 | "show": false, 119 | "thresholdLabels": false, 120 | "thresholdMarkers": true 121 | }, 122 | "id": 7, 123 | "interval": null, 124 | "isNew": true, 125 | "links": [], 126 | "mappingType": 1, 127 | "mappingTypes": [ 128 | { 129 | "name": "value to text", 130 | "value": 1 131 | }, 132 | { 133 | "name": "range to text", 134 | "value": 2 135 | } 136 | ], 137 | "maxDataPoints": 100, 138 | "nullPointMode": "connected", 139 | "nullText": null, 140 | "postfix": "", 141 | "postfixFontSize": "50%", 142 | "prefix": "", 143 | "prefixFontSize": "50%", 144 | "rangeMaps": [ 145 | { 146 | "from": "null", 147 | "text": "N/A", 148 | "to": "null" 149 | } 150 | ], 151 | "span": 2, 152 | "sparkline": { 153 | "fillColor": "rgba(31, 118, 189, 0.18)", 154 | "full": false, 155 | "lineColor": "rgb(31, 120, 193)", 156 | "show": false 157 | }, 158 | "targets": [ 159 | { 160 | "expr": "machine_cpu_cores", 161 | "interval": "", 162 | "intervalFactor": 2, 163 | "legendFormat": "", 164 | "metric": "machine_cpu_cores", 165 | "refId": "A", 166 | "step": 20 167 | } 168 | ], 169 | "thresholds": "", 170 | "title": "CPU Cores", 171 | "type": "singlestat", 172 | "valueFontSize": "80%", 173 | "valueMaps": [ 174 | { 175 | "op": "=", 176 | "text": "N/A", 177 | "value": "null" 178 | } 179 | ], 180 | "valueName": "avg" 181 | }, 182 | { 183 | "cacheTimeout": null, 184 | "colorBackground": false, 185 | "colorValue": false, 186 | "colors": [ 187 | "rgba(50, 172, 45, 0.97)", 188 | "rgba(237, 129, 40, 0.89)", 189 | "rgba(245, 54, 54, 0.9)" 190 | ], 191 | "datasource": "Prometheus", 192 | "editable": true, 193 | "error": false, 194 | "format": "percent", 195 | "gauge": { 196 | "maxValue": 100, 197 | "minValue": 0, 198 | "show": true, 199 | "thresholdLabels": false, 200 | "thresholdMarkers": true 201 | }, 202 | "id": 5, 203 | "interval": null, 204 | "isNew": true, 205 | "links": [], 206 | "mappingType": 1, 207 | "mappingTypes": [ 208 | { 209 | "name": "value to text", 210 | "value": 1 211 | }, 212 | { 213 | "name": "range to text", 214 | "value": 2 215 | } 216 | ], 217 | "maxDataPoints": 100, 218 | "nullPointMode": "connected", 219 | "nullText": null, 220 | "postfix": "", 221 | "postfixFontSize": "50%", 222 | "prefix": "", 223 | "prefixFontSize": "50%", 224 | "rangeMaps": [ 225 | { 226 | "from": "null", 227 | "text": "N/A", 228 | "to": "null" 229 | } 230 | ], 231 | "span": 2, 232 | "sparkline": { 233 | "fillColor": "rgba(31, 118, 189, 0.18)", 234 | "full": false, 235 | "lineColor": "rgb(31, 120, 193)", 236 | "show": false 237 | }, 238 | "targets": [ 239 | { 240 | "expr": "(sum(node_memory_MemTotal_bytes) - sum(node_memory_MemFree_bytes+node_memory_Buffers_bytes+node_memory_Cached_bytes) ) / sum(node_memory_MemTotal_bytes) * 100", 241 | "interval": "10s", 242 | "intervalFactor": 2, 243 | "legendFormat": "", 244 | "refId": "A", 245 | "step": 20 246 | } 247 | ], 248 | "thresholds": "65, 90", 249 | "title": "Memory Load", 250 | "transparent": false, 251 | "type": "singlestat", 252 | "valueFontSize": "80%", 253 | "valueMaps": [ 254 | { 255 | "op": "=", 256 | "text": "N/A", 257 | "value": "null" 258 | } 259 | ], 260 | "valueName": "avg", 261 | "timeFrom": "10s", 262 | "hideTimeOverride": true 263 | }, 264 | { 265 | "cacheTimeout": null, 266 | "colorBackground": false, 267 | "colorValue": false, 268 | "colors": [ 269 | "rgba(245, 54, 54, 0.9)", 270 | "rgba(237, 129, 40, 0.89)", 271 | "rgba(50, 172, 45, 0.97)" 272 | ], 273 | "datasource": "Prometheus", 274 | "decimals": 2, 275 | "editable": true, 276 | "error": false, 277 | "format": "bytes", 278 | "gauge": { 279 | "maxValue": 100, 280 | "minValue": 0, 281 | "show": false, 282 | "thresholdLabels": false, 283 | "thresholdMarkers": true 284 | }, 285 | "id": 2, 286 | "interval": null, 287 | "isNew": true, 288 | "links": [], 289 | "mappingType": 1, 290 | "mappingTypes": [ 291 | { 292 | "name": "value to text", 293 | "value": 1 294 | }, 295 | { 296 | "name": "range to text", 297 | "value": 2 298 | } 299 | ], 300 | "maxDataPoints": 100, 301 | "nullPointMode": "connected", 302 | "nullText": null, 303 | "postfix": "", 304 | "postfixFontSize": "50%", 305 | "prefix": "", 306 | "prefixFontSize": "50%", 307 | "rangeMaps": [ 308 | { 309 | "from": "null", 310 | "text": "N/A", 311 | "to": "null" 312 | } 313 | ], 314 | "span": 2, 315 | "sparkline": { 316 | "fillColor": "rgba(31, 118, 189, 0.18)", 317 | "full": false, 318 | "lineColor": "rgb(31, 120, 193)", 319 | "show": false 320 | }, 321 | "targets": [ 322 | { 323 | "expr": "sum(container_memory_usage_bytes{image!=\"\"})", 324 | "interval": "10s", 325 | "intervalFactor": 2, 326 | "legendFormat": "", 327 | "refId": "A", 328 | "step": 20 329 | } 330 | ], 331 | "thresholds": "", 332 | "timeFrom": "10s", 333 | "title": "Used Memory", 334 | "transparent": false, 335 | "type": "singlestat", 336 | "valueFontSize": "80%", 337 | "valueMaps": [ 338 | { 339 | "op": "=", 340 | "text": "N/A", 341 | "value": "null" 342 | } 343 | ], 344 | "valueName": "avg", 345 | "hideTimeOverride": true 346 | }, 347 | { 348 | "cacheTimeout": null, 349 | "colorBackground": false, 350 | "colorValue": false, 351 | "colors": [ 352 | "rgba(50, 172, 45, 0.97)", 353 | "rgba(237, 129, 40, 0.89)", 354 | "rgba(245, 54, 54, 0.9)" 355 | ], 356 | "datasource": "Prometheus", 357 | "decimals": null, 358 | "editable": true, 359 | "error": false, 360 | "format": "percent", 361 | "gauge": { 362 | "maxValue": 100, 363 | "minValue": 0, 364 | "show": true, 365 | "thresholdLabels": false, 366 | "thresholdMarkers": true 367 | }, 368 | "id": 6, 369 | "interval": null, 370 | "isNew": true, 371 | "links": [], 372 | "mappingType": 1, 373 | "mappingTypes": [ 374 | { 375 | "name": "value to text", 376 | "value": 1 377 | }, 378 | { 379 | "name": "range to text", 380 | "value": 2 381 | } 382 | ], 383 | "maxDataPoints": 100, 384 | "nullPointMode": "connected", 385 | "nullText": null, 386 | "postfix": "", 387 | "postfixFontSize": "50%", 388 | "prefix": "", 389 | "prefixFontSize": "50%", 390 | "rangeMaps": [ 391 | { 392 | "from": "null", 393 | "text": "N/A", 394 | "to": "null" 395 | } 396 | ], 397 | "span": 2, 398 | "sparkline": { 399 | "fillColor": "rgba(31, 118, 189, 0.18)", 400 | "full": false, 401 | "lineColor": "rgb(31, 120, 193)", 402 | "show": false 403 | }, 404 | "targets": [ 405 | { 406 | "expr": "(node_filesystem_size_bytes{fstype=\"aufs\"} - node_filesystem_free_bytes{fstype=\"aufs\"}) / node_filesystem_size_bytes{fstype=\"aufs\"} * 100", 407 | "interval": "30s", 408 | "intervalFactor": 1, 409 | "legendFormat": "", 410 | "refId": "A", 411 | "step": 30 412 | } 413 | ], 414 | "thresholds": "65, 90", 415 | "title": "Storage Load", 416 | "transparent": false, 417 | "type": "singlestat", 418 | "valueFontSize": "80%", 419 | "valueMaps": [ 420 | { 421 | "op": "=", 422 | "text": "N/A", 423 | "value": "null" 424 | } 425 | ], 426 | "valueName": "avg", 427 | "timeFrom": "10s", 428 | "hideTimeOverride": true 429 | }, 430 | { 431 | "cacheTimeout": null, 432 | "colorBackground": false, 433 | "colorValue": false, 434 | "colors": [ 435 | "rgba(245, 54, 54, 0.9)", 436 | "rgba(237, 129, 40, 0.89)", 437 | "rgba(50, 172, 45, 0.97)" 438 | ], 439 | "datasource": "Prometheus", 440 | "decimals": 2, 441 | "editable": true, 442 | "error": false, 443 | "format": "bytes", 444 | "gauge": { 445 | "maxValue": 100, 446 | "minValue": 0, 447 | "show": false, 448 | "thresholdLabels": false, 449 | "thresholdMarkers": true 450 | }, 451 | "id": 3, 452 | "interval": null, 453 | "isNew": true, 454 | "links": [], 455 | "mappingType": 1, 456 | "mappingTypes": [ 457 | { 458 | "name": "value to text", 459 | "value": 1 460 | }, 461 | { 462 | "name": "range to text", 463 | "value": 2 464 | } 465 | ], 466 | "maxDataPoints": 100, 467 | "nullPointMode": "connected", 468 | "nullText": null, 469 | "postfix": "", 470 | "postfixFontSize": "50%", 471 | "prefix": "", 472 | "prefixFontSize": "50%", 473 | "rangeMaps": [ 474 | { 475 | "from": "null", 476 | "text": "N/A", 477 | "to": "null" 478 | } 479 | ], 480 | "span": 2, 481 | "sparkline": { 482 | "fillColor": "rgba(31, 118, 189, 0.18)", 483 | "full": false, 484 | "lineColor": "rgb(31, 120, 193)", 485 | "show": false 486 | }, 487 | "targets": [ 488 | { 489 | "expr": "sum(container_fs_usage_bytes)", 490 | "interval": "30s", 491 | "intervalFactor": 2, 492 | "refId": "A", 493 | "step": 60 494 | } 495 | ], 496 | "thresholds": "", 497 | "title": "Used Storage", 498 | "transparent": false, 499 | "type": "singlestat", 500 | "valueFontSize": "80%", 501 | "valueMaps": [ 502 | { 503 | "op": "=", 504 | "text": "N/A", 505 | "value": "null" 506 | } 507 | ], 508 | "valueName": "avg", 509 | "timeFrom": "10s", 510 | "hideTimeOverride": true 511 | } 512 | ], 513 | "title": "Overview" 514 | }, 515 | { 516 | "collapse": false, 517 | "editable": true, 518 | "height": "150px", 519 | "panels": [ 520 | { 521 | "aliasColors": {}, 522 | "bars": true, 523 | "datasource": "Prometheus", 524 | "decimals": 0, 525 | "editable": true, 526 | "error": false, 527 | "fill": 1, 528 | "grid": { 529 | "threshold1": null, 530 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 531 | "threshold2": null, 532 | "threshold2Color": "rgba(234, 112, 112, 0.22)", 533 | "thresholdLine": false 534 | }, 535 | "id": 9, 536 | "isNew": true, 537 | "legend": { 538 | "avg": false, 539 | "current": false, 540 | "max": false, 541 | "min": false, 542 | "show": false, 543 | "total": false, 544 | "values": false 545 | }, 546 | "lines": false, 547 | "linewidth": 2, 548 | "links": [], 549 | "nullPointMode": "connected", 550 | "percentage": false, 551 | "pointradius": 5, 552 | "points": false, 553 | "renderer": "flot", 554 | "seriesOverrides": [], 555 | "span": 4, 556 | "stack": false, 557 | "steppedLine": false, 558 | "targets": [ 559 | { 560 | "expr": "scalar(count(container_memory_usage_bytes{image!=\"\"}) > 0)", 561 | "interval": "", 562 | "intervalFactor": 2, 563 | "legendFormat": "containers", 564 | "refId": "A", 565 | "step": 2 566 | } 567 | ], 568 | "timeFrom": null, 569 | "timeShift": null, 570 | "title": "Running Containers", 571 | "tooltip": { 572 | "msResolution": true, 573 | "shared": true, 574 | "sort": 0, 575 | "value_type": "cumulative" 576 | }, 577 | "type": "graph", 578 | "xaxis": { 579 | "show": true 580 | }, 581 | "yaxes": [ 582 | { 583 | "format": "none", 584 | "label": "", 585 | "logBase": 1, 586 | "max": null, 587 | "min": 0, 588 | "show": true 589 | }, 590 | { 591 | "format": "short", 592 | "label": null, 593 | "logBase": 1, 594 | "max": null, 595 | "min": null, 596 | "show": false 597 | } 598 | ] 599 | }, 600 | { 601 | "aliasColors": {}, 602 | "bars": true, 603 | "datasource": "Prometheus", 604 | "decimals": 2, 605 | "editable": true, 606 | "error": false, 607 | "fill": 1, 608 | "grid": { 609 | "threshold1": null, 610 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 611 | "threshold2": null, 612 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 613 | }, 614 | "id": 10, 615 | "isNew": true, 616 | "legend": { 617 | "avg": false, 618 | "current": false, 619 | "max": false, 620 | "min": false, 621 | "show": false, 622 | "total": false, 623 | "values": false 624 | }, 625 | "lines": false, 626 | "linewidth": 2, 627 | "links": [], 628 | "nullPointMode": "connected", 629 | "percentage": false, 630 | "pointradius": 5, 631 | "points": false, 632 | "renderer": "flot", 633 | "seriesOverrides": [ 634 | { 635 | "alias": "load 1m", 636 | "color": "#BF1B00" 637 | } 638 | ], 639 | "span": 4, 640 | "stack": false, 641 | "steppedLine": false, 642 | "targets": [ 643 | { 644 | "expr": "node_load1", 645 | "interval": "", 646 | "intervalFactor": 2, 647 | "legendFormat": "load 1m", 648 | "metric": "node_load1", 649 | "refId": "A", 650 | "step": 2 651 | } 652 | ], 653 | "timeFrom": null, 654 | "timeShift": null, 655 | "title": "System Load", 656 | "tooltip": { 657 | "msResolution": true, 658 | "shared": true, 659 | "sort": 0, 660 | "value_type": "cumulative" 661 | }, 662 | "type": "graph", 663 | "xaxis": { 664 | "show": true 665 | }, 666 | "yaxes": [ 667 | { 668 | "format": "short", 669 | "label": null, 670 | "logBase": 1, 671 | "max": null, 672 | "min": 0, 673 | "show": true 674 | }, 675 | { 676 | "format": "short", 677 | "label": null, 678 | "logBase": 1, 679 | "max": null, 680 | "min": null, 681 | "show": false 682 | } 683 | ] 684 | }, 685 | { 686 | "aliasColors": {}, 687 | "bars": false, 688 | "datasource": "Prometheus", 689 | "editable": true, 690 | "error": false, 691 | "fill": 1, 692 | "grid": { 693 | "threshold1": null, 694 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 695 | "threshold2": null, 696 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 697 | }, 698 | "id": 15, 699 | "isNew": true, 700 | "legend": { 701 | "alignAsTable": true, 702 | "avg": true, 703 | "current": false, 704 | "max": true, 705 | "min": true, 706 | "rightSide": true, 707 | "show": false, 708 | "total": false, 709 | "values": true 710 | }, 711 | "lines": true, 712 | "linewidth": 2, 713 | "links": [], 714 | "nullPointMode": "connected", 715 | "percentage": false, 716 | "pointradius": 5, 717 | "points": false, 718 | "renderer": "flot", 719 | "seriesOverrides": [ 720 | { 721 | "alias": "read", 722 | "yaxis": 1 723 | }, 724 | { 725 | "alias": "written", 726 | "yaxis": 1 727 | }, 728 | { 729 | "alias": "io time", 730 | "yaxis": 2 731 | } 732 | ], 733 | "span": 4, 734 | "stack": false, 735 | "steppedLine": false, 736 | "targets": [ 737 | { 738 | "expr": "sum(irate(node_disk_read_bytes_total[5m]))", 739 | "interval": "2s", 740 | "intervalFactor": 4, 741 | "legendFormat": "read", 742 | "metric": "", 743 | "refId": "A", 744 | "step": 8 745 | }, 746 | { 747 | "expr": "sum(irate(node_disk_written_bytes_total[5m]))", 748 | "interval": "2s", 749 | "intervalFactor": 4, 750 | "legendFormat": "written", 751 | "metric": "", 752 | "refId": "B", 753 | "step": 8 754 | }, 755 | { 756 | "expr": "sum(irate(node_disk_io_time_seconds_total[5m]))", 757 | "interval": "2s", 758 | "intervalFactor": 4, 759 | "legendFormat": "io time", 760 | "metric": "", 761 | "refId": "C", 762 | "step": 8 763 | } 764 | ], 765 | "timeFrom": null, 766 | "timeShift": null, 767 | "title": "I/O Usage", 768 | "tooltip": { 769 | "msResolution": true, 770 | "shared": true, 771 | "sort": 0, 772 | "value_type": "cumulative" 773 | }, 774 | "type": "graph", 775 | "xaxis": { 776 | "show": true 777 | }, 778 | "yaxes": [ 779 | { 780 | "format": "bytes", 781 | "label": null, 782 | "logBase": 1, 783 | "max": null, 784 | "min": null, 785 | "show": true 786 | }, 787 | { 788 | "format": "ms", 789 | "label": null, 790 | "logBase": 1, 791 | "max": null, 792 | "min": null, 793 | "show": true 794 | } 795 | ] 796 | } 797 | ], 798 | "title": "Host stats" 799 | }, 800 | { 801 | "collapse": false, 802 | "editable": true, 803 | "height": "250px", 804 | "panels": [ 805 | { 806 | "aliasColors": {}, 807 | "bars": false, 808 | "datasource": "Prometheus", 809 | "decimals": 2, 810 | "editable": true, 811 | "error": false, 812 | "fill": 1, 813 | "grid": { 814 | "threshold1": null, 815 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 816 | "threshold2": null, 817 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 818 | }, 819 | "id": 8, 820 | "isNew": true, 821 | "legend": { 822 | "alignAsTable": true, 823 | "avg": true, 824 | "current": false, 825 | "max": true, 826 | "min": true, 827 | "rightSide": true, 828 | "show": true, 829 | "total": false, 830 | "values": true 831 | }, 832 | "lines": true, 833 | "linewidth": 2, 834 | "links": [], 835 | "nullPointMode": "connected", 836 | "percentage": false, 837 | "pointradius": 5, 838 | "points": false, 839 | "renderer": "flot", 840 | "seriesOverrides": [], 841 | "span": 12, 842 | "stack": false, 843 | "steppedLine": false, 844 | "targets": [ 845 | { 846 | "expr": "sum by (name) (rate(container_cpu_usage_seconds_total{image!=\"\"}[1m])) / scalar(count(node_cpu_seconds_total{mode=\"user\"})) * 100", 847 | "intervalFactor": 10, 848 | "legendFormat": "{{ name }}", 849 | "metric": "container_cpu_user_seconds_total", 850 | "refId": "A", 851 | "step": 10 852 | } 853 | ], 854 | "timeFrom": null, 855 | "timeShift": null, 856 | "title": "Container CPU Usage", 857 | "tooltip": { 858 | "msResolution": true, 859 | "shared": true, 860 | "sort": 2, 861 | "value_type": "cumulative" 862 | }, 863 | "type": "graph", 864 | "xaxis": { 865 | "show": true 866 | }, 867 | "yaxes": [ 868 | { 869 | "format": "percent", 870 | "label": null, 871 | "logBase": 1, 872 | "max": null, 873 | "min": 0, 874 | "show": true 875 | }, 876 | { 877 | "format": "short", 878 | "label": null, 879 | "logBase": 1, 880 | "max": null, 881 | "min": null, 882 | "show": false 883 | } 884 | ] 885 | } 886 | ], 887 | "title": "CPU" 888 | }, 889 | { 890 | "collapse": false, 891 | "editable": true, 892 | "height": "250px", 893 | "panels": [ 894 | { 895 | "aliasColors": {}, 896 | "bars": false, 897 | "datasource": "Prometheus", 898 | "decimals": 2, 899 | "editable": true, 900 | "error": false, 901 | "fill": 1, 902 | "grid": { 903 | "threshold1": null, 904 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 905 | "threshold2": null, 906 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 907 | }, 908 | "id": 11, 909 | "isNew": true, 910 | "legend": { 911 | "alignAsTable": true, 912 | "avg": true, 913 | "current": false, 914 | "max": true, 915 | "min": true, 916 | "rightSide": true, 917 | "show": true, 918 | "total": false, 919 | "values": true 920 | }, 921 | "lines": true, 922 | "linewidth": 2, 923 | "links": [], 924 | "nullPointMode": "connected", 925 | "percentage": false, 926 | "pointradius": 5, 927 | "points": false, 928 | "renderer": "flot", 929 | "seriesOverrides": [], 930 | "span": 12, 931 | "stack": false, 932 | "steppedLine": false, 933 | "targets": [ 934 | { 935 | "expr": "sum by (name)(container_memory_usage_bytes{image!=\"\"})", 936 | "intervalFactor": 1, 937 | "legendFormat": "{{ name }}", 938 | "metric": "container_memory_usage", 939 | "refId": "A", 940 | "step": 1 941 | } 942 | ], 943 | "timeFrom": null, 944 | "timeShift": null, 945 | "title": "Container Memory Usage", 946 | "tooltip": { 947 | "msResolution": true, 948 | "shared": true, 949 | "sort": 0, 950 | "value_type": "cumulative" 951 | }, 952 | "type": "graph", 953 | "xaxis": { 954 | "show": true 955 | }, 956 | "yaxes": [ 957 | { 958 | "format": "bytes", 959 | "label": null, 960 | "logBase": 1, 961 | "max": null, 962 | "min": 0, 963 | "show": true 964 | }, 965 | { 966 | "format": "short", 967 | "label": null, 968 | "logBase": 1, 969 | "max": null, 970 | "min": null, 971 | "show": false 972 | } 973 | ] 974 | }, 975 | { 976 | "aliasColors": {}, 977 | "bars": false, 978 | "datasource": "Prometheus", 979 | "decimals": 2, 980 | "editable": true, 981 | "error": false, 982 | "fill": 1, 983 | "grid": { 984 | "threshold1": null, 985 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 986 | "threshold2": null, 987 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 988 | }, 989 | "id": 12, 990 | "isNew": true, 991 | "legend": { 992 | "alignAsTable": true, 993 | "avg": true, 994 | "current": false, 995 | "max": true, 996 | "min": true, 997 | "rightSide": true, 998 | "show": true, 999 | "total": false, 1000 | "values": true 1001 | }, 1002 | "lines": true, 1003 | "linewidth": 2, 1004 | "links": [], 1005 | "nullPointMode": "connected", 1006 | "percentage": false, 1007 | "pointradius": 5, 1008 | "points": false, 1009 | "renderer": "flot", 1010 | "seriesOverrides": [], 1011 | "span": 12, 1012 | "stack": false, 1013 | "steppedLine": false, 1014 | "targets": [ 1015 | { 1016 | "expr": "sum by (name) (container_memory_cache{image!=\"\"})", 1017 | "intervalFactor": 2, 1018 | "legendFormat": "{{name}}", 1019 | "metric": "container_memory_cache", 1020 | "refId": "A", 1021 | "step": 2 1022 | } 1023 | ], 1024 | "timeFrom": null, 1025 | "timeShift": null, 1026 | "title": "Container Cached Memory Usage", 1027 | "tooltip": { 1028 | "msResolution": true, 1029 | "shared": true, 1030 | "sort": 0, 1031 | "value_type": "cumulative" 1032 | }, 1033 | "type": "graph", 1034 | "xaxis": { 1035 | "show": true 1036 | }, 1037 | "yaxes": [ 1038 | { 1039 | "format": "bytes", 1040 | "label": null, 1041 | "logBase": 1, 1042 | "max": null, 1043 | "min": 0, 1044 | "show": true 1045 | }, 1046 | { 1047 | "format": "short", 1048 | "label": null, 1049 | "logBase": 1, 1050 | "max": null, 1051 | "min": null, 1052 | "show": false 1053 | } 1054 | ] 1055 | } 1056 | ], 1057 | "title": "Memory" 1058 | }, 1059 | { 1060 | "collapse": false, 1061 | "editable": true, 1062 | "height": "250px", 1063 | "panels": [ 1064 | { 1065 | "aliasColors": {}, 1066 | "bars": false, 1067 | "datasource": "Prometheus", 1068 | "decimals": 2, 1069 | "editable": true, 1070 | "error": false, 1071 | "fill": 1, 1072 | "grid": { 1073 | "threshold1": null, 1074 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 1075 | "threshold2": null, 1076 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 1077 | }, 1078 | "id": 13, 1079 | "isNew": true, 1080 | "legend": { 1081 | "alignAsTable": true, 1082 | "avg": true, 1083 | "current": false, 1084 | "max": true, 1085 | "min": true, 1086 | "rightSide": true, 1087 | "show": true, 1088 | "total": false, 1089 | "values": true 1090 | }, 1091 | "lines": true, 1092 | "linewidth": 2, 1093 | "links": [], 1094 | "nullPointMode": "connected", 1095 | "percentage": false, 1096 | "pointradius": 5, 1097 | "points": false, 1098 | "renderer": "flot", 1099 | "seriesOverrides": [], 1100 | "span": 12, 1101 | "stack": false, 1102 | "steppedLine": false, 1103 | "targets": [ 1104 | { 1105 | "expr": "sum by (name) (rate(container_network_receive_bytes_total{image!=\"\"}[1m]))", 1106 | "intervalFactor": 10, 1107 | "legendFormat": "{{ name }}", 1108 | "metric": "container_network_receive_bytes_total", 1109 | "refId": "A", 1110 | "step": 10 1111 | } 1112 | ], 1113 | "timeFrom": null, 1114 | "timeShift": null, 1115 | "title": "Container Network Input", 1116 | "tooltip": { 1117 | "msResolution": true, 1118 | "shared": true, 1119 | "sort": 2, 1120 | "value_type": "cumulative" 1121 | }, 1122 | "type": "graph", 1123 | "xaxis": { 1124 | "show": true 1125 | }, 1126 | "yaxes": [ 1127 | { 1128 | "format": "bytes", 1129 | "label": null, 1130 | "logBase": 1, 1131 | "max": null, 1132 | "min": 0, 1133 | "show": true 1134 | }, 1135 | { 1136 | "format": "short", 1137 | "label": null, 1138 | "logBase": 1, 1139 | "max": null, 1140 | "min": null, 1141 | "show": false 1142 | } 1143 | ] 1144 | }, 1145 | { 1146 | "aliasColors": {}, 1147 | "bars": false, 1148 | "datasource": "Prometheus", 1149 | "decimals": 2, 1150 | "editable": true, 1151 | "error": false, 1152 | "fill": 1, 1153 | "grid": { 1154 | "threshold1": null, 1155 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 1156 | "threshold2": null, 1157 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 1158 | }, 1159 | "id": 14, 1160 | "isNew": true, 1161 | "legend": { 1162 | "alignAsTable": true, 1163 | "avg": true, 1164 | "current": false, 1165 | "max": true, 1166 | "min": true, 1167 | "rightSide": true, 1168 | "show": true, 1169 | "total": false, 1170 | "values": true 1171 | }, 1172 | "lines": true, 1173 | "linewidth": 2, 1174 | "links": [], 1175 | "nullPointMode": "connected", 1176 | "percentage": false, 1177 | "pointradius": 5, 1178 | "points": false, 1179 | "renderer": "flot", 1180 | "seriesOverrides": [], 1181 | "span": 12, 1182 | "stack": false, 1183 | "steppedLine": false, 1184 | "targets": [ 1185 | { 1186 | "expr": "sum by (name) (rate(container_network_transmit_bytes_total{image!=\"\"}[1m]))", 1187 | "intervalFactor": 10, 1188 | "legendFormat": "{{ name }}", 1189 | "metric": "container_network_transmit_bytes_total", 1190 | "refId": "A", 1191 | "step": 10 1192 | } 1193 | ], 1194 | "timeFrom": null, 1195 | "timeShift": null, 1196 | "title": "Container Network Output", 1197 | "tooltip": { 1198 | "msResolution": true, 1199 | "shared": true, 1200 | "sort": 2, 1201 | "value_type": "cumulative" 1202 | }, 1203 | "type": "graph", 1204 | "xaxis": { 1205 | "show": true 1206 | }, 1207 | "yaxes": [ 1208 | { 1209 | "format": "bytes", 1210 | "label": null, 1211 | "logBase": 1, 1212 | "max": null, 1213 | "min": 0, 1214 | "show": true 1215 | }, 1216 | { 1217 | "format": "short", 1218 | "label": null, 1219 | "logBase": 1, 1220 | "max": null, 1221 | "min": null, 1222 | "show": false 1223 | } 1224 | ] 1225 | } 1226 | ], 1227 | "title": "Network" 1228 | } 1229 | ], 1230 | "time": { 1231 | "from": "now-15m", 1232 | "to": "now" 1233 | }, 1234 | "timepicker": { 1235 | "refresh_intervals": [ 1236 | "5s", 1237 | "10s", 1238 | "30s", 1239 | "1m", 1240 | "5m", 1241 | "15m", 1242 | "30m", 1243 | "1h", 1244 | "2h", 1245 | "1d" 1246 | ], 1247 | "time_options": [ 1248 | "5m", 1249 | "15m", 1250 | "1h", 1251 | "6h", 1252 | "12h", 1253 | "24h", 1254 | "2d", 1255 | "7d", 1256 | "30d" 1257 | ] 1258 | }, 1259 | "templating": { 1260 | "list": [] 1261 | }, 1262 | "annotations": { 1263 | "list": [] 1264 | }, 1265 | "refresh": "10s", 1266 | "schemaVersion": 12, 1267 | "version": 8, 1268 | "links": [], 1269 | "gnetId": null 1270 | } -------------------------------------------------------------------------------- /grafana/provisioning/dashboards/docker_host.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": null, 3 | "title": "Docker Host", 4 | "description": "Docker host metrics", 5 | "tags": [ 6 | "system" 7 | ], 8 | "style": "dark", 9 | "timezone": "browser", 10 | "editable": true, 11 | "hideControls": false, 12 | "sharedCrosshair": true, 13 | "rows": [ 14 | { 15 | "collapse": false, 16 | "editable": true, 17 | "height": "100px", 18 | "panels": [ 19 | { 20 | "cacheTimeout": null, 21 | "colorBackground": false, 22 | "colorValue": false, 23 | "colors": [ 24 | "rgba(245, 54, 54, 0.9)", 25 | "rgba(237, 129, 40, 0.89)", 26 | "rgba(50, 172, 45, 0.97)" 27 | ], 28 | "datasource": "Prometheus", 29 | "decimals": 1, 30 | "editable": true, 31 | "error": false, 32 | "format": "s", 33 | "gauge": { 34 | "maxValue": 100, 35 | "minValue": 0, 36 | "show": false, 37 | "thresholdLabels": false, 38 | "thresholdMarkers": true 39 | }, 40 | "id": 1, 41 | "interval": null, 42 | "isNew": true, 43 | "links": [], 44 | "mappingType": 1, 45 | "mappingTypes": [ 46 | { 47 | "name": "value to text", 48 | "value": 1 49 | }, 50 | { 51 | "name": "range to text", 52 | "value": 2 53 | } 54 | ], 55 | "maxDataPoints": 100, 56 | "nullPointMode": "connected", 57 | "nullText": null, 58 | "postfix": "s", 59 | "postfixFontSize": "80%", 60 | "prefix": "", 61 | "prefixFontSize": "50%", 62 | "rangeMaps": [ 63 | { 64 | "from": "null", 65 | "text": "N/A", 66 | "to": "null" 67 | } 68 | ], 69 | "span": 2, 70 | "sparkline": { 71 | "fillColor": "rgba(31, 118, 189, 0.18)", 72 | "full": false, 73 | "lineColor": "rgb(31, 120, 193)", 74 | "show": false 75 | }, 76 | "targets": [ 77 | { 78 | "expr": "node_time_seconds - node_boot_time_seconds", 79 | "interval": "30s", 80 | "intervalFactor": 1, 81 | "refId": "A", 82 | "step": 30 83 | } 84 | ], 85 | "thresholds": "", 86 | "title": "Uptime", 87 | "type": "singlestat", 88 | "valueFontSize": "80%", 89 | "valueMaps": [ 90 | { 91 | "op": "=", 92 | "text": "N/A", 93 | "value": "null" 94 | } 95 | ], 96 | "valueName": "avg", 97 | "timeFrom": "10s", 98 | "hideTimeOverride": true 99 | }, 100 | { 101 | "cacheTimeout": null, 102 | "colorBackground": false, 103 | "colorValue": false, 104 | "colors": [ 105 | "rgba(245, 54, 54, 0.9)", 106 | "rgba(237, 129, 40, 0.89)", 107 | "rgba(50, 172, 45, 0.97)" 108 | ], 109 | "datasource": "Prometheus", 110 | "editable": true, 111 | "error": false, 112 | "format": "percent", 113 | "gauge": { 114 | "maxValue": 100, 115 | "minValue": 0, 116 | "show": false, 117 | "thresholdLabels": false, 118 | "thresholdMarkers": true 119 | }, 120 | "id": 13, 121 | "interval": null, 122 | "isNew": true, 123 | "links": [], 124 | "mappingType": 1, 125 | "mappingTypes": [ 126 | { 127 | "name": "value to text", 128 | "value": 1 129 | }, 130 | { 131 | "name": "range to text", 132 | "value": 2 133 | } 134 | ], 135 | "maxDataPoints": 100, 136 | "nullPointMode": "connected", 137 | "nullText": null, 138 | "postfix": "", 139 | "postfixFontSize": "50%", 140 | "prefix": "", 141 | "prefixFontSize": "50%", 142 | "rangeMaps": [ 143 | { 144 | "from": "null", 145 | "text": "N/A", 146 | "to": "null" 147 | } 148 | ], 149 | "span": 2, 150 | "sparkline": { 151 | "fillColor": "rgba(31, 118, 189, 0.18)", 152 | "full": false, 153 | "lineColor": "rgb(31, 120, 193)", 154 | "show": false 155 | }, 156 | "targets": [ 157 | { 158 | "expr": "sum(rate(node_cpu_seconds_total{mode=\"idle\"}[1m])) * 100 / scalar(count(node_cpu_seconds_total{mode=\"user\"}))", 159 | "interval": "10s", 160 | "intervalFactor": 2, 161 | "legendFormat": "", 162 | "refId": "A", 163 | "step": 20 164 | } 165 | ], 166 | "thresholds": "", 167 | "title": "CPU Idle", 168 | "type": "singlestat", 169 | "valueFontSize": "80%", 170 | "valueMaps": [ 171 | { 172 | "op": "=", 173 | "text": "N/A", 174 | "value": "null" 175 | } 176 | ], 177 | "valueName": "avg", 178 | "timeFrom": "10s", 179 | "hideTimeOverride": true 180 | }, 181 | { 182 | "cacheTimeout": null, 183 | "colorBackground": false, 184 | "colorValue": false, 185 | "colors": [ 186 | "rgba(245, 54, 54, 0.9)", 187 | "rgba(237, 129, 40, 0.89)", 188 | "rgba(50, 172, 45, 0.97)" 189 | ], 190 | "datasource": "Prometheus", 191 | "editable": true, 192 | "error": false, 193 | "format": "none", 194 | "gauge": { 195 | "maxValue": 100, 196 | "minValue": 0, 197 | "show": false, 198 | "thresholdLabels": false, 199 | "thresholdMarkers": true 200 | }, 201 | "id": 12, 202 | "interval": null, 203 | "isNew": true, 204 | "links": [], 205 | "mappingType": 1, 206 | "mappingTypes": [ 207 | { 208 | "name": "value to text", 209 | "value": 1 210 | }, 211 | { 212 | "name": "range to text", 213 | "value": 2 214 | } 215 | ], 216 | "maxDataPoints": 100, 217 | "nullPointMode": "connected", 218 | "nullText": null, 219 | "postfix": "", 220 | "postfixFontSize": "50%", 221 | "prefix": "", 222 | "prefixFontSize": "50%", 223 | "rangeMaps": [ 224 | { 225 | "from": "null", 226 | "text": "N/A", 227 | "to": "null" 228 | } 229 | ], 230 | "span": 2, 231 | "sparkline": { 232 | "fillColor": "rgba(31, 118, 189, 0.18)", 233 | "full": false, 234 | "lineColor": "rgb(31, 120, 193)", 235 | "show": false 236 | }, 237 | "targets": [ 238 | { 239 | "expr": "machine_cpu_cores", 240 | "intervalFactor": 2, 241 | "metric": "machine_cpu_cores", 242 | "refId": "A", 243 | "step": 2 244 | } 245 | ], 246 | "thresholds": "", 247 | "title": "CPU Cores", 248 | "type": "singlestat", 249 | "valueFontSize": "80%", 250 | "valueMaps": [ 251 | { 252 | "op": "=", 253 | "text": "N/A", 254 | "value": "null" 255 | } 256 | ], 257 | "valueName": "avg", 258 | "timeFrom": "10s", 259 | "hideTimeOverride": true 260 | }, 261 | { 262 | "cacheTimeout": null, 263 | "colorBackground": false, 264 | "colorValue": false, 265 | "colors": [ 266 | "rgba(245, 54, 54, 0.9)", 267 | "rgba(237, 129, 40, 0.89)", 268 | "rgba(50, 172, 45, 0.97)" 269 | ], 270 | "datasource": "Prometheus", 271 | "editable": true, 272 | "error": false, 273 | "format": "bytes", 274 | "gauge": { 275 | "maxValue": 100, 276 | "minValue": 0, 277 | "show": false, 278 | "thresholdLabels": false, 279 | "thresholdMarkers": true 280 | }, 281 | "id": 2, 282 | "interval": null, 283 | "isNew": true, 284 | "links": [], 285 | "mappingType": 1, 286 | "mappingTypes": [ 287 | { 288 | "name": "value to text", 289 | "value": 1 290 | }, 291 | { 292 | "name": "range to text", 293 | "value": 2 294 | } 295 | ], 296 | "maxDataPoints": 100, 297 | "nullPointMode": "connected", 298 | "nullText": null, 299 | "postfix": "", 300 | "postfixFontSize": "50%", 301 | "prefix": "", 302 | "prefixFontSize": "50%", 303 | "rangeMaps": [ 304 | { 305 | "from": "null", 306 | "text": "N/A", 307 | "to": "null" 308 | } 309 | ], 310 | "span": 2, 311 | "sparkline": { 312 | "fillColor": "rgba(31, 118, 189, 0.18)", 313 | "full": false, 314 | "lineColor": "rgb(31, 120, 193)", 315 | "show": false 316 | }, 317 | "targets": [ 318 | { 319 | "expr": "node_memory_MemAvailable_bytes", 320 | "interval": "30s", 321 | "intervalFactor": 2, 322 | "legendFormat": "", 323 | "refId": "A", 324 | "step": 60 325 | } 326 | ], 327 | "thresholds": "", 328 | "title": "Available Memory", 329 | "type": "singlestat", 330 | "valueFontSize": "80%", 331 | "valueMaps": [ 332 | { 333 | "op": "=", 334 | "text": "N/A", 335 | "value": "null" 336 | } 337 | ], 338 | "valueName": "avg", 339 | "timeFrom": "10s", 340 | "hideTimeOverride": true 341 | }, 342 | { 343 | "cacheTimeout": null, 344 | "colorBackground": false, 345 | "colorValue": false, 346 | "colors": [ 347 | "rgba(245, 54, 54, 0.9)", 348 | "rgba(237, 129, 40, 0.89)", 349 | "rgba(50, 172, 45, 0.97)" 350 | ], 351 | "datasource": "Prometheus", 352 | "editable": true, 353 | "error": false, 354 | "format": "bytes", 355 | "gauge": { 356 | "maxValue": 100, 357 | "minValue": 0, 358 | "show": false, 359 | "thresholdLabels": false, 360 | "thresholdMarkers": true 361 | }, 362 | "id": 3, 363 | "interval": null, 364 | "isNew": true, 365 | "links": [], 366 | "mappingType": 1, 367 | "mappingTypes": [ 368 | { 369 | "name": "value to text", 370 | "value": 1 371 | }, 372 | { 373 | "name": "range to text", 374 | "value": 2 375 | } 376 | ], 377 | "maxDataPoints": 100, 378 | "nullPointMode": "connected", 379 | "nullText": null, 380 | "postfix": "", 381 | "postfixFontSize": "50%", 382 | "prefix": "", 383 | "prefixFontSize": "50%", 384 | "rangeMaps": [ 385 | { 386 | "from": "null", 387 | "text": "N/A", 388 | "to": "null" 389 | } 390 | ], 391 | "span": 2, 392 | "sparkline": { 393 | "fillColor": "rgba(31, 118, 189, 0.18)", 394 | "full": false, 395 | "lineColor": "rgb(31, 120, 193)", 396 | "show": false 397 | }, 398 | "targets": [ 399 | { 400 | "expr": "node_memory_SwapFree_bytes", 401 | "interval": "30s", 402 | "intervalFactor": 2, 403 | "refId": "A", 404 | "step": 60 405 | } 406 | ], 407 | "thresholds": "", 408 | "title": "Free Swap", 409 | "type": "singlestat", 410 | "valueFontSize": "80%", 411 | "valueMaps": [ 412 | { 413 | "op": "=", 414 | "text": "N/A", 415 | "value": "null" 416 | } 417 | ], 418 | "valueName": "avg", 419 | "timeFrom": "10s", 420 | "hideTimeOverride": true 421 | }, 422 | { 423 | "cacheTimeout": null, 424 | "colorBackground": false, 425 | "colorValue": false, 426 | "colors": [ 427 | "rgba(245, 54, 54, 0.9)", 428 | "rgba(237, 129, 40, 0.89)", 429 | "rgba(50, 172, 45, 0.97)" 430 | ], 431 | "datasource": "Prometheus", 432 | "editable": true, 433 | "error": false, 434 | "format": "bytes", 435 | "gauge": { 436 | "maxValue": 100, 437 | "minValue": 0, 438 | "show": false, 439 | "thresholdLabels": false, 440 | "thresholdMarkers": true 441 | }, 442 | "id": 4, 443 | "interval": null, 444 | "isNew": true, 445 | "links": [], 446 | "mappingType": 1, 447 | "mappingTypes": [ 448 | { 449 | "name": "value to text", 450 | "value": 1 451 | }, 452 | { 453 | "name": "range to text", 454 | "value": 2 455 | } 456 | ], 457 | "maxDataPoints": 100, 458 | "nullPointMode": "connected", 459 | "nullText": null, 460 | "postfix": "", 461 | "postfixFontSize": "50%", 462 | "prefix": "", 463 | "prefixFontSize": "50%", 464 | "rangeMaps": [ 465 | { 466 | "from": "null", 467 | "text": "N/A", 468 | "to": "null" 469 | } 470 | ], 471 | "span": 2, 472 | "sparkline": { 473 | "fillColor": "rgba(31, 118, 189, 0.18)", 474 | "full": false, 475 | "lineColor": "rgb(31, 120, 193)", 476 | "show": false 477 | }, 478 | "targets": [ 479 | { 480 | "expr": "sum(node_filesystem_free_bytes{fstype=\"ext4\"})", 481 | "interval": "30s", 482 | "intervalFactor": 1, 483 | "legendFormat": "", 484 | "refId": "A", 485 | "step": 30 486 | } 487 | ], 488 | "thresholds": "", 489 | "title": "Free Storage", 490 | "type": "singlestat", 491 | "valueFontSize": "80%", 492 | "valueMaps": [ 493 | { 494 | "op": "=", 495 | "text": "N/A", 496 | "value": "null" 497 | } 498 | ], 499 | "valueName": "avg", 500 | "timeFrom": "10s", 501 | "hideTimeOverride": true 502 | } 503 | ], 504 | "title": "Available resources" 505 | }, 506 | { 507 | "collapse": false, 508 | "editable": true, 509 | "height": "150px", 510 | "panels": [ 511 | { 512 | "aliasColors": {}, 513 | "bars": true, 514 | "datasource": "Prometheus", 515 | "decimals": 2, 516 | "editable": true, 517 | "error": false, 518 | "fill": 1, 519 | "grid": { 520 | "threshold1": null, 521 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 522 | "threshold2": null, 523 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 524 | }, 525 | "id": 9, 526 | "isNew": true, 527 | "legend": { 528 | "avg": false, 529 | "current": false, 530 | "max": false, 531 | "min": false, 532 | "show": false, 533 | "total": false, 534 | "values": false 535 | }, 536 | "lines": false, 537 | "linewidth": 2, 538 | "links": [], 539 | "nullPointMode": "connected", 540 | "percentage": false, 541 | "pointradius": 5, 542 | "points": false, 543 | "renderer": "flot", 544 | "seriesOverrides": [ 545 | { 546 | "alias": "load 1m", 547 | "color": "#1F78C1" 548 | } 549 | ], 550 | "span": 4, 551 | "stack": false, 552 | "steppedLine": false, 553 | "targets": [ 554 | { 555 | "expr": "node_load1", 556 | "interval": "10s", 557 | "intervalFactor": 1, 558 | "legendFormat": "load 1m", 559 | "refId": "A", 560 | "step": 10 561 | } 562 | ], 563 | "timeFrom": null, 564 | "timeShift": null, 565 | "title": "Load Average 1m", 566 | "tooltip": { 567 | "msResolution": true, 568 | "shared": true, 569 | "sort": 0, 570 | "value_type": "cumulative" 571 | }, 572 | "type": "graph", 573 | "xaxis": { 574 | "show": true 575 | }, 576 | "yaxes": [ 577 | { 578 | "format": "short", 579 | "label": null, 580 | "logBase": 1, 581 | "max": null, 582 | "min": 0, 583 | "show": true 584 | }, 585 | { 586 | "format": "short", 587 | "label": null, 588 | "logBase": 1, 589 | "max": null, 590 | "min": null, 591 | "show": false 592 | } 593 | ] 594 | }, 595 | { 596 | "aliasColors": {}, 597 | "bars": true, 598 | "datasource": "Prometheus", 599 | "editable": true, 600 | "error": false, 601 | "fill": 1, 602 | "grid": { 603 | "threshold1": null, 604 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 605 | "threshold2": null, 606 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 607 | }, 608 | "id": 10, 609 | "isNew": true, 610 | "legend": { 611 | "avg": false, 612 | "current": false, 613 | "max": false, 614 | "min": false, 615 | "show": false, 616 | "total": false, 617 | "values": false 618 | }, 619 | "lines": false, 620 | "linewidth": 2, 621 | "links": [], 622 | "nullPointMode": "connected", 623 | "percentage": false, 624 | "pointradius": 5, 625 | "points": false, 626 | "renderer": "flot", 627 | "seriesOverrides": [ 628 | { 629 | "alias": "blocked by I/O", 630 | "color": "#58140C" 631 | } 632 | ], 633 | "span": 4, 634 | "stack": true, 635 | "steppedLine": false, 636 | "targets": [ 637 | { 638 | "expr": "node_procs_running", 639 | "interval": "10s", 640 | "intervalFactor": 1, 641 | "legendFormat": "running", 642 | "metric": "node_procs_running", 643 | "refId": "A", 644 | "step": 10 645 | }, 646 | { 647 | "expr": "node_procs_blocked", 648 | "interval": "10s", 649 | "intervalFactor": 1, 650 | "legendFormat": "blocked by I/O", 651 | "metric": "node_procs_blocked", 652 | "refId": "B", 653 | "step": 10 654 | } 655 | ], 656 | "timeFrom": null, 657 | "timeShift": null, 658 | "title": "Processes", 659 | "tooltip": { 660 | "msResolution": true, 661 | "shared": true, 662 | "sort": 2, 663 | "value_type": "individual" 664 | }, 665 | "type": "graph", 666 | "xaxis": { 667 | "show": true 668 | }, 669 | "yaxes": [ 670 | { 671 | "format": "short", 672 | "label": null, 673 | "logBase": 1, 674 | "max": null, 675 | "min": 0, 676 | "show": true 677 | }, 678 | { 679 | "format": "short", 680 | "label": null, 681 | "logBase": 1, 682 | "max": null, 683 | "min": null, 684 | "show": false 685 | } 686 | ] 687 | }, 688 | { 689 | "aliasColors": {}, 690 | "bars": true, 691 | "datasource": "Prometheus", 692 | "editable": true, 693 | "error": false, 694 | "fill": 1, 695 | "grid": { 696 | "threshold1": null, 697 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 698 | "threshold2": null, 699 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 700 | }, 701 | "id": 11, 702 | "isNew": true, 703 | "legend": { 704 | "avg": false, 705 | "current": false, 706 | "max": false, 707 | "min": false, 708 | "show": false, 709 | "total": false, 710 | "values": false 711 | }, 712 | "lines": false, 713 | "linewidth": 2, 714 | "links": [], 715 | "nullPointMode": "connected", 716 | "percentage": false, 717 | "pointradius": 5, 718 | "points": false, 719 | "renderer": "flot", 720 | "seriesOverrides": [ 721 | { 722 | "alias": "interrupts", 723 | "color": "#806EB7" 724 | } 725 | ], 726 | "span": 4, 727 | "stack": false, 728 | "steppedLine": false, 729 | "targets": [ 730 | { 731 | "expr": " irate(node_intr_total[5m])", 732 | "interval": "10s", 733 | "intervalFactor": 1, 734 | "legendFormat": "interrupts", 735 | "metric": "node_intr_total", 736 | "refId": "A", 737 | "step": 10 738 | } 739 | ], 740 | "timeFrom": null, 741 | "timeShift": null, 742 | "title": "Interrupts", 743 | "tooltip": { 744 | "msResolution": true, 745 | "shared": true, 746 | "sort": 0, 747 | "value_type": "cumulative" 748 | }, 749 | "type": "graph", 750 | "xaxis": { 751 | "show": true 752 | }, 753 | "yaxes": [ 754 | { 755 | "format": "short", 756 | "label": null, 757 | "logBase": 1, 758 | "max": null, 759 | "min": null, 760 | "show": true 761 | }, 762 | { 763 | "format": "short", 764 | "label": null, 765 | "logBase": 1, 766 | "max": null, 767 | "min": null, 768 | "show": false 769 | } 770 | ] 771 | } 772 | ], 773 | "title": "Load" 774 | }, 775 | { 776 | "collapse": false, 777 | "editable": true, 778 | "height": "250px", 779 | "panels": [ 780 | { 781 | "aliasColors": {}, 782 | "bars": false, 783 | "datasource": "Prometheus", 784 | "decimals": 2, 785 | "editable": true, 786 | "error": false, 787 | "fill": 4, 788 | "grid": { 789 | "threshold1": null, 790 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 791 | "threshold2": null, 792 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 793 | }, 794 | "id": 5, 795 | "isNew": true, 796 | "legend": { 797 | "alignAsTable": true, 798 | "avg": true, 799 | "current": false, 800 | "max": true, 801 | "min": true, 802 | "rightSide": true, 803 | "show": true, 804 | "total": false, 805 | "values": true 806 | }, 807 | "lines": true, 808 | "linewidth": 2, 809 | "links": [], 810 | "nullPointMode": "connected", 811 | "percentage": false, 812 | "pointradius": 5, 813 | "points": false, 814 | "renderer": "flot", 815 | "seriesOverrides": [], 816 | "span": 12, 817 | "stack": true, 818 | "steppedLine": false, 819 | "targets": [ 820 | { 821 | "expr": "sum(rate(node_cpu_seconds_total[1m])) by (mode) * 100 / scalar(count(node_cpu_seconds_total{mode=\"user\"}))", 822 | "intervalFactor": 10, 823 | "legendFormat": "{{ mode }}", 824 | "metric": "node_cpu_seconds_total", 825 | "refId": "A", 826 | "step": 10 827 | } 828 | ], 829 | "timeFrom": null, 830 | "timeShift": null, 831 | "title": "CPU Usage", 832 | "tooltip": { 833 | "msResolution": true, 834 | "shared": true, 835 | "sort": 2, 836 | "value_type": "individual" 837 | }, 838 | "type": "graph", 839 | "xaxis": { 840 | "show": true 841 | }, 842 | "yaxes": [ 843 | { 844 | "format": "percent", 845 | "label": null, 846 | "logBase": 1, 847 | "max": 100, 848 | "min": 0, 849 | "show": true 850 | }, 851 | { 852 | "format": "short", 853 | "label": null, 854 | "logBase": 1, 855 | "max": null, 856 | "min": 0, 857 | "show": true 858 | } 859 | ] 860 | } 861 | ], 862 | "title": "CPU" 863 | }, 864 | { 865 | "collapse": false, 866 | "editable": true, 867 | "height": "250px", 868 | "panels": [ 869 | { 870 | "aliasColors": {}, 871 | "bars": false, 872 | "datasource": "Prometheus", 873 | "decimals": 2, 874 | "editable": true, 875 | "error": false, 876 | "fill": 4, 877 | "grid": { 878 | "threshold1": null, 879 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 880 | "threshold2": null, 881 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 882 | }, 883 | "id": 6, 884 | "isNew": true, 885 | "legend": { 886 | "alignAsTable": true, 887 | "avg": true, 888 | "current": false, 889 | "max": true, 890 | "min": true, 891 | "rightSide": true, 892 | "show": true, 893 | "total": false, 894 | "values": true 895 | }, 896 | "lines": true, 897 | "linewidth": 2, 898 | "links": [], 899 | "nullPointMode": "null", 900 | "percentage": false, 901 | "pointradius": 5, 902 | "points": false, 903 | "renderer": "flot", 904 | "seriesOverrides": [ 905 | { 906 | "alias": "Used", 907 | "color": "#BF1B00" 908 | }, 909 | { 910 | "alias": "Free", 911 | "color": "#7EB26D" 912 | }, 913 | { 914 | "alias": "Buffers", 915 | "color": "#6ED0E0" 916 | }, 917 | { 918 | "alias": "Cached", 919 | "color": "#EF843C" 920 | } 921 | ], 922 | "span": 12, 923 | "stack": true, 924 | "steppedLine": false, 925 | "targets": [ 926 | { 927 | "expr": "node_memory_MemTotal_bytes - (node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes)", 928 | "intervalFactor": 1, 929 | "legendFormat": "Used", 930 | "refId": "A", 931 | "step": 1 932 | }, 933 | { 934 | "expr": "node_memory_MemFree_bytes", 935 | "intervalFactor": 1, 936 | "legendFormat": "Free", 937 | "refId": "B", 938 | "step": 1 939 | }, 940 | { 941 | "expr": "node_memory_Buffers_bytes", 942 | "intervalFactor": 1, 943 | "legendFormat": "Buffers", 944 | "refId": "C", 945 | "step": 1 946 | }, 947 | { 948 | "expr": "node_memory_Cached_bytes", 949 | "intervalFactor": 1, 950 | "legendFormat": "Cached", 951 | "refId": "D", 952 | "step": 1 953 | } 954 | ], 955 | "timeFrom": null, 956 | "timeShift": null, 957 | "title": "Memory Usage", 958 | "tooltip": { 959 | "msResolution": true, 960 | "shared": true, 961 | "sort": 2, 962 | "value_type": "individual" 963 | }, 964 | "type": "graph", 965 | "xaxis": { 966 | "show": true 967 | }, 968 | "yaxes": [ 969 | { 970 | "format": "bytes", 971 | "label": null, 972 | "logBase": 1, 973 | "max": null, 974 | "min": null, 975 | "show": true 976 | }, 977 | { 978 | "format": "short", 979 | "label": null, 980 | "logBase": 1, 981 | "max": null, 982 | "min": null, 983 | "show": true 984 | } 985 | ] 986 | } 987 | ], 988 | "title": "Memory" 989 | }, 990 | { 991 | "collapse": false, 992 | "editable": true, 993 | "height": "250px", 994 | "panels": [ 995 | { 996 | "aliasColors": {}, 997 | "bars": false, 998 | "datasource": "Prometheus", 999 | "decimals": 2, 1000 | "editable": true, 1001 | "error": false, 1002 | "fill": 1, 1003 | "grid": { 1004 | "threshold1": null, 1005 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 1006 | "threshold2": null, 1007 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 1008 | }, 1009 | "id": 7, 1010 | "isNew": true, 1011 | "legend": { 1012 | "alignAsTable": true, 1013 | "avg": true, 1014 | "current": false, 1015 | "max": true, 1016 | "min": true, 1017 | "rightSide": true, 1018 | "show": true, 1019 | "total": false, 1020 | "values": true 1021 | }, 1022 | "lines": true, 1023 | "linewidth": 2, 1024 | "links": [], 1025 | "nullPointMode": "connected", 1026 | "percentage": false, 1027 | "pointradius": 5, 1028 | "points": false, 1029 | "renderer": "flot", 1030 | "seriesOverrides": [ 1031 | { 1032 | "alias": "read", 1033 | "yaxis": 1 1034 | }, 1035 | { 1036 | "alias": "written", 1037 | "yaxis": 1 1038 | }, 1039 | { 1040 | "alias": "io time", 1041 | "yaxis": 2 1042 | } 1043 | ], 1044 | "span": 12, 1045 | "stack": false, 1046 | "steppedLine": false, 1047 | "targets": [ 1048 | { 1049 | "expr": "sum(irate(node_disk_read_bytes_total[1m]))", 1050 | "interval": "", 1051 | "intervalFactor": 1, 1052 | "legendFormat": "read", 1053 | "metric": "node_disk_read_bytes_total", 1054 | "refId": "A", 1055 | "step": 1 1056 | }, 1057 | { 1058 | "expr": "sum(irate(node_disk_written_bytes_total[1m]))", 1059 | "intervalFactor": 1, 1060 | "legendFormat": "written", 1061 | "metric": "node_disk_written_bytes_total", 1062 | "refId": "B", 1063 | "step": 1 1064 | }, 1065 | { 1066 | "expr": "sum(irate(node_disk_io_time_seconds_total[1m]))", 1067 | "intervalFactor": 1, 1068 | "legendFormat": "io time", 1069 | "metric": "node_disk_io_time_seconds_total", 1070 | "refId": "C", 1071 | "step": 1 1072 | } 1073 | ], 1074 | "timeFrom": null, 1075 | "timeShift": null, 1076 | "title": "I/O Usage", 1077 | "tooltip": { 1078 | "msResolution": true, 1079 | "shared": true, 1080 | "sort": 0, 1081 | "value_type": "cumulative" 1082 | }, 1083 | "type": "graph", 1084 | "xaxis": { 1085 | "show": true 1086 | }, 1087 | "yaxes": [ 1088 | { 1089 | "format": "Bps", 1090 | "label": null, 1091 | "logBase": 1, 1092 | "max": null, 1093 | "min": 0, 1094 | "show": true 1095 | }, 1096 | { 1097 | "format": "ms", 1098 | "label": null, 1099 | "logBase": 1, 1100 | "max": null, 1101 | "min": null, 1102 | "show": true 1103 | } 1104 | ] 1105 | } 1106 | ], 1107 | "title": "I/O" 1108 | }, 1109 | { 1110 | "collapse": false, 1111 | "editable": true, 1112 | "height": "250px", 1113 | "panels": [ 1114 | { 1115 | "aliasColors": {}, 1116 | "bars": false, 1117 | "datasource": "Prometheus", 1118 | "decimals": 2, 1119 | "editable": true, 1120 | "error": false, 1121 | "fill": 4, 1122 | "grid": { 1123 | "threshold1": null, 1124 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 1125 | "threshold2": null, 1126 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 1127 | }, 1128 | "id": 8, 1129 | "isNew": true, 1130 | "legend": { 1131 | "alignAsTable": true, 1132 | "avg": true, 1133 | "current": false, 1134 | "max": true, 1135 | "min": true, 1136 | "rightSide": true, 1137 | "show": true, 1138 | "total": false, 1139 | "values": true 1140 | }, 1141 | "lines": true, 1142 | "linewidth": 2, 1143 | "links": [], 1144 | "nullPointMode": "connected", 1145 | "percentage": false, 1146 | "pointradius": 5, 1147 | "points": false, 1148 | "renderer": "flot", 1149 | "seriesOverrides": [], 1150 | "span": 12, 1151 | "stack": true, 1152 | "steppedLine": false, 1153 | "targets": [ 1154 | { 1155 | "expr": "irate(node_network_receive_bytes_total{device!=\"lo\"}[1m])", 1156 | "intervalFactor": 1, 1157 | "legendFormat": "In: {{ device }}", 1158 | "metric": "node_network_receive_bytes_total", 1159 | "refId": "A", 1160 | "step": 1 1161 | }, 1162 | { 1163 | "expr": "irate(node_network_transmit_bytes_total{device!=\"lo\"}[1m])", 1164 | "intervalFactor": 1, 1165 | "legendFormat": "Out: {{ device }}", 1166 | "metric": "node_network_transmit_bytes_total", 1167 | "refId": "B", 1168 | "step": 1 1169 | } 1170 | ], 1171 | "timeFrom": null, 1172 | "timeShift": null, 1173 | "title": "Network Usage", 1174 | "tooltip": { 1175 | "msResolution": true, 1176 | "shared": true, 1177 | "sort": 2, 1178 | "value_type": "individual" 1179 | }, 1180 | "type": "graph", 1181 | "xaxis": { 1182 | "show": true 1183 | }, 1184 | "yaxes": [ 1185 | { 1186 | "format": "Bps", 1187 | "label": null, 1188 | "logBase": 1, 1189 | "max": null, 1190 | "min": 0, 1191 | "show": true 1192 | }, 1193 | { 1194 | "format": "short", 1195 | "label": null, 1196 | "logBase": 1, 1197 | "max": null, 1198 | "min": null, 1199 | "show": false 1200 | } 1201 | ] 1202 | } 1203 | ], 1204 | "title": "Network" 1205 | }, 1206 | { 1207 | "collapse": false, 1208 | "editable": true, 1209 | "height": "250px", 1210 | "panels": [ 1211 | { 1212 | "aliasColors": {}, 1213 | "bars": false, 1214 | "datasource": "Prometheus", 1215 | "decimals": 2, 1216 | "editable": true, 1217 | "error": false, 1218 | "fill": 4, 1219 | "grid": { 1220 | "threshold1": null, 1221 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 1222 | "threshold2": null, 1223 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 1224 | }, 1225 | "id": 14, 1226 | "isNew": true, 1227 | "legend": { 1228 | "alignAsTable": true, 1229 | "avg": true, 1230 | "current": false, 1231 | "max": true, 1232 | "min": true, 1233 | "rightSide": false, 1234 | "show": true, 1235 | "total": false, 1236 | "values": true 1237 | }, 1238 | "lines": true, 1239 | "linewidth": 2, 1240 | "links": [], 1241 | "nullPointMode": "connected", 1242 | "percentage": false, 1243 | "pointradius": 5, 1244 | "points": false, 1245 | "renderer": "flot", 1246 | "seriesOverrides": [ 1247 | { 1248 | "alias": "Used", 1249 | "color": "#890F02" 1250 | }, 1251 | { 1252 | "alias": "Free", 1253 | "color": "#7EB26D" 1254 | } 1255 | ], 1256 | "span": 6, 1257 | "stack": true, 1258 | "steppedLine": false, 1259 | "targets": [ 1260 | { 1261 | "expr": "node_memory_SwapTotal_bytes - node_memory_SwapFree_bytes", 1262 | "interval": "10s", 1263 | "intervalFactor": 1, 1264 | "legendFormat": "Used", 1265 | "refId": "A", 1266 | "step": 10 1267 | }, 1268 | { 1269 | "expr": "node_memory_SwapFree_bytes", 1270 | "interval": "10s", 1271 | "intervalFactor": 1, 1272 | "legendFormat": "Free", 1273 | "refId": "B", 1274 | "step": 10 1275 | } 1276 | ], 1277 | "timeFrom": null, 1278 | "timeShift": null, 1279 | "title": "Swap Usage", 1280 | "tooltip": { 1281 | "msResolution": true, 1282 | "shared": true, 1283 | "sort": 2, 1284 | "value_type": "individual" 1285 | }, 1286 | "type": "graph", 1287 | "xaxis": { 1288 | "show": true 1289 | }, 1290 | "yaxes": [ 1291 | { 1292 | "format": "bytes", 1293 | "label": null, 1294 | "logBase": 1, 1295 | "max": null, 1296 | "min": 0, 1297 | "show": true 1298 | }, 1299 | { 1300 | "format": "short", 1301 | "label": null, 1302 | "logBase": 1, 1303 | "max": null, 1304 | "min": null, 1305 | "show": false 1306 | } 1307 | ] 1308 | }, 1309 | { 1310 | "aliasColors": {}, 1311 | "bars": false, 1312 | "datasource": "Prometheus", 1313 | "decimals": 2, 1314 | "editable": true, 1315 | "error": false, 1316 | "fill": 1, 1317 | "grid": { 1318 | "threshold1": null, 1319 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 1320 | "threshold2": null, 1321 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 1322 | }, 1323 | "id": 15, 1324 | "isNew": true, 1325 | "legend": { 1326 | "alignAsTable": true, 1327 | "avg": true, 1328 | "current": false, 1329 | "max": true, 1330 | "min": true, 1331 | "show": true, 1332 | "total": false, 1333 | "values": true 1334 | }, 1335 | "lines": true, 1336 | "linewidth": 2, 1337 | "links": [], 1338 | "nullPointMode": "connected", 1339 | "percentage": false, 1340 | "pointradius": 5, 1341 | "points": false, 1342 | "renderer": "flot", 1343 | "seriesOverrides": [], 1344 | "span": 6, 1345 | "stack": false, 1346 | "steppedLine": false, 1347 | "targets": [ 1348 | { 1349 | "expr": "rate(node_vmstat_pswpin[1m]) * 4096 or irate(node_vmstat_pswpin[5m]) * 4096", 1350 | "interval": "10s", 1351 | "intervalFactor": 1, 1352 | "legendFormat": "In", 1353 | "refId": "A", 1354 | "step": 10 1355 | }, 1356 | { 1357 | "expr": "rate(node_vmstat_pswpout[1m]) * 4096 or irate(node_vmstat_pswpout[5m]) * 4096", 1358 | "interval": "10s", 1359 | "intervalFactor": 1, 1360 | "legendFormat": "Out", 1361 | "refId": "B", 1362 | "step": 10 1363 | } 1364 | ], 1365 | "timeFrom": null, 1366 | "timeShift": null, 1367 | "title": "Swap I/O", 1368 | "tooltip": { 1369 | "msResolution": true, 1370 | "shared": true, 1371 | "sort": 0, 1372 | "value_type": "cumulative" 1373 | }, 1374 | "type": "graph", 1375 | "xaxis": { 1376 | "show": true 1377 | }, 1378 | "yaxes": [ 1379 | { 1380 | "format": "Bps", 1381 | "label": null, 1382 | "logBase": 1, 1383 | "max": null, 1384 | "min": 0, 1385 | "show": true 1386 | }, 1387 | { 1388 | "format": "short", 1389 | "label": null, 1390 | "logBase": 1, 1391 | "max": null, 1392 | "min": null, 1393 | "show": false 1394 | } 1395 | ] 1396 | } 1397 | ], 1398 | "title": "New row" 1399 | } 1400 | ], 1401 | "time": { 1402 | "from": "now-15m", 1403 | "to": "now" 1404 | }, 1405 | "timepicker": { 1406 | "refresh_intervals": [ 1407 | "5s", 1408 | "10s", 1409 | "30s", 1410 | "1m", 1411 | "5m", 1412 | "15m", 1413 | "30m", 1414 | "1h", 1415 | "2h", 1416 | "1d" 1417 | ], 1418 | "time_options": [ 1419 | "5m", 1420 | "15m", 1421 | "1h", 1422 | "6h", 1423 | "12h", 1424 | "24h", 1425 | "2d", 1426 | "7d", 1427 | "30d" 1428 | ] 1429 | }, 1430 | "templating": { 1431 | "list": [] 1432 | }, 1433 | "annotations": { 1434 | "list": [] 1435 | }, 1436 | "refresh": "10s", 1437 | "schemaVersion": 12, 1438 | "version": 2, 1439 | "links": [], 1440 | "gnetId": null 1441 | } -------------------------------------------------------------------------------- /grafana/provisioning/dashboards/nginx_container.json: -------------------------------------------------------------------------------- 1 | { 2 | "id": null, 3 | "title": "Nginx", 4 | "description": "Nginx exporter metrics", 5 | "tags": [ 6 | "nginx" 7 | ], 8 | "style": "dark", 9 | "timezone": "browser", 10 | "editable": true, 11 | "hideControls": false, 12 | "sharedCrosshair": true, 13 | "rows": [ 14 | { 15 | "collapse": false, 16 | "editable": true, 17 | "height": "250px", 18 | "panels": [ 19 | { 20 | "aliasColors": {}, 21 | "bars": false, 22 | "datasource": "Prometheus", 23 | "decimals": 2, 24 | "editable": true, 25 | "error": false, 26 | "fill": 1, 27 | "grid": { 28 | "threshold1": null, 29 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 30 | "threshold2": null, 31 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 32 | }, 33 | "id": 3, 34 | "isNew": true, 35 | "legend": { 36 | "alignAsTable": true, 37 | "avg": true, 38 | "current": true, 39 | "max": true, 40 | "min": true, 41 | "rightSide": true, 42 | "show": true, 43 | "total": false, 44 | "values": true 45 | }, 46 | "lines": true, 47 | "linewidth": 2, 48 | "links": [], 49 | "nullPointMode": "connected", 50 | "percentage": false, 51 | "pointradius": 5, 52 | "points": false, 53 | "renderer": "flot", 54 | "seriesOverrides": [], 55 | "span": 12, 56 | "stack": false, 57 | "steppedLine": false, 58 | "targets": [ 59 | { 60 | "expr": "sum(irate(nginx_connections_processed_total{stage=\"any\"}[5m])) by (stage)", 61 | "hide": false, 62 | "interval": "", 63 | "intervalFactor": 10, 64 | "legendFormat": "requests", 65 | "metric": "", 66 | "refId": "B", 67 | "step": 10 68 | } 69 | ], 70 | "timeFrom": null, 71 | "timeShift": null, 72 | "title": "Requests/sec", 73 | "tooltip": { 74 | "msResolution": false, 75 | "shared": true, 76 | "sort": 0, 77 | "value_type": "cumulative" 78 | }, 79 | "type": "graph", 80 | "xaxis": { 81 | "show": true 82 | }, 83 | "yaxes": [ 84 | { 85 | "format": "short", 86 | "label": null, 87 | "logBase": 1, 88 | "max": null, 89 | "min": 0, 90 | "show": true 91 | }, 92 | { 93 | "format": "short", 94 | "label": null, 95 | "logBase": 1, 96 | "max": null, 97 | "min": null, 98 | "show": true 99 | } 100 | ] 101 | }, 102 | { 103 | "aliasColors": {}, 104 | "bars": false, 105 | "datasource": "Prometheus", 106 | "decimals": 2, 107 | "editable": true, 108 | "error": false, 109 | "fill": 1, 110 | "grid": { 111 | "threshold1": null, 112 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 113 | "threshold2": null, 114 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 115 | }, 116 | "id": 2, 117 | "isNew": true, 118 | "legend": { 119 | "alignAsTable": true, 120 | "avg": true, 121 | "current": true, 122 | "max": true, 123 | "min": true, 124 | "rightSide": true, 125 | "show": true, 126 | "total": false, 127 | "values": true 128 | }, 129 | "lines": true, 130 | "linewidth": 2, 131 | "links": [], 132 | "nullPointMode": "connected", 133 | "percentage": false, 134 | "pointradius": 5, 135 | "points": false, 136 | "renderer": "flot", 137 | "seriesOverrides": [], 138 | "span": 12, 139 | "stack": false, 140 | "steppedLine": false, 141 | "targets": [ 142 | { 143 | "expr": "sum(nginx_connections_current) by (state)", 144 | "interval": "", 145 | "intervalFactor": 2, 146 | "legendFormat": "{{state}}", 147 | "metric": "", 148 | "refId": "A", 149 | "step": 2 150 | } 151 | ], 152 | "timeFrom": null, 153 | "timeShift": null, 154 | "title": "Connections", 155 | "tooltip": { 156 | "msResolution": false, 157 | "shared": true, 158 | "sort": 0, 159 | "value_type": "cumulative" 160 | }, 161 | "type": "graph", 162 | "xaxis": { 163 | "show": true 164 | }, 165 | "yaxes": [ 166 | { 167 | "format": "short", 168 | "label": null, 169 | "logBase": 1, 170 | "max": null, 171 | "min": 0, 172 | "show": true 173 | }, 174 | { 175 | "format": "short", 176 | "label": null, 177 | "logBase": 1, 178 | "max": null, 179 | "min": null, 180 | "show": true 181 | } 182 | ] 183 | }, 184 | { 185 | "aliasColors": {}, 186 | "bars": false, 187 | "datasource": "Prometheus", 188 | "decimals": 2, 189 | "editable": true, 190 | "error": false, 191 | "fill": 1, 192 | "grid": { 193 | "threshold1": null, 194 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 195 | "threshold2": null, 196 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 197 | }, 198 | "id": 1, 199 | "isNew": true, 200 | "legend": { 201 | "alignAsTable": true, 202 | "avg": true, 203 | "current": true, 204 | "max": true, 205 | "min": true, 206 | "rightSide": true, 207 | "show": true, 208 | "total": false, 209 | "values": true 210 | }, 211 | "lines": true, 212 | "linewidth": 2, 213 | "links": [], 214 | "nullPointMode": "connected", 215 | "percentage": false, 216 | "pointradius": 5, 217 | "points": false, 218 | "renderer": "flot", 219 | "seriesOverrides": [], 220 | "span": 12, 221 | "stack": false, 222 | "steppedLine": false, 223 | "targets": [ 224 | { 225 | "expr": "sum(irate(nginx_connections_processed_total{stage!=\"any\"}[5m])) by (stage)", 226 | "hide": false, 227 | "interval": "", 228 | "intervalFactor": 10, 229 | "legendFormat": "{{stage}}", 230 | "metric": "", 231 | "refId": "B", 232 | "step": 10 233 | } 234 | ], 235 | "timeFrom": null, 236 | "timeShift": null, 237 | "title": "Connections rate", 238 | "tooltip": { 239 | "msResolution": false, 240 | "shared": true, 241 | "sort": 0, 242 | "value_type": "cumulative" 243 | }, 244 | "type": "graph", 245 | "xaxis": { 246 | "show": true 247 | }, 248 | "yaxes": [ 249 | { 250 | "format": "short", 251 | "label": null, 252 | "logBase": 1, 253 | "max": null, 254 | "min": 0, 255 | "show": true 256 | }, 257 | { 258 | "format": "short", 259 | "label": null, 260 | "logBase": 1, 261 | "max": null, 262 | "min": null, 263 | "show": true 264 | } 265 | ] 266 | } 267 | ], 268 | "title": "Nginx exporter metrics" 269 | }, 270 | { 271 | "collapse": false, 272 | "editable": true, 273 | "height": "250px", 274 | "panels": [ 275 | { 276 | "aliasColors": {}, 277 | "bars": false, 278 | "datasource": null, 279 | "editable": true, 280 | "error": false, 281 | "fill": 1, 282 | "grid": { 283 | "threshold1": null, 284 | "threshold1Color": "rgba(216, 200, 27, 0.27)", 285 | "threshold2": null, 286 | "threshold2Color": "rgba(234, 112, 112, 0.22)" 287 | }, 288 | "id": 4, 289 | "isNew": true, 290 | "legend": { 291 | "alignAsTable": true, 292 | "avg": true, 293 | "current": true, 294 | "max": true, 295 | "min": true, 296 | "rightSide": true, 297 | "show": true, 298 | "total": false, 299 | "values": true 300 | }, 301 | "lines": true, 302 | "linewidth": 2, 303 | "links": [], 304 | "nullPointMode": "connected", 305 | "percentage": false, 306 | "pointradius": 5, 307 | "points": false, 308 | "renderer": "flot", 309 | "seriesOverrides": [], 310 | "span": 12, 311 | "stack": false, 312 | "steppedLine": false, 313 | "targets": [ 314 | { 315 | "expr": "sum(rate(container_cpu_usage_seconds_total{name=~\"nginx\"}[5m])) / count(node_cpu_seconds_total{mode=\"system\"}) * 100", 316 | "intervalFactor": 2, 317 | "legendFormat": "nginx", 318 | "refId": "A", 319 | "step": 2 320 | } 321 | ], 322 | "timeFrom": null, 323 | "timeShift": null, 324 | "title": "CPU usage", 325 | "tooltip": { 326 | "msResolution": false, 327 | "shared": true, 328 | "sort": 0, 329 | "value_type": "cumulative" 330 | }, 331 | "type": "graph", 332 | "xaxis": { 333 | "show": true 334 | }, 335 | "yaxes": [ 336 | { 337 | "format": "short", 338 | "label": null, 339 | "logBase": 1, 340 | "max": null, 341 | "min": null, 342 | "show": true 343 | }, 344 | { 345 | "format": "short", 346 | "label": null, 347 | "logBase": 1, 348 | "max": null, 349 | "min": null, 350 | "show": true 351 | } 352 | ] 353 | } 354 | ], 355 | "title": "Nginx container metrics" 356 | } 357 | ], 358 | "time": { 359 | "from": "now-15m", 360 | "to": "now" 361 | }, 362 | "timepicker": { 363 | "refresh_intervals": [ 364 | "5s", 365 | "10s", 366 | "30s", 367 | "1m", 368 | "5m", 369 | "15m", 370 | "30m", 371 | "1h", 372 | "2h", 373 | "1d" 374 | ], 375 | "time_options": [ 376 | "5m", 377 | "15m", 378 | "1h", 379 | "6h", 380 | "12h", 381 | "24h", 382 | "2d", 383 | "7d", 384 | "30d" 385 | ] 386 | }, 387 | "templating": { 388 | "list": [] 389 | }, 390 | "annotations": { 391 | "list": [] 392 | }, 393 | "refresh": "10s", 394 | "schemaVersion": 12, 395 | "version": 9, 396 | "links": [], 397 | "gnetId": null 398 | } -------------------------------------------------------------------------------- /grafana/provisioning/datasources/datasource.yml: -------------------------------------------------------------------------------- 1 | apiVersion: 1 2 | 3 | datasources: 4 | - name: Prometheus 5 | type: prometheus 6 | access: proxy 7 | orgId: 1 8 | url: http://prometheus:9090 9 | basicAuth: false 10 | isDefault: true 11 | editable: true -------------------------------------------------------------------------------- /main.py: -------------------------------------------------------------------------------- 1 | from contextlib import asynccontextmanager 2 | from fastapi import FastAPI 3 | from fastapi_cache import FastAPICache 4 | from fastapi_cache.backends.redis import RedisBackend 5 | from fastapi_cache.decorator import cache 6 | 7 | from redis import asyncio as aioredis 8 | 9 | from api_v1 import register_routers 10 | from app_includes import ( 11 | register_errors, 12 | register_middlewares, 13 | register_prometheus, 14 | ) 15 | from config import settings 16 | 17 | 18 | def start_app() -> FastAPI: 19 | """ 20 | Создание приложения со всеми настройками 21 | """ 22 | app = FastAPI(lifespan=lifespan) 23 | register_routers(app=app) 24 | register_errors(app=app) 25 | register_middlewares(app=app) 26 | register_prometheus(app=app) 27 | return app 28 | 29 | 30 | @asynccontextmanager 31 | async def lifespan(app: FastAPI): 32 | redis = aioredis.from_url(settings.redis.redis_url) 33 | FastAPICache.init(RedisBackend(redis), prefix='fastapi-cache') 34 | yield 35 | 36 | 37 | app = start_app() 38 | 39 | 40 | @app.get(path='/test') 41 | @cache() 42 | async def test_end_point(): 43 | return dict(hello='world') 44 | -------------------------------------------------------------------------------- /prometheus/alert.rules: -------------------------------------------------------------------------------- 1 | groups: 2 | - name: targets 3 | rules: 4 | - alert: monitor_service_down 5 | expr: up == 0 6 | for: 30s 7 | labels: 8 | severity: critical 9 | annotations: 10 | summary: "Monitor service non-operational" 11 | description: "Service {{ $labels.instance }} is down." 12 | 13 | - name: host 14 | rules: 15 | - alert: high_cpu_load 16 | expr: node_load1 > 1.5 17 | for: 30s 18 | labels: 19 | severity: warning 20 | annotations: 21 | summary: "Server under high load" 22 | description: "Docker host is under high load, the avg load 1m is at {{ $value}}. Reported by instance {{ $labels.instance }} of job {{ $labels.job }}." 23 | 24 | - alert: high_memory_load 25 | expr: (sum(node_memory_MemTotal_bytes) - sum(node_memory_MemFree_bytes + node_memory_Buffers_bytes + node_memory_Cached_bytes) ) / sum(node_memory_MemTotal_bytes) * 100 > 85 26 | for: 30s 27 | labels: 28 | severity: warning 29 | annotations: 30 | summary: "Server memory is almost full" 31 | description: "Docker host memory usage is {{ humanize $value}}%. Reported by instance {{ $labels.instance }} of job {{ $labels.job }}." 32 | 33 | - alert: high_storage_load 34 | expr: (node_filesystem_size_bytes{fstype="aufs"} - node_filesystem_free_bytes{fstype="aufs"}) / node_filesystem_size_bytes{fstype="aufs"} * 100 > 85 35 | for: 30s 36 | labels: 37 | severity: warning 38 | annotations: 39 | summary: "Server storage is almost full" 40 | description: "Docker host storage usage is {{ humanize $value}}%. Reported by instance {{ $labels.instance }} of job {{ $labels.job }}." 41 | 42 | - name: containers 43 | rules: 44 | - alert: jenkins_down 45 | expr: absent(container_memory_usage_bytes{name="jenkins"}) 46 | for: 30s 47 | labels: 48 | severity: critical 49 | annotations: 50 | summary: "Jenkins down" 51 | description: "Jenkins container is down for more than 30 seconds." 52 | 53 | - alert: jenkins_high_cpu 54 | expr: sum(rate(container_cpu_usage_seconds_total{name="jenkins"}[1m])) / count(node_cpu_seconds_total{mode="system"}) * 100 > 10 55 | for: 30s 56 | labels: 57 | severity: warning 58 | annotations: 59 | summary: "Jenkins high CPU usage" 60 | description: "Jenkins CPU usage is {{ humanize $value}}%." 61 | 62 | - alert: jenkins_high_memory 63 | expr: sum(container_memory_usage_bytes{name="jenkins"}) > 1200000000 64 | for: 30s 65 | labels: 66 | severity: warning 67 | annotations: 68 | summary: "Jenkins high memory usage" 69 | description: "Jenkins memory consumption is at {{ humanize $value}}." -------------------------------------------------------------------------------- /prometheus/prometheus.yml: -------------------------------------------------------------------------------- 1 | global: 2 | scrape_interval: 15s 3 | scrape_timeout: 10s 4 | evaluation_interval: 15s 5 | external_labels: 6 | monitor: 'docker-host-alpha' 7 | 8 | rule_files: 9 | - "alert.rules" 10 | 11 | scrape_configs: 12 | 13 | - job_name: prometheus 14 | honor_timestamps: true 15 | scrape_interval: 15s 16 | scrape_timeout: 10s 17 | metrics_path: /metrics 18 | scheme: http 19 | follow_redirects: true 20 | static_configs: 21 | - targets: 22 | - localhost:9090 23 | 24 | - job_name: 'fast_api' 25 | scrape_interval: 10s 26 | metrics_path: /metrics 27 | static_configs: 28 | - targets: ['fast_api:8000'] 29 | 30 | alerting: 31 | alertmanagers: 32 | - follow_redirects: true 33 | enable_http2: true 34 | scheme: http 35 | timeout: 10s 36 | api_version: v2 37 | static_configs: 38 | - targets: 39 | - 'alertmanager:9093' 40 | -------------------------------------------------------------------------------- /pyproject.toml: -------------------------------------------------------------------------------- 1 | [tool.poetry] 2 | name = "almaz-test" 3 | version = "0.1.0" 4 | description = "" 5 | authors = ["Alex Pavlov "] 6 | readme = "README.md" 7 | 8 | [tool.poetry.dependencies] 9 | python = "^3.11" 10 | fastapi = "^0.115.5" 11 | uvicorn = "^0.32.0" 12 | sqlalchemy = {extras = ["asyncio"], version = "^2.0.36"} 13 | asyncpg = "^0.30.0" 14 | pydantic-settings = "^2.6.1" 15 | httpx = "^0.27.2" 16 | celery = "^5.4.0" 17 | flower = "^2.0.1" 18 | loguru = "^0.7.2" 19 | alembic = "^1.14.0" 20 | pytest = "^8.3.3" 21 | prometheus-fastapi-instrumentator = "^7.0.0" 22 | fastapi-users = {extras = ["sqlalchemy"], version = "^14.0.0"} 23 | redis = {extras = ["redis"], version = "^5.2.1"} 24 | fastapi-cache2 = "^0.2.2" 25 | 26 | 27 | [tool.poetry.group.dev.dependencies] 28 | black = "^24.10.0" 29 | pytest-asyncio = "^0.24.0" 30 | asgi-lifespan = "^2.1.0" 31 | flake8 = "^7.1.1" 32 | 33 | [build-system] 34 | requires = ["poetry-core"] 35 | build-backend = "poetry.core.masonry.api" 36 | --------------------------------------------------------------------------------