Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand All @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
Expand Down Expand Up @@ -68,8 +69,7 @@ public final HttpTrace receivedRequest(TraceableRequest request) {
*/
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.TIME_TAKEN, () -> calculateTimeTaken(trace), trace::setTimeTaken);
setIfIncluded(Include.SESSION_ID, sessionId, trace::setSessionId);
setIfIncluded(Include.PRINCIPAL, principal, trace::setPrincipal);
trace.setResponse(new HttpTrace.Response(new FilteredTraceableResponse(response)));
Expand Down Expand Up @@ -102,6 +102,10 @@ private Map<String, List<String>> getHeadersIfIncluded(Include include,
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
}

private long calculateTimeTaken(HttpTrace trace) {
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - trace.getNanoTime());
}

private final class FilteredTraceableRequest implements TraceableRequest {

private final TraceableRequest delegate;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2019 the original author or authors.
* Copyright 2012-2020 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.
Expand Down Expand Up @@ -46,6 +46,8 @@ public final class HttpTrace {

private volatile Long timeTaken;

private final transient long nanoTime;

/**
* Creates a fully-configured {@code HttpTrace} instance. Primarily for use by
* {@link HttpTraceRepository} implementations when recreating a trace from a
Expand All @@ -67,11 +69,13 @@ public HttpTrace(Request request, Response response, Instant timestamp, Principa
this.principal = principal;
this.session = session;
this.timeTaken = timeTaken;
this.nanoTime = 0;
}

HttpTrace(TraceableRequest request) {
this.request = new Request(request);
this.timestamp = Instant.now();
this.nanoTime = System.nanoTime();
}

public Instant getTimestamp() {
Expand Down Expand Up @@ -118,6 +122,10 @@ void setTimeTaken(long timeTaken) {
this.timeTaken = timeTaken;
}

long getNanoTime() {
return this.nanoTime;
}

/**
* Trace of an HTTP request.
*/
Expand Down