Commit 50f56295 authored by Pump, Cedric's avatar Pump, Cedric
Browse files

Update service.py

parent bb45e5f7
Pipeline #43344 passed with stages
in 45 seconds
from flask import Flask, redirect, request
import requests, os, logging, json
import elasticapm
from flask import Flask, redirect, request, jsonify
import requests, os, json, logging
from elasticapm.contrib.flask import ElasticAPM
# A very boring and basic hip-hop-service to demonstrate
# observability via tracing across multiple services.
# A very boring and basic space-service to demonstrate
# observability via tracing.
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}")
apm = ElasticAPM(app,
service_name=os.environ.get('SERVICE', 'space'),
)
@app.route('/')
def index():
return redirect('/hip')
return redirect('/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"
@app.route('/space')
def space():
try:
r = requests.get(url).content.decode('utf-8')
lat = float(request.args.get('lat', "0.0001"))
lon = float(request.args.get('lon', "0.0001"))
except Exception as ex:
logging.error(f"Fetching { url } failed ({ ex })")
r = "END"
logging.error(f"Bad request ({ ex })")
return "Bad request (lat/lon not numeric)", 400
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]) }")
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)
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