Skip to content

Instantly share code, notes, and snippets.

@cmicali
Last active January 9, 2016 02:45

Revisions

  1. cmicali revised this gist May 30, 2014. 1 changed file with 8 additions and 0 deletions.
    8 changes: 8 additions & 0 deletions service.yml
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,8 @@
    logging:
    level: INFO
    loggers:
    org.hibernate: WARN
    org.hibernate.SQL: INFO
    appenders:
    - type: console
    logFormat: "%-5p [%d{ISO8601}] [%mdc{OriginRequestId}] [%mdc{CurrentAccountId}] %c: %m%n%xEx"
  2. cmicali revised this gist May 20, 2014. 1 changed file with 42 additions and 0 deletions.
    42 changes: 42 additions & 0 deletions RequestId.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,42 @@
    package com.sagedevices.platform.jersey.core;

    import com.sagedevices.platform.api.util.StringUtil;

    public class RequestId {

    public static final String HEADER_NAME_ORIGIN = "X-Origin-Request-Id";
    public static final String HEADER_NAME_LOCAL = "X-Local-Request-Id";

    public static final String MDC_NAME_LOCAL = "LocalRequestId";
    public static final String MDC_NAME_ORIGIN = "OriginRequestId";

    public enum GeneratorType {

    SimpleIncrement("SimpleIncrement"),
    RandomLong("RandomLong"),
    UUID("UUID");

    private String value;

    GeneratorType(String value) {
    this.value = value;
    }

    public String getValue() {
    return this.value;
    }

    public static GeneratorType fromString(String text) {
    if (StringUtil.isNotEmpty(text)) {
    for (GeneratorType g: GeneratorType.values()) {
    if (text.equalsIgnoreCase(g.value)) {
    return g;
    }
    }
    }
    throw new IllegalArgumentException("No generator of type " + text);
    }

    }

    }
  3. cmicali created this gist May 20, 2014.
    79 changes: 79 additions & 0 deletions RequestIdFilter.java
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@
    package com.sagedevices.platform.jersey.filters;

    import com.google.common.base.Strings;
    import com.sagedevices.platform.jersey.core.RequestId;
    import com.sagedevices.platform.util.EncodingUtil;
    import org.slf4j.MDC;

    import javax.servlet.*;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import java.io.IOException;
    import java.util.UUID;
    import java.util.concurrent.ThreadLocalRandom;
    import java.util.concurrent.atomic.AtomicInteger;

    public class RequestIdFilter implements Filter {

    protected RequestId.GeneratorType requestIdGenerator;

    public AtomicInteger originCounter;
    public AtomicInteger localCounter;

    public RequestIdFilter() {
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    requestIdGenerator = RequestId.GeneratorType.valueOf(filterConfig.getInitParameter("RequestIdGenerator"));
    if (requestIdGenerator == RequestId.GeneratorType.SimpleIncrement) {
    originCounter = new AtomicInteger(0);
    localCounter = new AtomicInteger(0);
    }
    }

    @Override
    public void destroy() { /* unused */ }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    HttpServletResponse resp = (HttpServletResponse) response;

    // Generate the local request id
    final String localRequestId = getNewRequestId(requestIdGenerator, localCounter);
    MDC.put(RequestId.MDC_NAME_LOCAL, localRequestId);
    resp.addHeader(RequestId.HEADER_NAME_LOCAL, EncodingUtil.urlEncode(localRequestId));

    String originRequestId = req.getHeader(RequestId.HEADER_NAME_ORIGIN);
    if (Strings.isNullOrEmpty(originRequestId)) {
    originRequestId = getNewRequestId(requestIdGenerator, originCounter);
    }
    if (!Strings.isNullOrEmpty(originRequestId)) {
    MDC.put(RequestId.MDC_NAME_ORIGIN, originRequestId);
    resp.addHeader(RequestId.HEADER_NAME_ORIGIN, EncodingUtil.urlEncode(originRequestId));
    }

    try {
    chain.doFilter(request, response);
    } finally {
    MDC.remove(RequestId.MDC_NAME_LOCAL);
    MDC.remove(RequestId.MDC_NAME_ORIGIN);
    }

    }

    public static String getNewRequestId(RequestId.GeneratorType generator, AtomicInteger counter) {
    switch (generator) {
    case SimpleIncrement:
    return String.valueOf(counter.incrementAndGet());
    case RandomLong:
    return String.valueOf(ThreadLocalRandom.current().nextLong()).substring(1);
    case UUID:
    default:
    return UUID.randomUUID().toString();
    }
    }

    }