service.py 1018 Bytes
Newer Older
Nane Kratzke's avatar
Nane Kratzke committed
1
from flask import Flask, redirect, request
Nane Kratzke's avatar
Nane Kratzke committed
2
import requests, os, logging, json
Nane Kratzke's avatar
Nane Kratzke committed
3
import elasticapm
Nane Kratzke's avatar
Nane Kratzke committed
4
from elasticapm.contrib.flask import ElasticAPM
5

Nane Kratzke's avatar
Nane Kratzke committed
6
# A very boring and basic hip-hop-service to demonstrate 
Nane Kratzke's avatar
Nane Kratzke committed
7
# observability via tracing across multiple services.
Nane Kratzke's avatar
Nane Kratzke committed
8

Nane Kratzke's avatar
Nane Kratzke committed
9
10
logging.basicConfig(level=logging.INFO)

Nane Kratzke's avatar
Nane Kratzke committed
11
service = os.environ.get('HIPHOP', 'hiphop')
12
13
tier = int(os.environ.get('TIER', '1'))
app = Flask(__name__)
Nane Kratzke's avatar
Nane Kratzke committed
14
15
16
apm = ElasticAPM(app, 
   service_name=os.environ.get('SERVICE', 'space'),
)
17
18
19
20
21
22
23

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

@app.route('/hip')
def hip():
Nane Kratzke's avatar
Nane Kratzke committed
24
    logging.info(f"Request from { request.remote_addr } with headers { json.dumps(dict(request.headers)) }")
Nane Kratzke's avatar
Nane Kratzke committed
25
    r = ""
Nane Kratzke's avatar
Nane Kratzke committed
26
    url = f"http://{service}-{tier + 1}/hip"
27
    try:
Nane Kratzke's avatar
Nane Kratzke committed
28
        r = requests.get(url).content
29
    except Exception as ex:
Nane Kratzke's avatar
Nane Kratzke committed
30
        logging.error(f"Fetching { url } failed ({ ex })") 
Nane Kratzke's avatar
Nane Kratzke committed
31
        r = "END"
Nane Kratzke's avatar
Nane Kratzke committed
32
33
    
    hip = "hip " if tier == 1 else ""
Nane Kratzke's avatar
Nane Kratzke committed
34

Nane Kratzke's avatar
Nane Kratzke committed
35
    return f"{hip} hop-{ tier } -> { r }".strip()
Nane Kratzke's avatar
Nane Kratzke committed
36
37

app.run(host="0.0.0.0", port=80)