Skip to content

Commit d7f9435

Browse files
committed
Add setup code for API call metrics for sync
1 parent 299ed84 commit d7f9435

File tree

22 files changed

+679
-338
lines changed

22 files changed

+679
-338
lines changed

codegen/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
<artifactId>http-client-spi</artifactId>
5858
<version>${awsjavasdk.version}</version>
5959
</dependency>
60+
<dependency>
61+
<groupId>software.amazon.awssdk</groupId>
62+
<artifactId>metrics-spi</artifactId>
63+
<version>${awsjavasdk.version}</version>
64+
</dependency>
6065
<dependency>
6166
<groupId>software.amazon.awssdk</groupId>
6267
<artifactId>regions</artifactId>

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/AsyncClientInterface.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import software.amazon.awssdk.core.SdkClient;
4848
import software.amazon.awssdk.core.async.AsyncRequestBody;
4949
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
50+
import software.amazon.awssdk.metrics.MetricCollector;
5051
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;
5152

5253
public class AsyncClientInterface implements ClassSpec {
@@ -284,6 +285,12 @@ private MethodSpec traditionalMethod(OperationModel opModel) {
284285
.addParameter(requestType, opModel.getInput().getVariableName())
285286
.addJavadoc(opModel.getDocs(model, ClientType.ASYNC));
286287

288+
289+
String metricCollectorName = "apiCallMetricCollector";
290+
291+
builder.addStatement("$1T $2N = $1T.create($3S)",
292+
MetricCollector.class, metricCollectorName, "ApiCall");
293+
287294
if (opModel.hasStreamingInput()) {
288295
builder.addParameter(ClassName.get(AsyncRequestBody.class), "requestBody");
289296
} else if (opModel.hasEventStreamInput()) {

codegen/src/main/java/software/amazon/awssdk/codegen/poet/client/SyncClientClass.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import javax.lang.model.element.Modifier;
3333
import software.amazon.awssdk.annotations.SdkInternalApi;
3434
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
35+
import software.amazon.awssdk.awscore.metrics.AwsCoreMetric;
3536
import software.amazon.awssdk.codegen.docs.SimpleMethodOverload;
3637
import software.amazon.awssdk.codegen.emitters.GeneratorTaskParams;
3738
import software.amazon.awssdk.codegen.model.config.customization.UtilitiesMethod;
@@ -52,6 +53,8 @@
5253
import software.amazon.awssdk.core.client.handler.SyncClientHandler;
5354
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRefreshCache;
5455
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRequest;
56+
import software.amazon.awssdk.metrics.MetricCollector;
57+
import software.amazon.awssdk.metrics.MetricPublisher;
5558

5659
//TODO Make SyncClientClass extend SyncClientInterface (similar to what we do in AsyncClientClass)
5760
public class SyncClientClass implements ClassSpec {
@@ -186,7 +189,28 @@ private List<MethodSpec> operationMethodSpecs(OperationModel opModel) {
186189
method.endControlFlow();
187190
}
188191

189-
method.addCode(protocolSpec.executionHandler(opModel));
192+
String metricCollectorName = "apiCallMetricCollector";
193+
194+
method.addStatement("$1T $2N = $1T.create($3S)",
195+
MetricCollector.class, metricCollectorName, "ApiCall");
196+
197+
method.addStatement("$N.reportMetric($T.$L, $S)", metricCollectorName, AwsCoreMetric.class, "SERVICE_ID",
198+
model.getMetadata().getServiceId());
199+
method.addStatement("$N.reportMetric($T.$L, $S)", metricCollectorName, AwsCoreMetric.class, "OPERATION_NAME",
200+
opModel.getOperationName());
201+
202+
String publisherName = "metricPublisher";
203+
204+
method.beginControlFlow("try")
205+
.addCode(protocolSpec.executionHandler(opModel))
206+
.endControlFlow()
207+
.beginControlFlow("finally")
208+
.addStatement("$T $N = clientConfiguration.option($T.$L)",
209+
MetricPublisher.class, publisherName, SdkClientOption.class, "METRIC_PUBLISHER")
210+
.beginControlFlow("if ($N != null)", publisherName)
211+
.addStatement("$N.publish($N.collect())", publisherName, metricCollectorName)
212+
.endControlFlow()
213+
.endControlFlow();
190214

191215
methods.add(method.build());
192216

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
178178
"errorResponseHandler",
179179
opModel.getInput().getVariableName());
180180

181+
codeBlock.add(".withMetricCollector($N)", "apiCallMetricCollector");
182+
181183
if (opModel.hasStreamingInput()) {
182184
codeBlock.add(".withRequestBody(requestBody)")
183185
.add(".withMarshaller($L)", syncStreamingMarshaller(model, opModel, marshaller));

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ public CodeBlock executionHandler(OperationModel opModel) {
113113
"responseHandler",
114114
"errorResponseHandler",
115115
opModel.getInput().getVariableName());
116+
117+
codeBlock.add(".withMetricCollector($N)", "apiCallMetricCollector");
118+
116119
if (opModel.hasStreamingInput()) {
117120
return codeBlock.add(".withRequestBody(requestBody)")
118121
.add(".withMarshaller($L));", syncStreamingMarshaller(intermediateModel, opModel, marshaller))

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,9 @@ public CodeBlock executionHandler(OperationModel opModel) {
120120
opModel.getOperationName(),
121121
"responseHandler",
122122
opModel.getInput().getVariableName());
123+
124+
codeBlock.add(".withMetricCollector($N)", "apiCallMetricCollector");
125+
123126
if (opModel.hasStreamingInput()) {
124127
return codeBlock.add(".withRequestBody(requestBody)")
125128
.add(".withMarshaller($L));", syncStreamingMarshaller(intermediateModel, opModel, marshaller))

0 commit comments

Comments
 (0)