Skip to content

Commit 6dee7e6

Browse files
committed
Merge pull request #22266 from dreis2211
* gh-22266: Polish "Measure with nanoseconds in HttpExchangeTracer" Measure with nanoseconds in HttpExchangeTracer Closes gh-22266
2 parents ec3433a + cb7f99a commit 6dee7e6

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/http/HttpExchangeTracer.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.List;
2323
import java.util.Map;
2424
import java.util.Set;
25+
import java.util.concurrent.TimeUnit;
2526
import java.util.function.Consumer;
2627
import java.util.function.Predicate;
2728
import java.util.function.Supplier;
@@ -68,8 +69,7 @@ public final HttpTrace receivedRequest(TraceableRequest request) {
6869
*/
6970
public final void sendingResponse(HttpTrace trace, TraceableResponse response, Supplier<Principal> principal,
7071
Supplier<String> sessionId) {
71-
setIfIncluded(Include.TIME_TAKEN, () -> System.currentTimeMillis() - trace.getTimestamp().toEpochMilli(),
72-
trace::setTimeTaken);
72+
setIfIncluded(Include.TIME_TAKEN, () -> calculateTimeTaken(trace), trace::setTimeTaken);
7373
setIfIncluded(Include.SESSION_ID, sessionId, trace::setSessionId);
7474
setIfIncluded(Include.PRINCIPAL, principal, trace::setPrincipal);
7575
trace.setResponse(new HttpTrace.Response(new FilteredTraceableResponse(response)));
@@ -102,6 +102,10 @@ private Map<String, List<String>> getHeadersIfIncluded(Include include,
102102
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
103103
}
104104

105+
private long calculateTimeTaken(HttpTrace trace) {
106+
return TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - trace.getStartNanoTime());
107+
}
108+
105109
private final class FilteredTraceableRequest implements TraceableRequest {
106110

107111
private final TraceableRequest delegate;

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/trace/http/HttpTrace.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -46,6 +46,8 @@ public final class HttpTrace {
4646

4747
private volatile Long timeTaken;
4848

49+
private final long startNanoTime;
50+
4951
/**
5052
* Creates a fully-configured {@code HttpTrace} instance. Primarily for use by
5153
* {@link HttpTraceRepository} implementations when recreating a trace from a
@@ -67,11 +69,13 @@ public HttpTrace(Request request, Response response, Instant timestamp, Principa
6769
this.principal = principal;
6870
this.session = session;
6971
this.timeTaken = timeTaken;
72+
this.startNanoTime = 0;
7073
}
7174

7275
HttpTrace(TraceableRequest request) {
7376
this.request = new Request(request);
7477
this.timestamp = Instant.now();
78+
this.startNanoTime = System.nanoTime();
7579
}
7680

7781
public Instant getTimestamp() {
@@ -118,6 +122,10 @@ void setTimeTaken(long timeTaken) {
118122
this.timeTaken = timeTaken;
119123
}
120124

125+
long getStartNanoTime() {
126+
return this.startNanoTime;
127+
}
128+
121129
/**
122130
* Trace of an HTTP request.
123131
*/

0 commit comments

Comments
 (0)