All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.transferwise.common.baseutils.tracing.MdcXRequestIdHolder Maven / Gradle / Ivy

package com.transferwise.common.baseutils.tracing;

import com.transferwise.common.baseutils.ExceptionUtils;
import com.transferwise.common.baseutils.function.RunnableWithException;
import java.util.UUID;
import java.util.concurrent.Callable;
import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;

/**
 * Old Tw tracking system.
 *
 * @deprecated in favor of Jaegar tracing.
 */
public class MdcXRequestIdHolder implements IXRequestIdHolder {

  @Setter
  private String mdcKey;

  public MdcXRequestIdHolder() {
    this("xRequestId");
  }

  public MdcXRequestIdHolder(String mdcKey) {
    this.mdcKey = mdcKey;
  }

  @Override
  public String generate() {
    return UUID.randomUUID().toString();
  }

  @Override
  public String current() {
    return MDC.get(mdcKey);
  }

  @Override
  public void set(String requestId) {
    if (requestId == null) {
      MDC.remove(mdcKey);
    } else {
      MDC.put(mdcKey, requestId);
    }
  }

  @Override
  public void remove(String requestId) {
    MDC.remove(mdcKey);
  }

  @Override
  public  T with(Object o, Callable callable) {
    return ExceptionUtils.doUnchecked(() -> {
      String currentXRequestId = current();
      try {
        String xRequestId = currentXRequestId;
        if (o instanceof IWithXRequestId) {
          xRequestId = ((IWithXRequestId) o).getXRequestId();
        }
        if (StringUtils.isEmpty(xRequestId)) {
          xRequestId = generate();
        }
        set(xRequestId);
        return callable.call();
      } finally {
        set(currentXRequestId);
      }
    });
  }

  @Override
  public void with(Object o, RunnableWithException runnable) {
    with(o, () -> {
      runnable.run();
      return null;
    });
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy