public interface RequestId
The request ID is useful for logging and correlation.
RequestId.Generator
,
bind()
,
bindAndLog()
Modifier and Type | Interface and Description |
---|---|
static interface |
RequestId.Generator
Generates a unique request ID.
|
Modifier and Type | Method and Description |
---|---|
static Handler |
bind()
Creates a new request ID (using the
RequestId.Generator from the context) and inserts it into the request registry. |
static Handler |
bindAndLog() |
String |
getId()
The of the request.
|
String getId()
static Handler bind()
RequestId.Generator
from the context) and inserts it into the request registry.
The RequestId
can then be obtained from the request registry and used in response headers for example.
import ratpack.handling.RequestId;
import ratpack.http.client.ReceivedResponse;
import ratpack.test.embed.EmbeddedApp;
import static org.junit.Assert.*;
public class Example {
public static void main(String... args) throws Exception {
EmbeddedApp.fromHandlers(chain -> chain
.handler(RequestId.bind())
.handler(ctx -> {
ctx.getResponse().getHeaders().add("X-Request-Id", ctx.getRequest().get(RequestId.class).getId());
ctx.render("ok");
})
).test(httpClient -> {
ReceivedResponse response = httpClient.get();
assertEquals("ok", response.getBody().getText());
// Default request ID generator generates random UUIDs (i.e. 36 chars long)
assertEquals(36, response.getHeaders().get("X-Request-Id").length());
});
}
}
To use a different strategy for generating IDs, put your own implementation of RequestId.Generator
into the context registry before this handler.
static Handler bindAndLog()