-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathapp.py
65 lines (53 loc) · 2.28 KB
/
app.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
import os
from flask import Flask
from opentelemetry import trace, baggage, metrics
from opentelemetry.context import attach, detach
# use environment variables
# export HYPERDX_API_KEY=abc123
# export OTEL_SERVICE_NAME=otel-python-example
# export DEBUG=true
# HYPERDX_ENABLE_LOCAL_VISUALIZATIONS=true
# To enable metrics, set a metrics dataset
# export HYPERDX_METRICS_DATASET=otel-python-example-metrics
app = Flask(__name__)
# tracing
tracer = trace.get_tracer("hello_world_flask_tracer")
# metrics
meter = metrics.get_meter("hello_world_flask_meter")
bee_counter = meter.create_counter("bee_counter")
@app.route("/")
# Recommended: use attach and detach tokens for Context management with Baggage
def hello_world():
# adding baggage attributes (key, value)
token = attach(baggage.set_baggage("queen", "bee"))
with tracer.start_as_current_span(name="honey") as span:
# adding baggage attributes (key, value)
token_honey = attach(baggage.set_baggage("honey", "bee"))
# setting a span attribute directly (key, value)
span.set_attribute("message", "hello world!")
with tracer.start_as_current_span(name="child"):
# this goes nowhere if it is the final span in a trace
child_token = attach(
baggage.set_baggage("bee", "that_maybe_doesnt_propagate")
)
detach(child_token)
detach(token_honey)
detach(token)
# counter incremented by 1, attributes (route) associated with the increment
bee_counter.add(1, {'app.route': '/'})
return "Hello World"
@app.route("/ctx")
# For manually passing around the Context for baggage
def hello_ctx_world():
ctx = baggage.set_baggage("worker", "bees")
with tracer.start_as_current_span(name="bumble", context=ctx):
ctx = baggage.set_baggage("bumble", "bees", ctx)
# say you have more business logic before adding additional baggage
ctx = baggage.set_baggage("additional", "bees", ctx)
with tracer.start_as_current_span(name="last", context=ctx):
# this goes nowhere if it is the final span in a trace
ctx = baggage.set_baggage("last", "bee", ctx)
bee_counter.add(1, {'app.route': '/ctx'})
return "Hello Context World"
if __name__ == "__main__":
app.run(host="0.0.0.0", port=5000)