Skip to content

Commit ff09daf

Browse files
committed
Implement SERVICE_ENDPOINT core metric
1 parent c166bd3 commit ff09daf

File tree

7 files changed

+30
-0
lines changed

7 files changed

+30
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"contributor": "",
4+
"type": "bugfix",
5+
"description": "Implement SERVICE_ENDPOINT core metric"
6+
}

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/pipeline/stages/ApiCallAttemptMetricCollectionStage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.awssdk.core.internal.http.pipeline.stages;
1717

1818
import static software.amazon.awssdk.core.internal.util.MetricUtils.collectHttpMetrics;
19+
import static software.amazon.awssdk.core.internal.util.MetricUtils.collectHttpRequestMetrics;
1920
import static software.amazon.awssdk.core.internal.util.MetricUtils.createAttemptMetricsCollector;
2021

2122
import java.time.Duration;
@@ -47,6 +48,8 @@ public Response<OutputT> execute(SdkHttpFullRequest input, RequestExecutionConte
4748
context.attemptMetricCollector(apiCallAttemptMetrics);
4849
reportBackoffDelay(context);
4950

51+
collectHttpRequestMetrics(apiCallAttemptMetrics, input);
52+
5053
Response<OutputT> response = wrapped.execute(input, context);
5154

5255
collectHttpMetrics(apiCallAttemptMetrics, response.httpResponse());

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/http/pipeline/stages/AsyncApiCallAttemptMetricCollectionStage.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package software.amazon.awssdk.core.internal.http.pipeline.stages;
1717

1818
import static software.amazon.awssdk.core.internal.util.MetricUtils.collectHttpMetrics;
19+
import static software.amazon.awssdk.core.internal.util.MetricUtils.collectHttpRequestMetrics;
1920
import static software.amazon.awssdk.core.internal.util.MetricUtils.createAttemptMetricsCollector;
2021

2122
import java.time.Duration;
@@ -52,6 +53,8 @@ public CompletableFuture<Response<OutputT>> execute(SdkHttpFullRequest input,
5253
context.attemptMetricCollector(apiCallAttemptMetrics);
5354
reportBackoffDelay(context);
5455

56+
collectHttpRequestMetrics(apiCallAttemptMetrics, input);
57+
5558
CompletableFuture<Response<OutputT>> executeFuture = wrapped.execute(input, context);
5659
CompletableFuture<Response<OutputT>> metricsCollectedFuture = executeFuture.whenComplete((r, t) -> {
5760
if (t == null) {

core/sdk-core/src/main/java/software/amazon/awssdk/core/internal/util/MetricUtils.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import software.amazon.awssdk.core.internal.http.RequestExecutionContext;
2626
import software.amazon.awssdk.core.metrics.CoreMetric;
2727
import software.amazon.awssdk.http.HttpMetric;
28+
import software.amazon.awssdk.http.SdkHttpFullRequest;
2829
import software.amazon.awssdk.http.SdkHttpFullResponse;
2930
import software.amazon.awssdk.metrics.MetricCollector;
3031
import software.amazon.awssdk.metrics.NoOpMetricCollector;
@@ -65,6 +66,12 @@ public static <T> Pair<T, Duration> measureDurationUnsafe(Callable<T> c) throws
6566
return Pair.of(result, d);
6667
}
6768

69+
public static void collectHttpRequestMetrics(MetricCollector metricCollector, SdkHttpFullRequest httpRequest) {
70+
if (metricCollector != null && !(metricCollector instanceof NoOpMetricCollector) && httpRequest != null) {
71+
metricCollector.reportMetric(CoreMetric.SERVICE_ENDPOINT, httpRequest.getUri());
72+
}
73+
}
74+
6875
public static void collectHttpMetrics(MetricCollector metricCollector, SdkHttpFullResponse httpResponse) {
6976
if (metricCollector != null && !(metricCollector instanceof NoOpMetricCollector) && httpResponse != null) {
7077
metricCollector.reportMetric(HttpMetric.HTTP_STATUS_CODE, httpResponse.statusCode());

core/sdk-core/src/main/java/software/amazon/awssdk/core/metrics/CoreMetric.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package software.amazon.awssdk.core.metrics;
1717

18+
import java.net.URI;
1819
import java.time.Duration;
1920
import software.amazon.awssdk.annotations.SdkPublicApi;
2021
import software.amazon.awssdk.core.retry.RetryPolicy;
@@ -50,6 +51,12 @@ public final class CoreMetric {
5051
public static final SdkMetric<Integer> RETRY_COUNT =
5152
metric("RetryCount", Integer.class, MetricLevel.ERROR);
5253

54+
/**
55+
* The endpoint for the service.
56+
*/
57+
public static final SdkMetric<URI> SERVICE_ENDPOINT =
58+
metric("ServiceEndpoint", URI.class, MetricLevel.ERROR);
59+
5360
/**
5461
* The duration of the API call. This includes all call attempts made.
5562
*

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/metrics/CoreMetricsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,8 @@ public void testApiCall_operationSuccessful_addsMetrics() {
181181
assertThat(capturedCollection.metricValues(CoreMetric.MARSHALLING_DURATION).get(0))
182182
.isGreaterThanOrEqualTo(Duration.ZERO);
183183
assertThat(capturedCollection.metricValues(CoreMetric.RETRY_COUNT)).containsExactly(0);
184+
assertThat(capturedCollection.metricValues(CoreMetric.SERVICE_ENDPOINT).get(0)).isEqualTo(URI.create(
185+
"https://customresponsemetadata.us-west-2.amazonaws.com"));
184186

185187
assertThat(capturedCollection.children()).hasSize(1);
186188
MetricCollection attemptCollection = capturedCollection.children().get(0);

test/codegen-generated-classes-test/src/test/java/software/amazon/awssdk/services/metrics/async/BaseAsyncCoreMetricsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ private void verifyApiCallCollection(MetricCollection capturedCollection) {
201201
.isGreaterThanOrEqualTo(Duration.ZERO);
202202
assertThat(capturedCollection.metricValues(CoreMetric.API_CALL_DURATION).get(0))
203203
.isGreaterThan(FIXED_DELAY);
204+
assertThat(capturedCollection.metricValues(CoreMetric.SERVICE_ENDPOINT).get(0))
205+
.isEqualTo(URI.create("http://localhost"));
204206
}
205207

206208
void stubSuccessfulResponse() {

0 commit comments

Comments
 (0)