org.springframework.boot.actuate.trace.http.HttpExchangeTracer Maven / Gradle / Ivy
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.actuate.trace.http;
import java.net.URI;
import java.security.Principal;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import org.springframework.http.HttpHeaders;
/**
* Traces an HTTP request-response exchange.
*
* @author Andy Wilkinson
* @since 2.0.0
*/
public class HttpExchangeTracer {
private final Set includes;
/**
* Creates a new {@code HttpExchangeTracer} that will use the given {@code includes}
* to determine the contents of its traces.
* @param includes the includes
*/
public HttpExchangeTracer(Set includes) {
this.includes = includes;
}
/**
* 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));
}
/**
* 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, Supplier 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)));
}
/**
* Post-process the given mutable map of request {@code headers}.
* @param headers the headers to post-process
*/
protected void postProcessRequestHeaders(Map> headers) {
}
private T getIfIncluded(Include include, Supplier valueSupplier) {
return this.includes.contains(include) ? valueSupplier.get() : null;
}
private void setIfIncluded(Include include, Supplier supplier,
Consumer consumer) {
if (this.includes.contains(include)) {
consumer.accept(supplier.get());
}
}
private Map> getHeadersIfIncluded(Include include,
Supplier