Search in sources :

Example 1 with MDCCloseable

use of org.slf4j.MDC.MDCCloseable in project open-kilda by telstra.

the class NorthboundExceptionHandler method handleMessageException.

/**
 * Handles NorthboundException exception.
 *
 * @param exception the NorthboundException instance
 * @param request   the WebRequest caused exception
 * @return the ResponseEntity object instance
 */
@ExceptionHandler(MessageException.class)
protected ResponseEntity<Object> handleMessageException(MessageException exception, WebRequest request) {
    HttpStatus status;
    switch(exception.getErrorType()) {
        case NOT_FOUND:
            status = HttpStatus.NOT_FOUND;
            break;
        case DATA_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case PARAMETERS_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case REQUEST_INVALID:
            status = HttpStatus.BAD_REQUEST;
            break;
        case OPERATION_TIMED_OUT:
            status = HttpStatus.REQUEST_TIMEOUT;
            break;
        case ALREADY_EXISTS:
            status = HttpStatus.CONFLICT;
            break;
        case AUTH_FAILED:
            status = HttpStatus.UNAUTHORIZED;
            break;
        case UNPROCESSABLE_REQUEST:
            status = HttpStatus.UNPROCESSABLE_ENTITY;
            break;
        case INTERNAL_ERROR:
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            break;
        case NOT_PERMITTED:
            status = HttpStatus.FORBIDDEN;
            break;
        case NOT_IMPLEMENTED:
            status = HttpStatus.NOT_IMPLEMENTED;
            break;
        default:
            status = HttpStatus.INTERNAL_SERVER_ERROR;
            break;
    }
    MessageError error = new MessageError(exception.getCorrelationId(), exception.getTimestamp(), exception.getErrorType().toString(), exception.getMessage(), exception.getErrorDescription());
    try (MDCCloseable mdcCloseable = MDC.putCloseable(CORRELATION_ID, exception.getCorrelationId())) {
        logger.warn(format("Error %s caught.", error), exception);
    }
    return super.handleExceptionInternal(exception, error, new HttpHeaders(), status, request);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) HttpStatus(org.springframework.http.HttpStatus) MessageError(org.openkilda.messaging.error.MessageError) MDCCloseable(org.slf4j.MDC.MDCCloseable) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler)

Example 2 with MDCCloseable

use of org.slf4j.MDC.MDCCloseable 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)

Example 3 with MDCCloseable

use of org.slf4j.MDC.MDCCloseable in project open-kilda by telstra.

the class KafkaMessageListener method onMessage.

/**
 * Handles all messages from kafka and sends to corresponding component for further processing.
 *
 * @param message received  message.
 */
@KafkaHandler
public void onMessage(@Header(name = KafkaHeaders.RECEIVED_MESSAGE_KEY, required = false) String key, Message message) {
    try (MDCCloseable closable = MDC.putCloseable(Utils.CORRELATION_ID, message.getCorrelationId())) {
        log.debug("Message received: {} - {}", Thread.currentThread().getId(), message);
        messageProcessor.processRequest(message, key);
    }
}
Also used : MDCCloseable(org.slf4j.MDC.MDCCloseable) KafkaHandler(org.springframework.kafka.annotation.KafkaHandler)

Example 4 with MDCCloseable

use of org.slf4j.MDC.MDCCloseable in project open-kilda by telstra.

the class KafkaMessageListener method onMessage.

/**
 * Handles all messages from kafka and sends to corresponding component for further processing.
 * <p/>
 * @param message received message.
 */
@KafkaHandler
public void onMessage(Message message) {
    try (MDCCloseable closable = MDC.putCloseable(CORRELATION_ID, message.getCorrelationId())) {
        logger.debug("Message received: {} - {}", Thread.currentThread().getId(), message);
        messagingChannel.onResponse(message);
    }
}
Also used : MDCCloseable(org.slf4j.MDC.MDCCloseable) KafkaHandler(org.springframework.kafka.annotation.KafkaHandler)

Aggregations

MDCCloseable (org.slf4j.MDC.MDCCloseable)4 KafkaHandler (org.springframework.kafka.annotation.KafkaHandler)2 MessageError (org.openkilda.messaging.error.MessageError)1 RequestCorrelationClosable (org.openkilda.northbound.utils.RequestCorrelationId.RequestCorrelationClosable)1 HttpHeaders (org.springframework.http.HttpHeaders)1 HttpStatus (org.springframework.http.HttpStatus)1 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)1 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)1