service.py 1.36 KB
Newer Older
Nane Kratzke's avatar
Nane Kratzke committed
1
from flask import Flask, redirect, request
2
from jaeger_client import Config
Nane Kratzke's avatar
Nane Kratzke committed
3
from flask_opentracing import FlaskTracer #, 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
# A very boring and basic hip-hop-service to demonstrate 
Nane Kratzke's avatar
Nane Kratzke committed
10
# observability via tracing.
Nane Kratzke's avatar
Nane Kratzke committed
11

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

config = Config(config = {
    'sampler': { 
        'type': 'const', 
        'param': 1
Nane Kratzke's avatar
update    
Nane Kratzke committed
20
    }, 
21
22
23
24
    'logging': True,
    'reporter_batch_size': 1
}, service_name=service)

Nane Kratzke's avatar
Nane Kratzke committed
25
26
27
#tracer = config.initialize_tracer()
tracer = FlaskTracer(config.initialize_tracer(), True, app)
#tracing = FlaskTracing(tracer, True, app)
28
29
30
31
32
33
34

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

@app.route('/hip')
def hip():
Nane Kratzke's avatar
Nane Kratzke committed
35
    print(request.headers)
Nane Kratzke's avatar
Nane Kratzke committed
36
    r = ""
37
    try:
Nane Kratzke's avatar
Nane Kratzke committed
38
        parent_span = tracer.get_span(request)
Nane Kratzke's avatar
Nane Kratzke committed
39
40
41
42
43
        with opentracing.tracer.start_span(f"{service}-{tier + 1}", child_of=parent_span) as span:
            url = f"http://{service}-{tier + 1}"
            span.set_tag("http.url", url)
            r = requests.get(url)
            span.set_tag("http.status_code", r.status_code)
Nane Kratzke's avatar
Nane Kratzke committed
44
        print(r)
Nane Kratzke's avatar
update    
Nane Kratzke committed
45
        r = r.content.decode('utf-8')
46
    except Exception as ex:
Nane Kratzke's avatar
Nane Kratzke committed
47
48
        print(ex)
        traceback.print_exc() 
Nane Kratzke's avatar
Nane Kratzke committed
49
50
        r = "END"
    return f"hop-{ tier } -> { r }"
Nane Kratzke's avatar
Nane Kratzke committed
51
52

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