Skip to content

Instantly share code, notes, and snippets.

@carlosalberto
Last active April 3, 2019 15:58
Show Gist options
  • Save carlosalberto/fb8f69681a56acf83d02bb2695ee4891 to your computer and use it in GitHub Desktop.
Save carlosalberto/fb8f69681a56acf83d02bb2695ee4891 to your computer and use it in GitHub Desktop.
// FOLLOWS_FROM use scenario.
public class Promise<T> {
public Promise() {
// 1. Capture the active Span.
this.parentSpan = tracer.activeSpan();
}
// 2. Add callacks that will be invoked asynchronously
// upon success.
public void onSuccess(Callback<T> callback) {
successCallbacks.add(callback);
}
public void success(final T result) {
// 3. Upon succcess, invoke the callbacks asynchronously
// through a service executor. Each one will do an independent,
// post-processing task that is *not* core part of the parent
// Span/task.
for (Callback<T> callback: successCallbacks) {
executorService.submit(new Runnable {
@Override
public void run() {
Span span = tracer.buildSpan("postProcessingTask")
.addReference(References.FOLLOWS_FROM, parentSpan.context())
.start();
try (Scope scope = tracer.activateSpan(span)) {
// 4. Do a post-processing task and create a child Span.
} finally {
span.finish();
}
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment