service.py 1.68 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
Nane Kratzke's avatar
Nane Kratzke committed
5
from opentracing.propagation import Format
6
import requests
Nane Kratzke's avatar
Nane Kratzke committed
7
import traceback 
Nane Kratzke's avatar
Nane Kratzke committed
8
import os
9

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

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

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

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

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

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

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