opentracing-contrib / java-reactor

OpenTracing instrumentation for Reactor
Apache License 2.0
12 stars 7 forks source link

No active span within Mono.create #4

Closed Danny02 closed 4 years ago

Danny02 commented 5 years ago

Hi,

I'm using a library which tries to log to the active span from within a Mono.create block. The TracerSubscriber does not seem to set the active span for this case.

        Span foo = tracer.buildSpan("foo").start();

        Mono.create(sink -> {
            tracer.activeSpan().log("hoho");
            sink.success("asdas");
        })
            .subscriberContext(c -> c.put(Span.class, foo))
            .doOnTerminate(() -> foo.finish())
            .block();
Danny02 commented 5 years ago

It is the same thing with Mono.fromRunnable. The problem is easy to spot:

    @Override
    public void subscribe(CoreSubscriber<? super T> actual) {
        MonoRunnableEagerSubscription s = new MonoRunnableEagerSubscription();
        actual.onSubscribe(s);
        if (s.isCancelled()) {
            return;
        }

        try {
            run.run();
            actual.onComplete();
        } catch (Throwable ex) {
            actual.onError(Operators.onOperatorError(ex, actual.currentContext()));
        }
    }

actual.onSubscribe(s); opens and closes the scope of the active span, but run.run() is called after it. I guess it is the same problem with Mono.create

jam01 commented 5 years ago

This is probably related to https://github.com/opentracing-contrib/java-reactor/issues/3 where it's an issue with the executors. Did you register the hooks manually?