Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@

services:
fastapi:
build: .
build:
context: .
dockerfile: Dockerfile
image: fastapi-app
networks:
- app-network
deploy:
replicas: 3
restart_policy:
condition: on-failure
environment:
- APP_ENV=production
prometheus:
image: prom/prometheus:latest
ports:
Expand Down
8 changes: 6 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import logging
from fastapi import FastAPI
import os
from fastapi import FastAPI, HTTPException
from prometheus_fastapi_instrumentator import Instrumentator
from prometheus_client import Counter

Expand Down Expand Up @@ -33,4 +34,7 @@ async def info():
async def health():
logger.info("Accessing health endpoint")
custom_requests.labels(endpoint="health").inc()
return {"status": "healthy"}
env = os.getenv("APP_ENV", "production")
if env != "production":
raise HTTPException(status_code=503, detail="Not in production mode")
return {"status": "healthy", "env": env}
1 change: 1 addition & 0 deletions project4_fastapi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"message":"Welcome to my DevOps API!"}
1 change: 1 addition & 0 deletions project4_health.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"status":"healthy"}
1 change: 1 addition & 0 deletions project4_healthcheck.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"status":"healthy"}
1 change: 1 addition & 0 deletions project4_metrics.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"status":"success","data":{"resultType":"vector","result":[{"metric":{"__name__":"custom_requests_total","endpoint":"root","instance":"fastapi:80","job":"fastapi"},"value":[1756641419.820,"1"]},{"metric":{"__name__":"custom_requests_total","endpoint":"health","instance":"fastapi:80","job":"fastapi"},"value":[1756641419.820,"1"]}]}}
1 change: 1 addition & 0 deletions project4_metrics_final.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"status":"success","data":{"resultType":"vector","result":[]}}
6 changes: 6 additions & 0 deletions project4_ps.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
NAME IMAGE COMMAND SERVICE CREATED STATUS PORTS
server-setup-fastapi-1 fastapi-app "uvicorn main:app --…" fastapi About a minute ago Up About a minute
server-setup-fastapi-2 fastapi-app "uvicorn main:app --…" fastapi About a minute ago Up About a minute
server-setup-fastapi-3 fastapi-app "uvicorn main:app --…" fastapi About a minute ago Up About a minute
server-setup-nginx-1 nginx:latest "/docker-entrypoint.…" nginx About a minute ago Up About a minute 0.0.0.0:80->80/tcp, [::]:80->80/tcp
server-setup-prometheus-1 prom/prometheus:latest "/bin/prometheus --c…" prometheus About a minute ago Up About a minute 0.0.0.0:9090->9090/tcp, [::]:9090->9090/tcp
25 changes: 25 additions & 0 deletions project4_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
============================= test session starts ==============================
platform linux -- Python 3.12.3, pytest-8.4.1, pluggy-1.6.0 -- /home/ubuntu/server-setup/venv/bin/python3
cachedir: .pytest_cache
rootdir: /home/ubuntu/server-setup
plugins: anyio-4.10.0, httpx-0.35.0
collecting ... collected 4 items

test_main.py::test_root PASSED [ 25%]
test_main.py::test_health FAILED [ 50%]
test_main.py::test_info PASSED [ 75%]
test_main.py::test_metrics PASSED [100%]

=================================== FAILURES ===================================
_________________________________ test_health __________________________________

def test_health():
response = client.get("/health")
> assert response.status_code == 200
E assert 503 == 200
E + where 503 = <Response [503 Service Unavailable]>.status_code

test_main.py:15: AssertionError
=========================== short test summary info ============================
FAILED test_main.py::test_health - assert 503 == 200
========================= 1 failed, 3 passed in 0.63s ==========================
2 changes: 1 addition & 1 deletion test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def test_root():
def test_health():
response = client.get("/health")
assert response.status_code == 200
assert response.json() == {"status": "healthy"}
assert response.json().get("status") == "healthy"


def test_info():
Expand Down