From 9ac225ef2f696114e373ddaa05d965c07cc09f18 Mon Sep 17 00:00:00 2001 From: Anas WS Date: Mon, 8 Apr 2024 12:58:21 +0530 Subject: [PATCH 1/5] Readme file updated alongwith other validations and changes --- .env.example | 20 ++--- README.md | 122 +++++++++++++++++--------- app/app.py | 4 +- app/config/base.py | 15 +--- app/config/redis_config.py | 2 + app/routes/home/home.py | 2 +- app/utils/slack_notification_utils.py | 2 +- app/wrappers/cache_wrappers.py | 28 +++--- compose.yaml | 6 +- 9 files changed, 110 insertions(+), 91 deletions(-) diff --git a/.env.example b/.env.example index 0b92de1..e180692 100644 --- a/.env.example +++ b/.env.example @@ -1,19 +1,17 @@ +SECRET_KEY=your_secret_key +# Database Configuration +DB_NAME= +DB_HOSTNAME=localhost +DB_PORT=3306 DB_USERNAME= DB_PASSWORD= -DB_HOSTNAME= -DB_PORT= -DB_NAME= -SECRET_KEY= -REDIS_URL= +DB_ROOT_PASSWORD= +# Additional Configuration +REDIS_URL=redis://localhost:6379/ SENTRY_DSN= SLACK_WEBHOOK_URL= -ENVIRONMENT_NAME= -DB_ROOT_PASSWORD= //this is applicable for .env.local file only -// you will get these data from signoz portal +# Advance Usage - Signoz Configuration OTEL_RESOURCE_ATTRIBUTES= OTEL_EXPORTER_OTLP_ENDPOINT= OTEL_EXPORTER_OTLP_HEADERS= OTEL_EXPORTER_OTLP_PROTOCOL= -CACHE_ENABLED= -SENTRY_ENABLED= -SLACK_ENABLED= diff --git a/README.md b/README.md index 439433d..8f1ebc7 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,20 @@ This repository provides a template for creating and deploying a FastAPI project - [Features](#features) - [Getting started](#getting-started) + - Initialize & Setup Environment + - Variables Configuration + - Database Migrations + - Redis Dependency & Installation + - Celery Dependency + - Run the Project - [Advanced Usage](#advanced-usage) + - Circuit breakers + - Configuring Signoz Monitoring Tool + - Logging with Signoz + - Database Monitoring Using Percona + - Deploy Service on AWS ECS + - Related Dashboard Links + - Additional Useful scripts ### Features @@ -38,23 +51,33 @@ This repository provides a template for creating and deploying a FastAPI project - Docker - mysql -#### 1. Initialize Local Environment +#### 1. Initialize & Setup Environment -- To set up your local environment, run the following script: +- To initialize and set up your environment, run the following script: ``` ./scripts/initialize-env.sh ``` -This script installs the necessary dependencies and prepares the environment for running the FastAPI application on your local machine. +This script installs the necessary dependencies and prepares the environment for running the FastAPI application on your machine. -Create a .env.local file with reference of .env.example - Run following command to inject env file - ```shell - set -a source .env.local set +a - ``` +#### 2. Variables Configuration +Update database environment variables in your .env.local file: +``` +DB_NAME= +DB_HOSTNAME=localhost +DB_PORT=3306 +DB_USERNAME= +DB_PASSWORD= +DB_ROOT_PASSWORD= +``` +Additional Configuration (Optional): +``` +SENTRY_DSN= +SLACK_WEBHOOK_URL= +``` -#### 2. Database Migrations +#### 3. Database Migrations Create new database migrations when you make changes to your models. Use the following command: ``` alembic revision -m 'brief description of changes' @@ -68,24 +91,40 @@ alembic upgrade head ``` This command updates the database schema to reflect the latest changes defined in the migration scripts -#### 3. Redis Dependency +#### 4. Redis Dependency +##### Install Locally: ``` -docker run --name recorder-redis -p 6379:6379 -d redis:alpine +# Mac +brew install redis +brew services start redis + +# Windows +Please refer: https://developer.redis.com/create/windows/ + +# Linux +sudo apt install redis +sudo systemctl enable redis +sudo systemctl start redis +sudo systemctl status redis # verify status + ``` -or add the REDIS_URL in .env.local file +##### Install via docker: +``` +docker run --name recorder-redis -p 6379:6379 -d redis:alpine +``` -#### 4. Celery Dependency -- Run following command to initiallize the celery worker - ```shell - celery -A app.app.celery worker -l info - ``` -- Turn Up Celery Flower with - ```shell - flower --broker=${REDIS_URL}/6 --port=5555 - ``` +#### 5. Celery Dependency +Run following command to initiallize the celery worker +``` +celery -A app.app.celery worker -l info +``` +Turn Up Celery Flower with +``` +flower --broker=${REDIS_URL}/6 --port=5555 +``` -#### 5. Run the Project +#### 6. Run the Project ##### Running Application Locally @@ -106,23 +145,9 @@ This script upgrades the database migrations using Alembic and starts the FastAP docker compose --env-file .env.docker up ``` +### Advanced Usage -#### 6. Deploy Service on AWS ECS -To deploy the FastAPI application on AWS ECS, use the following script: - -``` -./scripts/setup-ecs.sh develop -``` - -The setup-ecs.sh script leverages AWS Copilot to deploy the service. Provide the environment name as an argument (e.g., develop). The script creates and deploys an environment, then deploys the FastAPI service on that environment. - -Note: Ensure you have AWS credentials configured and AWS Copilot installed for successful deployment. - -#### New to AWS Copilot? -If you are new to AWS Copilot or you want to learn more about AWS Copilot, please refer to [this helpful article](https://www.wednesday.is/writing-tutorials/how-to-use-copilot-to-deploy-projects-on-ecs) that guides you through the process of setting up AWS Copilot locally as well as also helps you understand how you can publish and update an application using 4 simple steps. - - -#### 7. Circuit breakers +#### Circuit breakers Using the Circuit Breaker for External API Calls @@ -147,9 +172,7 @@ async def external_service_endpoint(): raise HTTPException(status_code=503, detail="Service temporarily unavailable") ``` -### Advanced Usage - -#### Using Signoz Monitoring Tool +#### Configuring Signoz Monitoring Tool To utilize Signoz for monitoring your applications, follow these steps: @@ -236,6 +259,21 @@ To monitor your database using Percona, follow these steps: By following these steps, you'll successfully configure Percona to monitor your MySQL database. +#### Deploy Service on AWS ECS +To deploy the FastAPI application on AWS ECS, use the following script: + +``` +./scripts/setup-ecs.sh develop +``` + +The setup-ecs.sh script leverages AWS Copilot to deploy the service. Provide the environment name as an argument (e.g., develop). The script creates and deploys an environment, then deploys the FastAPI service on that environment. + +Note: Ensure you have AWS credentials configured and AWS Copilot installed for successful deployment. + +#### New to AWS Copilot? +If you are new to AWS Copilot or you want to learn more about AWS Copilot, please refer to [this helpful article](https://www.wednesday.is/writing-tutorials/how-to-use-copilot-to-deploy-projects-on-ecs) that guides you through the process of setting up AWS Copilot locally as well as also helps you understand how you can publish and update an application using 4 simple steps. + + #### Dashboard Links - Percona: https://localhost:443 - Flower: http://localhost:5556 @@ -243,7 +281,7 @@ By following these steps, you'll successfully configure Percona to monitor your - Swagger UI: http://localhost:8000 -#### Useful scripts +#### Additional Useful scripts - Tests - scripts/run_tests.sh - Linting & Formatting - scripts/lint_and_format.sh - Load tests - scripts/load_tests.sh (Change [locust.conf](https://docs.locust.io/en/stable/configuration.html) accordinly) diff --git a/app/app.py b/app/app.py index 5628933..a4d09fc 100644 --- a/app/app.py +++ b/app/app.py @@ -21,7 +21,7 @@ # Sentry Initialization -if settings.SENTRY_ENABLED: +if settings.SENTRY_DSN: sentry_sdk.init( dsn=settings.SENTRY_DSN, traces_sample_rate=1.0, # Sample rate of 100% @@ -49,7 +49,7 @@ app.add_middleware(RequestIdInjection) app.add_middleware(CacheMiddleware, cached_endpoints=cached_endpoints.CACHED_ENDPOINTS) -if settings.SENTRY_ENABLED: +if settings.SENTRY_DSN: try: app.add_middleware(SentryAsgiMiddleware) except Exception as e: diff --git a/app/config/base.py b/app/config/base.py index 825a694..d91332c 100644 --- a/app/config/base.py +++ b/app/config/base.py @@ -11,19 +11,10 @@ class DBSettings(BaseSettings): DB_PASSWORD: str class Config: - env_file = ".env" + env_file = ".env.local" -class FlagFeatureSettings(BaseSettings): - CACHE_ENABLED: bool - SENTRY_ENABLED: bool = False - SLACK_ENABLED: bool = False - - class Config: - env_file = ".env" - - -class Settings(FlagFeatureSettings, DBSettings): +class Settings(DBSettings): SECRET_KEY: str REDIS_URL: str SENTRY_DSN: str | None @@ -32,7 +23,7 @@ class Settings(FlagFeatureSettings, DBSettings): CACHE_MAX_AGE: int = 60 class Config: - env_file = ".env" + env_file = ".env.local" class CachedEndpoints(BaseSettings): diff --git a/app/config/redis_config.py b/app/config/redis_config.py index 1e64665..36c8174 100644 --- a/app/config/redis_config.py +++ b/app/config/redis_config.py @@ -6,4 +6,6 @@ async def get_redis_pool(): + if not settings.REDIS_URL: + raise ValueError("REDIS_URL is not set") return asyncio.from_url(settings.REDIS_URL, encoding="utf-8", decode_responses=True) diff --git a/app/routes/home/home.py b/app/routes/home/home.py index 675a41f..67545ff 100644 --- a/app/routes/home/home.py +++ b/app/routes/home/home.py @@ -35,7 +35,7 @@ async def external_service_endpoint(): @home_router.get("/sentry-test", tags=["Home"]) def sentry_endpoint(): - if not settings.SENTRY_ENABLED: + if not settings.SENTRY_DSN: raise HTTPException(status_code=503, detail="Sentry is not enabled") raise CentryTestException("Sentry Test") diff --git a/app/utils/slack_notification_utils.py b/app/utils/slack_notification_utils.py index e27329e..6fbb606 100644 --- a/app/utils/slack_notification_utils.py +++ b/app/utils/slack_notification_utils.py @@ -16,7 +16,7 @@ def send_slack_message(payload): Returns: HTTP response code, i.e., """ - if not settings.SLACK_ENABLEDs: + if not settings.SLACK_WEBHOOK_URL: return webhook_url = settings.SLACK_WEBHOOK_URL diff --git a/app/wrappers/cache_wrappers.py b/app/wrappers/cache_wrappers.py index 08f6c32..65b8f66 100644 --- a/app/wrappers/cache_wrappers.py +++ b/app/wrappers/cache_wrappers.py @@ -1,32 +1,24 @@ from __future__ import annotations -from app.config.base import settings from app.config.redis_config import get_redis_pool class CacheUtils: - CACHE_ENABLED = settings.CACHE_ENABLED - @classmethod async def create_cache(cls, resp, key: str, ex: int = 60): - if cls.CACHE_ENABLED: - redis = await get_redis_pool() - await redis.set(key, resp, ex=ex) + redis = await get_redis_pool() + await redis.set(key, resp, ex=ex) @classmethod async def retrieve_cache(cls, key: str): - if cls.CACHE_ENABLED: - redis = await get_redis_pool() - data = await redis.get(key) - if not data: - return None, None - expire = await redis.ttl(key) - return data, expire - return None, None + redis = await get_redis_pool() + data = await redis.get(key) + if not data: + return None, None + expire = await redis.ttl(key) + return data, expire @classmethod async def invalidate_cache(cls, key: str): - if cls.CACHE_ENABLED: - redis = await get_redis_pool() - await redis.delete(key) - return None + redis = await get_redis_pool() + await redis.delete(key) diff --git a/compose.yaml b/compose.yaml index e988f28..9a1ffef 100644 --- a/compose.yaml +++ b/compose.yaml @@ -45,14 +45,12 @@ services: restart: always container_name: fast-api-app env_file: - - ./.env.${ENVIRONMENT_NAME} + - ./.env.local depends_on: wait-for-db: condition: service_completed_successfully ports: - "${SERVER_PORT}:${SERVER_PORT}" - environment: - ENVIRONMENT_NAME: "${ENVIRONMENT_NAME}" networks: - fast-api-network @@ -62,7 +60,7 @@ services: dockerfile: Dockerfile command: ['celery', '-A', 'app.app.celery', 'worker', '-l', 'info'] env_file: - - .env.${ENVIRONMENT_NAME} + - .env.local depends_on: - redis - app From 43ad83df2bd0c016671fe84b961fca1650401e38 Mon Sep 17 00:00:00 2001 From: Anas WS Date: Mon, 8 Apr 2024 13:28:07 +0530 Subject: [PATCH 2/5] check_environment_variables added --- README.md | 2 +- app/app.py | 3 +++ app/config/base.py | 12 +++++++++++- app/config/redis_config.py | 2 -- scripts/initialize-env.sh | 10 +++++----- 5 files changed, 20 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index 8f1ebc7..ba2a351 100644 --- a/README.md +++ b/README.md @@ -69,12 +69,12 @@ DB_HOSTNAME=localhost DB_PORT=3306 DB_USERNAME= DB_PASSWORD= -DB_ROOT_PASSWORD= ``` Additional Configuration (Optional): ``` SENTRY_DSN= SLACK_WEBHOOK_URL= +DB_ROOT_PASSWORD= ``` #### 3. Database Migrations diff --git a/app/app.py b/app/app.py index a4d09fc..a181b4c 100644 --- a/app/app.py +++ b/app/app.py @@ -27,6 +27,9 @@ traces_sample_rate=1.0, # Sample rate of 100% ) +# Check required environment variables +settings.check_environment_variables() + app = FastAPI( title="FastAPI Template", description="This is my first API use FastAPI", diff --git a/app/config/base.py b/app/config/base.py index d91332c..bbd3239 100644 --- a/app/config/base.py +++ b/app/config/base.py @@ -18,13 +18,23 @@ class Settings(DBSettings): SECRET_KEY: str REDIS_URL: str SENTRY_DSN: str | None - SLACK_WEBHOOK_URL: str + SLACK_WEBHOOK_URL: str | None ALLOWED_HOSTS: list = ["*"] CACHE_MAX_AGE: int = 60 class Config: env_file = ".env.local" + def check_environment_variables(self): + if not self.DB_HOSTNAME or not self.DB_PORT or not self.DB_NAME or not self.DB_USERNAME or not self.DB_PASSWORD: + raise ValueError("Database environment variables are not set") + + if not self.SECRET_KEY: + raise ValueError("SECRET_KEY is not set") + + if not self.REDIS_URL: + raise ValueError("REDIS_URL is not set") + class CachedEndpoints(BaseSettings): CACHED_ENDPOINTS: list = ["/cache-sample/"] diff --git a/app/config/redis_config.py b/app/config/redis_config.py index 36c8174..1e64665 100644 --- a/app/config/redis_config.py +++ b/app/config/redis_config.py @@ -6,6 +6,4 @@ async def get_redis_pool(): - if not settings.REDIS_URL: - raise ValueError("REDIS_URL is not set") return asyncio.from_url(settings.REDIS_URL, encoding="utf-8", decode_responses=True) diff --git a/scripts/initialize-env.sh b/scripts/initialize-env.sh index ed1f20d..d37db2e 100755 --- a/scripts/initialize-env.sh +++ b/scripts/initialize-env.sh @@ -1,15 +1,12 @@ echo "Enabling Pre Commit Hooks" -#Enabling Pre-Commit hooks for git pre-commit install echo echo "Initializing local python environment" -# Initialize python environment python -m venv venv echo echo "Activating local python environment" -# Activate python environment source ./venv/bin/activate echo echo "Local python environment activated" @@ -17,7 +14,6 @@ echo "Local python environment activated" echo echo "Upgrading pip" echo -# Upgrade pip pip install --upgrade pip echo echo "pip upgraded" @@ -25,6 +21,10 @@ echo "pip upgraded" echo echo "Installing project dependencies" echo -# Install requirements/dependencies pip install -r requirements.txt --no-cache-dir + +echo +echo "Copying .env.example to .env.local" +cp .env.example .env.local + opentelemetry-bootstrap --action=install From 79ce5f0a2db4d8a9ba0d81ec868e608a0845c111 Mon Sep 17 00:00:00 2001 From: Anas WS Date: Mon, 8 Apr 2024 14:19:07 +0530 Subject: [PATCH 3/5] Removed unneccessary requirements --- .env.example | 3 +- README.md | 30 +++++++-------- requirements.txt | 82 +---------------------------------------- scripts/local_server.sh | 16 +++++++- 4 files changed, 33 insertions(+), 98 deletions(-) diff --git a/.env.example b/.env.example index e180692..dbc0751 100644 --- a/.env.example +++ b/.env.example @@ -5,7 +5,6 @@ DB_HOSTNAME=localhost DB_PORT=3306 DB_USERNAME= DB_PASSWORD= -DB_ROOT_PASSWORD= # Additional Configuration REDIS_URL=redis://localhost:6379/ SENTRY_DSN= @@ -15,3 +14,5 @@ OTEL_RESOURCE_ATTRIBUTES= OTEL_EXPORTER_OTLP_ENDPOINT= OTEL_EXPORTER_OTLP_HEADERS= OTEL_EXPORTER_OTLP_PROTOCOL= +# Advance Usage - Docker Configuration +DB_ROOT_PASSWORD= diff --git a/README.md b/README.md index ba2a351..eb89f4b 100644 --- a/README.md +++ b/README.md @@ -6,20 +6,20 @@ This repository provides a template for creating and deploying a FastAPI project - [Features](#features) - [Getting started](#getting-started) - - Initialize & Setup Environment - - Variables Configuration - - Database Migrations - - Redis Dependency & Installation - - Celery Dependency - - Run the Project + - [Initialize & Setup Environment](#1-initialize--setup-environment) + - [Variables Configuration](#2-variables-configuration) + - [Database Migrations](#3-database-migrations) + - [Redis Dependency & Installation](#4-redis-dependency) + - [Celery Dependency](#5-celery-dependency) + - [Run the Project](#6-run-the-project) - [Advanced Usage](#advanced-usage) - - Circuit breakers - - Configuring Signoz Monitoring Tool - - Logging with Signoz - - Database Monitoring Using Percona - - Deploy Service on AWS ECS - - Related Dashboard Links - - Additional Useful scripts + - [Circuit breakers](#circuit-breakers) + - [Configuring Signoz Monitoring Tool](#configuring-signoz-monitoring-tool) + - [Logging with Signoz](#logging-with-signoz) + - [Database Monitoring Using Percona](#database-monitoring-using-percona) + - [Deploy Service on AWS ECS](#deploy-service-on-aws-ecs) + - [Related Dashboard Links](#related-dashboard-links) + - [Additional Useful scripts](#additional-useful-scripts) ### Features @@ -119,7 +119,7 @@ Run following command to initiallize the celery worker ``` celery -A app.app.celery worker -l info ``` -Turn Up Celery Flower with +[Optional] Turn Up Celery Flower with ``` flower --broker=${REDIS_URL}/6 --port=5555 ``` @@ -274,7 +274,7 @@ Note: Ensure you have AWS credentials configured and AWS Copilot installed for s If you are new to AWS Copilot or you want to learn more about AWS Copilot, please refer to [this helpful article](https://www.wednesday.is/writing-tutorials/how-to-use-copilot-to-deploy-projects-on-ecs) that guides you through the process of setting up AWS Copilot locally as well as also helps you understand how you can publish and update an application using 4 simple steps. -#### Dashboard Links +#### Related Dashboard Links - Percona: https://localhost:443 - Flower: http://localhost:5556 - Locust UI: http://localhost:8089 diff --git a/requirements.txt b/requirements.txt index 7aae68e..45cfcf6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,122 +1,44 @@ -aiohttp>3.4 -aiosignal==1.3.1 alchemy-mock alembic>1 anyio==3.7.0 -argilla>1.1 -async-timeout==4.0.2 -attrs==23.1.0 -backoff==2.2.1 black celery>=5.3.6 certifi==2023.5.7 -cffi==1.15.1 -chardet==5.1.0 -charset-normalizer==3.1.0 -click==8.1.3 -clickhouse-connect==0.5.25 coloredlogs==15.0.1 contextvars coverage==7.2.7 cryptography==40.0.2 -dataclasses-json==0.5.7 -Deprecated==1.2.14 email_validator -et-xmlfile==1.1.0 fastapi==0.95.2 fastapi-pagination flake8 -flatbuffers==23.5.26 flower==2.0.1 freezegun==1.4.0 -frozenlist==1.3.3 -ftfy==6.1.1 -greenlet==2.0.2 -h11 hiredis -httpcore -httptools==0.5.0 -httpx -humanfriendly==10.0 -idna==3.4 -iniconfig==2.0.0 -itsdangerous -joblib==1.2.0 -langchain locust>=2.0 -lxml==4.9.2 -lz4==4.3.2 -Markdown==3.4.3 -marshmallow==3.19.0 -marshmallow-enum==1.5.1 -monotonic==1.6 -mpmath==1.3.0 -msg-parser==1.2.0 -multidict==6.0.4 -mypy-extensions==1.0.0 -nltk==3.8.1 -olefile==0.46 openai==0.27.7 openapi-schema-pydantic==1.2.4 openpyxl==3.1.2 -overrides==7.3.1 -packaging==23.1 -pdf2image -pdfminer.six==20221105 -Pillow>=9.0 -pluggy==1.0.0 -posthog==3.0.1 +opentelemetry-distro==0.41b0 +opentelemetry-exporter-otlp==1.20.0 pre-commit -protobuf==4.23.2 pybreaker -pycparser==2.21 pydantic==1.10.8 pyjwt pylint PyMySQL==1.0.3 -pypandoc==1.11 pytest==7.3.1 pytest-asyncio pytest-cov pytest-sqlalchemy-mock==0.1.5 pytest_mock_resources -python-dateutil==2.8.2 -python-docx==0.8.11 python-dotenv==1.0.0 -python-magic==0.4.15 -python-pptx==0.6.21 -python_on_whales -pytz==2023.3 -PyYAML==6.0 redis>4 -regex==2023.5.5 requests==2.31.0 -rfc3986==1.5.0 sentry-sdk -six==1.16.0 -sniffio==1.3.0 SQLAlchemy==2.0.15 starlette==0.27.0 -sympy==1.12 -tabulate==0.9.0 -tenacity==8.2.2 -tiktoken==0.4.0 -tokenizers==0.13.3 -tqdm==4.65.0 -typing-inspect==0.9.0 typing_extensions==4.6.2 -unstructured==0.6.11 urllib3==2.0.2 uvicorn==0.22.0 -uvloop==0.17.0 -watchfiles==0.19.0 -wcwidth==0.2.6 -websockets==11.0.3 werkzeug -wrapt==1.14.0 -xlrd==2.0.1 -XlsxWriter==3.1.2 -yarl==1.9.2 -zstandard==0.21.0 -opentelemetry-distro==0.41b0 -opentelemetry-exporter-otlp==1.20.0 \ No newline at end of file diff --git a/scripts/local_server.sh b/scripts/local_server.sh index c8d6221..37b28aa 100755 --- a/scripts/local_server.sh +++ b/scripts/local_server.sh @@ -1,2 +1,14 @@ -# Run migrations and start server -alembic upgrade head && opentelemetry-instrument uvicorn app.app:app --host 0.0.0.0 --port 8000 +echo +echo "Activating local python environment" +source ./venv/bin/activate +echo +echo "Local python environment activated" + +echo +echo "Running migrations" +alembic upgrade head +echo + +echo +echo "Starting server" +opentelemetry-instrument uvicorn app.app:app --host 0.0.0.0 --port 8000 From 44803b2690fc5fbc43d7563d99db627ee16c44f5 Mon Sep 17 00:00:00 2001 From: Anas WS Date: Mon, 8 Apr 2024 15:27:46 +0530 Subject: [PATCH 4/5] Fixed opentelematery warning --- README.md | 9 +++++++++ scripts/initialize-env.sh | 8 ++++---- scripts/local_server.sh | 16 +++++++++------- 3 files changed, 22 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index eb89f4b..d0cabce 100644 --- a/README.md +++ b/README.md @@ -61,6 +61,15 @@ This repository provides a template for creating and deploying a FastAPI project This script installs the necessary dependencies and prepares the environment for running the FastAPI application on your machine. +##### Activate the environment +``` +# Mac & Linux: +source ./venv/bin/activate + +# Windows +.\venv\scripts\activate +``` + #### 2. Variables Configuration Update database environment variables in your .env.local file: ``` diff --git a/scripts/initialize-env.sh b/scripts/initialize-env.sh index d37db2e..284dfd9 100755 --- a/scripts/initialize-env.sh +++ b/scripts/initialize-env.sh @@ -1,7 +1,3 @@ -echo "Enabling Pre Commit Hooks" -pre-commit install -echo - echo "Initializing local python environment" python -m venv venv @@ -27,4 +23,8 @@ echo echo "Copying .env.example to .env.local" cp .env.example .env.local +echo "Enabling Pre Commit Hooks" +pre-commit install +echo + opentelemetry-bootstrap --action=install diff --git a/scripts/local_server.sh b/scripts/local_server.sh index 37b28aa..4376e42 100755 --- a/scripts/local_server.sh +++ b/scripts/local_server.sh @@ -1,9 +1,3 @@ -echo -echo "Activating local python environment" -source ./venv/bin/activate -echo -echo "Local python environment activated" - echo echo "Running migrations" alembic upgrade head @@ -11,4 +5,12 @@ echo echo echo "Starting server" -opentelemetry-instrument uvicorn app.app:app --host 0.0.0.0 --port 8000 + +if [ -z "$OTEL_EXPORTER_OTLP_PROTOCOL" ] +then + echo "OTEL_EXPORTER_OTLP_PROTOCOL is not set. Starting server without opentelemetry" + uvicorn app.app:app --host 0.0.0.0 --port 8000 +else + echo "OTEL_EXPORTER_OTLP_PROTOCOL is set. Starting server with opentelemetry" + opentelemetry-instrument uvicorn app.app:app --host 0.0.0.0 --port 8000 +fi From 452a65cd62c9621747c2316cf82eca12b7a7e965 Mon Sep 17 00:00:00 2001 From: Anas WS Date: Mon, 8 Apr 2024 16:38:04 +0530 Subject: [PATCH 5/5] reverted enviornment_name --- .env.example | 5 +++-- compose.yaml | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.env.example b/.env.example index dbc0751..57c291e 100644 --- a/.env.example +++ b/.env.example @@ -5,6 +5,7 @@ DB_HOSTNAME=localhost DB_PORT=3306 DB_USERNAME= DB_PASSWORD= +DB_ROOT_PASSWORD= # Additional Configuration REDIS_URL=redis://localhost:6379/ SENTRY_DSN= @@ -14,5 +15,5 @@ OTEL_RESOURCE_ATTRIBUTES= OTEL_EXPORTER_OTLP_ENDPOINT= OTEL_EXPORTER_OTLP_HEADERS= OTEL_EXPORTER_OTLP_PROTOCOL= -# Advance Usage - Docker Configuration -DB_ROOT_PASSWORD= +# Docker Configuration +ENVIRONMENT_NAME= diff --git a/compose.yaml b/compose.yaml index 9a1ffef..1254f5f 100644 --- a/compose.yaml +++ b/compose.yaml @@ -45,12 +45,14 @@ services: restart: always container_name: fast-api-app env_file: - - ./.env.local + - ./.env.${ENVIRONMENT_NAME} depends_on: wait-for-db: condition: service_completed_successfully ports: - "${SERVER_PORT}:${SERVER_PORT}" + environment: + ENVIRONMENT_NAME: "${ENVIRONMENT_NAME}" networks: - fast-api-network @@ -60,7 +62,7 @@ services: dockerfile: Dockerfile command: ['celery', '-A', 'app.app.celery', 'worker', '-l', 'info'] env_file: - - .env.local + - .env.${ENVIRONMENT_NAME} depends_on: - redis - app