Skip to content

Commit de95cd2

Browse files
committed
Flip HttpClient.Metrics to extend HttpClientContext.Metrics instead of the other way around
1 parent 5a1c2b9 commit de95cd2

File tree

4 files changed

+39
-42
lines changed

4 files changed

+39
-42
lines changed

client/src/main/java/io/avaje/http/client/DHttpClientContext.java

+6-7
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import java.io.IOException;
44
import java.lang.reflect.Constructor;
55
import java.lang.reflect.ParameterizedType;
6-
import java.net.http.HttpClient;
76
import java.net.http.HttpHeaders;
87
import java.net.http.HttpRequest;
98
import java.net.http.HttpResponse;
@@ -24,7 +23,7 @@ final class DHttpClientContext implements HttpClientContext, SpiHttpClient {
2423
static final String AUTHORIZATION = "Authorization";
2524
private static final String BEARER = "Bearer ";
2625

27-
private final HttpClient httpClient;
26+
private final java.net.http.HttpClient httpClient;
2827
private final String baseUrl;
2928
private final Duration requestTimeout;
3029
private final BodyAdapter bodyAdapter;
@@ -41,7 +40,7 @@ final class DHttpClientContext implements HttpClientContext, SpiHttpClient {
4140
private final LongAdder metricResMicros = new LongAdder();
4241
private final LongAccumulator metricResMaxMicros = new LongAccumulator(Math::max, 0);
4342

44-
DHttpClientContext(HttpClient httpClient, String baseUrl, Duration requestTimeout, BodyAdapter bodyAdapter, RetryHandler retryHandler, RequestListener requestListener, AuthTokenProvider authTokenProvider, RequestIntercept intercept) {
43+
DHttpClientContext(java.net.http.HttpClient httpClient, String baseUrl, Duration requestTimeout, BodyAdapter bodyAdapter, RetryHandler retryHandler, RequestListener requestListener, AuthTokenProvider authTokenProvider, RequestIntercept intercept) {
4544
this.httpClient = httpClient;
4645
this.baseUrl = baseUrl;
4746
this.requestTimeout = requestTimeout;
@@ -106,17 +105,17 @@ public UrlBuilder url() {
106105
}
107106

108107
@Override
109-
public HttpClient httpClient() {
108+
public java.net.http.HttpClient httpClient() {
110109
return httpClient;
111110
}
112111

113112
@Override
114-
public Metrics metrics() {
113+
public HttpClient.Metrics metrics() {
115114
return metrics(false);
116115
}
117116

118117
@Override
119-
public Metrics metrics(boolean reset) {
118+
public HttpClient.Metrics metrics(boolean reset) {
120119
if (reset) {
121120
return new DMetrics(metricResTotal.sumThenReset(), metricResError.sumThenReset(), metricResBytes.sumThenReset(), metricResMicros.sumThenReset(), metricResMaxMicros.getThenReset());
122121
} else {
@@ -128,7 +127,7 @@ void metricsString(int stringBody) {
128127
metricResBytes.add(stringBody);
129128
}
130129

131-
static final class DMetrics implements Metrics {
130+
static final class DMetrics implements HttpClient.Metrics {
132131

133132
private final long totalCount;
134133
private final long errorCount;

client/src/main/java/io/avaje/http/client/HttpClient.java

+3-32
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,14 @@ static Builder builder() {
7878
* <p>
7979
* These metrics are collected for all requests sent via this context.
8080
*/
81-
HttpClientContext.Metrics metrics();
81+
HttpClient.Metrics metrics();
8282

8383
/**
8484
* Return the current metrics with the option of resetting the underlying counters.
8585
* <p>
8686
* These metrics are collected for all requests sent via this context.
8787
*/
88-
HttpClientContext.Metrics metrics(boolean reset);
88+
HttpClient.Metrics metrics(boolean reset);
8989

9090
/**
9191
* Builds the HttpClient.
@@ -331,36 +331,7 @@ interface State {
331331
/**
332332
* Statistic metrics collected to provide an overview of activity of this client.
333333
*/
334-
interface Metrics {
334+
interface Metrics extends HttpClientContext.Metrics {
335335

336-
/**
337-
* Return the total number of responses.
338-
*/
339-
long totalCount();
340-
341-
/**
342-
* Return the total number of error responses (status code >= 300).
343-
*/
344-
long errorCount();
345-
346-
/**
347-
* Return the total response bytes (excludes streaming responses).
348-
*/
349-
long responseBytes();
350-
351-
/**
352-
* Return the total response time in microseconds.
353-
*/
354-
long totalMicros();
355-
356-
/**
357-
* Return the max response time in microseconds (since the last reset).
358-
*/
359-
long maxMicros();
360-
361-
/**
362-
* Return the average response time in microseconds.
363-
*/
364-
long avgMicros();
365336
}
366337
}

client/src/main/java/io/avaje/http/client/HttpClientContext.java

+29-1
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,35 @@ interface State extends io.avaje.http.client.HttpClient.Builder.State {
275275
/**
276276
* Statistic metrics collected to provide an overview of activity of this client.
277277
*/
278-
interface Metrics extends io.avaje.http.client.HttpClient.Metrics {
278+
interface Metrics {
279+
/**
280+
* Return the total number of responses.
281+
*/
282+
long totalCount();
283+
284+
/**
285+
* Return the total number of error responses (status code >= 300).
286+
*/
287+
long errorCount();
288+
289+
/**
290+
* Return the total response bytes (excludes streaming responses).
291+
*/
292+
long responseBytes();
279293

294+
/**
295+
* Return the total response time in microseconds.
296+
*/
297+
long totalMicros();
298+
299+
/**
300+
* Return the max response time in microseconds (since the last reset).
301+
*/
302+
long maxMicros();
303+
304+
/**
305+
* Return the average response time in microseconds.
306+
*/
307+
long avgMicros();
280308
}
281309
}

client/src/test/java/io/avaje/http/client/HelloControllerTest.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package io.avaje.http.client;
22

3-
import com.fasterxml.jackson.core.JsonProcessingException;
43
import com.fasterxml.jackson.databind.ObjectMapper;
54
import org.example.webserver.ErrorResponse;
65
import org.example.webserver.HelloDto;
@@ -23,7 +22,7 @@
2322
import java.util.stream.Collectors;
2423
import java.util.stream.Stream;
2524

26-
import static java.net.http.HttpClient.Version.*;
25+
import static java.net.http.HttpClient.Version.HTTP_1_1;
2726
import static org.assertj.core.api.Assertions.assertThat;
2827
import static org.junit.jupiter.api.Assertions.*;
2928

0 commit comments

Comments
 (0)