-
-
Save palazzem/270648ac5e15dc2b71de54f8e941b4ba to your computer and use it in GitHub Desktop.
ot_example_with_block.py
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
# 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