Skip to content

Commit 4270d1e

Browse files
authored
fix: add app version to user agent (#590)
1 parent 659cf3c commit 4270d1e

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

crates/fig_aws_common/src/user_agent_override_interceptor.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ use tracing::warn;
2424
/// string. This is used in AWS CloudShell where they want to track usage by version.
2525
const AWS_TOOLING_USER_AGENT: &str = "AWS_TOOLING_USER_AGENT";
2626

27+
const VERSION_HEADER: &str = "Version";
28+
const VERSION_VALUE: &str = env!("CARGO_PKG_VERSION");
29+
2730
#[derive(Debug)]
2831
enum UserAgentOverrideInterceptorError {
2932
MissingApiMetadata,
@@ -96,6 +99,14 @@ impl Intercept for UserAgentOverrideInterceptor {
9699
let aws_tooling_user_agent = env.get(AWS_TOOLING_USER_AGENT);
97100
let mut ua = AwsUserAgent::new_from_environment(env, api_metadata.clone());
98101

102+
let ver = format!("{VERSION_HEADER}/{VERSION_VALUE}");
103+
match AdditionalMetadata::new(clean_metadata(&ver)) {
104+
Ok(md) => {
105+
ua.add_additional_metadata(md);
106+
},
107+
Err(err) => panic!("Failed to parse version: {err}"),
108+
};
109+
99110
let maybe_app_name = cfg.load::<AppName>();
100111
if let Some(app_name) = maybe_app_name {
101112
ua.set_app_name(app_name.clone());
@@ -186,6 +197,8 @@ mod tests {
186197
let ua = context.request().headers().get(USER_AGENT).unwrap();
187198
println!("User-Agent: {ua}");
188199
assert!(ua.contains(&format!("app/{APP_NAME_STR}")));
200+
assert!(ua.contains(VERSION_HEADER));
201+
assert!(ua.contains(VERSION_VALUE));
189202
}
190203

191204
#[test]
@@ -207,5 +220,7 @@ mod tests {
207220
assert!(ua.contains(&format!("app/{APP_NAME_STR}")));
208221
assert!(ua.contains("exec-env/CloudShell"));
209222
assert!(ua.contains("md/AWS-CloudShell-2024.08.29"));
223+
assert!(ua.contains(VERSION_HEADER));
224+
assert!(ua.contains(VERSION_VALUE));
210225
}
211226
}

0 commit comments

Comments
 (0)