Skip to content

Commit b7ca81c

Browse files
committed
#27 - ENH: Add HttpClientRequest label ... typically for grouping / using with metrics collection and reporting
1 parent 8a9944f commit b7ca81c

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

http-client/client/src/main/java/io/avaje/http/client/DHttpClientRequest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ class DHttpClientRequest implements HttpClientRequest, HttpClientResponse {
5151
private boolean skipAuthToken;
5252
private boolean suppressLogging;
5353
private long startAsyncNanos;
54+
private String label;
5455

5556
DHttpClientRequest(DHttpClientContext context, Duration requestTimeout) {
5657
this.context = context;
@@ -71,6 +72,17 @@ public HttpClientRequest suppressLogging() {
7172
return this;
7273
}
7374

75+
@Override
76+
public HttpClientRequest label(String label) {
77+
this.label = label;
78+
return this;
79+
}
80+
81+
@Override
82+
public String label() {
83+
return label;
84+
}
85+
7486
@Override
7587
public HttpClientRequest requestTimeout(Duration requestTimeout) {
7688
this.requestTimeout = requestTimeout;

http-client/client/src/main/java/io/avaje/http/client/HttpClientRequest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public interface HttpClientRequest {
4545
*/
4646
HttpClientRequest suppressLogging();
4747

48+
/**
49+
* Set a label for the request. The label is intended to be used to group and
50+
* identify metrics for the request.
51+
*
52+
* @param label The label that can be used to identify metrics for the request
53+
*/
54+
HttpClientRequest label(String label);
55+
56+
/**
57+
* Return the label that has been set on this request.
58+
* <p>
59+
* Typically the label would be read in {@link RequestIntercept#afterResponse(HttpResponse, HttpClientRequest)}
60+
* to assign request execution metrics.
61+
*/
62+
String label();
63+
4864
/**
4965
* Set the request timeout to use for this request. When not set the default
5066
* request timeout will be used.

http-client/client/src/test/java/io/avaje/http/client/RetryTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ HttpClientContext initClientWithRetry() {
2525
@Test
2626
void retryTest() {
2727
HttpResponse<String> res = clientContext.request()
28+
.label("http_client_hello_retry")
2829
.path("hello/retry")
2930
.GET()
3031
.asString();
@@ -33,11 +34,13 @@ void retryTest() {
3334

3435
assertThat(myIntercept.responseTimeMicros).isGreaterThan(1);
3536
assertThat(myIntercept.counter).isEqualTo(1);
37+
assertThat(myIntercept.label).isEqualTo("http_client_hello_retry");
3638
}
3739

3840
static class MyIntercept implements RequestIntercept {
3941
int counter;
4042
long responseTimeMicros;
43+
String label;
4144

4245
/**
4346
* Not called for the retry attempts. Only called on the final success or error response.
@@ -46,6 +49,7 @@ static class MyIntercept implements RequestIntercept {
4649
public void afterResponse(HttpResponse<?> response, HttpClientRequest request) {
4750
counter++;
4851
responseTimeMicros = request.responseTimeMicros();
52+
label = request.label();
4953
}
5054
}
5155
}

0 commit comments

Comments
 (0)