Skip to content

Add setup code for API call metrics for sync #1872

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 5, 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
5 changes: 5 additions & 0 deletions codegen/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@
<artifactId>http-client-spi</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>metrics-spi</artifactId>
<version>${awsjavasdk.version}</version>
</dependency>
<dependency>
<groupId>software.amazon.awssdk</groupId>
<artifactId>regions</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import software.amazon.awssdk.core.SdkClient;
import software.amazon.awssdk.core.async.AsyncRequestBody;
import software.amazon.awssdk.core.async.AsyncResponseTransformer;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.regions.providers.DefaultAwsRegionProviderChain;

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


String metricCollectorName = "apiCallMetricCollector";

builder.addStatement("$1T $2N = $1T.create($3S)",
MetricCollector.class, metricCollectorName, "ApiCall");

if (opModel.hasStreamingInput()) {
builder.addParameter(ClassName.get(AsyncRequestBody.class), "requestBody");
} else if (opModel.hasEventStreamInput()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import javax.lang.model.element.Modifier;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
import software.amazon.awssdk.awscore.metrics.AwsCoreMetric;
import software.amazon.awssdk.codegen.docs.SimpleMethodOverload;
import software.amazon.awssdk.codegen.emitters.GeneratorTaskParams;
import software.amazon.awssdk.codegen.model.config.customization.UtilitiesMethod;
Expand All @@ -52,6 +53,8 @@
import software.amazon.awssdk.core.client.handler.SyncClientHandler;
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRefreshCache;
import software.amazon.awssdk.core.endpointdiscovery.EndpointDiscoveryRequest;
import software.amazon.awssdk.metrics.MetricCollector;
import software.amazon.awssdk.metrics.MetricPublisher;

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

method.addCode(protocolSpec.executionHandler(opModel));
String metricCollectorName = "apiCallMetricCollector";

method.addStatement("$1T $2N = $1T.create($3S)",
MetricCollector.class, metricCollectorName, "ApiCall");

method.addStatement("$N.reportMetric($T.$L, $S)", metricCollectorName, AwsCoreMetric.class, "SERVICE_ID",
model.getMetadata().getServiceId());
method.addStatement("$N.reportMetric($T.$L, $S)", metricCollectorName, AwsCoreMetric.class, "OPERATION_NAME",
opModel.getOperationName());

String publisherName = "metricPublisher";

method.beginControlFlow("try")
.addCode(protocolSpec.executionHandler(opModel))
.endControlFlow()
.beginControlFlow("finally")
.addStatement("$T $N = clientConfiguration.option($T.$L)",
MetricPublisher.class, publisherName, SdkClientOption.class, "METRIC_PUBLISHER")
.beginControlFlow("if ($N != null)", publisherName)
.addStatement("$N.publish($N.collect())", publisherName, metricCollectorName)
.endControlFlow()
.endControlFlow();

methods.add(method.build());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,8 @@ public CodeBlock executionHandler(OperationModel opModel) {
"errorResponseHandler",
opModel.getInput().getVariableName());

codeBlock.add(".withMetricCollector($N)", "apiCallMetricCollector");

if (opModel.hasStreamingInput()) {
codeBlock.add(".withRequestBody(requestBody)")
.add(".withMarshaller($L)", syncStreamingMarshaller(model, opModel, marshaller));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,9 @@ public CodeBlock executionHandler(OperationModel opModel) {
"responseHandler",
"errorResponseHandler",
opModel.getInput().getVariableName());

codeBlock.add(".withMetricCollector($N)", "apiCallMetricCollector");

if (opModel.hasStreamingInput()) {
return codeBlock.add(".withRequestBody(requestBody)")
.add(".withMarshaller($L));", syncStreamingMarshaller(intermediateModel, opModel, marshaller))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ public CodeBlock executionHandler(OperationModel opModel) {
opModel.getOperationName(),
"responseHandler",
opModel.getInput().getVariableName());

codeBlock.add(".withMetricCollector($N)", "apiCallMetricCollector");

if (opModel.hasStreamingInput()) {
return codeBlock.add(".withRequestBody(requestBody)")
.add(".withMarshaller($L));", syncStreamingMarshaller(intermediateModel, opModel, marshaller))
Expand Down
Loading