Skip to content

Commit d5ae59d

Browse files
committed
Polish
Closes gh-15594
1 parent 21df40b commit d5ae59d

File tree

7 files changed

+81
-44
lines changed

7 files changed

+81
-44
lines changed

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/DefaultRestTemplateExchangeTagsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/client/RestTemplateExchangeTags.java

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -35,6 +35,7 @@
3535
* @author Andy Wilkinson
3636
* @author Jon Schneider
3737
* @author Nishant Raut
38+
* @author Brian Clozel
3839
* @since 2.0.0
3940
*/
4041
public final class RestTemplateExchangeTags {
@@ -137,36 +138,37 @@ public static Tag clientName(HttpRequest request) {
137138
* @since 2.2.0
138139
*/
139140
public static Tag outcome(ClientHttpResponse response) {
140-
if (response != null) {
141-
HttpStatus status = extractStatus(response);
142-
if (status != null) {
143-
if (status.is1xxInformational()) {
144-
return OUTCOME_INFORMATIONAL;
145-
}
146-
if (status.is2xxSuccessful()) {
147-
return OUTCOME_SUCCESS;
148-
}
149-
if (status.is3xxRedirection()) {
150-
return OUTCOME_REDIRECTION;
151-
}
152-
if (status.is4xxClientError()) {
153-
return OUTCOME_CLIENT_ERROR;
154-
}
141+
HttpStatus status = extractStatus(response);
142+
if (status != null) {
143+
if (status.is1xxInformational()) {
144+
return OUTCOME_INFORMATIONAL;
145+
}
146+
if (status.is2xxSuccessful()) {
147+
return OUTCOME_SUCCESS;
148+
}
149+
if (status.is3xxRedirection()) {
150+
return OUTCOME_REDIRECTION;
151+
}
152+
if (status.is4xxClientError()) {
153+
return OUTCOME_CLIENT_ERROR;
154+
}
155+
if (status.is5xxServerError()) {
156+
return OUTCOME_SERVER_ERROR;
155157
}
156-
return OUTCOME_SERVER_ERROR;
157158
}
158159
return OUTCOME_UNKNOWN;
159160
}
160161

161162
private static HttpStatus extractStatus(ClientHttpResponse response) {
162-
163163
try {
164-
return response.getStatusCode();
164+
if (response != null) {
165+
return response.getStatusCode();
166+
}
167+
return null;
165168
}
166-
catch (IOException ex) {
169+
catch (IOException | IllegalArgumentException exc) {
167170
return null;
168171
}
169-
170172
}
171173

172174
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/DefaultWebClientExchangeTagsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTags.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -133,9 +133,9 @@ public static Tag clientName(ClientRequest request) {
133133
* @since 2.2.0
134134
*/
135135
public static Tag outcome(ClientResponse response) {
136-
if (response != null) {
137-
HttpStatus status = response.statusCode();
138-
if (status != null) {
136+
try {
137+
if (response != null) {
138+
HttpStatus status = response.statusCode();
139139
if (status.is1xxInformational()) {
140140
return OUTCOME_INFORMATIONAL;
141141
}
@@ -148,10 +148,15 @@ public static Tag outcome(ClientResponse response) {
148148
if (status.is4xxClientError()) {
149149
return OUTCOME_CLIENT_ERROR;
150150
}
151+
if (status.is5xxServerError()) {
152+
return OUTCOME_SERVER_ERROR;
153+
}
151154
}
152-
return OUTCOME_SERVER_ERROR;
155+
return OUTCOME_UNKNOWN;
156+
}
157+
catch (IllegalArgumentException exc) {
158+
return OUTCOME_UNKNOWN;
153159
}
154-
return OUTCOME_UNKNOWN;
155160
}
156161

157162
}
Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -16,23 +16,28 @@
1616

1717
package org.springframework.boot.actuate.metrics.web.client;
1818

19+
import java.io.IOException;
20+
1921
import io.micrometer.core.instrument.Tag;
2022
import org.junit.Test;
2123

2224
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.client.ClientHttpResponse;
2326
import org.springframework.mock.http.client.MockClientHttpResponse;
2427

2528
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.BDDMockito.given;
30+
import static org.mockito.Mockito.mock;
2631

2732
/**
2833
* Tests for {@link RestTemplateExchangeTags}.
2934
*
3035
* @author Nishant Raut
36+
* @author Brian Clozel
37+
* @author Brian Clozel
3138
*/
3239
public class RestTemplateExchangeTagsTests {
3340

34-
private MockClientHttpResponse response;
35-
3641
@Test
3742
public void outcomeTagIsUnknownWhenResponseStatusIsNull() {
3843
Tag tag = RestTemplateExchangeTags.outcome(null);
@@ -41,40 +46,58 @@ public void outcomeTagIsUnknownWhenResponseStatusIsNull() {
4146

4247
@Test
4348
public void outcomeTagIsInformationalWhenResponseIs1xx() {
44-
this.response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.CONTINUE);
45-
Tag tag = RestTemplateExchangeTags.outcome(this.response);
49+
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
50+
HttpStatus.CONTINUE);
51+
Tag tag = RestTemplateExchangeTags.outcome(response);
4652
assertThat(tag.getValue()).isEqualTo("INFORMATIONAL");
4753
}
4854

4955
@Test
5056
public void outcomeTagIsSuccessWhenResponseIs2xx() {
51-
this.response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.OK);
52-
Tag tag = RestTemplateExchangeTags.outcome(this.response);
57+
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
58+
HttpStatus.OK);
59+
Tag tag = RestTemplateExchangeTags.outcome(response);
5360
assertThat(tag.getValue()).isEqualTo("SUCCESS");
5461
}
5562

5663
@Test
5764
public void outcomeTagIsRedirectionWhenResponseIs3xx() {
58-
this.response = new MockClientHttpResponse("foo".getBytes(),
65+
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
5966
HttpStatus.MOVED_PERMANENTLY);
60-
Tag tag = RestTemplateExchangeTags.outcome(this.response);
67+
Tag tag = RestTemplateExchangeTags.outcome(response);
6168
assertThat(tag.getValue()).isEqualTo("REDIRECTION");
6269
}
6370

6471
@Test
6572
public void outcomeTagIsClientErrorWhenResponseIs4xx() {
66-
this.response = new MockClientHttpResponse("foo".getBytes(),
73+
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
6774
HttpStatus.BAD_REQUEST);
68-
Tag tag = RestTemplateExchangeTags.outcome(this.response);
75+
Tag tag = RestTemplateExchangeTags.outcome(response);
6976
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
7077
}
7178

7279
@Test
7380
public void outcomeTagIsServerErrorWhenResponseIs5xx() {
74-
this.response = new MockClientHttpResponse("foo".getBytes(),
81+
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(),
7582
HttpStatus.BAD_GATEWAY);
76-
Tag tag = RestTemplateExchangeTags.outcome(this.response);
83+
Tag tag = RestTemplateExchangeTags.outcome(response);
7784
assertThat(tag.getValue()).isEqualTo("SERVER_ERROR");
7885
}
7986

87+
@Test
88+
public void outcomeTagIsUnknownWhenResponseThrowsIOException() throws Exception {
89+
ClientHttpResponse response = mock(ClientHttpResponse.class);
90+
given(response.getStatusCode()).willThrow(IOException.class);
91+
Tag tag = RestTemplateExchangeTags.outcome(response);
92+
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
93+
}
94+
95+
@Test
96+
public void outcomeTagIsUnknownForCustomResponseStatus() throws Exception {
97+
ClientHttpResponse response = mock(ClientHttpResponse.class);
98+
given(response.getStatusCode()).willThrow(IllegalArgumentException.class);
99+
Tag tag = RestTemplateExchangeTags.outcome(response);
100+
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
101+
}
102+
80103
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/DefaultWebClientExchangeTagsProviderTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/metrics/web/reactive/client/WebClientExchangeTagsTests.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -155,4 +155,11 @@ public void outcomeTagIsServerErrorWhenResponseIs5xx() {
155155
assertThat(tag.getValue()).isEqualTo("SERVER_ERROR");
156156
}
157157

158+
@Test
159+
public void outcomeTagIsUknownWhenResponseStatusIsUknown() {
160+
given(this.response.statusCode()).willThrow(IllegalArgumentException.class);
161+
Tag tag = WebClientExchangeTags.outcome(this.response);
162+
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
163+
}
164+
158165
}

0 commit comments

Comments
 (0)