Created
November 24, 2020 17:50
-
-
Save tedsuo/7614f861a61f2de4682854346f4f6f56 to your computer and use it in GitHub Desktop.
Python span context management
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from opentelemetry import trace | |
# create a tracer and name it after your package | |
tracer = trace.get_tracer(__name__) | |
@app.route("/hello") | |
def hello(): | |
# add latency to the parent span | |
sleep(20 / 1000) | |
# Manage the span lifecycle correctly by | |
# creating a new context for the spab a span | |
with tracer.start_as_current_span("server_span") as span: | |
# get_current_span return the correct span | |
trace.propagation.get_current_span().add_attributes({"label": "value"}) | |
# add latency to the child span | |
sleep(30 / 1000) | |
return "hello" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment