|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://aws.amazon.com/apache2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package software.amazon.awssdk.core.internal.util; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Java6Assertions.assertThat; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.verify; |
| 21 | +import static software.amazon.awssdk.core.client.config.SdkClientOption.METRIC_PUBLISHER; |
| 22 | + |
| 23 | +import java.io.IOException; |
| 24 | +import java.time.Duration; |
| 25 | +import java.util.Optional; |
| 26 | +import org.assertj.core.api.Assertions; |
| 27 | +import org.junit.Rule; |
| 28 | +import org.junit.Test; |
| 29 | +import org.junit.rules.ExpectedException; |
| 30 | +import software.amazon.awssdk.core.RequestOverrideConfiguration; |
| 31 | +import software.amazon.awssdk.core.SdkRequestOverrideConfiguration; |
| 32 | +import software.amazon.awssdk.core.client.config.SdkClientConfiguration; |
| 33 | +import software.amazon.awssdk.core.client.config.SdkClientOption; |
| 34 | +import software.amazon.awssdk.core.http.HttpResponseHandler; |
| 35 | +import software.amazon.awssdk.core.metrics.CoreMetric; |
| 36 | +import software.amazon.awssdk.http.SdkHttpFullResponse; |
| 37 | +import software.amazon.awssdk.metrics.MetricCollector; |
| 38 | +import software.amazon.awssdk.metrics.MetricPublisher; |
| 39 | +import software.amazon.awssdk.utils.Pair; |
| 40 | + |
| 41 | +public class MetricUtilsTest { |
| 42 | + @Rule |
| 43 | + public ExpectedException thrown = ExpectedException.none(); |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testMeasureDuration_returnsAccurateDurationInformation() { |
| 47 | + long testDurationNanos = Duration.ofMillis(1).toNanos(); |
| 48 | + |
| 49 | + Pair<Object, Duration> measuredExecute = MetricUtils.measureDuration(() -> { |
| 50 | + long start = System.nanoTime(); |
| 51 | + // spin thread instead of Thread.sleep() for a bit more accuracy... |
| 52 | + while (System.nanoTime() - start < testDurationNanos) { |
| 53 | + } |
| 54 | + return "foo"; |
| 55 | + }); |
| 56 | + |
| 57 | + assertThat(measuredExecute.right()).isGreaterThanOrEqualTo(Duration.ofNanos(testDurationNanos)); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void testMeasureDuration_returnsCallableReturnValue() { |
| 62 | + String result = "foo"; |
| 63 | + |
| 64 | + Pair<String, Duration> measuredExecute = MetricUtils.measureDuration(() -> result); |
| 65 | + |
| 66 | + assertThat(measuredExecute.left()).isEqualTo(result); |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + public void testMeasureDurationUnsafe_doesNotWrapException() throws Exception { |
| 71 | + IOException ioe = new IOException("boom"); |
| 72 | + |
| 73 | + thrown.expect(IOException.class); |
| 74 | + try { |
| 75 | + MetricUtils.measureDurationUnsafe(() -> { |
| 76 | + throw ioe; |
| 77 | + }); |
| 78 | + } catch (IOException caught) { |
| 79 | + assertThat(caught).isSameAs(ioe); |
| 80 | + throw caught; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + @Test |
| 85 | + public void testMeasureDuration_doesNotWrapException() { |
| 86 | + RuntimeException e = new RuntimeException("boom"); |
| 87 | + |
| 88 | + thrown.expect(RuntimeException.class); |
| 89 | + |
| 90 | + try { |
| 91 | + MetricUtils.measureDuration(() -> { |
| 92 | + throw e; |
| 93 | + }); |
| 94 | + } catch (RuntimeException caught) { |
| 95 | + assertThat(caught).isSameAs(e); |
| 96 | + throw caught; |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void testCollectHttpMetrics_collectsAllExpectedMetrics() { |
| 102 | + MetricCollector mockCollector = mock(MetricCollector.class); |
| 103 | + |
| 104 | + int statusCode = 200; |
| 105 | + String requestId = "request-id"; |
| 106 | + String amznRequestId = "amzn-request-id"; |
| 107 | + String requestId2 = "request-id-2"; |
| 108 | + |
| 109 | + SdkHttpFullResponse response = SdkHttpFullResponse.builder() |
| 110 | + .statusCode(statusCode) |
| 111 | + .putHeader("x-amz-request-id", requestId) |
| 112 | + .putHeader(HttpResponseHandler.X_AMZN_REQUEST_ID_HEADER, amznRequestId) |
| 113 | + .putHeader(HttpResponseHandler.X_AMZ_ID_2_HEADER, requestId2) |
| 114 | + .build(); |
| 115 | + |
| 116 | + MetricUtils.collectHttpMetrics(mockCollector, response); |
| 117 | + |
| 118 | + verify(mockCollector).reportMetric(CoreMetric.HTTP_STATUS_CODE, statusCode); |
| 119 | + verify(mockCollector).reportMetric(CoreMetric.AWS_REQUEST_ID, requestId); |
| 120 | + verify(mockCollector).reportMetric(CoreMetric.AWS_REQUEST_ID, amznRequestId); |
| 121 | + verify(mockCollector).reportMetric(CoreMetric.AWS_EXTENDED_REQUEST_ID, requestId2); |
| 122 | + |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void resolvePublisher_requestConfigNull_ShouldUseSdkClientConfig() { |
| 127 | + MetricPublisher metricPublisher = mock(MetricPublisher.class); |
| 128 | + |
| 129 | + SdkClientConfiguration config = SdkClientConfiguration.builder().option(METRIC_PUBLISHER, metricPublisher).build(); |
| 130 | + RequestOverrideConfiguration requestOverrideConfiguration = null; |
| 131 | + Optional<MetricPublisher> result = MetricUtils.resolvePublisher(config, requestOverrideConfiguration); |
| 132 | + Assertions.assertThat(result).isEqualTo(Optional.of(metricPublisher)); |
| 133 | + } |
| 134 | + |
| 135 | + @Test |
| 136 | + public void resolvePublisher_requestConfigNotNull_shouldTakePrecedence() { |
| 137 | + MetricPublisher metricPublisher = mock(MetricPublisher.class); |
| 138 | + |
| 139 | + SdkClientConfiguration config = SdkClientConfiguration.builder().option(METRIC_PUBLISHER, mock(MetricPublisher.class)).build(); |
| 140 | + RequestOverrideConfiguration requestOverrideConfiguration = SdkRequestOverrideConfiguration.builder().metricPublisher(metricPublisher).build(); |
| 141 | + Optional<MetricPublisher> result = MetricUtils.resolvePublisher(config, requestOverrideConfiguration); |
| 142 | + Assertions.assertThat(result).isEqualTo(Optional.of(metricPublisher)); |
| 143 | + } |
| 144 | +} |
0 commit comments