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

Adding user-agent metrics for flexible checksums #3851

Merged
merged 3 commits into from
Oct 2, 2024
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
6 changes: 3 additions & 3 deletions aws/rust-runtime/Cargo.lock

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

59 changes: 58 additions & 1 deletion aws/rust-runtime/aws-inlineable/src/http_request_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use aws_runtime::{auth::SigV4OperationSigningConfig, content_encoding::header_va
use aws_sigv4::http_request::SignableBody;
use aws_smithy_checksums::ChecksumAlgorithm;
use aws_smithy_checksums::{body::calculate, http::HttpChecksum};
use aws_smithy_runtime::client::sdk_feature::SmithySdkFeature;
use aws_smithy_runtime_api::box_error::BoxError;
use aws_smithy_runtime_api::client::interceptors::context::{
BeforeSerializationInterceptorContextMut, BeforeTransmitInterceptorContextMut, Input,
Expand Down Expand Up @@ -152,7 +153,7 @@ where
/// Calculate a checksum and modify the request to include the checksum as a header
/// (for in-memory request bodies) or a trailer (for streaming request bodies).
/// Streaming bodies must be sized or this will return an error.
fn modify_before_signing(
fn modify_before_retry_loop(
&self,
context: &mut BeforeTransmitInterceptorContextMut<'_>,
_runtime_components: &RuntimeComponents,
Expand Down Expand Up @@ -187,12 +188,68 @@ where
let checksum_algorithm = incorporate_custom_default(state.checksum_algorithm, cfg)
.unwrap_or(ChecksumAlgorithm::Crc32);

// Set the user-agent metric for the selected checksum algorithm
match checksum_algorithm {
ChecksumAlgorithm::Crc32 => {
cfg.interceptor_state()
.store_append(SmithySdkFeature::FlexibleChecksumsReqCrc32);
}
ChecksumAlgorithm::Crc32c => {
cfg.interceptor_state()
.store_append(SmithySdkFeature::FlexibleChecksumsReqCrc32c);
}
#[allow(deprecated)]
ChecksumAlgorithm::Md5 => {
tracing::warn!(more_info = "Unsupported ChecksumAlgorithm MD5 set");
}
ChecksumAlgorithm::Sha1 => {
cfg.interceptor_state()
.store_append(SmithySdkFeature::FlexibleChecksumsReqSha1);
}
ChecksumAlgorithm::Sha256 => {
cfg.interceptor_state()
.store_append(SmithySdkFeature::FlexibleChecksumsReqSha256);
}
unsupported => tracing::warn!(
more_info = "Unsupported value of ChecksumAlgorithm detected when setting user-agent metrics",
unsupported = ?unsupported),
}

let request = context.request_mut();
add_checksum_for_request_body(request, checksum_algorithm, cfg)?;
}

Ok(())
}

/// Set the user-agent metrics for `RequestChecksumCalculation` here to avoid ownership issues
/// with the mutable borrow of cfg in `modify_before_signing`
fn read_after_serialization(
&self,
_context: &aws_smithy_runtime_api::client::interceptors::context::BeforeTransmitInterceptorContextRef<'_>,
_runtime_components: &RuntimeComponents,
cfg: &mut ConfigBag,
) -> Result<(), BoxError> {
let request_checksum_calculation = cfg
.load::<RequestChecksumCalculation>()
.unwrap_or(&RequestChecksumCalculation::WhenSupported);

match request_checksum_calculation {
RequestChecksumCalculation::WhenSupported => {
cfg.interceptor_state()
.store_append(SmithySdkFeature::FlexibleChecksumsReqWhenSupported);
}
RequestChecksumCalculation::WhenRequired => {
cfg.interceptor_state()
.store_append(SmithySdkFeature::FlexibleChecksumsReqWhenRequired);
}
unsupported => tracing::warn!(
more_info = "Unsupported value of RequestChecksumCalculation when setting user-agent metrics",
unsupported = ?unsupported),
};

Ok(())
}
}

fn incorporate_custom_default(
Expand Down
20 changes: 20 additions & 0 deletions aws/rust-runtime/aws-inlineable/src/http_response_checksum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
//! Interceptor for handling Smithy `@httpChecksum` response checksumming

use aws_smithy_checksums::ChecksumAlgorithm;
use aws_smithy_runtime::client::sdk_feature::SmithySdkFeature;
use aws_smithy_runtime_api::box_error::BoxError;
use aws_smithy_runtime_api::client::interceptors::context::{
BeforeDeserializationInterceptorContextMut, BeforeSerializationInterceptorContextMut, Input,
Expand Down Expand Up @@ -78,6 +79,25 @@ where
layer.store_put(ResponseChecksumInterceptorState { validation_enabled });
cfg.push_layer(layer);

let response_checksum_validation = cfg
.load::<ResponseChecksumValidation>()
.unwrap_or(&ResponseChecksumValidation::WhenSupported);

// Set the user-agent feature metric for the response checksum config
match response_checksum_validation {
ResponseChecksumValidation::WhenSupported => {
cfg.interceptor_state()
.store_append(SmithySdkFeature::FlexibleChecksumsResWhenSupported);
}
ResponseChecksumValidation::WhenRequired => {
cfg.interceptor_state()
.store_append(SmithySdkFeature::FlexibleChecksumsResWhenRequired);
}
unsupported => tracing::warn!(
more_info = "Unsupported value of ResponseChecksumValidation when setting user-agent metrics",
unsupported = ?unsupported),
};

Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion aws/rust-runtime/aws-runtime/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "aws-runtime"
version = "1.4.3"
version = "1.4.4"
authors = ["AWS Rust SDK Team <aws-sdk-rust@amazon.com>"]
description = "Runtime support code for the AWS SDK. This crate isn't intended to be used directly."
edition = "2021"
Expand Down
41 changes: 39 additions & 2 deletions aws/rust-runtime/aws-runtime/src/user_agent/metrics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,17 @@ iterable_enum!(
AccountIdModeDisabled,
AccountIdModeRequired,
Sigv4aSigning,
ResolvedAccountId
ResolvedAccountId,
FlexibleChecksumsReqCrc32,
FlexibleChecksumsReqCrc32c,
FlexibleChecksumsReqCrc64,
FlexibleChecksumsReqSha1,
FlexibleChecksumsReqSha256,
FlexibleChecksumsReqWhenSupported,
FlexibleChecksumsReqWhenRequired,
FlexibleChecksumsResWhenSupported,
FlexibleChecksumsResWhenRequired,
DdbMapper
);

pub(crate) trait ProvideBusinessMetric {
Expand All @@ -141,6 +151,23 @@ impl ProvideBusinessMetric for SmithySdkFeature {
Paginator => Some(BusinessMetric::Paginator),
GzipRequestCompression => Some(BusinessMetric::GzipRequestCompression),
ProtocolRpcV2Cbor => Some(BusinessMetric::ProtocolRpcV2Cbor),
FlexibleChecksumsReqCrc32 => Some(BusinessMetric::FlexibleChecksumsReqCrc32),
ysaito1001 marked this conversation as resolved.
Show resolved Hide resolved
FlexibleChecksumsReqCrc32c => Some(BusinessMetric::FlexibleChecksumsReqCrc32c),
FlexibleChecksumsReqCrc64 => Some(BusinessMetric::FlexibleChecksumsReqCrc64),
FlexibleChecksumsReqSha1 => Some(BusinessMetric::FlexibleChecksumsReqSha1),
FlexibleChecksumsReqSha256 => Some(BusinessMetric::FlexibleChecksumsReqSha256),
FlexibleChecksumsReqWhenSupported => {
Some(BusinessMetric::FlexibleChecksumsReqWhenSupported)
}
FlexibleChecksumsReqWhenRequired => {
Some(BusinessMetric::FlexibleChecksumsReqWhenRequired)
}
FlexibleChecksumsResWhenSupported => {
Some(BusinessMetric::FlexibleChecksumsResWhenSupported)
}
FlexibleChecksumsResWhenRequired => {
Some(BusinessMetric::FlexibleChecksumsResWhenRequired)
}
otherwise => {
// This may occur if a customer upgrades only the `aws-smithy-runtime-api` crate
// while continuing to use an outdated version of an SDK crate or the `aws-runtime`
Expand Down Expand Up @@ -250,7 +277,17 @@ mod tests {
"ACCOUNT_ID_MODE_DISABLED": "Q",
"ACCOUNT_ID_MODE_REQUIRED": "R",
"SIGV4A_SIGNING": "S",
"RESOLVED_ACCOUNT_ID": "T"
"RESOLVED_ACCOUNT_ID": "T",
"FLEXIBLE_CHECKSUMS_REQ_CRC32": "U",
"FLEXIBLE_CHECKSUMS_REQ_CRC32C": "V",
"FLEXIBLE_CHECKSUMS_REQ_CRC64": "W",
"FLEXIBLE_CHECKSUMS_REQ_SHA1": "X",
"FLEXIBLE_CHECKSUMS_REQ_SHA256": "Y",
"FLEXIBLE_CHECKSUMS_REQ_WHEN_SUPPORTED": "Z",
"FLEXIBLE_CHECKSUMS_REQ_WHEN_REQUIRED": "a",
"FLEXIBLE_CHECKSUMS_RES_WHEN_SUPPORTED": "b",
"FLEXIBLE_CHECKSUMS_RES_WHEN_REQUIRED": "c",
"DDB_MAPPER": "d"
}
"#;

Expand Down
21 changes: 14 additions & 7 deletions aws/rust-runtime/aws-runtime/src/user_agent/test_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,8 @@ static RE: Lazy<Regex> = Lazy::new(|| Regex::new(r"m/([A-Za-z0-9+/=_,-]+)").unwr
/// Refer to the end of the parent module file `user_agent.rs` for the complete ABNF specification
/// of `business-metrics`.
pub fn assert_ua_contains_metric_values(user_agent: &str, values: &[&str]) {
match RE.find(user_agent) {
Some(matched) => {
let csv = matched
.as_str()
.strip_prefix("m/")
.expect("prefix `m/` is guaranteed to exist by regex match");
let metrics: Vec<&str> = csv.split(',').collect();
match extract_ua_values(user_agent) {
Some(metrics) => {
let mut missed = vec![];

for value in values.iter() {
Expand All @@ -43,6 +38,18 @@ pub fn assert_ua_contains_metric_values(user_agent: &str, values: &[&str]) {
}
}

/// Extract the metric values from the `user_agent` string
pub fn extract_ua_values(user_agent: &str) -> Option<Vec<&str>> {
RE.find(user_agent).map(|matched| {
matched
.as_str()
.strip_prefix("m/")
.expect("prefix `m/` is guaranteed to exist by regex match")
.split(',')
.collect()
})
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
Loading
Loading