-
Notifications
You must be signed in to change notification settings - Fork 912
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
dagnir
merged 1 commit into
aws:sdk-metrics-development-2
from
dagnir:metric-utils-test
Jun 18, 2020
Merged
Metric utils test #1906
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
144 changes: 144 additions & 0 deletions
144
core/sdk-core/src/test/java/software/amazon/awssdk/core/internal/util/MetricUtilsTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
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)); | ||
} | ||
} |
55 changes: 0 additions & 55 deletions
55
core/sdk-core/src/test/java/software/amazon/awssdk/core/util/MetricUtilsTest.java
This file was deleted.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok!