Skip to content

Instantly share code, notes, and snippets.

@NegatioN
Created July 14, 2016 00:13
Show Gist options
  • Save NegatioN/ae41290bb38a0aed1d8e8864620a3f99 to your computer and use it in GitHub Desktop.
Save NegatioN/ae41290bb38a0aed1d8e8864620a3f99 to your computer and use it in GitHub Desktop.
public final class BraveRxJavaSchedulersHook extends RxJavaSchedulersHook {
@Override
public Action0 onSchedule(Action0 action) {
ServerSpanThreadBinder binder = ZipkinHolder.getInstance().serverSpanThreadBinder();
ServerSpan span = binder.getCurrentServerSpan();
return () -> {
binder.setCurrentSpan(span);
action.call();
};
}
}
public final class ZipkinHolder {
static {
System.setProperty(
"rxjava.plugin.RxJavaSchedulersHook.implementation",
BraveRxJavaSchedulersHook.class.getCanonicalName());
}
private static volatile FINNHystrixHooks finnHystrixHooks = new NoopFINNHystrixHooks();
public static synchronized void registerFINNHystrixHooks(FINNHystrixHooks hooks){
finnHystrixHooks = hooks;
}
private static final Log LOG = LogFactory.getLog(ZipkinHolder.class);
private static volatile Brave BRAVE;
public static Brave getInstance() {
if (BRAVE == null) {
BRAVE = new Brave.Builder(getServiceName())
.spanCollector(FailSilentlyCollector.wrap(ZipkinHolder::createSpanCollector))
.traceSampler(createSampler())
.build();
setupRx();
}
return BRAVE;
}
private static void setupRx() {
try {
LOG.info("Setting up Hystrix and RxJava hools...");
final RxJavaSchedulersHook hook = RxJavaPlugins.getInstance().getSchedulersHook();
LOG.info("RxJavaSchedulersHook = " + hook.getClass().getCanonicalName());
final HystrixConcurrencyStrategy concurrencyStrategy = HystrixPlugins.getInstance().getConcurrencyStrategy();
LOG.info("HystrixConcurrencyStrategy = " + concurrencyStrategy.getClass().getCanonicalName());
} catch(IllegalStateException ex) {
LOG.warn("Couldn't register Hystrix/RX scheduler hooks.", ex);
}
}
}
@codefromthecrypt
Copy link

cool stuff!

onSchedule should clear current span in a finally, right?

@eirslett
Copy link

onSchedule should clear current span in a finally, right?

Probably. This code doen't clean up after itself, it assumes that the next span that is started overwrites the state. (effectively cleaning up after the previous span)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment