Skip to content

Commit 1f53f9c

Browse files
committed
Support core metrics for async clients
1 parent 9032023 commit 1f53f9c

File tree

28 files changed

+704
-116
lines changed

28 files changed

+704
-116
lines changed

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/JsonProtocolSpec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
243243
"$L" +
244244
".withResponseHandler($L)\n" +
245245
".withErrorResponseHandler(errorResponseHandler)\n" +
246+
".withMetricCollector(apiCallMetricCollector)\n" +
246247
hostPrefixExpression(opModel) +
247248
discoveredEndpoint(opModel) +
248249
asyncRequestBody +

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/QueryProtocolSpec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public CodeBlock asyncExecutionHandler(IntermediateModel intermediateModel, Oper
141141
".withMarshaller($L)" +
142142
".withResponseHandler(responseHandler)" +
143143
".withErrorResponseHandler($N)\n" +
144+
".withMetricCollector(apiCallMetricCollector)\n" +
144145
hostPrefixExpression(opModel) +
145146
asyncRequestBody +
146147
".withInput($L) $L);",

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/specs/XmlProtocolSpec.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public CodeBlock executionHandler(OperationModel opModel) {
112112
.add("\n\nreturn clientHandler.execute(new $T<$T, $T>()" +
113113
".withOperationName(\"$N\")\n" +
114114
".withCombinedResponseHandler($N)" +
115+
".withMetricCollector(apiCallMetricCollector)\n" +
115116
hostPrefixExpression(opModel) +
116117
discoveredEndpoint(opModel) +
117118
".withInput($L)",

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-async-client-class.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,8 @@ public CompletableFuture<APostOperationResponse> aPostOperation(APostOperationRe
177177
.withOperationName("APostOperation")
178178
.withMarshaller(new APostOperationRequestMarshaller(protocolFactory))
179179
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
180-
.hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest));
180+
.withMetricCollector(apiCallMetricCollector).hostPrefixExpression(resolvedHostExpression)
181+
.withInput(aPostOperationRequest));
181182
AwsRequestOverrideConfiguration requestOverrideConfig = aPostOperationRequest.overrideConfiguration().orElse(null);
182183
executeFuture.whenComplete((r, e) -> {
183184
Optional<MetricPublisher> metricPublisher = MetricUtils.resolvePublisher(clientConfiguration,
@@ -237,7 +238,7 @@ public CompletableFuture<APostOperationWithOutputResponse> aPostOperationWithOut
237238
.withOperationName("APostOperationWithOutput")
238239
.withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory))
239240
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
240-
.withInput(aPostOperationWithOutputRequest));
241+
.withMetricCollector(apiCallMetricCollector).withInput(aPostOperationWithOutputRequest));
241242
AwsRequestOverrideConfiguration requestOverrideConfig = aPostOperationWithOutputRequest.overrideConfiguration()
242243
.orElse(null);
243244
executeFuture.whenComplete((r, e) -> {
@@ -320,8 +321,8 @@ public CompletableFuture<Void> eventStreamOperation(EventStreamOperationRequest
320321
.withMarshaller(new EventStreamOperationRequestMarshaller(protocolFactory))
321322
.withAsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody.fromPublisher(adapted))
322323
.withFullDuplex(true).withResponseHandler(responseHandler)
323-
.withErrorResponseHandler(errorResponseHandler).withInput(eventStreamOperationRequest),
324-
restAsyncResponseTransformer);
324+
.withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector)
325+
.withInput(eventStreamOperationRequest), restAsyncResponseTransformer);
325326
AwsRequestOverrideConfiguration requestOverrideConfig = eventStreamOperationRequest.overrideConfiguration().orElse(
326327
null);
327328
executeFuture.whenComplete((r, e) -> {
@@ -398,7 +399,7 @@ public CompletableFuture<EventStreamOperationWithOnlyInputResponse> eventStreamO
398399
.withMarshaller(new EventStreamOperationWithOnlyInputRequestMarshaller(protocolFactory))
399400
.withAsyncRequestBody(software.amazon.awssdk.core.async.AsyncRequestBody.fromPublisher(adapted))
400401
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
401-
.withInput(eventStreamOperationWithOnlyInputRequest));
402+
.withMetricCollector(apiCallMetricCollector).withInput(eventStreamOperationWithOnlyInputRequest));
402403
AwsRequestOverrideConfiguration requestOverrideConfig = eventStreamOperationWithOnlyInputRequest
403404
.overrideConfiguration().orElse(null);
404405
executeFuture.whenComplete((r, e) -> {
@@ -477,6 +478,7 @@ public CompletableFuture<Void> eventStreamOperationWithOnlyOutput(
477478
.withOperationName("EventStreamOperationWithOnlyOutput")
478479
.withMarshaller(new EventStreamOperationWithOnlyOutputRequestMarshaller(protocolFactory))
479480
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
481+
.withMetricCollector(apiCallMetricCollector)
480482
.withInput(eventStreamOperationWithOnlyOutputRequest), restAsyncResponseTransformer);
481483
AwsRequestOverrideConfiguration requestOverrideConfig = eventStreamOperationWithOnlyOutputRequest
482484
.overrideConfiguration().orElse(null);
@@ -547,7 +549,7 @@ public CompletableFuture<GetWithoutRequiredMembersResponse> getWithoutRequiredMe
547549
.withOperationName("GetWithoutRequiredMembers")
548550
.withMarshaller(new GetWithoutRequiredMembersRequestMarshaller(protocolFactory))
549551
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
550-
.withInput(getWithoutRequiredMembersRequest));
552+
.withMetricCollector(apiCallMetricCollector).withInput(getWithoutRequiredMembersRequest));
551553
AwsRequestOverrideConfiguration requestOverrideConfig = getWithoutRequiredMembersRequest.overrideConfiguration()
552554
.orElse(null);
553555
executeFuture.whenComplete((r, e) -> {
@@ -605,7 +607,7 @@ public CompletableFuture<PaginatedOperationWithResultKeyResponse> paginatedOpera
605607
.withOperationName("PaginatedOperationWithResultKey")
606608
.withMarshaller(new PaginatedOperationWithResultKeyRequestMarshaller(protocolFactory))
607609
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
608-
.withInput(paginatedOperationWithResultKeyRequest));
610+
.withMetricCollector(apiCallMetricCollector).withInput(paginatedOperationWithResultKeyRequest));
609611
AwsRequestOverrideConfiguration requestOverrideConfig = paginatedOperationWithResultKeyRequest
610612
.overrideConfiguration().orElse(null);
611613
executeFuture.whenComplete((r, e) -> {
@@ -740,7 +742,7 @@ public CompletableFuture<PaginatedOperationWithoutResultKeyResponse> paginatedOp
740742
.withOperationName("PaginatedOperationWithoutResultKey")
741743
.withMarshaller(new PaginatedOperationWithoutResultKeyRequestMarshaller(protocolFactory))
742744
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
743-
.withInput(paginatedOperationWithoutResultKeyRequest));
745+
.withMetricCollector(apiCallMetricCollector).withInput(paginatedOperationWithoutResultKeyRequest));
744746
AwsRequestOverrideConfiguration requestOverrideConfig = paginatedOperationWithoutResultKeyRequest
745747
.overrideConfiguration().orElse(null);
746748
executeFuture.whenComplete((r, e) -> {
@@ -882,8 +884,8 @@ public CompletableFuture<StreamingInputOperationResponse> streamingInputOperatio
882884
AsyncStreamingRequestMarshaller.builder()
883885
.delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory))
884886
.asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler)
885-
.withErrorResponseHandler(errorResponseHandler).withAsyncRequestBody(requestBody)
886-
.withInput(streamingInputOperationRequest));
887+
.withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector)
888+
.withAsyncRequestBody(requestBody).withInput(streamingInputOperationRequest));
887889
AwsRequestOverrideConfiguration requestOverrideConfig = streamingInputOperationRequest.overrideConfiguration()
888890
.orElse(null);
889891
executeFuture.whenComplete((r, e) -> {
@@ -958,8 +960,8 @@ public <ReturnT> CompletableFuture<ReturnT> streamingInputOutputOperation(
958960
new StreamingInputOutputOperationRequestMarshaller(protocolFactory))
959961
.asyncRequestBody(requestBody).transferEncoding(true).build())
960962
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
961-
.withAsyncRequestBody(requestBody).withInput(streamingInputOutputOperationRequest),
962-
asyncResponseTransformer);
963+
.withMetricCollector(apiCallMetricCollector).withAsyncRequestBody(requestBody)
964+
.withInput(streamingInputOutputOperationRequest), asyncResponseTransformer);
963965
AwsRequestOverrideConfiguration requestOverrideConfig = streamingInputOutputOperationRequest.overrideConfiguration()
964966
.orElse(null);
965967
executeFuture.whenComplete((r, e) -> {
@@ -1028,7 +1030,8 @@ public <ReturnT> CompletableFuture<ReturnT> streamingOutputOperation(
10281030
.withOperationName("StreamingOutputOperation")
10291031
.withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory))
10301032
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
1031-
.withInput(streamingOutputOperationRequest), asyncResponseTransformer);
1033+
.withMetricCollector(apiCallMetricCollector).withInput(streamingOutputOperationRequest),
1034+
asyncResponseTransformer);
10321035
AwsRequestOverrideConfiguration requestOverrideConfig = streamingOutputOperationRequest.overrideConfiguration()
10331036
.orElse(null);
10341037
executeFuture.whenComplete((r, e) -> {

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-endpoint-discovery-async.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ public CompletableFuture<DescribeEndpointsResponse> describeEndpoints(DescribeEn
113113
.withOperationName("DescribeEndpoints")
114114
.withMarshaller(new DescribeEndpointsRequestMarshaller(protocolFactory))
115115
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
116-
.withInput(describeEndpointsRequest));
116+
.withMetricCollector(apiCallMetricCollector).withInput(describeEndpointsRequest));
117117
AwsRequestOverrideConfiguration requestOverrideConfig = describeEndpointsRequest.overrideConfiguration().orElse(null);
118118
executeFuture.whenComplete((r, e) -> {
119119
Optional<MetricPublisher> metricPublisher = MetricUtils.resolvePublisher(clientConfiguration,
@@ -176,7 +176,8 @@ public CompletableFuture<TestDiscoveryIdentifiersRequiredResponse> testDiscovery
176176
.withOperationName("TestDiscoveryIdentifiersRequired")
177177
.withMarshaller(new TestDiscoveryIdentifiersRequiredRequestMarshaller(protocolFactory))
178178
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
179-
.discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryIdentifiersRequiredRequest));
179+
.withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint)
180+
.withInput(testDiscoveryIdentifiersRequiredRequest));
180181
AwsRequestOverrideConfiguration requestOverrideConfig = testDiscoveryIdentifiersRequiredRequest
181182
.overrideConfiguration().orElse(null);
182183
executeFuture.whenComplete((r, e) -> {
@@ -239,7 +240,8 @@ public CompletableFuture<TestDiscoveryOptionalResponse> testDiscoveryOptional(
239240
.withOperationName("TestDiscoveryOptional")
240241
.withMarshaller(new TestDiscoveryOptionalRequestMarshaller(protocolFactory))
241242
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
242-
.discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryOptionalRequest));
243+
.withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint)
244+
.withInput(testDiscoveryOptionalRequest));
243245
AwsRequestOverrideConfiguration requestOverrideConfig = testDiscoveryOptionalRequest.overrideConfiguration().orElse(
244246
null);
245247
executeFuture.whenComplete((r, e) -> {
@@ -302,7 +304,8 @@ public CompletableFuture<TestDiscoveryRequiredResponse> testDiscoveryRequired(
302304
.withOperationName("TestDiscoveryRequired")
303305
.withMarshaller(new TestDiscoveryRequiredRequestMarshaller(protocolFactory))
304306
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
305-
.discoveredEndpoint(cachedEndpoint).withInput(testDiscoveryRequiredRequest));
307+
.withMetricCollector(apiCallMetricCollector).discoveredEndpoint(cachedEndpoint)
308+
.withInput(testDiscoveryRequiredRequest));
306309
AwsRequestOverrideConfiguration requestOverrideConfig = testDiscoveryRequiredRequest.overrideConfiguration().orElse(
307310
null);
308311
executeFuture.whenComplete((r, e) -> {

codegen/src/test/resources/software/amazon/awssdk/codegen/poet/client/test-query-async-client-class.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,8 @@ public CompletableFuture<APostOperationResponse> aPostOperation(APostOperationRe
109109
.withOperationName("APostOperation")
110110
.withMarshaller(new APostOperationRequestMarshaller(protocolFactory))
111111
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
112-
.hostPrefixExpression(resolvedHostExpression).withInput(aPostOperationRequest));
112+
.withMetricCollector(apiCallMetricCollector).hostPrefixExpression(resolvedHostExpression)
113+
.withInput(aPostOperationRequest));
113114
AwsRequestOverrideConfiguration requestOverrideConfig = aPostOperationRequest.overrideConfiguration().orElse(null);
114115
executeFuture.whenComplete((r, e) -> {
115116
Optional<MetricPublisher> metricPublisher = MetricUtils.resolvePublisher(clientConfiguration,
@@ -166,7 +167,7 @@ public CompletableFuture<APostOperationWithOutputResponse> aPostOperationWithOut
166167
.withOperationName("APostOperationWithOutput")
167168
.withMarshaller(new APostOperationWithOutputRequestMarshaller(protocolFactory))
168169
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
169-
.withInput(aPostOperationWithOutputRequest));
170+
.withMetricCollector(apiCallMetricCollector).withInput(aPostOperationWithOutputRequest));
170171
AwsRequestOverrideConfiguration requestOverrideConfig = aPostOperationWithOutputRequest.overrideConfiguration()
171172
.orElse(null);
172173
executeFuture.whenComplete((r, e) -> {
@@ -227,8 +228,8 @@ public CompletableFuture<StreamingInputOperationResponse> streamingInputOperatio
227228
AsyncStreamingRequestMarshaller.builder()
228229
.delegateMarshaller(new StreamingInputOperationRequestMarshaller(protocolFactory))
229230
.asyncRequestBody(requestBody).build()).withResponseHandler(responseHandler)
230-
.withErrorResponseHandler(errorResponseHandler).withAsyncRequestBody(requestBody)
231-
.withInput(streamingInputOperationRequest));
231+
.withErrorResponseHandler(errorResponseHandler).withMetricCollector(apiCallMetricCollector)
232+
.withAsyncRequestBody(requestBody).withInput(streamingInputOperationRequest));
232233
AwsRequestOverrideConfiguration requestOverrideConfig = streamingInputOperationRequest.overrideConfiguration()
233234
.orElse(null);
234235
executeFuture.whenComplete((r, e) -> {
@@ -288,7 +289,8 @@ public <ReturnT> CompletableFuture<ReturnT> streamingOutputOperation(
288289
.withOperationName("StreamingOutputOperation")
289290
.withMarshaller(new StreamingOutputOperationRequestMarshaller(protocolFactory))
290291
.withResponseHandler(responseHandler).withErrorResponseHandler(errorResponseHandler)
291-
.withInput(streamingOutputOperationRequest), asyncResponseTransformer);
292+
.withMetricCollector(apiCallMetricCollector).withInput(streamingOutputOperationRequest),
293+
asyncResponseTransformer);
292294
AwsRequestOverrideConfiguration requestOverrideConfig = streamingOutputOperationRequest.overrideConfiguration()
293295
.orElse(null);
294296
executeFuture.whenComplete((r, e) -> {

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/NoOpMetricCollector.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,14 @@
1616
package software.amazon.awssdk.metrics;
1717

1818
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.utils.Logger;
1920

2021
/**
2122
* A metric collector that doesn't do anything.
2223
*/
2324
@SdkPublicApi
2425
public final class NoOpMetricCollector implements MetricCollector {
26+
private static final Logger log = Logger.loggerFor(NoOpMetricCollector.class);
2527
private static final NoOpMetricCollector INSTANCE = new NoOpMetricCollector();
2628

2729
private NoOpMetricCollector() {
@@ -34,6 +36,7 @@ public String name() {
3436

3537
@Override
3638
public <T> void reportMetric(SdkMetric<T> metric, T data) {
39+
log.trace(() -> "Metrics reported: " + data);
3740
}
3841

3942
@Override

core/metrics-spi/src/main/java/software/amazon/awssdk/metrics/internal/DefaultMetricCollection.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public final class DefaultMetricCollection implements MetricCollection {
3535

3636

3737
public DefaultMetricCollection(String name, Map<SdkMetric<?>,
38-
List<MetricRecord<?>>> metrics,
38+
List<MetricRecord<?>>> metrics,
3939
List<MetricCollection> children) {
4040
this.name = name;
4141
this.metrics = metrics;
@@ -68,8 +68,8 @@ public List<MetricCollection> children() {
6868
@Override
6969
public Iterator<MetricRecord<?>> iterator() {
7070
return metrics.values().stream()
71-
.flatMap(List::stream)
72-
.iterator();
71+
.flatMap(List::stream)
72+
.iterator();
7373
}
7474

7575
@Override

0 commit comments

Comments
 (0)