Search in sources :

Example 1 with RequestCorrelationClosable

use of org.openkilda.northbound.utils.RequestCorrelationId.RequestCorrelationClosable in project open-kilda by telstra.

the class RequestCorrelationIdTest method shouldResetCorrelationIdOnClose.

@Test
public void shouldResetCorrelationIdOnClose() {
    // given
    RequestCorrelationId.create(String.format("test-%s", UUID.randomUUID()));
    String before = RequestCorrelationId.getId();
    // when
    try (RequestCorrelationClosable closable = RequestCorrelationId.create(String.format("test-%s", UUID.randomUUID()))) {
        // then
        assertThat(RequestCorrelationId.getId(), not(equalTo(before)));
    }
    assertThat(RequestCorrelationId.getId(), equalTo(DEFAULT_CORRELATION_ID));
}
Also used : RequestCorrelationClosable(org.openkilda.northbound.utils.RequestCorrelationId.RequestCorrelationClosable) Test(org.junit.Test)

Example 2 with RequestCorrelationClosable

use of org.openkilda.northbound.utils.RequestCorrelationId.RequestCorrelationClosable in project open-kilda by telstra.

the class RequestCorrelationFilter method doFilterInternal.

/**
 * Generates new correlation_id and add it into passing correlation id in the header.
 */
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
    String correlationId = request.getHeader(CORRELATION_ID);
    boolean emptyCorrelationId = StringUtils.isBlank(correlationId);
    if (emptyCorrelationId) {
        correlationId = UUID.randomUUID().toString();
    } else {
        correlationId = RequestCorrelationId.chain(UUID.randomUUID().toString(), correlationId);
    }
    try (RequestCorrelationClosable requestCorrelation = RequestCorrelationId.create(correlationId)) {
        // MDC is picked up by the %X in log4j2 formatter .. resources/log4j2.xml
        try (MDCCloseable closable = MDC.putCloseable(CORRELATION_ID, correlationId)) {
            if (emptyCorrelationId) {
                logger.warn("CorrelationId was not sent, generated one: {}", correlationId);
            } else {
                logger.trace("Found correlationId in header. Chaining: {}", correlationId);
            }
            response.addHeader(CORRELATION_ID, correlationId);
            filterChain.doFilter(request, response);
        }
    }
}
Also used : MDCCloseable(org.slf4j.MDC.MDCCloseable) RequestCorrelationClosable(org.openkilda.northbound.utils.RequestCorrelationId.RequestCorrelationClosable)

Aggregations

RequestCorrelationClosable (org.openkilda.northbound.utils.RequestCorrelationId.RequestCorrelationClosable)2 Test (org.junit.Test)1 MDCCloseable (org.slf4j.MDC.MDCCloseable)1