-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathapp.py
More file actions
130 lines (110 loc) · 4.5 KB
/
Copy pathapp.py
File metadata and controls
130 lines (110 loc) · 4.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import otel
import logging
import requests
###################
# Instrumentation #
###################
from opentelemetry.instrumentation.flask import FlaskInstrumentor
from opentelemetry.instrumentation.jinja2 import Jinja2Instrumentor
from opentelemetry.instrumentation.requests import RequestsInstrumentor
from opentelemetry.instrumentation.pymongo import PymongoInstrumentor
from opentelemetry.instrumentation.system_metrics import SystemMetricsInstrumentor
# from opentelemetry.instrumentation.wsgi import OpenTelemetryMiddleware
from flask import Flask, render_template, jsonify
app = Flask(__name__, static_url_path='/', static_folder='application/static', template_folder='application/templates')
# app.wsgi_app = OpenTelemetryMiddleware(app.wsgi_app)
FlaskInstrumentor().instrument_app(app)
Jinja2Instrumentor().instrument()
RequestsInstrumentor().instrument()
PymongoInstrumentor().instrument()
SystemMetricsInstrumentor().instrument()
###############
# Application #
###############
@app.route("/")
def index():
otel.counter.add(1, attributes={"route": "/"})
return render_template("index.html", title="Flask Web Application")
@app.route("/ping", strict_slashes=False)
def ping():
logging.info("Ping")
otel.counter.add(1, attributes={"route": "/ping"})
return jsonify(ping="pong")
@app.route("/about")
def about():
logging.info("About")
otel.counter.add(1, attributes={"route": "/about"})
return render_template("about.html", title="Datacrunch - About")
@app.route("/statuspage", strict_slashes=False)
def statuspage():
logging.info("Statuspage")
otel.counter.add(1, attributes={"route": "/statuspage"})
return render_template("projects/statuspage.html", title="Simple Statuspage")
# API to convert Fahrenheit to Celcius, without error handling
@app.route("/convertC/<tempF>")
def convertC(tempF):
tempC = (5/9*(float(tempF))-32)
logging.info(f"[INFO] Converted {tempF}°F to {tempC:.2f}°C.")
otel.counter.add(1, attributes={"route": "/convertC"})
return f"{tempF}°F is {tempC:.2f}°C."
# API to convert Celcius to Fahrenheit, with error handling
@app.route("/convertF/<tempC>")
def convertF(tempC):
try:
tempF = 9/5*(float(tempC))+32
logging.info(f"[INFO] Converted {tempC}°F to {tempF:.2f}°C.")
otel.counter.add(1, attributes={"route": "/convertF"})
return f"{tempC}°C is {tempF:.2f}°F."
except ValueError:
logging.warning("[WARN] Invalid temperature!")
otel.counter.add(1, attributes={"route": "/convertF"})
return "Invalid temperature value", 400
except Exception as e:
logging.exception("[ERROR] Unexpected error in convertF")
otel.counter.add(1, attributes={"route": "/convertF"})
return f"Error: {e}", 500
@app.route("/extfib/<int:n>")
def extfib(n):
try:
# Read endpoint from environment variable
endpoint = os.environ.get("LAMBDA_ENDPOINT")
if not endpoint:
logging.error("[ERROR] LAMBDA_ENDPOINT environment variable not set.")
return "Endpoint not configured", 500
# Call external Lambda endpoint with ?n=<n>
response = requests.get(f"{endpoint}", params={"n": n})
response.raise_for_status()
data = response.json()
result = data.get("result")
logging.info(f"[INFO] the {n}th Fibonacci number is {result}.")
otel.counter.add(1, attributes={"route": "/extfib"})
return str(result)
except Exception as e:
logging.warning(f"[WARN] Failed to fetch Fibonacci number: {e}")
otel.counter.add(1, attributes={"route": "/extfib"})
return "Error fetching result", 500
##############
# Blueprints #
##############
# API to calculate the nth prime number and how long it takes
from application.projects.prime import prime
app.register_blueprint(prime)
# API to calculate the nth fibonacci number
from application.projects.fibonacci import fibonacci
app.register_blueprint(fibonacci)
# API to validate credit card numbers
from application.projects.luhn import luhn
app.register_blueprint(luhn)
# Get COVID data and plot on chart
from application.projects.covid import covid
app.register_blueprint(covid)
# MongoDB Atlas sample weather data route
from application.projects.mongodb import mongodb as mongodb_blueprint
app.register_blueprint(mongodb_blueprint)
# Input number to check divisibility
from application.projects.divisibility import divisibility
app.register_blueprint(divisibility)
# Run Flask Web Application, new comment
if __name__ == "__main__":
app.run()