Skip to content

Commit

Permalink
Merge branch '1.1.x' into 1.2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
shakuzen committed Aug 29, 2019
2 parents 57f5950 + 537fc5c commit a27753b
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@

import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
* Factory methods for creating {@link Tag Tags} related to a request-response exchange
Expand All @@ -47,6 +50,18 @@ public final class RestTemplateExchangeTags {

private static final Tag OUTCOME_SERVER_ERROR = Tag.of("outcome", "SERVER_ERROR");

private static final Map<HttpStatus.Series, Tag> SERIES_OUTCOMES;

static {
Map<HttpStatus.Series, Tag> seriesOutcomes = new HashMap<>();
seriesOutcomes.put(HttpStatus.Series.INFORMATIONAL, OUTCOME_INFORMATIONAL);
seriesOutcomes.put(HttpStatus.Series.SUCCESSFUL, OUTCOME_SUCCESS);
seriesOutcomes.put(HttpStatus.Series.REDIRECTION, OUTCOME_REDIRECTION);
seriesOutcomes.put(HttpStatus.Series.CLIENT_ERROR, OUTCOME_CLIENT_ERROR);
seriesOutcomes.put(HttpStatus.Series.SERVER_ERROR, OUTCOME_SERVER_ERROR);
SERIES_OUTCOMES = Collections.unmodifiableMap(seriesOutcomes);
}

private RestTemplateExchangeTags() {
}

Expand Down Expand Up @@ -141,26 +156,15 @@ public static Tag clientName(HttpRequest request) {
public static Tag outcome(ClientHttpResponse response) {
try {
if (response != null) {
HttpStatus statusCode = response.getStatusCode();
if (statusCode.is1xxInformational()) {
return OUTCOME_INFORMATIONAL;
}
if (statusCode.is2xxSuccessful()) {
return OUTCOME_SUCCESS;
}
if (statusCode.is3xxRedirection()) {
return OUTCOME_REDIRECTION;
}
if (statusCode.is4xxClientError()) {
return OUTCOME_CLIENT_ERROR;
}
if (statusCode.is5xxServerError()) {
return OUTCOME_SERVER_ERROR;
HttpStatus.Series series = HttpStatus.Series.valueOf(response.getRawStatusCode());
if (series != null) {
return SERIES_OUTCOMES.getOrDefault(series, OUTCOME_UNKNOWN);
}
}
return OUTCOME_UNKNOWN;
} catch (IOException | IllegalArgumentException e) {
return OUTCOME_UNKNOWN;
}
catch (IOException | IllegalArgumentException ex) {
// Continue
}
return OUTCOME_UNKNOWN;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/**
* Copyright 2019 Pivotal Software, Inc.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.micrometer.spring.web.client;

import io.micrometer.core.instrument.Tag;
import org.junit.Test;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.mock.http.client.MockClientHttpResponse;

import java.io.IOException;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;

/**
* Tests for {@link RestTemplateExchangeTags}.
*
* @author Nishant Raut
* @author Brian Clozel
*/
public class RestTemplateExchangeTagsTest {

@Test
public void outcomeTagIsUnknownWhenResponseIsNull() {
Tag tag = RestTemplateExchangeTags.outcome(null);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

@Test
public void outcomeTagIsInformationalWhenResponseIs1xx() {
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.CONTINUE);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("INFORMATIONAL");
}

@Test
public void outcomeTagIsSuccessWhenResponseIs2xx() {
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.OK);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("SUCCESS");
}

@Test
public void outcomeTagIsRedirectionWhenResponseIs3xx() {
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.MOVED_PERMANENTLY);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("REDIRECTION");
}

@Test
public void outcomeTagIsClientErrorWhenResponseIs4xx() {
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.BAD_REQUEST);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
}

@Test
public void outcomeTagIsServerErrorWhenResponseIs5xx() {
ClientHttpResponse response = new MockClientHttpResponse("foo".getBytes(), HttpStatus.BAD_GATEWAY);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("SERVER_ERROR");
}

@Test
public void outcomeTagIsUnknownWhenResponseThrowsIOException() throws Exception {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getRawStatusCode()).willThrow(IOException.class);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

@Test
public void outcomeTagIsUnknownForCustomResponseStatus() throws Exception {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getRawStatusCode()).willThrow(IllegalArgumentException.class);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

@Test
public void outcomeTagIsClientErrorWhenResponseIsNonStandardInClientSeries() throws IOException {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getRawStatusCode()).willReturn(490);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("CLIENT_ERROR");
}

@Test
public void outcomeTagIsUnknownWhenResponseStatusIsInUnknownSeries() throws IOException {
ClientHttpResponse response = mock(ClientHttpResponse.class);
given(response.getRawStatusCode()).willReturn(701);
Tag tag = RestTemplateExchangeTags.outcome(response);
assertThat(tag.getValue()).isEqualTo("UNKNOWN");
}

}

0 comments on commit a27753b

Please sign in to comment.