Skip to content

Instantly share code, notes, and snippets.

@palazzem
Last active June 21, 2017 10:01
Show Gist options
  • Save palazzem/270648ac5e15dc2b71de54f8e941b4ba to your computer and use it in GitHub Desktop.
Save palazzem/270648ac5e15dc2b71de54f8e941b4ba to your computer and use it in GitHub Desktop.
ot_example_with_block.py
# Some sort of top-level handler
def handle_purchase_async(done_callback):
with tracer.start_active_span("purchase") as span:
# add a marker so that it can be resumed later
span.capture()
# record_transaction() would return right away
record_transaction(_finish_recording, done_callback)
# Note: the Span is NOT finished here because the capture()
# has incremented a refcounter, so exiting from the context
# manager only reactivates the previous Span
do_something_else()
def _finish_recording(status, span, done_callback):
# the span reference must be passed (or get from a closure) like
# it is in Java for the Continuation class; the Span must be reactivated
tracer.active_span_source.make_active(span)
with span:
if status.is_error():
span.set_tag("error", True)
# Note: the `span` has been captured once and deactivated once in
# this context manager, so the finish() is called automatically
# Invoke the handle_purchase_async callback
done_callback()
def done_callback():
# Note: because here we don't use capture(), the Span is finished
# immediately after exiting the context manager
with tracer.start_active_span("db.write_result") as span:
db_call()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment