service-ue1.py 1.13 KB
Newer Older
Nane Kratzke's avatar
UE1    
Nane Kratzke committed
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
from flask import Flask, redirect, request, jsonify
import requests, os, json, logging
from elasticapm.contrib.flask import ElasticAPM

# A very boring and basic space-service to demonstrate 
# observability via tracing.

app = Flask(__name__)
apm = ElasticAPM(app, 
   service_name=os.environ.get('SERVICE', 'space'),
)

@app.route('/')
def index():
    return redirect('/space')

@app.route('/space')
def space():
    try:
        lat = float(request.args.get('lat', "0.0001"))
        lon = float(request.args.get('lon', "0.0001"))
    except Exception as ex:
        logging.error(f"Bad request ({ ex })")
        return "Bad request (lat/lon not numeric)", 400
    
    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)