service.py 938 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
Nane Kratzke's avatar
Nane Kratzke committed
3
from elasticapm.contrib.flask import ElasticAPM
4

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

Nane Kratzke's avatar
Nane Kratzke committed
8
service = os.environ.get('HIPHOP', 'hiphop')
9
10
tier = int(os.environ.get('TIER', '1'))
app = Flask(__name__)
Nane Kratzke's avatar
Nane Kratzke committed
11
apm = ElasticAPM(app, service_name=service)
12
13
14
15
16
17
18

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

@app.route('/hip')
def hip():
Nane Kratzke's avatar
Nane Kratzke committed
19
    logging.info(f"Request from: { request.environ.get('HTTP_X_REAL_IP', request.remote_addr) }")
Nane Kratzke's avatar
Nane Kratzke committed
20
    r = ""
Nane Kratzke's avatar
Nane Kratzke committed
21
    url = f"http://{service}-{tier + 1}/hip"
22
    try:
Nane Kratzke's avatar
Nane Kratzke committed
23
        r = requests.get(url)
Nane Kratzke's avatar
update    
Nane Kratzke committed
24
        r = r.content.decode('utf-8')
25
    except Exception as ex:
Nane Kratzke's avatar
Nane Kratzke committed
26
        logging.error(f"Fetching { url } failed ({ ex })") 
Nane Kratzke's avatar
Nane Kratzke committed
27
        r = "END"
Nane Kratzke's avatar
Nane Kratzke committed
28
29
30
    
    hip = "hip " if tier == 1 else ""
    return f"{hip} hop-{ tier } -> { r }".strip()
Nane Kratzke's avatar
Nane Kratzke committed
31
32

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