/** * Begins the tracing of the exchange that was initiated by the given {@code request} * being received. * @param request the received request * @return the HTTP trace for the */ public final HttpTrace receivedRequest(TraceableRequest request) { return new HttpTrace(new FilteredTraceableRequest(request)); }
@Override public String getMethod() { return this.delegate.getMethod(); }
@Override public Map<String, List<String>> getHeaders() { Map<String, List<String>> headers = getHeadersIfIncluded( Include.REQUEST_HEADERS, this.delegate::getHeaders, this::includedHeader); postProcessRequestHeaders(headers); return headers; }
/** * Ends the tracing of the exchange that is being concluded by sending the given * {@code response}. * @param trace the trace for the exchange * @param response the response that concludes the exchange * @param principal a supplier for the exchange's principal * @param sessionId a supplier for the id of the exchange's session */ public final void sendingResponse(HttpTrace trace, TraceableResponse response, Supplier<Principal> principal, Supplier<String> sessionId) { setIfIncluded(Include.TIME_TAKEN, () -> System.currentTimeMillis() - trace.getTimestamp().toEpochMilli(), trace::setTimeTaken); setIfIncluded(Include.SESSION_ID, sessionId, trace::setSessionId); setIfIncluded(Include.PRINCIPAL, principal, trace::setPrincipal); trace.setResponse( new HttpTrace.Response(new FilteredTraceableResponse(response))); }
@Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { if (!isRequestValid(request)) { filterChain.doFilter(request, response); return; } TraceableHttpServletRequest traceableRequest = new TraceableHttpServletRequest( request); HttpTrace trace = this.tracer.receivedRequest(traceableRequest); int status = HttpStatus.INTERNAL_SERVER_ERROR.value(); try { filterChain.doFilter(request, response); status = response.getStatus(); } finally { TraceableHttpServletResponse traceableResponse = new TraceableHttpServletResponse( (status != response.getStatus()) ? new CustomStatusResponseWrapper(response, status) : response); this.tracer.sendingResponse(trace, traceableResponse, request::getUserPrincipal, () -> getSessionId(request)); this.repository.add(trace); } }
private Request(TraceableRequest request) { this(request.getMethod(), request.getUri(), request.getHeaders(), request.getRemoteAddress()); }
public String httpTrace(String arg) { return JsonUtils.asJson(httpTraceEndpoint.traces()); } }
Response(TraceableResponse response) { this(response.getStatus(), response.getHeaders()); }
@ReadOperation public HttpTraceDescriptor traces() { return new HttpTraceDescriptor(this.repository.findAll()); }
@Override public URI getUri() { return this.delegate.getUri(); }
@Override public int getStatus() { return this.delegate.getStatus(); }
void setPrincipal(java.security.Principal principal) { if (principal != null) { this.principal = new Principal(principal.getName()); } }
void setSessionId(String sessionId) { if (StringUtils.hasText(sessionId)) { this.session = new Session(sessionId); } }
HttpTrace(TraceableRequest request) { this.request = new Request(request); this.timestamp = Instant.now(); }
@Bean @ConditionalOnMissingBean(HttpTraceRepository.class) public InMemoryHttpTraceRepository traceRepository() { return new InMemoryHttpTraceRepository(); }
@Bean @ConditionalOnBean(HttpTraceRepository.class) @ConditionalOnMissingBean public HttpTraceEndpoint httpTraceEndpoint(HttpTraceRepository traceRepository) { return new HttpTraceEndpoint(traceRepository); }
@Bean @ConditionalOnMissingBean public HttpExchangeTracer httpExchangeTracer(HttpTraceProperties traceProperties) { return new HttpExchangeTracer(traceProperties.getInclude()); }
private Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain, Principal principal, WebSession session) { ServerWebExchangeTraceableRequest request = new ServerWebExchangeTraceableRequest( exchange); HttpTrace trace = this.tracer.receivedRequest(request); return chain.filter(exchange).doAfterSuccessOrError((aVoid, ex) -> { TraceableServerHttpResponse response = new TraceableServerHttpResponse( (ex != null) ? new CustomStatusResponseDecorator(ex, exchange.getResponse()) : exchange.getResponse()); this.tracer.sendingResponse(trace, response, () -> principal, () -> getStartedSessionId(session)); this.repository.add(trace); }); }
/** * Http traces method * * @return httptrace */ @ShellMethod(key = "httptrace", value = "Display httptrace endpoint.") @ShellMethodAvailability("httptraceAvailability") public HttpTraceEndpoint.HttpTraceDescriptor httptrace() { return httptrace.traces(); }
/** * Http traces method * * @return httptrace */ @ShellMethod(key = "httptrace", value = "Display httptrace endpoint.") @ShellMethodAvailability("httptraceAvailability") public Object httptrace() { return prettify(httptrace.traces()); }