Added docker creation initial process

This commit is contained in:
João Silva
2023-11-29 12:10:45 +00:00
parent e5fffe4d15
commit 32d1e79c57
25 changed files with 207 additions and 18 deletions

10
.gitignore vendored
View File

@@ -1,10 +1,10 @@
# Python
__pycache__/
*.pyc
app/__pycache__/
app/*.pyc
# Logs
logs/
*.log
app/logs/
app/*.log
# Environment variables
config/.env
app/config/.env

36
Dockerfile Normal file
View File

@@ -0,0 +1,36 @@
# Use an official Python runtime as a parent image
FROM python:3.12-slim
# Set the working directory in the container
WORKDIR /app
# Copy the current directory contents into the container at /app
COPY . /app
# Create a virtual environment and install dependencies
RUN python -m venv /venv
ENV PATH=/venv/bin:$PATH
# Install any needed packages specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt
# Make port 8000 available to the world outside this container
EXPOSE 8000
# Define environment variable
#ENV NAME World
ENV DB_HOST=""
ENV DB_PORT=3306
ENV DB_USER=""
ENV DB_PASSWORD=""
ENV DB_DATABASE=""
ENV SECRET_KEY=""
ENV ALGORITHM="HS256"
ENV ACCESS_TOKEN_EXPIRE_MINUTES=30
ENV STRAVA_CLIENT_ID=""
ENV STRAVA_CLIENT_SECRET=""
ENV STRAVA_AUTH_CODE=""
ENV JAEGER_HOST=""
ENV STRAVA_DAYS_ACTIVITIES_ONLINK=30
# Run app.py when the container launches
CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

0
README.md Normal file
View File

Binary file not shown.

Binary file not shown.

View File

@@ -107,6 +107,10 @@ def remove_expired_tokens():
minutes=int(os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES"))
)
# Add tags related to the expiration check
trace.get_current_span().set_attribute("expiration_time", expiration_time.isoformat())
trace.get_current_span().set_attribute("token_expire_minutes", os.getenv("ACCESS_TOKEN_EXPIRE_MINUTES"))
# Delete expired access tokens using SQLAlchemy ORM
with get_db_session() as db_session:
rows_deleted = (
@@ -116,6 +120,10 @@ def remove_expired_tokens():
)
db_session.commit()
# Add tags related to the database operation
trace.get_current_span().set_attribute("tokens_deleted", rows_deleted)
trace.get_current_span().set_attribute("database_commit", "success")
logger.info(f"{rows_deleted} access tokens deleted from the database")
# Log a success event
trace.get_current_span().add_event(
@@ -124,6 +132,11 @@ def remove_expired_tokens():
)
except Exception as e:
logger.error(e)
# Add tags related to the error
trace.get_current_span().set_attribute("error_message", str(e))
trace.get_current_span().set_attribute("database_commit", "failure")
trace.get_current_span().set_status(
trace.status.Status(trace.StatusCode.ERROR, str(e))
)

Binary file not shown.

Binary file not shown.

View File

@@ -8,19 +8,28 @@ from controllers import (
stravaController,
)
from datetime import datetime, timedelta
# from opentelemetry import trace
# from opentelemetry.exporter.jaeger.thrift import JaegerExporter
# from opentelemetry.sdk.resources import Resource
# from opentelemetry.sdk.trace import TracerProvider
# from opentelemetry.sdk.trace.export import SimpleSpanProcessor
# from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from opentelemetry import trace
from opentelemetry.exporter.jaeger.thrift import JaegerExporter
from opentelemetry.exporter.otlp.proto.grpc.trace_exporter import OTLPSpanExporter
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import SimpleSpanProcessor
from opentelemetry.sdk.trace.export import BatchSpanProcessor
from opentelemetry.instrumentation.fastapi import FastAPIInstrumentor
from dotenv import load_dotenv
# from dotenv import load_dotenv
import logging
import os
# import os
app = FastAPI()
load_dotenv("config/.env")
# load_dotenv("config/.env")
logger = logging.getLogger("myLogger")
logger.setLevel(logging.DEBUG)
@@ -34,18 +43,29 @@ file_handler.setFormatter(formatter)
logger.addHandler(file_handler)
# Configure Jaeger Exporter
jaeger_exporter = JaegerExporter(
agent_host_name=os.getenv('JAEGER_HOST'), # Update with your Jaeger host
agent_port=6831, # Update with your Jaeger port
)
# jaeger_exporter = JaegerExporter(
# agent_host_name=os.getenv("JAEGER_HOST"), # Update with your Jaeger host
# agent_port=6831, # Update with your Jaeger port
# )
# Create a TracerProvider with Jaeger exporter
tracer_provider = TracerProvider(resource=Resource.create({"service.name": "gearguardian-api"}))
trace.set_tracer_provider(tracer_provider)
tracer_provider.add_span_processor(SimpleSpanProcessor(jaeger_exporter))
# tracer_provider = TracerProvider(
# resource=Resource.create({"service.name": "gearguardian-api"})
# )
# trace.set_tracer_provider(tracer_provider)
# tracer_provider.add_span_processor(SimpleSpanProcessor(jaeger_exporter))
# trace.set_tracer_provider(TracerProvider(resource=Resource.create().add_attribute("service.name", "backend_api")))
trace.set_tracer_provider(
TracerProvider(resource=Resource.create({"service.name": "backend_api"}))
)
trace.get_tracer_provider().add_span_processor(
BatchSpanProcessor(OTLPSpanExporter(endpoint="http://192.168.2.80:4317"))
)
# Instrument FastAPI app
FastAPIInstrumentor.instrument_app(app, tracer_provider=tracer_provider)
FastAPIInstrumentor.instrument_app(app)
# Router files
app.include_router(sessionController.router)
@@ -69,6 +89,7 @@ scheduler.add_job(
minutes=60,
)
@app.on_event("startup")
async def startup_event():
# Get the tracer
@@ -78,6 +99,7 @@ async def startup_event():
with tracer.start_as_current_span("startup_event"):
print("Backend started!")
# Add the background scheduler to the app's shutdown event
@app.on_event("shutdown")
async def shutdown_event():

View File

@@ -0,0 +1,18 @@
receivers:
otlp:
protocols:
http:
processors:
batch:
exporters:
jaeger:
endpoint: "jaeger:14268" # Use the service name defined in your Jaeger service
service:
pipelines:
traces:
receivers: [otlp]
processors: [batch]
exporters: [jaeger]

88
docker-compose.yml Normal file
View File

@@ -0,0 +1,88 @@
version: '3'
services:
# API logic
gearguardian-api:
container_name: gearguardian-api
image: your-image-name
environment:
- DB_HOST=<change me> #required
- DB_PORT=3306 #optional - will use port 3306 by default
- DB_USER=<change me> #required
- DB_PASSWORD=<change me> #required
- DB_DATABASE=<change me> #required
- SECRET_KEY=<change me> #required
- ALGORITHM=HS256 #optional - will use HS256 by default
- ACCESS_TOKEN_EXPIRE_MINUTES=30 #optional - will use 30 minutes by default
- STRAVA_CLIENT_ID=<change me> #required
- STRAVA_CLIENT_SECRET=<change me> #required
- STRAVA_AUTH_CODE=<change me> #required
- JAEGER_HOST=<change me> #required
- STRAVA_DAYS_ACTIVITIES_ONLINK=30 #optional - will use 30 days by default
ports:
- "98:8000"
volumes:
- <host_path>/gearguardian-api:/app
env_file:
- ./app/config/.env # this will be removed. .env file will be moved to root folder
depends_on:
- mariadb
- otel-collector
networks:
- backend_network
restart: unless-stopped
# mysql mariadb logic
mariadb:
image: mariadb:latest
container_name: mariadb
environment:
- MYSQL_ROOT_PASSWORD=<change me> #required
- MYSQL_DATABASE=<change me> #required
- MYSQL_USER=<change me> #required
- MYSQL_PASSWORD=<change me> #required
ports:
- "3306:3306"
volumes:
- <host_path>/mariadb:/var/lib/mysql
networks:
- backend_network
restart: unless-stopped
otel-collector:
image: otel/opentelemetry-collector-contrib:latest
container_name: otel-collector
# Add any necessary configuration for the OpenTelemetry Collector here
volumes:
- <host_path>/otel-collector/otel-collector-config.yaml:/etc/otel-collector-config.yaml
ports:
- "4317:4317"
networks:
- backend_network
restart: unless-stopped
jaeger:
container_name: jaeger
image: jaegertracing/all-in-one:latest
environment:
- PUID=1000
- PGID=1000
- TZ=Europe/Lisbon
- COLLECTOR_ZIPKIN_HOST_PORT=:9411
ports:
- 6831:6831/udp
- 6832:6832/udp
- 5778:5778
- 16686:16686
- 4317:4317
- 4318:4318
- 14250:14250
- 14268:14268
- 14269:14269
- 9411:9411
networks:
- backend_network
restart: unless-stopped
networks:
backend_network:
driver: bridge

12
requirements.txt Normal file
View File

@@ -0,0 +1,12 @@
fastapi
uvicorn
sqlalchemy
mysqlclient
mysql-connector-python
python-jose[cryptography]
passlib[bcrypt]
apscheduler
requests
stravalib
opentelemetry-sdk
opentelemetry-instrumentation-fastapi