r2dbc-proxy
r2dbc-proxy copied to clipboard
I can not get value from context to write log.
How to get value from context.
Can you expand your question? If you want to use MDC, you need to set the values to MDC in order to reference them.
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
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?
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.
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