-
Notifications
You must be signed in to change notification settings - Fork 3
/
jaeger.py
77 lines (67 loc) · 2.62 KB
/
jaeger.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import random
import logging
from fastapi import FastAPI
from jaeger_client import Config
from opentracing_instrumentation.request_context import get_current_span, span_in_context
app = FastAPI()
def init_tracer(service):
logging.getLogger('').handlers = []
logging.basicConfig(format='%(message)s', level=logging.DEBUG)
config = Config(
config={
'sampler': {
'type': 'const',
'param': 1,
},
'logging': True,
},
service_name=service
)
return config.initialize_tracer()
tracer = init_tracer('booking_movie')
def booking_mgr(movie):
with tracer.start_span('booking') as span:
span.set_tag('Movie', movie)
with span_in_context(span):
cinema_details = check_cinema(movie)
showtime_details = check_showtime(cinema_details)
book_show(showtime_details)
def check_cinema(movie):
with tracer.start_span('CheckCinema', child_of=get_current_span()) as span:
with span_in_context(span):
cinema_details = f"{movie} Details"
flags = ['false', 'true', 'false']
random_flag = random.choice(flags)
span.set_tag('error', random_flag)
span.log_kv({'event': 'CheckCinema', 'value': cinema_details})
return cinema_details
def check_showtime(cinema_details):
with tracer.start_span('CheckShowtime', child_of=get_current_span()) as span:
with span_in_context(span):
showtime_details = f"Showtime {cinema_details} Details"
flags = ['false', 'true', 'false']
random_flag = random.choice(flags)
span.set_tag('error', random_flag)
span.log_kv({'event': 'CheckShowtime', 'value': showtime_details})
return showtime_details
def book_show(showtime_details):
with tracer.start_span('BookShow', child_of=get_current_span()) as span:
with span_in_context(span):
ticket_details = "Ticket Details"
flags = ['false', 'true', 'false']
random_flag = random.choice(flags)
span.set_tag('error', random_flag)
span.log_kv({'event': 'BookShow', 'value': ticket_details})
print(showtime_details)
print(ticket_details)
@app.get("/book_movie/{movie}")
async def book_movie(movie: str):
booking_mgr(movie)
return {"message": f"Booking for movie {movie} completed successfully."}
# Ensure to close the tracer when done
@app.on_event("shutdown")
def shutdown_tracer():
tracer.close()
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000)