r2dbc-proxy icon indicating copy to clipboard operation
r2dbc-proxy copied to clipboard

I can not get value from context to write log.

Open nguyenquangtrung opened this issue 2 years ago • 3 comments

How to get value from context. [%date{yyyy/MM/ddHH:mm:ss.SSS}]\t%-5level\t%X{MARKER}\t%X{X_TRACK_ID}\t%X{USER_EMAIL}\t%message%n

nguyenquangtrung avatar May 13 '22 10:05 nguyenquangtrung

Can you expand your question? If you want to use MDC, you need to set the values to MDC in order to reference them.

ttddyy avatar May 14 '22 05:05 ttddyy

You need to implement CoreSubscriber yourself, refer to the implementation in ReactorSleuth in SpringCloudSleuth. Be careful to use Threadlocal or MDC in the implementation class of CoreSubscriber

liuzzzz avatar May 17 '22 15:05 liuzzzz

You need to implement CoreSubscriber yourself, refer to the implementation in ReactorSleuth in SpringCloudSleuth. Be careful to use Threadlocal or MDC in the implementation class of CoreSubscriber

Liuzzz, Is the SpringCloudSleuth can help to thread safety forwarding MDC context to ProxyConnectionFactory.builder().onBeforeQuery() or maybe there is another way to do logging database request/response with MDC context?

DmitriyKolusenko avatar Jun 24 '22 13:06 DmitriyKolusenko

You need to implement CoreSubscriber yourself, refer to the implementation in ReactorSleuth in SpringCloudSleuth. Be careful to use Threadlocal or MDC in the implementation class of CoreSubscriber

Liuzzz, Is the SpringCloudSleuth can help to thread safety forwarding MDC context to ProxyConnectionFactory.builder().onBeforeQuery() or maybe there is another way to do logging database request/response with MDC context?

For Spring's ReactorSleuth component, TraceId, SpanId and other information must be stored in the MDC or thread context, so that we can see the content of [xxx,xxx] in the Console. I mean you need to refer to the implementation of ReactorSleuth, and then implement it according to your own demands your own.

liuzzzz avatar Oct 28 '22 08:10 liuzzzz

You need to implement CoreSubscriber yourself, refer to the implementation in ReactorSleuth in SpringCloudSleuth. Be careful to use Threadlocal or MDC in the implementation class of CoreSubscriber

Liuzzz, Is the SpringCloudSleuth can help to thread safety forwarding MDC context to ProxyConnectionFactory.builder().onBeforeQuery() or maybe there is another way to do logging database request/response with MDC context?

For Spring's ReactorSleuth component, TraceId, SpanId and other information must be stored in the MDC or thread context, so that we can see the content of [xxx,xxx] in the Console. I mean you need to refer to the implementation of ReactorSleuth, and then implement it according to your own demands your own.

Like this,

public class LocalAttributeSubscriber implements CoreSubscriber<Object>, Subscription, Scannable, Fuseable.QueueSubscription<Object> {

    private final CoreSubscriber<Object> actual;
    private Subscription s;

    public LocalAttributeSubscriber(CoreSubscriber actual) {
        this.actual = actual;
    }

    @Override
    public Context currentContext() {
        return this.actual.currentContext();
    }

    @Override
    public void request(long n) {
        try (LocalAttribute attr = LocalAttribute.maybe(currentContext())) {
            this.s.request(n);
        }
    }

    @Override
    public void cancel() {
        try (LocalAttribute attr = LocalAttribute.maybe(currentContext())) {
            this.s.cancel();
        }
    }

    @Override
    public void onSubscribe(Subscription s) {
        this.s = s;
        try (LocalAttribute attr = LocalAttribute.maybe(currentContext())) {
            this.actual.onSubscribe(this);
        }
    }

    @Override
    public void onNext(Object o) {
        try (LocalAttribute attr = LocalAttribute.maybe(currentContext())) {
            this.actual.onNext(o);
        }
    }

    @Override
    public void onError(Throwable t) {
        try (LocalAttribute attr = LocalAttribute.maybe(currentContext())) {
            this.actual.onError(t);
        }
    }

    @Override
    public void onComplete() {
        try (LocalAttribute attr = LocalAttribute.maybe(currentContext())) {
            this.actual.onComplete();
        }
    }

    @Override
    public int requestFusion(int requestedMode) {
        return Fuseable.NONE;
    }

    @Override
    public Object poll() {
        return null;
    }

    @Override
    public int size() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return true;
    }

    @Override
    public void clear() {

    }

    @Override
    public Object scanUnsafe(Attr key) {
        if (key == Attr.PARENT) {
            return this.s;
        }
        else if (key == Attr.RUN_STYLE) {
            return Attr.RunStyle.SYNC;
        }
        else {
            return key == Attr.ACTUAL ? this.actual : null;
        }
    }
}
public abstract class AbstractLocalAttribute implements Closeable, ILocalAttribute {
    public static final AbstractLocalAttribute EMPTY = new AbstractLocalAttribute() {
        @Override
        public <K, V> void setAttribute(K key, V val) {

        }

        @Override
        public <K, V> V getAttribute(K key) {
            return null;
        }

        @Override
        public <K, V> V getAttribute(K key, V defaultValue) {
            return defaultValue;
        }
    };
    private static final ThreadLocal<? super AbstractLocalAttribute> LOCAL = new ThreadLocal<>();
}

The "LocalAttribute" contains field ThreadLocal

liuzzzz avatar Oct 28 '22 08:10 liuzzzz