Merge pull request #10 from rbkmoney/mdc

update MdcContext, add ServerHandlerMdcDecorator
This commit is contained in:
malkoas 2019-10-30 15:30:13 +03:00 committed by GitHub
commit 53584eeeb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 8 deletions

View File

@ -8,7 +8,7 @@
<groupId>com.rbkmoney</groupId>
<artifactId>adapter-common-lib</artifactId>
<version>0.0.8</version>
<version>0.0.9</version>
<description></description>

View File

@ -0,0 +1,56 @@
package com.rbkmoney.adapter.common.handler;
import com.rbkmoney.damsel.proxy_provider.*;
import lombok.RequiredArgsConstructor;
import org.apache.thrift.TException;
import org.slf4j.MDC;
import java.nio.ByteBuffer;
import static com.rbkmoney.adapter.common.logback.mdc.MdcContext.mdcPutContext;
@RequiredArgsConstructor
public class ServerHandlerMdcDecorator implements ProviderProxySrv.Iface {
private final ProviderProxySrv.Iface serverHandlerLogDecorator;
public RecurrentTokenProxyResult generateToken(RecurrentTokenContext recurrentTokenContext) throws TException {
mdcPutContext(recurrentTokenContext);
try {
return serverHandlerLogDecorator.generateToken(recurrentTokenContext);
} finally {
MDC.clear();
}
}
@Override
public RecurrentTokenCallbackResult handleRecurrentTokenCallback(ByteBuffer byteBuffer, RecurrentTokenContext recurrentTokenContext) throws TException {
mdcPutContext(recurrentTokenContext);
try {
return serverHandlerLogDecorator.handleRecurrentTokenCallback(byteBuffer, recurrentTokenContext);
} finally {
MDC.clear();
}
}
@Override
public PaymentProxyResult processPayment(PaymentContext paymentContext) throws TException {
mdcPutContext(paymentContext);
try {
return serverHandlerLogDecorator.processPayment(paymentContext);
} finally {
MDC.clear();
}
}
@Override
public PaymentCallbackResult handlePaymentCallback(ByteBuffer byteBuffer, PaymentContext paymentContext) throws TException {
mdcPutContext(paymentContext);
try {
return serverHandlerLogDecorator.handlePaymentCallback(byteBuffer, paymentContext);
} finally {
MDC.clear();
}
}
}

View File

@ -1,27 +1,22 @@
package com.rbkmoney.adapter.common.logback.mdc;
import com.rbkmoney.adapter.common.utils.converter.PaymentDataConverter;
import com.rbkmoney.damsel.domain.TransactionInfo;
import com.rbkmoney.damsel.proxy_provider.PaymentContext;
import com.rbkmoney.damsel.proxy_provider.RecurrentTokenContext;
import org.slf4j.MDC;
import java.util.Map;
import java.util.regex.Pattern;
public class MdcContext {
private static final String AMOUNT_KEY = "amount";
public static void mdcPutContext(RecurrentTokenContext context, String[] fieldsToPutInMdc) {
TransactionInfo transactionInfo = context.getTokenInfo().getTrx();
String amount = PaymentDataConverter.getFormattedAmount(context.getTokenInfo().getPaymentTool().getMinimalPaymentCost().getAmount()).toString();
MDC.put(AMOUNT_KEY, amount);
mdcPutContextTransactionInfo(transactionInfo, fieldsToPutInMdc);
}
public static void mdcPutContext(PaymentContext context, String[] fieldsToPutInMdc) {
TransactionInfo transactionInfo = context.getPaymentInfo().getPayment().getTrx();
String amount = PaymentDataConverter.getFormattedAmount(context.getPaymentInfo().getPayment().getCost().getAmount()).toString();
MDC.put(AMOUNT_KEY, amount);
mdcPutContextTransactionInfo(transactionInfo, fieldsToPutInMdc);
}
@ -34,8 +29,30 @@ public class MdcContext {
}
}
public static void mdcPutContext(PaymentContext context) {
TransactionInfo transactionInfo = context.getPaymentInfo().getPayment().getTrx();
mdcPutContextTransactionInfo(transactionInfo);
}
public static void mdcPutContext(RecurrentTokenContext context) {
TransactionInfo transactionInfo = context.getTokenInfo().getTrx();
mdcPutContextTransactionInfo(transactionInfo);
}
public static void mdcPutContextTransactionInfo(TransactionInfo transactionInfo) {
if (transactionInfo != null) {
Map<String, String> trxextra = transactionInfo.getExtra();
String maskPattern = "\\b\\d{6}([\\d\\s]{2,9})\\d{4}\\b";
Pattern pattern = Pattern.compile(maskPattern);
for (Map.Entry<String, String> extra : trxextra.entrySet()) {
if (pattern.matcher(extra.getValue()).find()) {
MDC.put(extra.getKey(), extra.getValue());
}
}
}
}
public static void mdcRemoveContext(String[] fieldsToPutInMdc) {
MDC.remove(AMOUNT_KEY);
for (String field : fieldsToPutInMdc) {
MDC.remove(field);
}