Skip to content

Metric utils test #1906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
import software.amazon.awssdk.http.async.AsyncExecuteRequest;
import software.amazon.awssdk.http.async.SdkAsyncHttpClient;
import software.amazon.awssdk.http.async.SdkAsyncHttpResponseHandler;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.utils.CompletableFutureUtils;
import utils.HttpTestUtils;
import utils.ValidSdkObjects;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.core.internal.util;

import static org.assertj.core.api.Java6Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static software.amazon.awssdk.core.client.config.SdkClientOption.METRIC_PUBLISHER;

import java.io.IOException;
import java.time.Duration;
import java.util.Optional;
import org.assertj.core.api.Assertions;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import software.amazon.awssdk.core.RequestOverrideConfiguration;
import software.amazon.awssdk.core.SdkRequestOverrideConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.core.http.HttpResponseHandler;
import software.amazon.awssdk.core.metrics.CoreMetric;
import software.amazon.awssdk.http.SdkHttpFullResponse;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.metrics.MetricPublisher;
import software.amazon.awssdk.utils.Pair;

public class MetricUtilsTest {
@Rule
public ExpectedException thrown = ExpectedException.none();

@Test
public void testMeasureDuration_returnsAccurateDurationInformation() {
long testDurationNanos = Duration.ofMillis(1).toNanos();

Pair<Object, Duration> measuredExecute = MetricUtils.measureDuration(() -> {
long start = System.nanoTime();
// spin thread instead of Thread.sleep() for a bit more accuracy...
while (System.nanoTime() - start < testDurationNanos) {
}
return "foo";
});

assertThat(measuredExecute.right()).isGreaterThanOrEqualTo(Duration.ofNanos(testDurationNanos));
}

@Test
public void testMeasureDuration_returnsCallableReturnValue() {
String result = "foo";

Pair<String, Duration> measuredExecute = MetricUtils.measureDuration(() -> result);

assertThat(measuredExecute.left()).isEqualTo(result);
}

@Test
public void testMeasureDurationUnsafe_doesNotWrapException() throws Exception {
IOException ioe = new IOException("boom");

thrown.expect(IOException.class);
try {
MetricUtils.measureDurationUnsafe(() -> {
throw ioe;
});
} catch (IOException caught) {
assertThat(caught).isSameAs(ioe);
throw caught;
}
}

@Test
public void testMeasureDuration_doesNotWrapException() {
RuntimeException e = new RuntimeException("boom");

thrown.expect(RuntimeException.class);

try {
MetricUtils.measureDuration(() -> {
throw e;
});
} catch (RuntimeException caught) {
assertThat(caught).isSameAs(e);
throw caught;
}
}

@Test
public void testCollectHttpMetrics_collectsAllExpectedMetrics() {
MetricCollector mockCollector = mock(MetricCollector.class);

int statusCode = 200;
String requestId = "request-id";
String amznRequestId = "amzn-request-id";
String requestId2 = "request-id-2";

SdkHttpFullResponse response = SdkHttpFullResponse.builder()
.statusCode(statusCode)
.putHeader("x-amz-request-id", requestId)
.putHeader(HttpResponseHandler.X_AMZN_REQUEST_ID_HEADER, amznRequestId)
.putHeader(HttpResponseHandler.X_AMZ_ID_2_HEADER, requestId2)
.build();

MetricUtils.collectHttpMetrics(mockCollector, response);

verify(mockCollector).reportMetric(CoreMetric.HTTP_STATUS_CODE, statusCode);
verify(mockCollector).reportMetric(CoreMetric.AWS_REQUEST_ID, requestId);
verify(mockCollector).reportMetric(CoreMetric.AWS_REQUEST_ID, amznRequestId);
Comment on lines +119 to +120
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we report the same metric with two different values?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Depending on the service, the Header used for the request ID can be differ between these two values. This is just to ensure we're catching both cases, I don't think a service will use both.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok!

verify(mockCollector).reportMetric(CoreMetric.AWS_EXTENDED_REQUEST_ID, requestId2);

}

@Test
public void resolvePublisher_requestConfigNull_ShouldUseSdkClientConfig() {
MetricPublisher metricPublisher = mock(MetricPublisher.class);

SdkClientConfiguration config = SdkClientConfiguration.builder().option(METRIC_PUBLISHER, metricPublisher).build();
RequestOverrideConfiguration requestOverrideConfiguration = null;
Optional<MetricPublisher> result = MetricUtils.resolvePublisher(config, requestOverrideConfiguration);
Assertions.assertThat(result).isEqualTo(Optional.of(metricPublisher));
}

@Test
public void resolvePublisher_requestConfigNotNull_shouldTakePrecedence() {
MetricPublisher metricPublisher = mock(MetricPublisher.class);

SdkClientConfiguration config = SdkClientConfiguration.builder().option(METRIC_PUBLISHER, mock(MetricPublisher.class)).build();
RequestOverrideConfiguration requestOverrideConfiguration = SdkRequestOverrideConfiguration.builder().metricPublisher(metricPublisher).build();
Optional<MetricPublisher> result = MetricUtils.resolvePublisher(config, requestOverrideConfiguration);
Assertions.assertThat(result).isEqualTo(Optional.of(metricPublisher));
}
}

This file was deleted.