Skip to content
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

add feature tracking for cbor protocol #2821

Merged
merged 2 commits into from
Oct 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions aws/middleware/user_agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ const (
UserAgentFeatureS3ExpressBucket = "J"
UserAgentFeatureS3AccessGrants = "K" // not yet implemented
UserAgentFeatureGZIPRequestCompression = "L"
UserAgentFeatureProtocolRPCV2CBOR = "M"
)

// RequestUserAgent is a build middleware that set the User-Agent for the request.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package software.amazon.smithy.aws.go.codegen.customization;

import static software.amazon.smithy.go.codegen.GoWriter.goTemplate;
import static software.amazon.smithy.go.codegen.SymbolUtils.buildPackageSymbol;

import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import software.amazon.smithy.aws.go.codegen.AwsGoDependency;
import software.amazon.smithy.codegen.core.Symbol;
import software.amazon.smithy.go.codegen.GoCodegenContext;
import software.amazon.smithy.go.codegen.GoWriter;
import software.amazon.smithy.go.codegen.SmithyGoTypes;
import software.amazon.smithy.go.codegen.integration.GoIntegration;
import software.amazon.smithy.go.codegen.integration.MiddlewareRegistrar;
import software.amazon.smithy.go.codegen.integration.RuntimeClientPlugin;
import software.amazon.smithy.model.Model;
import software.amazon.smithy.model.shapes.ServiceShape;
import software.amazon.smithy.protocol.traits.Rpcv2CborTrait;

/**
* Adds user agent tracking for basic features - i.e. simple model-based ones that do not require any additional in-code
* checks, such as a particular protocol.
*/
public class BasicUserAgentFeatures implements GoIntegration {
private static final List<Feature> FEATURES = List.of(
new Feature("ProtocolRPCV2CBOR", (model, service) -> service.hasTrait(Rpcv2CborTrait.class))
);

@Override
public List<RuntimeClientPlugin> getClientPlugins() {
return FEATURES.stream().map(Feature::getPlugin).toList();
}

@Override
public void writeAdditionalFiles(GoCodegenContext ctx) {
var model = ctx.model();
var service = ctx.settings().getService(model);
ctx.writerDelegator().useFileWriter("api_client.go", ctx.settings().getModuleName(),
GoWriter.ChainWritable.of(
FEATURES.stream()
.filter(it -> it.servicePredicate.test(model, service))
.map(Feature::getAddMiddleware)
.toList()
).compose());
}

private static final class Feature {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While this is named Feature it's really only for CBOR metrics due to the getAddMiddleware(). We should either make the CBOR metrics extend from this or rename this

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about the getAddMiddleware constrains it to CBOR? The feature ID variant is passed in.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, my point is that the class name is generic but the functionality is not, so we should either make it generic or name it accordingly

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The functionality of Feature is generic though - it's just that CBOR is the only thing using it. You could add any other static feature to the FEATURES list in the future.

e.g.

    private static final List<Feature> FEATURES = List.of(
           new Feature("ProtocolRPCV2CBOR", (model, service) -> service.hasTrait(Rpcv2CborTrait.class)),
           new Feature("ProtocolSparrowhawk", (model, service) -> etc())
    );

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK I'm dumb, I kept reading the functionality of getAddMiddleware as specific to CBOR since the code always calls the same middleware, but this is exactly what we want since this middleware is generic

public final Symbol featureId;
public final BiPredicate<Model, ServiceShape> servicePredicate;

public Feature(String id, BiPredicate<Model, ServiceShape> servicePredicate) {
this.featureId = AwsGoDependency.AWS_MIDDLEWARE.constSymbol("UserAgentFeature" + id);
this.servicePredicate = servicePredicate;
}

public RuntimeClientPlugin getPlugin() {
return RuntimeClientPlugin.builder()
.servicePredicate(servicePredicate)
.registerMiddleware(
MiddlewareRegistrar.builder()
.resolvedFunction(buildPackageSymbol("add" + featureId.getName()))
.useClientOptions()
.build()
)
.build();
}

public GoWriter.Writable getAddMiddleware() {
return goTemplate("""
func add$featureName:L(stack $stack:P, options Options) error {
ua, err := getOrAddRequestUserAgent(stack)
if err != nil {
return err
}

ua.AddUserAgentFeature($featureEnum:T)
return nil
}
""",
Map.of(
"stack", SmithyGoTypes.Middleware.Stack,
"featureName", featureId.getName(),
"featureEnum", featureId
));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,4 @@ software.amazon.smithy.aws.go.codegen.customization.RetryModeUserAgent
software.amazon.smithy.aws.go.codegen.customization.RequestCompressionUserAgent
software.amazon.smithy.aws.go.codegen.customization.s3.ExpressUserAgent
software.amazon.smithy.aws.go.codegen.customization.BackfillRequiredTrait
software.amazon.smithy.aws.go.codegen.customization.BasicUserAgentFeatures
10 changes: 10 additions & 0 deletions internal/protocoltest/smithyrpcv2cbor/api_client.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions internal/protocoltest/smithyrpcv2cbor/api_op_Float16.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading