service.py 1.05 KB
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
apm = ElasticAPM(app, 
Nane Kratzke's avatar
Nane Kratzke committed
15
   service_name=f"{ service }-{ tier }",
Nane Kratzke's avatar
Nane Kratzke committed
16
)
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
29
        #with elasticapm.capture_span(f"{ service }-{ tier + 1 }"):
        r = requests.get(url).content
30
    except Exception as ex:
Nane Kratzke's avatar
Nane Kratzke committed
31
        logging.error(f"Fetching { url } failed ({ ex })") 
Nane Kratzke's avatar
Nane Kratzke committed
32
        r = "END"
Nane Kratzke's avatar
Nane Kratzke committed
33
34
    
    hip = "hip " if tier == 1 else ""
Nane Kratzke's avatar
Nane Kratzke committed
35

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

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