Skip to content

Commit

Permalink
add retry mode business metric tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
Velfi committed Dec 17, 2024
1 parent 035fd72 commit 16a92c4
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,123 @@ class UserAgentDecoratorTest {
}
}

@Test
fun `it emits business metrics for retry modes`() {
val model =
"""
namespace test
use aws.auth#sigv4
use aws.api#service
use aws.protocols#restJson1
use smithy.rules#endpointRuleSet
@auth([sigv4])
@sigv4(name: "dontcare")
@endpointRuleSet({
"version": "1.0",
"rules": [{ "type": "endpoint", "conditions": [], "endpoint": { "url": "https://example.com" } }],
"parameters": {}
})
@service(sdkId: "dontcare")
@restJson1
service TestService { version: "2023-01-01", operations: [SomeOperation] }
@http(uri: "/SomeOperation", method: "GET")
@optionalAuth
operation SomeOperation {
input: SomeInput,
output: SomeOutput
}
@input
structure SomeInput {}
@output
structure SomeOutput {}
""".asSmithyModel()

awsSdkIntegrationTest(model) { ctx, rustCrate ->
rustCrate.integrationTest("retry_mode_feature_tracker") {
val rc = ctx.runtimeConfig
val moduleName = ctx.moduleUseName()

rust(
"""
use $moduleName::config::{Region, retry::RetryConfig};
use $moduleName::{Client, Config};
""",
)

tokioTest("should_emit_metric_in_user_agent_standard_mode") {
rustTemplate(
"""
let (http_client, rcvr) = #{capture_request}(#{None});
let config = Config::builder()
.region(Region::new("us-east-1"))
.http_client(http_client.clone())
.retry_config(RetryConfig::standard())
.with_test_defaults()
.build();
let client = Client::from_conf(config);
let _ = client.some_operation().send().await;
let expected_req = rcvr.expect_request();
let user_agent = expected_req
.headers()
.get("x-amz-user-agent")
.unwrap();
#{assert_ua_contains_metric_values}(user_agent, &["E"]);
""",
*preludeScope,
"assert_ua_contains_metric_values" to
AwsRuntimeType.awsRuntimeTestUtil(rc)
.resolve("user_agent::test_util::assert_ua_contains_metric_values"),
"capture_request" to RuntimeType.captureRequest(rc),
"disable_interceptor" to
RuntimeType.smithyRuntimeApiClient(rc)
.resolve("client::interceptors::disable_interceptor"),
"UserAgentInterceptor" to
AwsRuntimeType.awsRuntime(rc)
.resolve("user_agent::UserAgentInterceptor"),
)
}

tokioTest("should_emit_metric_in_user_agent_adaptive_mode") {
rustTemplate(
"""
let (http_client, rcvr) = #{capture_request}(#{None});
let config = Config::builder()
.region(Region::new("us-east-1"))
.http_client(http_client.clone())
.retry_config(RetryConfig::adaptive())
.with_test_defaults()
.build();
let client = Client::from_conf(config);
let _ = client.some_operation().send().await;
let expected_req = rcvr.expect_request();
let user_agent = expected_req
.headers()
.get("x-amz-user-agent")
.unwrap();
#{assert_ua_contains_metric_values}(user_agent, &["F"]);
""",
*preludeScope,
"assert_ua_contains_metric_values" to
AwsRuntimeType.awsRuntimeTestUtil(rc)
.resolve("user_agent::test_util::assert_ua_contains_metric_values"),
"capture_request" to RuntimeType.captureRequest(rc),
"disable_interceptor" to
RuntimeType.smithyRuntimeApiClient(rc)
.resolve("client::interceptors::disable_interceptor"),
"UserAgentInterceptor" to
AwsRuntimeType.awsRuntime(rc)
.resolve("user_agent::UserAgentInterceptor"),
)
}
}
}
}

@Test
fun `it emits business metric for checksum usage`() {
val model =
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/

package software.amazon.smithy.rust.codegen.client.smithy.customizations

import software.amazon.smithy.rust.codegen.client.smithy.ClientCodegenContext
import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceRuntimePluginCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceRuntimePluginSection
import software.amazon.smithy.rust.codegen.core.rustlang.InlineDependency
import software.amazon.smithy.rust.codegen.core.rustlang.Writable
import software.amazon.smithy.rust.codegen.core.rustlang.rust
import software.amazon.smithy.rust.codegen.core.rustlang.writable
import software.amazon.smithy.rust.codegen.core.smithy.RuntimeType

class RetryModeFeatureTrackerRuntimePluginCustomization(codegenContext: ClientCodegenContext) :
ServiceRuntimePluginCustomization() {
private val runtimeConfig = codegenContext.runtimeConfig

override fun section(section: ServiceRuntimePluginSection): Writable =
writable {
when (section) {
is ServiceRuntimePluginSection.RegisterRuntimeComponents -> {
section.registerInterceptor(this) {
rust(
"#T::new()",
RuntimeType.forInlineDependency(
InlineDependency.sdkFeatureTracker(runtimeConfig),
).resolve("retry_mode::RetryModeFeatureTrackerInterceptor"),
)
}
}

else -> emptySection
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import software.amazon.smithy.rust.codegen.client.smithy.customizations.Resilien
import software.amazon.smithy.rust.codegen.client.smithy.customizations.RetryClassifierConfigCustomization
import software.amazon.smithy.rust.codegen.client.smithy.customizations.RetryClassifierOperationCustomization
import software.amazon.smithy.rust.codegen.client.smithy.customizations.RetryClassifierServiceRuntimePluginCustomization
import software.amazon.smithy.rust.codegen.client.smithy.customizations.RetryModeFeatureTrackerRuntimePluginCustomization
import software.amazon.smithy.rust.codegen.client.smithy.customizations.TimeSourceCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.OperationCustomization
import software.amazon.smithy.rust.codegen.client.smithy.generators.ServiceRuntimePluginCustomization
Expand Down Expand Up @@ -134,5 +135,6 @@ class RequiredCustomizations : ClientCodegenDecorator {
): List<ServiceRuntimePluginCustomization> =
baseCustomizations +
ConnectionPoisoningRuntimePluginCustomization(codegenContext) +
RetryClassifierServiceRuntimePluginCustomization(codegenContext)
RetryClassifierServiceRuntimePluginCustomization(codegenContext) +
RetryModeFeatureTrackerRuntimePluginCustomization(codegenContext)
}
40 changes: 20 additions & 20 deletions rust-runtime/Cargo.lock

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

Loading

0 comments on commit 16a92c4

Please sign in to comment.