Skip to content

Instantly share code, notes, and snippets.

@tedsuo
Created November 24, 2020 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tedsuo/7614f861a61f2de4682854346f4f6f56 to your computer and use it in GitHub Desktop.
Save tedsuo/7614f861a61f2de4682854346f4f6f56 to your computer and use it in GitHub Desktop.
Python span context management
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