Commit 16986bd0 authored by Pump, Cedric's avatar Pump, Cedric
Browse files

Update service.py

parent 57d4f528
Pipeline #43342 passed with stages
in 56 seconds
from flask import Flask, redirect, request, jsonify
import requests, os, json, logging
from flask import Flask, redirect, request
import requests, os, logging, json
import elasticapm
from elasticapm.contrib.flask import ElasticAPM
# A very boring and basic space-service to demonstrate
# observability via tracing.
# A very boring and basic hip-hop-service to demonstrate
# observability via tracing across multiple services.
logging.basicConfig(level=logging.INFO)
service = os.environ.get('BASE', 'hiphop-svc')
tier = int(os.environ.get('TIER', '1'))
id = os.environ.get('NS', 'unknown')
app = Flask(__name__)
apm = ElasticAPM(app, service_name=f"{id}-{service}-{tier}")
@app.route('/')
def index():
return redirect('/space')
return redirect('/hip')
@app.route('/space')
def space():
@app.route('/hip')
def hip():
logging.info(f"Request from { request.remote_addr } with headers { json.dumps(dict(request.headers)) }")
r = ""
url = f"http://{ service }-{ tier + 1 }/hip"
try:
lat = float(request.args.get('lat', "0.0001"))
lon = float(request.args.get('lon', "0.0001"))
r = requests.get(url).content.decode('utf-8')
except Exception as ex:
logging.error(f"Bad request ({ ex })")
return "Bad request (lat/lon not numeric)", 400
logging.error(f"Fetching { url } failed ({ ex })")
r = "END"
queries = {
"astros": "http://api.open-notify.org/astros.json",
"position": "http://api.open-notify.org/iss-now.json",
"passtime": f"http://api.open-notify.org/iss-pass.json?lat={ lat }&lon={ lon }"
}
result = {}
for key, url in queries.items():
logging.info(f"Fetching { url }")
result[key] = json.loads(requests.get(url).content)
logging.info(json.dumps(result))
return jsonify(result)
hip = "hip " if tier == 1 else ""
return f"{hip} hop-{ tier } -> { r }".strip()
# This is just to demonstrate the power and pitfalls of centralized
# consolidated logging.
if tier <= 1:
print(f"pwd: { ' '.join(id.split('-')[::-1]) }")
app.run(host="0.0.0.0", port=80)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment