|
| 1 | +use crate::policies::{Policy, PolicyResult}; |
| 2 | +use crate::{Context, Request, Response}; |
| 3 | + |
| 4 | +use http::{header::USER_AGENT, HeaderValue}; |
| 5 | +use std::env::consts::{ARCH, OS}; |
| 6 | +use std::sync::Arc; |
| 7 | + |
| 8 | +#[derive(Clone, Debug, Default)] |
| 9 | +pub struct TelemetryOptions { |
| 10 | + pub application_id: Option<String>, |
| 11 | +} |
| 12 | + |
| 13 | +#[derive(Clone, Debug)] |
| 14 | +pub struct TelemetryPolicy { |
| 15 | + header: String, |
| 16 | +} |
| 17 | + |
| 18 | +/// Sets the User-Agent header with useful information in a typical format for Azure SDKs. |
| 19 | +/// |
| 20 | +/// Client libraries should create a `TelemetryPolicy` using `option_env!()` like so: |
| 21 | +/// ``` |
| 22 | +/// use azure_core::policies::{TelemetryOptions, TelemetryPolicy}; |
| 23 | +/// let policy = TelemetryPolicy::new(option_env!("CARGO_PKG_NAME"), option_env!("CARGO_PKG_VERSION"), &TelemetryOptions::default()); |
| 24 | +/// ``` |
| 25 | +impl<'a> TelemetryPolicy { |
| 26 | + pub fn new( |
| 27 | + crate_name: Option<&'a str>, |
| 28 | + crate_version: Option<&'a str>, |
| 29 | + options: &TelemetryOptions, |
| 30 | + ) -> Self { |
| 31 | + Self::new_with_rustc_version( |
| 32 | + crate_name, |
| 33 | + crate_version, |
| 34 | + option_env!("AZSDK_RUSTC_VERSION"), |
| 35 | + options, |
| 36 | + ) |
| 37 | + } |
| 38 | + |
| 39 | + fn new_with_rustc_version( |
| 40 | + crate_name: Option<&'a str>, |
| 41 | + crate_version: Option<&'a str>, |
| 42 | + rustc_version: Option<&'a str>, |
| 43 | + options: &TelemetryOptions, |
| 44 | + ) -> Self { |
| 45 | + const UNKNOWN: &'static str = "unknown"; |
| 46 | + let mut crate_name = crate_name.unwrap_or(UNKNOWN); |
| 47 | + let crate_version = crate_version.unwrap_or(UNKNOWN); |
| 48 | + let rustc_version = rustc_version.unwrap_or(UNKNOWN); |
| 49 | + let platform_info = format!("({}; {}; {})", rustc_version, OS, ARCH,); |
| 50 | + |
| 51 | + if let Some(name) = crate_name.strip_prefix("azure_") { |
| 52 | + crate_name = name; |
| 53 | + } |
| 54 | + |
| 55 | + let header = match &options.application_id { |
| 56 | + Some(application_id) => format!( |
| 57 | + "{} azsdk-rust-{}/{} {}", |
| 58 | + application_id, crate_name, crate_version, platform_info |
| 59 | + ), |
| 60 | + None => format!( |
| 61 | + "azsdk-rust-{}/{} {}", |
| 62 | + crate_name, crate_version, platform_info |
| 63 | + ), |
| 64 | + }; |
| 65 | + |
| 66 | + TelemetryPolicy { header: header } |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +#[async_trait::async_trait] |
| 71 | +impl Policy for TelemetryPolicy { |
| 72 | + async fn send( |
| 73 | + &self, |
| 74 | + ctx: &mut Context, |
| 75 | + request: &mut Request, |
| 76 | + next: &[Arc<dyn Policy>], |
| 77 | + ) -> PolicyResult<Response> { |
| 78 | + request |
| 79 | + .headers_mut() |
| 80 | + .insert(USER_AGENT, HeaderValue::from_str(&self.header)?); |
| 81 | + |
| 82 | + next[0].send(ctx, request, &next[1..]).await |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +#[cfg(test)] |
| 87 | +mod test { |
| 88 | + use super::*; |
| 89 | + |
| 90 | + #[test] |
| 91 | + fn test_without_application_id() { |
| 92 | + let policy = TelemetryPolicy::new_with_rustc_version( |
| 93 | + Some("azure_test"), // Tests that "azure_" is removed. |
| 94 | + Some("1.2.3"), |
| 95 | + Some("4.5.6"), |
| 96 | + &TelemetryOptions::default(), |
| 97 | + ); |
| 98 | + assert_eq!( |
| 99 | + policy.header, |
| 100 | + format!("azsdk-rust-test/1.2.3 (4.5.6; {}; {})", OS, ARCH) |
| 101 | + ); |
| 102 | + } |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_with_application_id() { |
| 106 | + let options = TelemetryOptions { |
| 107 | + application_id: Some("my_app".to_string()), |
| 108 | + }; |
| 109 | + let policy = TelemetryPolicy::new_with_rustc_version( |
| 110 | + Some("test"), |
| 111 | + Some("1.2.3"), |
| 112 | + Some("4.5.6"), |
| 113 | + &options, |
| 114 | + ); |
| 115 | + assert_eq!( |
| 116 | + policy.header, |
| 117 | + format!("my_app azsdk-rust-test/1.2.3 (4.5.6; {}; {})", OS, ARCH) |
| 118 | + ); |
| 119 | + } |
| 120 | + |
| 121 | + #[test] |
| 122 | + fn test_missing_env() { |
| 123 | + // Would simulate if option_env!("CARGO_PKG_NAME"), for example, returned None. |
| 124 | + let policy = |
| 125 | + TelemetryPolicy::new_with_rustc_version(None, None, None, &TelemetryOptions::default()); |
| 126 | + assert_eq!( |
| 127 | + policy.header, |
| 128 | + format!("azsdk-rust-unknown/unknown (unknown; {}; {})", OS, ARCH) |
| 129 | + ) |
| 130 | + } |
| 131 | +} |
0 commit comments