How to log incoming request details and corresponding response in table in cloud gateway.
<code>String logId = service.logRequest(name.toString(),request.getURI().getRawPath(), String.valueOf(request.getMethod()),remoteAddr,"",new Date());
</code>
<code>String logId = service.logRequest(name.toString(),request.getURI().getRawPath(), String.valueOf(request.getMethod()),remoteAddr,"",new Date());
</code>
String logId = service.logRequest(name.toString(),request.getURI().getRawPath(), String.valueOf(request.getMethod()),remoteAddr,"",new Date());
Able to fetch the request details
Storing the details in table. But issues comes when response details have to be stored in corresponding request row.
Previously in ZUUL used to store the LogId like below.
Request
<code>RequestContext ctx = RequestContext.getCurrentContext();
ctx.put("REQ-LOG-ID",LogId);
</code>
<code>RequestContext ctx = RequestContext.getCurrentContext();
ctx.put("REQ-LOG-ID",LogId);
</code>
RequestContext ctx = RequestContext.getCurrentContext();
ctx.put("REQ-LOG-ID",LogId);
Response
<code>String requestLogId = (String) ctx.get("REQ-LOG-ID");
//store the response details to corresponding Id
</code>
<code>String requestLogId = (String) ctx.get("REQ-LOG-ID");
//store the response details to corresponding Id
</code>
String requestLogId = (String) ctx.get("REQ-LOG-ID");
//store the response details to corresponding Id
Whats the replacement in Cloud gateway? We’re using Spring-Boot 3.2.0.
To log incoming request details and corresponding responses in Spring Cloud Gateway, you can use the GatewayFilter
to store the log ID and then retrieve it later for the response. Here’s how you can do it:
- Create a Custom Gateway Filter:
<code>@Component
public class LoggingGatewayFilter extends AbstractGatewayFilterFactory<LoggingGatewayFilter.Config> {
public LoggingGatewayFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
String logId = service.logRequest(/* parameters here */);
exchange.getAttributes().put("REQ-LOG-ID", logId);
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
String requestLogId = exchange.getAttribute("REQ-LOG-ID");
// Store response details using requestLogId
storeResponseDetails(requestLogId, /* response details here */);
}));
};
}
public static class Config {
// Configuration properties can be added here if needed
}
}
</code>
<code>@Component
public class LoggingGatewayFilter extends AbstractGatewayFilterFactory<LoggingGatewayFilter.Config> {
public LoggingGatewayFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
String logId = service.logRequest(/* parameters here */);
exchange.getAttributes().put("REQ-LOG-ID", logId);
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
String requestLogId = exchange.getAttribute("REQ-LOG-ID");
// Store response details using requestLogId
storeResponseDetails(requestLogId, /* response details here */);
}));
};
}
public static class Config {
// Configuration properties can be added here if needed
}
}
</code>
@Component
public class LoggingGatewayFilter extends AbstractGatewayFilterFactory<LoggingGatewayFilter.Config> {
public LoggingGatewayFilter() {
super(Config.class);
}
@Override
public GatewayFilter apply(Config config) {
return (exchange, chain) -> {
String logId = service.logRequest(/* parameters here */);
exchange.getAttributes().put("REQ-LOG-ID", logId);
return chain.filter(exchange).then(Mono.fromRunnable(() -> {
String requestLogId = exchange.getAttribute("REQ-LOG-ID");
// Store response details using requestLogId
storeResponseDetails(requestLogId, /* response details here */);
}));
};
}
public static class Config {
// Configuration properties can be added here if needed
}
}
- Register the Filter:
<code>@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, LoggingGatewayFilter loggingGatewayFilter) {
return builder.routes()
.route("your_route_id", r -> r.path("/your-path/**")
.filters(f -> f.filter(loggingGatewayFilter))
.uri("http://your-backend-service"))
.build();
}
}
</code>
<code>@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, LoggingGatewayFilter loggingGatewayFilter) {
return builder.routes()
.route("your_route_id", r -> r.path("/your-path/**")
.filters(f -> f.filter(loggingGatewayFilter))
.uri("http://your-backend-service"))
.build();
}
}
</code>
@Configuration
public class GatewayConfig {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder, LoggingGatewayFilter loggingGatewayFilter) {
return builder.routes()
.route("your_route_id", r -> r.path("/your-path/**")
.filters(f -> f.filter(loggingGatewayFilter))
.uri("http://your-backend-service"))
.build();
}
}
- Store Response Details:
In thestoreResponseDetails
method, implement logic to save the response details corresponding to therequestLogId
.
I hope it’ll be helpful to solve problem you’re facing.
1