service.py 1.24 KB
Newer Older
Nane Kratzke's avatar
Nane Kratzke committed
1
from flask import Flask, redirect
2
3
from jaeger_client import Config
from flask_opentracing import FlaskTracing
Nane Kratzke's avatar
Nane Kratzke committed
4
import opentracing
5
import requests
Nane Kratzke's avatar
Nane Kratzke committed
6
import traceback 
Nane Kratzke's avatar
Nane Kratzke committed
7
import os
8

Nane Kratzke's avatar
Nane Kratzke committed
9
10
11
# A very boring and basic hip-hop-service to demonstrate 
# observalability and tracing.

12
13
14
15
service = os.environ.get('HIPHOP', 'hiphop')
tier = int(os.environ.get('TIER', '1'))
app = Flask(__name__)

Nane Kratzke's avatar
say hw    
Nane Kratzke committed
16
17
print("Hello World")

18
19
20
21
config = Config(config = {
    'sampler': { 
        'type': 'const', 
        'param': 1
Nane Kratzke's avatar
update    
Nane Kratzke committed
22
    }, 
23
24
25
26
27
28
29
30
31
32
33
34
35
    'logging': True,
    'reporter_batch_size': 1
}, service_name=service)

jaeger_tracer = config.initialize_tracer()
tracing = FlaskTracing(jaeger_tracer, True, app)

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

@app.route('/hip')
def hip():
Nane Kratzke's avatar
Nane Kratzke committed
36
    r = ""
37
    try:
Nane Kratzke's avatar
Nane Kratzke committed
38
39
40
41
42
        current_span = tracing.get_span(request)
        text_carrier = {}
        tracing.inject(span, opentracing.Format.TEXT_MAP, text_carrier)
        print(text_carrier)
        r = requests.get(f"http://{service}-{tier + 1}", headers=text_carrier, allow_redirects=True)
Nane Kratzke's avatar
Nane Kratzke committed
43
        print(r)
Nane Kratzke's avatar
update    
Nane Kratzke committed
44
        r = r.content.decode('utf-8')
45
    except Exception as ex:
Nane Kratzke's avatar
Nane Kratzke committed
46
47
        print(ex)
        traceback.print_exc() 
Nane Kratzke's avatar
Nane Kratzke committed
48
49
50
51
        r = " -> END"
    return f"hop-{ tier }{ r }"

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