Skip to content

Commit 5c3aa16

Browse files
committed
Correction in app.py
1 parent 80e3996 commit 5c3aa16

2 files changed

Lines changed: 134 additions & 39 deletions

File tree

Backend/app.py

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,103 @@
11
from flask import Flask, Response
22
import os
33
import redis
4-
from prometheus_client import Counter, Histogram, generate_latest, Gauge, CONTENT_TYPE_LATEST # type: ignore[import]
54
import time
5+
from prometheus_client import ( # pyright: ignore[reportMissingImports]
6+
Counter,
7+
Histogram,
8+
Gauge,
9+
generate_latest,
10+
CONTENT_TYPE_LATEST,
11+
)
12+
13+
app = Flask(__name__)
14+
15+
# Prometheus Metrics
16+
REQUEST_COUNT = Counter(
17+
"http_requests_total",
18+
"Total HTTP Requests",
19+
["method", "endpoint", "http_status"],
20+
)
21+
22+
REQUEST_LATENCY = Histogram(
23+
"http_request_duration_seconds",
24+
"HTTP Request Latency",
25+
["method", "endpoint"],
26+
)
627

7-
REQUEST_COUNT = Counter("http_requests_total", "Total HTTP Requests", ["method", "endpoint", "http_status"])
8-
REQUEST_LATENCY = Histogram("http_request_duration_seconds", "HTTP Request Latency", ["method", "endpoint"])
9-
ACTIVE_REQUESTS = Gauge("active_requests", "Number of Active Requests")
28+
ACTIVE_REQUESTS = Gauge(
29+
"active_requests",
30+
"Number of Active Requests",
31+
)
1032

33+
# Redis Connection
1134
redis_client = redis.Redis(
1235
host=os.getenv("REDIS_HOST", "localhost"),
1336
port=int(os.getenv("APP_REDIS_PORT", 6379)),
14-
decode_responses=True
37+
decode_responses=True,
1538
)
16-
app = Flask(__name__)
39+
1740

1841
@app.route("/")
1942
def home():
2043
ACTIVE_REQUESTS.inc()
2144
start_time = time.time()
2245

2346
try:
24-
REQUEST_COUNT.labels(method="GET", endpoint="/", http_status=200).inc()
2547
count = redis_client.incr("visitor_count")
48+
49+
REQUEST_COUNT.labels(
50+
method="GET",
51+
endpoint="/",
52+
http_status="200"
53+
).inc()
54+
55+
return f"""
56+
<h1>AWS Production Flask App!</h1>
57+
<p>Visitor count: {count}</p>
58+
<p>Backend App is running on port 5000</p>
59+
"""
60+
2661
except Exception as e:
27-
return f"Error occurred: {str(e)}"
62+
REQUEST_COUNT.labels(
63+
method="GET",
64+
endpoint="/",
65+
http_status="500"
66+
).inc()
67+
68+
return f"Error occurred: {str(e)}", 500
2869

2970
finally:
3071
latency = time.time() - start_time
31-
REQUEST_LATENCY.observe(latency)
72+
73+
REQUEST_LATENCY.labels(
74+
method="GET",
75+
endpoint="/"
76+
).observe(latency)
77+
3278
ACTIVE_REQUESTS.dec()
33-
return f"""
34-
<h1>AWS Production Flask App!</h1>
35-
<p>Visitor count: {count}</p>
36-
<p>Backend App is running on port 5000</p>
37-
"""
79+
3880

3981
@app.route("/health")
4082
def health():
4183
try:
4284
redis_client.ping()
43-
return "Application Healthy"
85+
return "Application Healthy", 200
4486
except Exception as e:
45-
return f"Application Unhealthy: {str(e)}"
87+
return f"Application Unhealthy: {str(e)}", 500
88+
4689

4790
@app.route("/about")
4891
def about():
4992
return "Built by Harshu during Cloud Engineering Journey"
5093

51-
if __name__ == "__main__":
52-
app.run(host="0.0.0.0", port=5000)
53-
5494
@app.route("/metrics")
5595
def metrics():
56-
return Response(generate_latest(), 200, {"Content-Type": CONTENT_TYPE_LATEST})
96+
return Response(
97+
generate_latest(),
98+
mimetype=CONTENT_TYPE_LATEST,
99+
)
100+
101+
102+
if __name__ == "__main__":
103+
app.run(host="0.0.0.0", port=5000)

Frontend/app.py

Lines changed: 67 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,104 @@
11
from flask import Flask, Response
22
import os
33
import redis
4-
from prometheus_client import Counter, Histogram, generate_latest, Gauge, CONTENT_TYPE_LATEST # type: ignore[import]
54
import time
5+
from prometheus_client import ( # pyright: ignore[reportMissingImports]
6+
Counter,
7+
Histogram,
8+
Gauge,
9+
generate_latest,
10+
CONTENT_TYPE_LATEST,
11+
)
12+
13+
app = Flask(__name__)
614

7-
REQUEST_COUNT = Counter("http_requests_total", "Total HTTP Requests", ["method", "endpoint", "http_status"])
8-
REQUEST_LATENCY = Histogram("http_request_duration_seconds", "HTTP Request Latency", ["method", "endpoint"])
9-
ACTIVE_REQUESTS = Gauge("active_requests", "Number of Active Requests")
15+
# Prometheus Metrics
16+
REQUEST_COUNT = Counter(
17+
"http_requests_total",
18+
"Total HTTP Requests",
19+
["method", "endpoint", "http_status"],
20+
)
21+
22+
REQUEST_LATENCY = Histogram(
23+
"http_request_duration_seconds",
24+
"HTTP Request Latency",
25+
["method", "endpoint"],
26+
)
1027

28+
ACTIVE_REQUESTS = Gauge(
29+
"active_requests",
30+
"Number of Active Requests",
31+
)
32+
33+
# Redis Connection
1134
redis_client = redis.Redis(
1235
host=os.getenv("REDIS_HOST", "localhost"),
1336
port=int(os.getenv("APP_REDIS_PORT", 6379)),
14-
decode_responses=True
37+
decode_responses=True,
1538
)
16-
app = Flask(__name__)
39+
1740

1841
@app.route("/")
1942
def home():
2043
ACTIVE_REQUESTS.inc()
2144
start_time = time.time()
2245

2346
try:
24-
REQUEST_COUNT.labels(method="GET", endpoint="/", http_status=200).inc()
2547
count = redis_client.incr("visitor_count")
48+
49+
REQUEST_COUNT.labels(
50+
method="GET",
51+
endpoint="/",
52+
http_status="200"
53+
).inc()
54+
55+
return f"""
56+
<h1>AWS Production Flask App!</h1>
57+
<p>Visitor count: {count}</p>
58+
<p>Frontend App is running on port 5000</p>
59+
"""
60+
2661
except Exception as e:
27-
return f"Error occurred: {str(e)}"
62+
REQUEST_COUNT.labels(
63+
method="GET",
64+
endpoint="/",
65+
http_status="500"
66+
).inc()
67+
68+
return f"Error occurred: {str(e)}", 500
2869

2970
finally:
3071
latency = time.time() - start_time
31-
REQUEST_LATENCY.observe(latency)
72+
73+
REQUEST_LATENCY.labels(
74+
method="GET",
75+
endpoint="/"
76+
).observe(latency)
77+
3278
ACTIVE_REQUESTS.dec()
33-
return f"""
34-
<h1>AWS Production Flask App!</h1>
35-
<p>Visitor count: {count}</p>
36-
<p>Frontend App is running on port 5000</p>
37-
"""
79+
3880

3981
@app.route("/health")
4082
def health():
4183
try:
4284
redis_client.ping()
43-
return "Application Healthy"
85+
return "Application Healthy", 200
4486
except Exception as e:
45-
return f"Application Unhealthy: {str(e)}"
87+
return f"Application Unhealthy: {str(e)}", 500
88+
4689

4790
@app.route("/about")
4891
def about():
4992
return "Built by Harshu during Cloud Engineering Journey"
5093

51-
if __name__ == "__main__":
52-
app.run(host="0.0.0.0", port=5000)
5394

5495
@app.route("/metrics")
5596
def metrics():
56-
return Response(generate_latest(), 200, {"Content-Type": CONTENT_TYPE_LATEST})
97+
return Response(
98+
generate_latest(),
99+
mimetype=CONTENT_TYPE_LATEST,
100+
)
101+
102+
103+
if __name__ == "__main__":
104+
app.run(host="0.0.0.0", port=5000)

0 commit comments

Comments
 (0)