Skip to content

Wrong output for async servlets #24

@serban-petrescu

Description

@serban-petrescu

Summary

Currently, this library produces a misleading output if the Servlet is async enabled. This is because the filter would be called twice: once on the REQUEST dispatch, where the async processing would start and once on the ASYNC dispatch.

I understand that the logging context / correlation ID propagation is out of scope, but the request / response logging is actually faulty in this particular case.

Steps to reproduce:

It easiest to reproduce this with Spring (a lot easier to setup all the stuff):

  1. Create a new Spring Boot App.
  2. Add the filter.
@Bean
public FilterRegistrationBean loggingFilter() {
	FilterRegistrationBean bean = new FilterRegistrationBean(new RequestLoggingFilter());
	bean.addUrlPatterns("/*");
	return bean;
}
  1. Create a controller which returns a Future.
@RestController
class DemoController {
	private static final Logger LOG = LoggerFactory.getLogger(DemoController.class);

	@GetMapping("/something")
	public CompletableFuture<ResponseEntity<String>> doSomething() {
		LOG.info("Inside sync method.");
		return CompletableFuture.supplyAsync(() -> {
			LOG.info("Inside async supply.");
			return new ResponseEntity<>("Something!", HttpStatus.CREATED);
		});
	}
}

Expected behaviour:

The response is logged only once, with the correct size, status, etc.

Actual behaviour:

We get two logs from the RequestLoggingFilter (one for each dispatcher type). The first one has incorrect status / size / etc:

c.s.h.c.l.c.LogContext                   : generated new correlation id
c.e.d.DemoController                     : Inside sync method.
c.e.d.DemoController                     : Inside async supply.
c.s.h.c.l.s.f.RequestLoggingFilter       : {"request":"/something","referer":"-","response_sent_at":"2018-05-14T05:16:12.781Z","response_status":200,"method":"GET","response_size_b":-1,"request_size_b":-1,"remote_port":"redacted","layer":"[SERVLET]","remote_host":"redacted","x_forwarded_for":"-","remote_user":"-","protocol":"HTTP/1.1","remote_ip":"redacted","response_content_type":"-","request_received_at":"2018-05-14T05:16:12.759Z","response_time_ms":22.124503,"direction":"IN"}
c.s.h.c.l.c.LogContext                   : generated new correlation id
c.s.h.c.l.s.f.RequestLoggingFilter       : {"request":"/something","referer":"-","response_sent_at":"2018-05-14T05:16:12.817Z","response_status":201,"method":"GET","response_size_b":10,"request_size_b":-1,"remote_port":"redacted","layer":"[SERVLET]","remote_host":"redacted","x_forwarded_for":"-","remote_user":"-","protocol":"HTTP/1.1","remote_ip":"redacted","response_content_type":"text/plain;charset=UTF-8","request_received_at":"2018-05-14T05:16:12.803Z","response_time_ms":13.84298,"direction":"IN"}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions