-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.py
54 lines (37 loc) · 1.28 KB
/
server.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
#!/usr/bin/env python3
from flask import Flask
from opentelemetry import trace, baggage
from time import sleep
from opentelemetry.launcher import configure_opentelemetry
PORT = 8000
app = Flask(__name__)
tracer = trace.get_tracer(__name__)
configure_opentelemetry(
service_version="4.5.6",
log_level="DEBUG", # optional
propagators="baggage,tracecontext",
)
@app.route("/hello")
def hello():
# get the current span, created by flask instrumentation
current_span = trace.get_current_span()
# add more attributes to the server span
current_span.set_attribute("http.route", "some_route")
# pretend to do work and record the latency
sleep(20 / 1000)
with tracer.start_as_current_span("server_span") as span:
# add a baggage value
span.set_attribute("projectID", baggage.get_baggage("projectID"))
# add an event
span.add_event("event message", {"event_attributes": 1})
# pretend to do work and record the latency
sleep(30 / 1000)
# record an exception
try:
1 / 0
except ZeroDivisionError as error:
span.record_exception(error)
print("caught zero division error")
return "hello"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=PORT)